/**
***************************************************************************
 jwbemexecSSL.java
 This is a test program to send an Indication to CIM Listener using SSL.
 Program uses Java URL(SSL Enabled) to do the communication. Also you must
 pass a valid XML file as an input this program.

 The Initial Developer of the Original Code :
     Bapu Patil ( bapu_patil@hp.com)
     Hewlett-Packard Company.

 Modified by:

***************************************************************************
*/

package tests.IndTests;

import java.io.*;
import java.net.*;
import java.security.*;
import sun.misc.*;
//import javax.net.ssl.*;
import com.sun.net.ssl.*;

import java.net.HttpURLConnection.*;
import org.snia.wbemcmd.xml.Base64Encoder;


public class jwbemexecSSL {

//wbemexec [ -h hostname ] [ -p portnumber ]  [ -u username ] [ -w password ] [ -s ] [ inputfilepath ]
//wbemexec [ -h hostname ] [ -p portnumber ]  [ -s ] [ -f inputfilepath ]

  public static void main(String[] args) throws Exception 
  {
        String inputString = "";
        String input;
        String hostname=null;
        int portNumber=0;
        String username;
        String passwd;
        String xmlfile=null;
        String hostURL;
        boolean isSSL=false;
        HttpURLConnection c = null;
        HttpsURLConnection sc = null;
        

        if (args.length < 2) 
        {
            System.out.println("jwbemexec ");
            System.exit(1);
        }
      int i=0;
      while ( i <args.length )
      {
        if ( args[i].equals("-h") )
        {
            i++;
            hostname=args[i];
        }
        else if ( args[i].equals("-p") )
        {
            i++;
            portNumber=Integer.parseInt(args[i]);
        }
        else if ( args[i].equals("-u") )
        {
            i++;
            username=args[i];
        }
        else if ( args[i].equals("-w") )
        {
            i++;
            passwd=args[i];
        }
        else if ( args[i].equals("-f") )
        {
            i++;
            xmlfile=args[i];
        }
        else if ( args[i].equals("-s") )
        {
          isSSL = true;
        } 
        i++;
      }
      if ( isSSL == true )
      {
            //get SSL provider 
            System.setProperty("java.protocol.handler.pkgs",
                                 "com.sun.net.ssl.internal.www.protocol");
            //System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            hostURL="https://"+hostname+":"+portNumber+"/cimom";
      }
      else
      {
            hostURL="http://"+hostname+":"+portNumber+"/cimom";
      }
      Base64Encoder b64e=new Base64Encoder("guest:guest");
      String encoded=b64e.processString();

      //byte[] authCookie = "guest:guest".getBytes();
      //BASE64Encoder encoder = new BASE64Encoder();
      //String encoded = encoder.encodeBuffer(authCookie);
      System.out.println(encoded);


    BufferedReader in = new BufferedReader(new FileReader(xmlfile));
    while((input = in.readLine()) != null)
    {
       inputString = inputString + input;
    }

    in.close();

    System.out.println("Sending ************=" +inputString);
    System.out.println("\n");
    System.out.println("hostname " + hostURL);

    URL wbemURL = new URL(hostURL);
    if ( isSSL == true )
    {
        sc = (HttpsURLConnection)wbemURL.openConnection();
    }
    else
    {
        c = (HttpURLConnection)(wbemURL.openConnection());
            System.exit(1);
    }
    
    // My Hostname verifier
   /*
    sc.setHostnameVerifier( new HostnameVerifier()
    {
        public boolean verify( String urlHost, String certHost )
        {
         return (true);
           //if( !urlHost.equals( certHost ) )
           //{
            // System.out.println( " Ignoring certificate <" + certHost +"> does not match
             //              host <" + urlHost + "> " +"continuing anyway" );
            //}
             //return true;
        }
     });
      */

    sc.setDoOutput(true);
    if ( encoded != null )
         sc.setRequestProperty( "Authorization", "Basic " + encoded);
// C++
//CIMProtocolVersion: 1.0
//CIMOperation: MethodCall
//CIMMethod: SendTestIndication
//CIMObject: root/SampleProvider:RT_TestIndication
//
// SNIA
//CIMOperation: MethodCall
//CIMProtocolVersion: 1.0
//CIMMethod: Indication
//CIMObject: CIM_InstCreation

    sc.setRequestProperty( "CIMProtocolVersion", "1.0");
    sc.setRequestProperty( "CIMOperation", "MethodCall");
    sc.setRequestProperty( "CIMMethod", "Indication");
    sc.setRequestProperty( "CIMObject", "root/SampleProvider:RT_TestIndication");

    PrintWriter out = new PrintWriter(sc.getOutputStream());

    // Here's whether the parameter is set.
    out.println(inputString);
    out.flush();

    BufferedReader in2 = new BufferedReader(new InputStreamReader(sc.getInputStream()));

    String inputLine;
    while((inputLine = in2.readLine()) != null)
        System.out.println(inputLine);

    in2.close();
    out.close();
  }
}

