/**
***************************************************************************
 sendIndication.java
 This is a test program to send more than one Indication to CIM Listener.
 Program uses Java Sockets to do the communication.

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

 Modified by:

 Usage:

   Examples:
     java sendIndication -h myHost -p ListenerPort \
	   -u guest -w guest -f exportReq.xml

   SSL example:
    java  \
      -Djavax.net.ssl.trustStore=cimindCL.trust \
      -Djavax.net.ssl.keyStore=cimindCL.key  \
      -Djavax.net.ssl.keyStorePassword=cimindCLkeypass  \
       sendIndication -h sequoia -p 8189 -u guest -w guest \
       -f exportReq.xml -s

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


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

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

public class sendIndication
{

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

        if (args.length < 2) 
        {
            System.out.print(" java sendIndication ");
            System.out.println(" -h <hostname> -p <port> -u <user> -w <passwd> -f <xmlfile> [-s]" );
            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/listener1";
      }
      else
      {
            hostURL="http://"+hostname+":"+portNumber+"/cimom//listener1";
      }
      Base64Encoder b64e=new Base64Encoder("guest:guest");
      String encoded=b64e.processString();
      System.out.println(encoded);


    System.out.println("hostname " + hostURL);
    System.out.println("Sending the following request *********************");
    BufferedReader in = new BufferedReader(new FileReader(xmlfile));
    while((input = in.readLine()) != null)
    {
       inputString = inputString + input;
       System.out.println(input);
    }

    in.close();



    int ch;
    int bufSize;
    int maxBufSize=8192;
    byte[] inputBuf=new byte[maxBufSize];
    SSLSocket sslSocket = null;
    Socket    plainSocket = null;
    BufferedWriter wr=null;
    InputStream ins=null;
    
    try 
    {
        // Create a socket to the host
        InetAddress addr = InetAddress.getByName(hostname);

        if ( isSSL == true )
        {
          Security.addProvider(
                new com.sun.net.ssl.internal.ssl.Provider());            
          SSLSocketFactory sslFact =
                (SSLSocketFactory)SSLSocketFactory.getDefault();
          sslSocket = (SSLSocket)sslFact.createSocket(addr, portNumber);

          // Output 
          wr = new BufferedWriter(
                            new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
          // Input Stream reader
          ins = sslSocket.getInputStream();
        }
        else
        {
          plainSocket = new Socket(addr, portNumber);
          // Output 
          wr = new BufferedWriter(
                            new OutputStreamWriter(plainSocket.getOutputStream(), "UTF8"));
          // Input Stream reader
          ins = plainSocket.getInputStream();
        }
     } 
     catch (Exception e) 
     {
            System.out.println("Exception" + e);
     }

     try 
     {
   
        // Send header
        String path = "/cimom/listener1";


        wr.write("POST "+path+" HTTP/1.1\r\n");
        wr.write("Content-type: application/xml; charset=\"utf-8\" \r\n");
        wr.write("Content-Length: "+inputString.length() + "\r\n");
        wr.write( "CIMProtocolVersion: 1.0 \r\n");
        wr.write( "CIMOperation: MethodCall \r\n");
        wr.write( "CIMMethod: Indication \r\n");
        wr.write( "CIMObject: root/SampleProvider:RT_TestIndication \r\n");
        wr.write("\r\n");
    
        // Send input data
        wr.write(inputString);
        wr.flush();
    
        // Get response
        System.out.println("Response from the listener *******************");
        bufSize=ins.read(inputBuf,0,maxBufSize);
        if (bufSize<0)
        {
            System.out.println("Failed to read");
        }
        else
        {
           // System.out.println("Buffer size" + bufSize);
           String inputStr = new String(inputBuf);
           System.out.println(inputStr);
        }

      //ins.close();
      //wr.close();
        if ( isSSL == true )
          sslSocket.close();
        else
          plainSocket.close();
    } 
    catch (Exception e) { e.printStackTrace(); }

  }
}

