/*
 * @(#)HttpSsh.java
 *
 * Copyright (c) 2003 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */
package com.jscape.inet.httpssh;

import com.jscape.inet.http.Http;
import com.jscape.inet.ipclient.IpClientException;
import com.jscape.inet.ipclientssh.IpClientSsh;
import com.jscape.inet.ssh.util.SshParameters;
import com.jscape.inet.util.URLParser;

import java.io.IOException;

/**
 * Implements the basic functionality of a HTTP client tunnelling data through a secure SSH connection.<P>
 * Example Usage:
 * <p/>
 * <P>
 * <pre>
 *      // create SSH connection parameters
 *      SshParameters sshParams = new SshParameters(sshHostname,sshUsername,sshPassword);
 *
 *      // create new HttpSsh instance
 *      HttpSsh http = new HttpSsh(sshParams);
 *
 *      // build HTTP request
 *      HttpRequest request = new HttpRequest("http://www.myserver.com");
 *
 *      // get HTTP response
 *      try
 *     {
 *      HttpResponse response = http.getResponse(request);
 *      System.out.println(new String(response.getBody()));
 *     }
 *      catch(Exception e)
 *     {
 *      System.out.println(e);
 *     }
 * </pre>
 * </p>
 */
public class HttpSsh
        extends Http {

    private SshParameters sshParameters;
    private IpClientSsh sshClient;

    /**
     * Creates a new HttpSsh instance.
     */
    public HttpSsh() {
        super();
    }

    /**
     * Creates a new HttpSsh instance.
     *
     * @param sshParameters
     */
    public HttpSsh(SshParameters sshParameters) {
        super();
        this.sshParameters = sshParameters;
    }

    /**
     * Connects to HTTP server and establishes secure SSH tunnel.
     *
     * @throws IOException thrown if an I/O related error occurs
     */
    protected void connect()
            throws IOException {
        synchronized (this) {
            URLParser url = new URLParser(request.getURL());

            String serverHost = url.getHost();
            int serverPort = url.getPort();

            if (serverPort == -1) {
                serverPort = 80;
            }

            try {
                sshClient = new IpClientSsh(sshParameters, serverHost, serverPort, timeout);
                sshClient.connect();

                inputStream = sshClient.getInputStream();
                outputStream = sshClient.getOutputStream();
                fireHttpConnected();
            } catch (IpClientException ipe) {
                throw new IOException(ipe.getMessage());
            }
        }
    }

    /**
     * Closes connection to HTTP server and SSH server.
     */
    protected void disconnect() {
        synchronized (this) {
            // clean up
            try {
                sshClient.disconnect();
            } catch (Exception e) {
                // todo: ignore?
            } finally {
                socket = null;
                inputStream = null;
                outputStream = null;
                fireHttpDisconnected();
            }
        }
    }
}
