import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Vector;
import org.kxml.Xml;
import org.kxml.kdom.Element;
public class Operation
{
// Hold name of operation.
private String name;
// Hold Soap Action value of operation.
private String soapAction;
// Hold Soap Address.
private String soapAddress;
// Hold the port number.
// We will use 8080 if not specified.
private int port=8080;
// Hold input Namespace specified in WSDL.
private String inputNamespace;
// Holds output (return) Namespace specified
//in WSDL.
private String outputNamespace;
// Hold input Encodingstyle specified
//in WSDL.
private String inputEncodingstyle;
// Hold output (return) Encodingstyle
//specified in WSDL.
private String outputEncodingstyle;
// Flags to indicate whether
// input and output parameters are present.
private boolean ipFlag;
private boolean opFlag;
// Vectors to hold
// input/output Parameter objects.
private Vector ipVector;
private Vector opVector;
public Operation (Element operation){
parseOperation (operation);
ipVector = new Vector();
opVector = new Vector();
}//constructor
// Sends the authored xml method call
//(SOAP request)
// to a SOAP server and returns the
//response received
// as XML string.
public String send () throws UnknownHostException,
IOException {
String response = new String();
SoapTransport transport = new
SoapTransport(soapAddress, port);
//Setting SOAPAction header value.
transport.setSoapAction (soapAction);
String body = getBody();
transport.setBody(body);
response = transport.send();
return response;
}//send()
// Authors and returns an XML structure
//on the basis
// of information contained within
//this opertion.
private String getBody()
{
String body;
StringBuffer sb = new StringBuffer ();
// Authoring method element by method
//name and namespace.
// It is so simple that we don't
//need to use kXML.
sb.append("<wax:"+getName()
+" xmlns:wax=\""+getInputNamespace()+"\"");
if (isInputParameters()) {
sb.append(">\r\n");
Vector parameterVector =
getInputParameters();
// Initialize Parameter
//objects array
// with parameter vector size.
Parameter[] parameter =
new Parameter [parameterVector.size()];
for (int i=0; _
i<parameterVector.size(); i++) {
parameter[i] = (Parameter)
parameterVector.elementAt(i);
//Authoring method parameters.
sb.append("<"+parameter[i].getName()+" xsi:type=\"xsd:"+parameter[i].getType()+"\">");
sb.append(""+parameter[i].getValue()+"");
sb.append_
("</"+parameter[i].getName()+">\r\n");
}
sb.append("</wax:"+name+">\r\n");
}// if (isInputParameters())
else
sb.append("/>\r\n");
body = sb.toString();
return body;
}//getBody()
// Sets value of Input Parameter against
//parameter name provided.
public void setValue (String name, String value)
{
Vector parameterVector = getInputParameters();
Parameter[] parameter = new Parameter
[parameterVector.size()];
for (int i=0; i<ipVector.size(); i++) {
parameter[i] = (Parameter)
parameterVector.elementAt(i);
if (name.equalsIgnoreCase
(parameter[i].getName())) {
parameter[i].setValue(value);
ipVector.removeElementAt(i);
ipVector.insertElementAt(parameter[i], i);
break;
}//if (name.equalsIgnoreCase
(parameter[i].getName()))
}// for (int i=0; i<ipVector.size(); i++)
}//setValue
// Resolve values with namespace prefix
//e.g. "wax:WaxP".
// this method will return value without
//prefix e.g. "WaxP".
// Our client is only capable of working
//with data types
// defined by SOAP and WSDL.
// We will ignore user defined namespaces
//for data types.
private String resolveVal (String val)
{
if (val==null)
return null;
String value="";
if (val.indexOf(":")!=-1)
{
int colonpos = val.indexOf(":");
return value = val.substring (colonpos+1);
}
return val;
}//resolveVal
// It takes an <operation> element in
//the form of a DOM element.
// It will parse it and sets operationName,
//soapAction,
// <input> and <output> elements
//and their children.
private void parseOperation (Element operation)
{
setName (operation.getValue("name"));
int count = operation.getChildCount();
Element parameter = null;
for ( int k = 0; k<count; k++ )
{
if (operation.getType(k) ==
Xml.ELEMENT)
{
parameter = (Element)
operation.getChild(k);
if (parameter.getName().equals("operation"))
setSoapAction _
(parameter.getValue("soapAction"));
if (parameter.getName().equals("input")
|| parameter.getName().equals("output"))
setOperationAttributes(parameter);
}//if (operation.getType(k) == Xml.ELEMENT)
}//for ( int k = 0; k<count; k++ )
}//parseOperation
// This method takes <input> or
//<output> elmenet of <operation>
// and set the namespace and encodingstyle
//attribute values.
private void setOperationAttributes
(Element operationChild)
{
String name = operationChild.getName();
Element element = null;
int count = operationChild.getChildCount();
//Loop to get count no of parameters
//inside operation.
for ( int x = 0; x<count; x++ ) {
if (operationChild.getType(x) == Xml.ELEMENT)
{
element = (Element)
operationChild.getChild(x);
if (element != null &&
element.getName().equals("body"))
{
if (name.equalsIgnoreCase("input"))
{
setInputEncodingstyle
(element.getValue("encodingStyle"));
setInputNamespace
(element.getValue("namespace"));
} else {
setOutputEncodingstyle
(element.getValue("encodingStyle"));
setOutputNamespace
(element.getValue("namespace"));
}//else
}// if (element != null &&
element.getName().equals("body"))
}// if (operationChild.getType(x) ==
Xml.ELEMENT)
}//for ( int x = 0; x<count; x++ )
}//setOperationAttributes
// Adds Input parameter object to input Vector.
public void addInputParameter (Parameter parameter)
{
ipFlag = true;
ipVector.addElement(parameter);
}//addInputParameter
// Adds Output parameter object to output Vector.
public void addOutputParameter (Parameter parameter)
{
opFlag = true;
opVector.addElement(parameter);
}//addOutputParameter
// Sets soap access point url.
public void setSoapAddress(String soapAddress)
{
this.soapAddress = soapAddress;
}//setSoapAddress
// Returns soap access point url.
public String getSoapAddress()
{
return this.soapAddress;
}//setSoapAddress
// Sets server port.
public void setPort (int port)
{
this.port = port;
}//setPort
// Returns server port.
public int getPort ()
{
return port;
}//getPort
// Sets SoapAction for this operation.
public void setSoapAction(String soapAction )
{
this.soapAction = soapAction;
}//setSoapAction
// Returns SoapAction associated with this operation.
public String getSoapAction()
{
return soapAction;
}//getSoapAction
// Sets name of this operation.
public void setName( String name)
{
this.name = name;
}//setOperationName
// Returns name of this operation.
public String getName()
{
return name;
}//getOperationName
// Checks whether input parameters are present.
public boolean isInputParameters()
{
return ipFlag;
}//isInputParameters
// Checks whether output parameters are present.
public boolean isOutputParameters()
{
return opFlag;
}//isOutputParameters
// Returns number of input parameters.
public int getInputParametersCount()
{
return ipVector.size();
}//getInputParametersCount
// Returns number of output parameters.
public int getOutputParametersCount()
{
return opVector.size();
}//getOutputParametersCount()
// Returns vector of input Parameter objects.
public Vector getInputParameters()
{
return ipVector;
}//getInputParameters
// Returns vector of output Parameter objects.
public Vector getOutputParameters()
{
return opVector;
}//getInputParameters
// Sets Input Encodingstyle for this operation.
public void setInputEncodingstyle (String encodingStyle)
{
inputEncodingstyle = encodingStyle;
}//setInputEncodingStyle
// Sets Output Encodingstyle for this operation.
public void setOutputEncodingstyle (String encodingStyle)
{
outputEncodingstyle = encodingStyle;
}//setOutputEncodingStyle
// Returns Input Encodingstyle value.
public String getInputEncodingstyle ()
{
return inputEncodingstyle;
}//getInputEncodingStyle
// Returns Output Encodingstyle value.
public String getOutputEncodingstyle ()
{
return outputEncodingstyle;
}//getOutputEncodingStyle
// Sets InputNamespace of this operation .
public void setInputNamespace(String namespace)
{
inputNamespace = namespace;
}//setInputNamespace
// Sets OutpuNamespace of this operation .
public void setOutputNamespace(String namespace)
{
outputNamespace = namespace;
}//setOutNamespace
// Returns InputNamespace value.
public String getInputNamespace()
{
return inputNamespace;
}//getInNamespace
// Returns OutputNamespace value.
public String getOutputNamespace()
{
return outputNamespace;
}//getOutNamespace
}//class
|