Monday, September 12, 2011

JAX-WS Dispatch Clent

In this article i am going to discuss about how to create JAX-WS dispatch client As we know JAX-WS is an embeded feature of Java 6 which beats its counter part JAX-RPC because its faster than its counter part and adds flexibility to user to construct his own document literal style of Payload for advanced developer and also leverages to use any kind of parser and marshalling/unmarshalling api. But the current parser which JAX-WS uses is STAX which no doubt is the fasted parser till date.But for marshalling/unmarshalling the embedded JAX-WS uses JAXB if you like you can use other third party Data binding tool for your reference please read the link below from there you can decide which one to use :)
Data Binding performance
Now lets come to the real thing.Here goes my Dispatch client.
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
import org.services.encrypt.EncryptionException;
import org.services.encrypt.ShaEncrypter;

/**
*
* @author ajay.biswal
*/
public class ClientTest {

public static void main(String arg[]) throws SOAPException, EncryptionException {
QName serviceName = new QName("http://org.services.ajaywebservices/", "TestService");
QName portName = new QName("http://org.services.ajaywebservices/", "TestPort");
String endpointAddress =
"http://localhost:8080/myapps/Test";
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
Dispatch dispatch =
service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
Map> headers = new HashMap>();
ShaEncrypter encryptor = new ShaEncrypter();
String userEnc = encryptor.encrypt("ajaybiswal");
System.out.println("*******userEnc****" + userEnc);
String passEnc = encryptor.encrypt("ajaybiswal");
System.out.println("*******passEnc****" + passEnc);
String data = new String(userEnc + ":" + passEnc);
headers.put("Authorization", Collections.singletonList("Basic " + new String(data)));
BindingProvider bp = (BindingProvider) dispatch;
Map rc = bp.getRequestContext();
rc.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage request = factory.createMessage();
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPBody body = env.getBody();
SOAPElement operation = body.addChildElement("hasWorkflowgroup", "ns1",
"http://org.services.ajaywebservices/");
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("ajaybiswal");
request.saveChanges();
SOAPMessage response = dispatch.invoke(request);
QName responseName = new QName("http://org.services.ajaywebservices/", "hasWorkflowgroupResponse");
SOAPBodyElement bodyElement = (SOAPBodyElement) response.getSOAPBody().getChildElements(responseName).next();
String message = bodyElement.getFirstChild().getTextContent();
System.out.println("****REsponse******" + message);
}
}

Friday, May 13, 2011

A very simple way to send attachement to a webservices

As i know the SOA has a standard way i'e MTOM for sending and recieving attachement which i have described in one of my previous blog.What i found from this a some sort of inter-operatability issue as well as performance issue as i have to unnessarily encode the content(which one can think that the data is protected but the truth is its not bcos decoding is very easy) which even increases the size of the attachment also uncessary xml syntaxes are incorporated which even increases the size of data to be send while invoking the webservice.So i thought what if i want to have my own style of sending and recieving attachemts.Then what i can do is simply get the bytes of the attachment.using java.io.* api and send it as a parameter to the operation.But this solution can be used if you only want to send/recieve attachments.If you want to send more complex parameters then my suggestion is to use MTOM.