jueves, 10 de febrero de 2011

WebService con seguridad UsernameToken y oasis (Mensaje SOAP via Socket)

Aqui el ejemplo

package com.blogspot.toolboxnet;
import java.net.*;
import java.io.*;

public class SoapRequestStream {
 private String response = "Nothing";
 private static String wServer = "";
 private static String wPort = "";
 private static String wURI = "";

 public static void main(String[] args) throws Exception {
  SoapRequestStream se = new SoapRequestStream();
  String wParam1 = args[0]; // "1"
  String wParam2 = args[1]; // "2"
  wServer = args[2]; // "localhost"
  wPort = args[3]; // "8079"
  wURI = args[4]; // "/axis2/services/WebService";

  se.webServiceMethod(wParam1, wParam2);
 }

 public void webServiceMethod(String param1, String param2) {
  SoapRequestBuilder s = new SoapRequestBuilder();

  s.Server = wServer;
  s.WebServicePath = wURI;
  s.Port = Integer.parseInt(wPort);
  s.MethodName = "webServiceMethod";
  s.XmlNamespace = "
http://toolboxnet.blogspot.com/WebService";
  s.SoapAction = s.XmlNamespace+"/"+s.MethodName;
  response = s.sendRequest(param1, param2);
  System.out.println(response);
 }

}
class SoapRequestBuilder {
 String Server = "";
 String WebServicePath = "";
 String SoapAction = "";
 String MethodName = "";
 String XmlNamespace = "";
 int Port = 8080;

 public String sendRequest(String param1, String param2) {
  String retval = "";
  Socket socket = null;
  try {
   InetAddress  addr = InetAddress.getByName(Server);
   socket = new Socket(addr, Port);
  }
  catch (Exception ex1) {
   ex1.printStackTrace(System.out);
  }

  try {
   boolean autoflush = true;
   PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
   BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

   String username="username";
   String password="password";

   String sout="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgt=\"http://toolboxnet.blogspot.com/CGType\"><soapenv:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" soapenv:mustUnderstand=\"1\"><wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"UsernameToken-1\"><wsse:Username>"+username+"</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">"+password+"</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><cgt:webServiceMethod><cgt:param1>"+param1+"</cgt:param1><cgt:param2>"+param2+"</cgt:param2></cgt:webServiceMethod></soapenv:Body></soapenv:Envelope>";
   String NL="\n";
   String cab="";
   cab+="POST "+WebServicePath+" HTTP/1.1"+NL;
   cab+="Content-Type: text/xml; charset=UTF-8"+NL;
   cab+="SOAPAction: \"urn:webServiceMethod\""+NL;
   cab+="User-Agent: Axis2"+NL;
   cab+="Host: "+Server+":"+Port+NL;
   cab+="Content-Length: "+ (1+sout.length() )+NL;

   out.println(cab);
   out.println(sout);
   out.println();
   out.flush();

   System.out.println(cab);
   System.out.println(sout);

   int inputChar;
   StringBuffer sb = new StringBuffer(1000);

   int wait_seconds = 3;
   boolean timeout = false;
   long m = System.currentTimeMillis();

   while ( (inputChar = in.read()) != -1 && !timeout) {
    sb.append( ((char)inputChar) );
    if ( (System.currentTimeMillis() - m) > (1000 * wait_seconds)) timeout = true;
   }
   in.close();

   System.out.println(sb.toString());
   socket.close();
  }

  catch (Exception ex) {
   ex.printStackTrace(System.out);
  }

  return retval;
 }

}
Compartir: