/* * Copyright (c) 2000-2008 LabVantage Solutions, Inc. All rights reserved. * * No part of this work may be reproduced or distributed without the * permission of LabVantage Solutions, Inc. * * If you are not authorized by LabVantage to utilize this * software and/or documentation, you must immediately discontinue any * further use or viewing of this software and documentation. * Violaters will be prosecuted to the fullest extent of the law by * LabVantage Solutions, Inc. * * Developed by LabVantage Solutions, Inc. * 1160 US Highway 22 East * Bridgewater, NJ, 08807 */ package com.labvantage.training; import com.labvantage.sapphire.webservices.transport.ActionBlockTransportBean; import com.labvantage.sapphire.webservices.transport.PropertyListCollectionTransportBean; import com.labvantage.sapphire.webservices.transport.PropertyListTransportBean; import com.labvantage.sapphire.webservices.transport.PropertyValueTransportBean; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; import sapphire.util.ActionBlock; import sapphire.xml.PropertyList; import java.io.*; import org.apache.axis.client.Service; import org.apache.axis.client.Call; import javax.xml.namespace.QName; /** * Enter description here * * @author gstimson * @version $Author: dkemp $ * $Source: /extra/CVS/development/procedures/idea/config/fileTemplates/includes/File\040header.java,v $ * $Revision: 1.1 $ * $Date: 2006/06/30 21:08:51 $ * $State: Exp $ * $Id: File\040header.java,v 1.1 2006/06/30 21:08:51 dkemp Exp $ */ //@todo Update JavaDoc public class RunWebService2 { /** * This class shows how to call complex web services in R5.2 onwards. * * In R5.2 the webservices architecture was enhanced to allow real objects to be used and returned to and from SOAP * rather than everything serialising to strings. This means that more of the Java API is not available such as SDIData, * SDIRequest, ActionBlock, SDIAttachment, etc * All these complex objects are wrapped by Beans (e.g. ActionBlockTransportBean) which serialise and deserialise the * API objects. For example you can pass an action block object into a new ActionBlockTransportBean, pass it to the web service * call and see a new bean returned. This bean then has a toActionBlock to obtain a real action block. * * For backwards compatibility the existing methods that accept and return xml strings still exist and can be used. * */ private static String getInput(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ return br.readLine(); } catch( IOException e ){ System.out.println( e.getMessage() ); return ""; } } public static void main( String[] args ){ try { String server; String port; String webapp; if ( args.length > 0 ){ server = args[0]; System.out.println( "Server: " + server ); if ( args.length > 1 ){ port = args[1]; System.out.println( "Port: " + port ); if ( args.length > 2 ){ webapp = args[2]; System.out.println( "Web App: " + webapp ); } else{ System.out.print( "Enter Web App: " ); webapp = getInput(); } } else{ System.out.print( "Enter Port: " ); port = getInput(); System.out.print( "Enter Web App: " ); webapp = getInput(); } } else{ System.out.print( "Enter Server: " ); server = getInput(); System.out.print( "Enter Port: " ); port = getInput(); System.out.print( "Enter Web App: " ); webapp = getInput(); } String endpoint = "http://" + server + ":" + port + "/" + webapp + "/services/SapphireWS?wsdl"; System.out.println( "RunWebService started..." ); System.out.println( "Web service URL = " + endpoint ); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName(new QName("http://soapinterop.org/", "getConnectionId")); String database; String username ; String password; if ( args.length > 3 ){ database = args[3]; System.out.println( "Database: " + database ); if ( args.length > 4 ){ username = args[4]; System.out.println( "Username: " + username ); if ( args.length > 5 ){ password = args[5]; System.out.println( "Password: " + password ); } else{ System.out.print( "Enter Password: " ); password = getInput(); } } else{ System.out.print( "Enter Username: " ); username = getInput(); System.out.print( "Enter Password: " ); password = getInput(); } } else{ System.out.print( "Enter Database: " ); database = getInput(); System.out.print( "Enter Username: " ); username = getInput(); System.out.print( "Enter Password: " ); password = getInput(); } if ( database.length() > 0 && username.length() > 0 && password.length() > 0 ){ // dbid, user, pass String connectionid = (String) call.invoke( new Object[] { database, username, password } ); if ( connectionid != null && connectionid.length() > 0 ){ System.out.println("Sent....connectionid = '" + connectionid + "'"); String sampledesc; if ( args.length > 6 ){ sampledesc = args[6]; System.out.println( "Sample Description: " + sampledesc ); } else{ System.out.print( "Enter Sample Description: " ); sampledesc = getInput(); } call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName(new QName("http://soapinterop.org/", "processActionBlock")); ActionBlock ab = new ActionBlock(); PropertyList pl_addsdi = new PropertyList(); pl_addsdi.setProperty( "sdcid", "Sample" ); pl_addsdi.setProperty( "sampledesc", "Created From RunWebService2" ); pl_addsdi.setProperty( "copies", "1" ); pl_addsdi.setProperty( "newkeyid1", "[newkeyid1]" ); ab.setAction( "AddSDI", "AddSDI", "1", pl_addsdi ); PropertyList pl_editsdi = new PropertyList(); pl_editsdi.setProperty( "sdcid", "Sample" ); pl_editsdi.setProperty( "keyid1", "[newkeyid1]" ); pl_editsdi.setProperty( "sampledesc", sampledesc ); ab.setAction( "EditSDI", "EditSDI", "1", pl_editsdi ); // R5.2 web services use Beans to transport real objects accross soap. In .NET you then just see the real object returned, however in Axis // you need to register the type mapping for the bean // You can also use WSDLToJava to build the stubs instead of manual type mapping QName actionBlockTransportBeanQname = new QName( "com.labvantage.sapphire.webservices", "ActionBlockTransport" ); Class actionBlockTransportBeanClass = ActionBlockTransportBean.class; call.registerTypeMapping( actionBlockTransportBeanClass, actionBlockTransportBeanQname, BeanSerializerFactory.class, BeanDeserializerFactory.class ); // As the action block also uses property lists we also need to register the propertylist bean. As mentioned before in .NET and other languages this is not needed // and the serialisers are automatically picked up from the .WSDD and .WSDL files. QName propertyListTransportBeanQname = new QName( "com.labvantage.sapphire.webservices", "PropertyListTransport" ); Class propertyListTransportBeanClass = PropertyListTransportBean.class; call.registerTypeMapping( propertyListTransportBeanClass, propertyListTransportBeanQname, BeanSerializerFactory.class, BeanDeserializerFactory.class ); QName propertyListCollectionTransportBeanQname = new QName( "com.labvantage.sapphire.webservices", "PropertyListCollectionTransport" ); Class propertyListCollectionTransportBeanClass = PropertyListCollectionTransportBean.class; call.registerTypeMapping( propertyListCollectionTransportBeanClass, propertyListCollectionTransportBeanQname, BeanSerializerFactory.class, BeanDeserializerFactory.class ); QName propertyValueTransportBeanQname = new QName( "com.labvantage.sapphire.webservices", "PropertyValueTransport" ); Class propertyValueTransportBeanClass = PropertyValueTransportBean.class; call.registerTypeMapping( propertyValueTransportBeanClass, propertyValueTransportBeanQname, BeanSerializerFactory.class, BeanDeserializerFactory.class ); // now need to specify the types of the parameters and return object call.setReturnType( actionBlockTransportBeanQname ); call.addParameter( "connectionId", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN ); call.addParameter( "actionBlock", actionBlockTransportBeanQname, javax.xml.rpc.ParameterMode.IN ); // finally run the method converting back and forth from actionblock to bean transport ab = ((ActionBlockTransportBean) call.invoke( new Object[] { connectionid, new ActionBlockTransportBean(ab) } )).toActionBlock(); System.out.println("Sent....action block"); System.out.println( "New Sample: " + ab.getBlockProperty( "newkeyid1" ) ); } else{ System.out.println("Invalid username, database or password provided as no connection Id could be obtained."); } } else{ System.out.println("No username, database or password provided."); } } catch (Exception e) { System.err.println(e.toString()); } } }