SDK - Creating Java Proxy Classes for the TRIM Web Service

  • KM510478
  • 20-Sep-2008
  • 21-Nov-2013

Archived Content: This information is no longer maintained and is provided "as is" for your convenience.

Summary

SDK - Creating Java Proxy Classes for the TRIM Web Service

Reference

Creating Java Proxy Classes for the TRIM Web Service

Return to TRIM Web Service Articles page

This article describes how to create Java Proxy Classes for the TRIM Web Service using the TRIM Web Service WSDL file.

Required Components

It is assumed that you are using the following components:

(Java Development IDEs other than Eclipse may provide similar results, but they have not been tested).

Generating Java Proxy Classes from the TRIM Web Service WSDL file

The steps involved in generating Java Proxy Classes from the TRIM Web Service WSDL file are as follows:

Install the WSDL2Java Eclipse Plugin

Assuming you have installed Java 2 Platform Standard Edition 1.5.0 and Eclipse 3.1.2, you now need to install the WSDL2Java plugin for Eclipse.

Download the plugin and unzip it to the same directory you installed eclipse (e.g. If eclipse is in C:\eclipse, then unzip to C:\).

Setup your Java Project

Create a Project within Eclipse and save it.

On the Project menu click on Properties and then within the Properties dialog, click on the Java Build Path entry.

In the Libraries tab, click Add External JARs....

Navigate to the Eclipse Plugins folder,then to the org.apache.axis folder and finally enter the lib folder.

Select all of the jar files in this folder and click open to add them to your Project.

Click OK.

Download the TRIM Web Service WSDL file

Download the TRIM Web Service WSDL file to your local machine.

This involves using a web browser to connect to a running instance of the TRIM Web Service,

 eg: http://myserver/trimct2/trim.asmx 

and clicking on the Service Description link at the top of the page.

Once you have opened the Service Description, save the page with a .wsdl file extension.

 eg: TRIMWebService.wsdl.

Important Note

When sending a TRIMRequest object the Axis generated classes do not impose an ordering based on the order that the methods are called.

The Axis generated classes serialize the TrimRequest Object in order of the fields as they are listed in the WSDL.

This can lead, for example, to a Fetch Request coming before a RecordSearch request, which will produce an error because the Fetch request has no RecordSearch data to Fetch.

A solution to this is to move the Fetch element below the RecordSearch element in a local copy of the TRIM Web Service WSDL file before you generate your Proxy Classes.

Import the .wsdl file into your project

From within your project in Eclipse select File - Import - General - Filesystem, and navigate to your .wsdl file.

Select it and click Finish.

It is now included in your project.

Generate your Java Proxy Classes

From within your Project, select the .wsdl file you just imported.

Right click on it and click on the WSDL2Java - Generate menu entry.

Your Java Proxy Classes will now be automatically generated for you.

You can now begin coding against the TRIM Web Service using your newly generated Java Proxy Classes.

Programming Differences between .Net and Java

If you are familiar with programming against the TRIM Web Service using a .Net Language such as VB .Net or C#, you should be aware of a few minor differences in the way Java implements its TRIM Web Service Proxy Classes.

Java uses separate set and get methods in place of a single Property to set and get values

 RecordStringSearchClause rsc = new RecordStringSearchClause(); rsc.setType(RecordStringSearchClauseType.TitleWord); rsc.setArg("dugong");

Each type of Operation pertinent to an Object has a corresponding set Method defined within that Object

These can be used multiple times within a single Object instance,

 TrimRequest trimctRequest = new TrimRequest(); trimctRequest.setShortcutRecordUri(sru); trimctRequest.setFetch(fetch); trimctRequest.setRecordSearch(rs); trimctRequest.setFetch(fetch);

and will include And and Or Operations where relevant.

 rs.setRecordAndSearchClause(recordAndSearchClause); rs.setRecordOrSearchClause(recordOrSearchClause);

The RecordSearch Object must have certain properties set before it will work properly

These properties are Sort1, Sort2, Sort3 and FilterFinalizedState

 RecordSearch rs = new RecordSearch(); rs.setId("recordsearch"); rs.setRecordStringSearchClause(rsc); rs.setSort1(RecordSortFields.rsNone); rs.setSort2(RecordSortFields.rsNone); rs.setSort3(RecordSortFields.rsNone); rs.setFilterFinalizedState(FinalizedFilters.ffBoth);

Other TRIM Objects may have similar requirements.

Java uses the EngineSoap object to execute TRIM requests

 TrimRequest trimctRequest = new TrimRequest(); trimctRequest.setShortcutRecordUri(sru); trimctRequest.setFetch(fetch); trimctRequest.setRecordSearch(rs); trimctRequest.setFetch(fetch);	 java.net.URL host = new java.net.URL("http://192.168.150.185/trimct/trim.asmx"); 			 EngineLocator engine = new EngineLocator();			 EngineSoap soapEngine = engine.getEngineSoap(host);			 TrimResponse trimctResponse = soapEngine.execute(trimctRequest);

You may encounter other differences in approach in the Java model as against the .Net way of doing things

The trick is to be aware that differences of approach will exist and there is probably a way to do it in Java, you just have to do a little digging to find it out.

A Simple Sample

Keeping the above facts in mind, we can easily create a very simple sample in Java which accesses a TRIM Web Service, performing a Record Search and returning some information with a Fetch Operation.

public class TrimWebService {	  	public static void main(String[] args)	{  			Test();				}		private static void Test()	{		try		{			//Set up a Record Search Operation			RecordStringSearchClause rsc = new RecordStringSearchClause();			rsc.setType(RecordStringSearchClauseType.TitleWord);			rsc.setArg("dugong");									RecordSearch rs = new RecordSearch();			rs.setId("recordsearch");			rs.setRecordStringSearchClause(rsc);			rs.setSort1(RecordSortFields.rsNone);			rs.setSort2(RecordSortFields.rsNone);			rs.setSort3(RecordSortFields.rsNone);			rs.setFilterFinalizedState(FinalizedFilters.ffBoth);									//Find a Record by its URI			ShortcutRecordUri sru = new ShortcutRecordUri();			sru.setUri("313");						//Set up a Fetch Operation			SpecificationProperty recordNumber = new SpecificationProperty();			recordNumber.setName("recnumber");						SpecificationProperty recordTitle = new SpecificationProperty();			recordTitle.setName("rectitle");			SpecificationProperty recordRenditions = new SpecificationProperty();			recordRenditions .setName("child:recrendition");						SpecificationProperty recordRevisions = new SpecificationProperty();			recordRevisions.setName("child:recrevision");						SpecificationProperty[] specProperties = new SpecificationProperty[]{recordTitle, recordNumber, recordRenditions, recordRevisions};						Fetch fetch = new Fetch();			fetch.setId("recordfetch");			fetch.setItems(specProperties);									//Set up a TrimRequest Object			TrimRequest trimctRequest = new TrimRequest();			trimctRequest.setShortcutRecordUri(sru);			trimctRequest.setFetch(fetch);			trimctRequest.setRecordSearch(rs);			trimctRequest.setFetch(fetch);									//Setup the EngineSoap Object			java.net.URL host = new java.net.URL("http://myserver/trimct/trim.asmx"); 						EngineLocator engine = new EngineLocator();						EngineSoap soapEngine = engine.getEngineSoap(host);					                        //Execute the TrimRequest				TrimResponse trimctResponse = soapEngine.execute(trimctRequest);			                        //Report the results  			DoResults(trimctResponse);									}		catch(Exception err)		{			System.out.println(err.getMessage());		}			}		private static void DoResults(TrimResponse response)	{		SuccessResult sResult = response.getSuccessResult();				if(sResult != null)		{			System.out.println("SuccessResult");		}								ErrorResult errorResult = response.getErrorResult();		if(errorResult != null)		{			int errorNo = errorResult.getErrorNumber();			String errorMessage = errorResult.getMessage();			String errorId = errorResult.getId();			System.out.println("ErrorResult: " + errorId + " "  + errorNo + " " + errorMessage );		}						FetchInjectionUriResult fiuResult = response.getFetchInjectionUriResult();				if(fiuResult != null)		{			long fiuUri = fiuResult.getUri();			System.out.println("FetchInjectionUriResult Uri: "  + fiuUri );		}				FetchResult fResult = response.getFetchResult();		if(fResult != null)		{			String fetchId = fResult.getId();			System.out.println("FetchResult: " + fetchId);						TrimObject[] trimObjects = fResult.getObjects();						for(TrimObject trimObject:trimObjects)			{				long foUri = trimObject.getUri();								System.out.println("Fetch Uri: " +  String.valueOf(foUri));				Value[] vals = trimObject.getValues();								for(Value val: vals )				{					System.out.println(val.getName() + ": " + val.getVal());										Value[] innerVals = val.getChildren();										for(Value innerVal: innerVals)					{						System.out.println(innerVal.getName() + ": " + innerVal.getVal());												Value[] innermostVals = innerVal.getChildren();												for(Value innermostVal:innermostVals)						{							System.out.println(innermostVal.getName() + ": " + innermostVal.getVal());																											}											}				}										}		}				EndResponse endResponse = response.getEndResponse();				if(endResponse != null)		{			System.out.println("EndResponse");		}					}	}