Monday, April 25, 2011

Writing a Client for a SOAP web service

Given:
  1. Location of the wsdl
  2. The client jar file containing the Java objects to be used as parameters of the SOAP call. If this jar is not provided you create one using the wsconsume.sh tool in the JBOSS/bin/ folder.
  3. Ensure that the service is up and running and you can access the wsdl adding "?wsdl" to the end-point.
Steps:
  1. Ensure that client jar is in your project class path.
  2. Create a ServicePortFactory.java class as shown below
package com.myservice.ws.utils;

import ingestintegration.ingestintegration._public.AutoIngestService;

import com.myservice.contentingest.IngestFacade;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

import org.jboss.logging.Logger;

public class ServicePortFactory {

static Logger logger = Logger.getLogger(ServicePortFactory.class);
static URL BASE_SERVICE_URL;
static String WS_USER;
static String WS_PASSWORD;
static Service service = null;

static {
try {
BASE_SERVICE_URL = new URL("http://localhost:8080/mywebservice");
WS_USER = System.getProperty(
"com.ws.user", "");
WS_PASSWORD = System.getProperty(
"com.ws.password", "");
} catch (MalformedURLException e) {
logger.error("Cannot initialize system property.", e);
}
}

private static Service createService() throws MalformedURLException {

if (service == null) {
Authenticator.setDefault(new BasicHTTPAuthenticator(WS_USER,
WS_PASSWORD));
service = Service.create(new URL(BASE_SERVICE_URL,
"AutoIngestService?wsdl"), new QName(
"http://ingestIntegration/ingestintegration/public",
"AutoIngestService"));
}

return service;
}

public static AutoIngestService createPort() throws MalformedURLException {
Service service = createService();

AutoIngestService portType = service.getPort(new QName(
"http://ingestIntegration/ingestintegration/public",
"AutoIngestServiceSOAP12Port"), AutoIngestService.class);

((BindingProvider) portType).getRequestContext().put(
BindingProvider.USERNAME_PROPERTY, WS_USER);
((BindingProvider) portType).getRequestContext().put(
BindingProvider.PASSWORD_PROPERTY, WS_PASSWORD);
return portType;
}
}

Now use the code in this class to make a call to the webservice: Notice that the service proxy should be created using the factory. Also any no primitive parameters in the request should be created using factories, such as the LegacyIngest and the ONIXMetadata object listed below.

IngestFacade ingestFacade = ServicePortFactory.createMetadataPort();
ObjectFactory objFactory = new ObjectFactory();
IngestRequest req = objFactory.createIngestRequest();
LegacyIngest legacyIngest = objFactory.createLegacyIngest();
legacyIngest.setOwner("SCH");
legacyIngest.setSynchronous(true);
legacyIngest.setEAN13("1234567890123");

ONIXMetadata onixMeta = objFactory.createONIXMetadata();
onixMeta.setFileName("/tmp/Metadata_20110413173857.xml");
legacyIngest.setMetadata(onixMeta);

req.setLegacyIngest(legacyIngest);
ingestFacade.ingest(req);


0 Comments:

Post a Comment

<< Home