Programming web service requests in a Java environment
After you set up your Java™ web service environment, you can use GenericWebServiceSample.java as a basis for creating a web service request.
In summary, a Java application
contains the following code:
- Create a web service object
- To instantiate a web service object, use the following example:
CMBGenericWebServiceService cs = new CMBGenericWebServiceServiceLocator(); cmbservice = cs.getCMBGenericWebService(); - Authenticate the web service request
- For security, you must create an authentication object in each request.
- Create an instance of an item (if applicable)
- The GenericWebServiceSample.java sample creates an instance of an XYZ_InsPolicy item.
- Wrap the XML request
- The GenericWebServiceSample.java sample passes
parameters into predefined message templates to create XML requests.
For example:
All of the web service message templates are defined in the file, SampleMessageTemplate.java. The basic operations templates include:String requestXML = MessageFormat.format( SampleMessageTemplate.TEMPLATE, new Object[] { authenticationDataXML, pid }); CMBXMLResponse response = null; response = cmbservice.processXMLRequest(requestXML, null); String replyXML = response.getXmlResponseText(); return replyXML;- AUTHENTICATION_DATA_TEMPLATE
- CREATE_ITEM_TEMPLATE
- QUERY_TEMPLATE
- RETRIEVE_ITEM_WITH_RESOURCE_URL_TEMPLATE
- RETRIEVE_ITEM_WITH_ATTACHMENTS_TEMPLATE
- UPDATE_CLAIM_TEMPLATE
- CREATE_LINKS_TEMPLATE
- DELETE_LINKS_TEMPLATE
- UPDATE_POLICY_TEMPLATE
- UPDATE_POLICY_EXT_TEMPLATE
- DELETE_ITEM_TEMPLATE
- BATCH_DELETE_TEMPLATE
- ADD_TO_FOLDER
- XYZ_InsPolicy_TEMPLATE
- XYZ_InsPolicy_FOLDER_TEMPLATE
- XYZ_InsPolicy__XYZ_Insured_TEMPLATE
- XYZ_InsPolicy__XYZ_VIN_TEMPLATE
- ICM_BASE_TEMPLATE
- XYZ_ClaimForm_TEMPLATE
- LIST_PROCESS_TEMPLATE
- START_PROCESS_TEMPLATE
- LIST_WORKPACKAGES_TEMPLATE
- CONTINUE_PROCESS_TEMPLATE
- TERMINATE_PROCESS_TEMPLATE
- Create a DOM from an XML string
- Building a DOM (Document Object Model) document out of an XML
message string helps you read the elements inside of the XML replies.
if (factory == null) { factory = DocumentBuilderFactory.newInstance(); } builder = factory.newDocumentBuilder(); Document document = null; document= builder.parse( new InputSource(new StringReader(replyXML))); - Attach binary content (if applicable)
- The web service client by using CMBGenericWebService endpoint can upload or download binary data to CMBGenericWebService endpoint that uses MIME as the attachment format.
- Parse the web services response
- The GenericWebServiceSample.java sample contains various methods for validating XML responses and reporting errors.
The following example wraps an XML response to retrieve the XYZ insurance policy (and a URL for its resource part) by using its persistent identifier.
- Java sample
-
public CMDocument retrievePolicyWithResourceURL( String authenticationDataXML, String pid) { String requestXML = MessageFormat.format( SampleMessageTemplate .RETRIEVE_ITEM_WITH_RESOURCE_URL_TEMPLATE, new Object[] { authenticationDataXML, pid }); CMBXMLResponse response = null; // call the web service with the xml request message try { response = cmbservice.processXMLRequest(requestXML, null); } catch (RemoteException e) { e.printStackTrace(); return null; } // Get the DOM object representing the xml message Document document = getDocument( response.getXmlResponseText()); if (document == null) { return null; } // parse the status of the response from web service if (parseRequestStatus(document) != true) { return null; } // return the array of PIDs for the resources associated // with the retrieved document Element policyElement = getElement(document, "XYZ_InsPolicy"); if (policyElement == null) { return null; } CMDocument policy = parsePolicy(policyElement); return policy; }