how to write a java client for a service, described by a schema
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   shane_omalley
Posted On:   Tuesday, October 30, 2001 07:31 AM

I have downloaded Apache SOAP implementation and got many of the samples working, but i'm a bit lost when I have to write a client to talk to a existing service described below.. I have scoured the web for online documentation or books on java SOAP clients and have found little... any help would be greeted with unbridled enthusiasm.... thanks, shane <!- - Request-Header - -> <!- - Response-Header - ->    More>>


I have downloaded Apache SOAP implementation and got many of the samples working, but i'm a bit lost when I have to write a client to talk to a existing service described below..



I have scoured the web for online documentation or books on java SOAP clients and have found little...
any help would be greeted with unbridled enthusiasm....


thanks,
shane



			


<!- - Request-Header - ->


<!- - Response-Header - ->





















   <<Less

Re: how to write a java client for a service, described by a schema

Posted By:   Bozidar_Dangubic  
Posted On:   Tuesday, October 30, 2001 08:07 AM

you need to create wsdl file for your service. soap download has examples and there are sites on the web with tutorials, etc... (official description on w3c.org/TR/wsdl). then you need to create a service. there is nothing to it, it is normal java class with some soap stuff. here is a simple one.

public class CacheService
{
private ApplicationCache applicationCache;
private Logger log;

/**
* Default Constructor.
* @throws SOAPException if an error occurs initializing the service.
*/
public CacheService() throws ServiceInitializationException
{
log = LoggerFactory.getLogger(CacheService.class.getName());
log.debug("About to instantiate ApplicationCache");
applicationCache = new ApplicationCache();
log.info("created successfully");
}

/**
* Looks up a Statute based on its number.
* @param soapCtx SOAP context supplied by SOAP container (not client)
* @param stauteNumber the number of the statute to lookup.
* @return the statute with the specified number
* @throws SOAPException if the statute could not be found
*/
public Statute getStatuteByNumber(SOAPContext soapCtx, String statuteNumber) throws SOAPException
{
log.info("getStatuteByNumber("+statuteNumber+")");
PersistentProxy proxy =
applicationCache.getByCode(Cache.STATUTE_CACHE, statuteNumber);

if (proxy != null)
{
return (Statute)proxy;
}
else
{
throw new SOAPException(Constants.FAULT_CODE_CLIENT,
"No statute found having number " +
statuteNumber);
}
}
}


then, once you have the service, you can create a client that accesses the service. here is a simple JUnit test that accesses service listed above.

public class TestCacheService extends TestCase
{
private URL url;
/**
* Constructor with test name.
* @param testName the name of the test.
*/
public TestCacheService(String testName)
{
super(testName);
}

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected void setUp() throws Exception
{
if (System.getProperty("use.tunnel") != null)
{
url = new URL("http://localhost:81/proto/rpcrouter");
}
else
{
url = new URL("http://localhost:80/proto/rpcrouter");
}
}

/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*/
protected void tearDown() throws Exception
{
}

/**
* Tests looking up a statute.
*/
public void testGetStatuteByNumberSuccess()
{
String statuteNumber = "13-26";

org.apache.soap.transport.http.SOAPHTTPConnection httpConnection =
new org.apache.soap.transport.http.SOAPHTTPConnection();

httpConnection.setUserName("admin");
httpConnection.setPassword("123");


Call call = new Call();
call.setSOAPTransport(httpConnection);
call.setSOAPMappingRegistry(new BOMMappingRegistry());
call.setTargetObjectURI("urn:CacheService");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setMethodName("getStatuteByNumber");

Vector params = new Vector();
params.addElement(new Parameter("statuteNumber",
String.class,
statuteNumber,
null));
call.setParams(params);
Response resp = null;

try
{
resp = call.invoke(url,"");
assertNotNull("SOAP response was null",resp);
}
catch (SOAPException sEx)
{
sEx.printStackTrace();
fail("Could not lookup CacheService: " + sEx.getMessage());
}

if (!resp.generatedFault())
{

Parameter p = resp.getReturnValue();
assertNotNull("Return Parameter was null",p);
Object tmp = p.getValue();
assertNotNull("Return Value was null", tmp);
Statute statute = (Statute)tmp;

assertEquals("Statute numbers don't match",
statuteNumber,
statute.getNumber());
}
else
{
Fault fault = resp.getFault();
fail("Fault looking up Statute: " +
"Fault Code -> " + fault.getFaultCode() +
"Fault String -> " + fault.getFaultString());
}
}
}


hope this gives you a general idea. you will probably need to create serializers for the types being transferred with soap, but all this is in documentation of soap. look in writing RPC clients for further information.
About | Sitemap | Contact