/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)SunRIHttpsURLConnection.java	1.3 02/01/12
 */

package com.sun.cts.implementation.sun;

import com.sun.cts.porting.*;

// Implementation Specific Classes
import com.sun.net.ssl.HttpsURLConnection;
import com.sun.net.ssl.HostnameVerifier;
import com.sun.enterprise.security.KeyTool;
import com.sun.cts.util.*;
import java.io.*;
import java.net.*;


/**
 * CTSHttpsURLConnection provides the HTTPS specific featurs
 *
 */
public class SunRIHttpsURLConnection implements CTSHttpsURLConnectionInterface {

    private HttpsURLConnection httpsURLConnection=null;
    private HostnameVerifier hostNameVerifier=null;
    private URL url=null;

     public void SunRIHttpsURLConnection()
     {
      //do nothing;
     }

     /**
      * This method is used to 
      *    a) open a HttpsURLConnection from a given URL 
      *    b) set the HostNameVerifier to default  MyHostNameVerifier
      *
      * @param url  url used to open HttpsURLConnection
      */
      public void init(URL _url) throws IOException
      {
	url = _url;
	KeyTool.initProvider();
	httpsURLConnection =(HttpsURLConnection)_url.openConnection() ;
        hostNameVerifier = new MyHostNameVerifier();
        httpsURLConnection.setHostnameVerifier(hostNameVerifier); 
      }

     /**
      *  Sets the value  of the doInput field for this Connection
      *
      *  @param doInput - the new value (the default is false)
      */
     public void setDoInput(boolean doInput) {
        httpsURLConnection.setDoInput(doInput);
      }

     /**
      *  Sets the value  of the doOutput field for this Connection
      *
      *  @param doOutput - the new value (the default is false)
      */
     public void setDoOutput(boolean doOutput){
        httpsURLConnection.setDoOutput(doOutput);
      }

     /**
      *  Sets the value  of the useCaches field for this Connection
      *  If the UseCaches flag on the connection is true, the connection
      *  is allowed to use whatever caches it can. If false, caches are to
      *  be ignored. The default value is set to true
      *
      *  @param usecaches - the new value (the default is true)
      */
     public void setUseCaches(boolean usecaches) {
        httpsURLConnection.setUseCaches(usecaches);
     }

     /**
      *  Returns an input stream that reads from the open connection
      *
      *  @return InputStream - inputStream
      */
     public InputStream getInputStream() throws IOException{
        return httpsURLConnection.getInputStream();
     }

     /**
      *  Returns an Output stream that writes to the open connection
      *
      *  @return OutputStream - outputStream
      */
     public OutputStream getOutputStream() throws IOException{
        return httpsURLConnection.getOutputStream();
     }

    /**
     * This class is used to set HostNameVerifier 
     * 
     * HostnameVerifier provides a callback mechanism so that implementers of 
     * this interface can supply a policy for handling the case where the host
     * to connect to and the server name from the certificate mismatch
     */
    public class MyHostNameVerifier implements HostnameVerifier{

       /**
        * Verify that the hostname from the URL is an acceptable match 
        * with the value from the common name entry in the server certificate's 
        * distinguished name. 
        *    (i.e the httpsHostName == name entry in the server certificate)
        *
        * For all practical reasons a  HttpsServer can have a certificate  
        * with any name on it. Because of this reason this implementation 
        * doesn't  verify the httpsHoseName with the name entry in
        * server ceritificate .
        * 
        * @param urlhostname  - the https hostname in the url
        * @param certHostName - the name entry in the server certificate 
        * @return boolean     - this implementation returns true always.
        */
  	public boolean verify(String urlhostname, String certHostName){

        	// The scope these CSIv2 tests is to test the principal 
                // propagation from intermediate container(web/EJB) to 
                // EJB container So this verify() method always returns true 
		return true;
	}
   }
 

}	
