TechnoBuzz

A Techno Blog, mainly about Java

Soap Client Homemade implementation

The Soap request:

<?xml version=”1.0″ encoding=”utf-8″?>

<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”&gt;
<soap:Body>
<GetInfoByZIP xmlns=”http://www.webserviceX.NET”&gt;
<USZip>27615</USZip>
</GetInfoByZIP>
</soap:Body>
</soap:Envelope>

The java program:

try {
// Set up the Web Service URL and SOAP Message file name
// http://www.webservicex.net/uszip.asmx
// src/exercise1/test.xml

String WebServiceUrl = args[0];
String SOAPFile = args[1];

//Create the connection where we’re going to send the file.
URL url = new URL(WebServiceUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

//Get the SOAP document ready to send

File f1 = new File(SOAPFile);
String path = f1.getAbsolutePath();
FileInputStream FileIn = new FileInputStream(path);
ByteArrayOutputStream ByteOut = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
while (true) {
int bytesRead = FileIn.read(buffer);
if (bytesRead == -1)
break;
ByteOut.write(buffer, 0, bytesRead);
}

byte[] b = ByteOut.toByteArray();
FileIn.close();

//Set the HTTP parameters.
httpConn.setRequestProperty(“Content-Length”, String.valueOf(b.length));
httpConn.setRequestProperty(“Content-Type”, “text/xml; charset=utf-8”);
httpConn.setRequestProperty(
“Accept”,
“application/soap+xml, application/dime, multipart/related, text/*”);
httpConn.setRequestProperty(“User-Agent”, “WSAD”);
httpConn.setRequestProperty(“Host”, “localhost”);
httpConn.setRequestProperty(“Cache-Control”, “no-chache”);
httpConn.setRequestProperty(“Pragma”, “no-chache”);
httpConn.setRequestProperty(“SOAPAction”, “http://www.webserviceX.NET/GetInfoByZIP&#8221;);
httpConn.setRequestMethod(“POST”);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);

//Send XML File
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();

//Get the response
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);

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

} catch (Exception e) {
System.out.println(“FaultString: ” + e.toString());
return;
} //end exception
}

Response:

<?xml version=”1.0″ encoding=”utf-8″?><soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”><soap:Body><GetInfoByZIPResponse xmlns=”http://www.webserviceX.NET”><GetInfoByZIPResult><NewDataSet xmlns=””><Table><CITY>Raleigh</CITY><STATE>NC</STATE><ZIP>27615</ZIP><AREA_CODE>919</AREA_CODE><TIME_ZONE>E</TIME_ZONE></Table></NewDataSet></GetInfoByZIPResult></GetInfoByZIPResponse></soap:Body></soap:Envelope>

October 21, 2012 Posted by | Uncategorized | Leave a comment

Soap Web Services Begin To End Part 3

Using the WSDL in Part 2, right click on it in the sidebar:

WebService -> Generate Java Bean Skeleton

Again move the notch up to  Test Service  and click Next until it completes

Then, you will see in src folder all the generated files.

This completes the Producer  side of the web service.

Now, to generate a client side, you generate a new dynamic project (i.e. Call it MyClientProject).  After the dynamic project is complete, you can go into the Producer project, and get the wsdl and place it in the new Client project. Right click on the WSDL, click WebService -> Generate Client.

 

http://localhost:8080/MyClientProject/sampleCostServiceProxy/TestClient.jsp

 

October 21, 2012 Posted by | Uncategorized | Leave a comment

Soap Web Services Begin To End Part 2

Create a Dynamic Web project in eclipse. In my case I called it NumbersService.

Create a new class :
learn.webservices.numbers.ElectricCompanyService :

public class ElectricCompanyService {

public int multiply(int number1, int number2)
{
return number1 * number2;
}

public int add(int number1, int number2)
{
return number1 – number2;
}

}

Right click in the siderbar on ElectricCompanyService

WebService-> Create Web Service

On the screen that comes up, move the selector up one notch where it shows Test Service

In Web Content you can find a folder named WSDL with the WSDL file

<?xml version=”1.0″ encoding=”UTF-8″?>
<wsdl:definitions targetNamespace=”http://numbers.webservices.learn&#8221; xmlns:apachesoap=”http://xml.apache.org/xml-soap&#8221; xmlns:impl=”http://numbers.webservices.learn&#8221; xmlns:intf=”http://numbers.webservices.learn&#8221; xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/&#8221; xmlns:wsdlsoap=”http://schemas.xmlsoap.org/wsdl/soap/&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<!–WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)–>
<wsdl:types>
<schema elementFormDefault=”qualified” targetNamespace=”http://numbers.webservices.learn&#8221; xmlns=”http://www.w3.org/2001/XMLSchema”&gt;
<element name=”add”>
<complexType>
<sequence>
<element name=”number1″ type=”xsd:int”/>
<element name=”number2″ type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”addResponse”>
<complexType>
<sequence>
<element name=”addReturn” type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”multiply”>
<complexType>
<sequence>
<element name=”number1″ type=”xsd:int”/>
<element name=”number2″ type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”multiplyResponse”>
<complexType>
<sequence>
<element name=”multiplyReturn” type=”xsd:int”/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>

<wsdl:message name=”multiplyResponse”>

<wsdl:part element=”impl:multiplyResponse” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”multiplyRequest”>

<wsdl:part element=”impl:multiply” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”addResponse”>

<wsdl:part element=”impl:addResponse” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”addRequest”>

<wsdl:part element=”impl:add” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:portType name=”ElectricCompanyService”>

<wsdl:operation name=”add”>

<wsdl:input message=”impl:addRequest” name=”addRequest”>

</wsdl:input>

<wsdl:output message=”impl:addResponse” name=”addResponse”>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name=”multiply”>

<wsdl:input message=”impl:multiplyRequest” name=”multiplyRequest”>

</wsdl:input>

<wsdl:output message=”impl:multiplyResponse” name=”multiplyResponse”>

</wsdl:output>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name=”ElectricCompanyServiceSoapBinding” type=”impl:ElectricCompanyService”>

<wsdlsoap:binding style=”document” transport=”http://schemas.xmlsoap.org/soap/http”/&gt;

<wsdl:operation name=”add”>

<wsdlsoap:operation soapAction=””/>

<wsdl:input name=”addRequest”>

<wsdlsoap:body use=”literal”/>

</wsdl:input>

<wsdl:output name=”addResponse”>

<wsdlsoap:body use=”literal”/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name=”multiply”>

<wsdlsoap:operation soapAction=””/>

<wsdl:input name=”multiplyRequest”>

<wsdlsoap:body use=”literal”/>

</wsdl:input>

<wsdl:output name=”multiplyResponse”>

<wsdlsoap:body use=”literal”/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name=”ElectricCompanyServiceService”>

<wsdl:port binding=”impl:ElectricCompanyServiceSoapBinding” name=”ElectricCompanyService”>

<wsdlsoap:address location=”http://localhost:8080/NumbersService/services/ElectricCompanyService”/&gt;

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

:

October 21, 2012 Posted by | Uncategorized | Leave a comment

Soap Web Services Begin To End Part I

Step 1 : Open Web Perspective -> Web Services Explorer




Launch Web services explorer:


Select WSDL page (next to star on right):


Enter the SDL URL : http://www.webservicex.net/stockquote.asmx?WSDL

Choose Soap enter stock quote and Go!

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:q0=”http://www.webserviceX.NET/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>

<soapenv:Body>

<q0:GetQuote>

  <q0:symbol>IBM</q0:symbol>

  </q0:GetQuote>

  </soapenv:Body>

  </soapenv:Envelope>

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>

<soap:Body>

<GetQuoteResponse xmlns=”http://www.webserviceX.NET/“>

  <GetQuoteResult><StockQuotes><Stock><Symbol>IBM</Symbol><Last>193.36</Last><Date>10/19/2012</Date><Time>4:03pm</Time><Change>-1.60</Change><Open>195.12</Open><High>196.08</High><Low>193.18</Low><Volume>6621563</Volume><MktCap>219.9B</MktCap><PreviousClose>194.96</PreviousClose><PercentageChange>-0.82%</PercentageChange><AnnRange>177.06 – 211.79</AnnRange><Earns>13.913</Earns><P-E>14.01</P-E><Name>International Bus</Name></Stock></StockQuotes></GetQuoteResult>

  </GetQuoteResponse>

  </soap:Body>

  </soap:Envelope>


Here is a WSDL for Mortgage  payments calculation:

http://www.webservicex.net/mortgage.asmx?WSDL

October 21, 2012 Posted by | Uncategorized | Leave a comment

Spring Documento

In web.xml with Spring MVC (Convenient ApplicationContext instantiation for web applications):

ContextLoader called  by ContextLoaderListener

[ref] : “ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext. This context can be used to load and unload the spring-managed beans ir-respective of what technology is being used in the controller layer(Struts or Spring MVC) ”

  • org.springframework.web.servlet.DispatcherServlet

[ref] : “DispatcherServlet creates its own WebApplicationContext and the handlers/controllers/view-resolvers are managed by this contextContextLoader”

“When ContextLoaderListener is used in tandem with DispatcherServlet, a root web-application-context is created first as said earlier and a child-context is also created by DispatcherSerlvet and is attached to the root application-context.”

http://static.springsource.org/spring/docs/3.0.x/reference/index.html

org.springframework.web.filter.DelegatingFilterProxy

[ref] Spring’s DelegatingFilterProxy provides the link between web.xml and the application context. .. “it finds a bean (“target bean” or “delegate”) in your Spring application context, an invoke it. How is it possible? Because this bean implements javax.servlet.Filter, its doFilter method is called.”

  • org.springframework.context.support.ReloadableResourceBundleMessageSource

[link ] :  The feature I’m talking about is the ability to reload (and detect changes in) i18n property files on-the-fly.

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

This interface that maps handlers based on HTTP paths expressed through the RequestMapping annotation at the type or method level.

 

October 14, 2012 Posted by | IoC | Leave a comment

Now Learning Soap Web Services

Soap is a lightweight protocol for exchanging structured information in a decentralized, distributed environment. It is an XML based protocol that consists of three parts:

  • an envelope that defines a framework for describing what is in a message and how to process it
  • a set of encoding rules for expressing instances of application-defined datatypes
  • a convention for representing remote procedure calls and responses

Soap Request, Soap Response

RPC Style, Document Style Soap

<soapenv:Envelope> <soapenv:Body>

Apache Axis is an implementation of the SOAP by Apache

WSDL document describes the interface of a web service in a language and protocol independent way:

  • service : Defines service
  • binding : possible ways to invoke service
  • port: defined endpoint
  • porttype: particular interface
  • operation: defines method
  • message :
  • types: datatypes with schema

WSDL………………………………………………………………………..
http://www.w3.org/2001/03/14-annotated-WSDL-examples

 

Examples;

http://javapapers.com/web-service/soap-web-service-introduction/

http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/

http://rantincsharp.wordpress.com/2008/10/14/a-simple-soap-web-service-example-in-eclipse-ganymede/

http://blog.sencide.com/2011/06/create-web-service-using-apache-axis2.html

October 3, 2012 Posted by | Web/Tech | Leave a comment