Posts

Showing posts from June, 2012

JAX-WS Webservice Endpoint and Client

JAX-WS based Webservice and Client Design Options. JAX-WS stands for Java Api for XML Web services. JAX-WS supports both SOAP 1.1 and SOAP 1.2. The following section tries to snapshot the different ways we can use JAX-WS to write both Web Services and Clients. What is Web service Endpoint? A web service endpoint is an server side resource that can be referenced by client and to which web services messages can be addressed. Generally, web services will have endpoint and a WSDL file to share its descriptions with clients. Clients use the web service endpoint description to generate/write code that can send SOAP messages to and receive SOAP messages from the web service endpoint. In JAX-WS the endpoints are annotated with the javax.jws.WebService annotation or   javax.jws.WebServiceProvider annotation, but not both at a time. Services designe options  . JAX-WS broadly supports two Webservice  models, SEI Interface Annotated with the javax.jws.WebService , where the

Multiple Expression Execution on XPath

How to specify Multiple XPath Expression at a time Sometimes we need to specify  multiple  XPath  e xpressions to evaluate XML file. For instance let's consider the following XML File. <FieldCollection> <VarificationField> <CreditCardNumber>378291726006009</CreditCardNumber> <CreditCardCVV>1234</CreditCardCVV> <fieldName>CreditCard</fieldName> </VarificationField> </FieldCollection> Say we need to select two different nodes say  CreditCardNumber and  CreditCardCVV out of a xml file and mask them. We can use "|" separator for specifying more then one path, like as follows. //*/CreditCardNumber/text() | //*/CreditCardCVV/text() As shown above we piped two different XPath to build a single XPathExpression which would be used to evaluate the XML documents. String path = "//*/CreditCardNumber/text() | //*/CreditCardCVV/text() "; XPathFactory factory = XPathFactory.newInstance

Publishing Web Service Quickly with Java Technology

javax.xml.ws.EndPoint can be used to publish a webservice without any Application Server or Servlet Container. It uses HTTP Server that comes with Java. The static method  publish takes the String endpoint address and web service implementor class object as parameter. public class EndpointPublish { public static void main(String[] args) { Endpoint.publish("http://localhost:8088/ws/SiddMyTestWS", new SimpleHellowServiceImpl ()); } } For example just running the above stand alone java program, we are able to publish a webservice with the address   http://localhost:8088/ws/SiddMyTestWS . Where  MyWSTest is the implemented Endpoint class. Implemented EndPoint class in JAX-WS should have the following properties The implemented class should be annotated  javax.jws.WebService or javax.jws.WebServiceProvider. The implemented class should not be final or abstract . Exposed web service method should be annotated with javax.jws.WebMethod. Web service method cannot

Quick Tomcat SSL Setup

Setting up SSL in Tomcat is not that tough using JDK and Tomcat alone.But for production environment  OpenSSL is recomended. In the following section we will see how we can setup SSL in tomcat, so that the client can establish HTTPS connection with the Tomcat Server Instance. Mainly we have to follow the following steps, Generating Keystore using JDK keytool utility Configuring the Tomcat Instance to use the Keystore . Lets see now these steps in details, 1) Generating Keystore using JDK keytool Utility Here we will be generating a keystore with certificate, which we can use in next step to configure the Tomcat Instance. It is assumed that you have have JDK 5 or higher installed in your box along with Tomcat 5 or higher. Now go to the command prompt and run the following keytool command. keytool - genkey -alias tomcatkeys - keys tore siddtomcat5.keystore where the alias name is tomcatkeys keystore  and keystore name is  siddtomcat5.keystore. It will ask you some

How to take JAX-WS request and response dump

Image
It took me a while to figure out how to see the request and response along with HTTP headers for JAX-WS based clients. Definitely we can write Handler to log the request/response, but that wont generate the HTTP headers. The quickest and best way for debugging purpose is to set Http TransportPipe.dump  property. We can set the following JVM property to see the Request and Response dump while invoking a particular web service using JAX-WS. The property should be set as, -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump="true" The dump generated will be outputted to System.out, and it contains The Whole SOAP Request and Response along with HTTP headers. To set the property go to Run Configuration , select the configuration and then go to the Arguments Tab. Enter the value in the box marked as  VM Arguments . Now upon calling web service from your client though eclipse, you should be getting the reqeust and response in the console.

JAX-WS wsimport tool to generate artifact classes

wsimport is a tool which generates Java-WS artifacts like the service end point classes from the wsdl. This tool comes with Java and used in top down approach when we need to create artifacts from wsdl. wsimport tools comes with JDK. The following shows how to use wsimport.bat on Windows. wsimport.bat -verbose -p javaexp.ws.wsgen  -d C:\Siddhartha\dev\Projects\JavaExp\MyJWS2\src  -keep CalculatorSimpleGen.wsdl The above command will generate the followings classes along with source (since we specified the -keep option). javaexp\ws\wsgen\CalculatorWS.java javaexp\ws\wsgen\CalculatorWSImplService.java javaexp\ws\wsgen\MathOperationRQ.java javaexp\ws\wsgen\MathOperationRS.java javaexp\ws\wsgen\MetaDataInfo.java javaexp\ws\wsgen\ObjectFactory.java javaexp\ws\wsgen\package-info.java javaexp\ws\wsgen\CalculatorWS.java javaexp\ws\wsgen\CalculatorWSImplService.java javaexp\ws\wsgen\MathOperationRQ.java javaexp\ws\wsgen\MathOperationRS.java javaexp\ws\wsgen\MetaDataInfo.java javaexp\

JDK Keytool for importing certificates to keystore

Keystore is a Certificate and Key management tools that comes with JDK. Keytool stores Key and Certificate in keystore. The following shows commonly used commands with Keystore. I found the following Keytool commands handy and very useful. Changing Alias Of Existing Certificate in Keystore ( keystore.javaexpblog  ). keytool -changealias -srcalias https -destalias sidd-https -keystore keystore.javaexpblog  -storepass mykeypassword. List all the certificates In a Keystore ( keystore.javaexpblog) . keytool -list -keystore keystore.javaexpblog -storepass mykeypassword. See Details of Certificate with alias https keytool -list -keystore keystore.javaexpblog -storepass mykeypassword -v -alias https. Deleting Certificate from Keystore sudo keytool -delete -alias sidd-e2 -keystore keystore.javaexpblog -storepass mykeypassword Importing New Cert ( certificate_2.cer ) To Existing Keystore keytool -import -trustcacerts -keystore keystore.javaexpblog -alias sidd-e2 -storepass my

JAX-RS Jersey Client Example With Post

Let's take example of the operateXMLNumbers restful service, which excepts data in XML format and also returns data in XML format. This service is simple and add two numbers and returns the data. Following show's the service method written using Jersey. Notice that the parameter is mapped object type MathRequest. The Full code can be found at google code. Service Method @POST @Path("operateXMLNumbers") @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) public MathResponse addTwoXMLNumbers(MathRequest rq) { String numa = rq.getOperandA().toString(); String numb = rq.getOperandB().toString(); String operator1 = rq.getOperationTypeNew(); Integer result = doCalculateCoreLogic(operator1, numa, numb); MathResponse rs = new MathResponse(); rs.setMathOperationRQ(rq); rs.setResult(result); return rs; } Client private static void callTwoXMLNumbers() { Client client = Client.create(); We