Setting up Java Based Web Service Client
To set up a java based web service client:
Download the WSDL and XSD files from Service Now server https://IP address/aimOSSTroubleTicketService/OSSJWSDLFile?baseURL=https://[IP Address]/aimOSSTroubleTicketService/JVTTroubleTicketWS , where IP address is the IP address of the Service Now host.
Download the
OSSJWSDLAndXSDFiles.zip
file containing the WSDL and XSD files. Extract the zip files to the required location.The zip file contains the following files:
JVTTroubleTicketSession.wsdl
WS-BaseNotification.wsdl
WS-Resource.wsdl
License.xml
xsd/notification/b-2.xsd
xsd/notification/bf-2.xsd
xsd/notification/r-2.xsd
xsd/notification/t-1.xsd
xsd/notification/ws-addr.xsd
troubleTicket/OSSJ-Common-v1-5.xsd
troubleTicket/OSSJ-Common-CBEBi-v1-5.xsd
troubleTicket/OSSJ-Common-CBECore-v1-5.xsd
troubleTicket/OSSJ-Common-CBEDatatypes-v1-5.xsd
troubleTicket/OSSJ-Common-CBELocation-v1-5.xsd
troubleTicket/OSSJ-Common-CBEParty-v1-5.xsd
troubleTicket/OSSJ-Common-SharedAlarm-v1-5.xsd
troubleTicket/OSSJ-TroubleTicket-CBETrouble-v1-2.xsd
troubleTicket/OSSJ-TroubleTicket-v1-2.xsd
troubleTicket/OSSJ-TroubleTicket_x790-v0-5.xsd
In a windows system, select START > RUN to open the command prompt. Type cmd in the Run dialog box, and then press OK. Navigate to the location where the zip file has been extracted.
Navigate to the location where the zip file is extracted and run the following command to generate the service Now OSS/J web service client binaries: wsimport –d [LOCATION_FOR_CLIENT_BINARIES] JVTTroubleTicketSession.wsdl. where LOCATION_FOR_CLIENT_BINARIES is the location to generate the web service client.
Example– OSSJTroubleTicketClient.java:
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.xml.bind.JAXBElement; import javax.xml.ws.BindingProvider; import javax.xml.ws.handler.Handler; import org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl; import org.ossj.wsdl.troubleticket.v1_2.JVTTroubleTicketSessionWSPort; import org.ossj.wsdl.troubleticket.v1_2.JVTTroubleTicketSessionWebService; import org.ossj.xml.common.ArrayOfString; import org.ossj.xml.troubleticket.v1_2.*; public class OSSJTroubleTicketClient { public static void main(String[] args) { try { //create web service client object JVTTroubleTicketSessionWebService webService1 = new JVTTroubleTicketSessionWebService(); //get the port from the webservice client JVTTroubleTicketSessionWSPort port = webService1.getJVTTroubleTicketSessionWSPort(); //disable SSL certificate verification - this will be needed when using HTTPS server. disableCertificateValidation(); //Authentication data must be added into SOAP request, for this creating a handler //chain which adds the authentication in SOAP header of the outgoing message. //The handler chain is then associated with the webservice port List<Handler> handlerChain = new ArrayList<Handler>(); handlerChain.add(new SOAPLoggingHandler()); BindingProvider bindingProvider = (BindingProvider) port; List<javax.xml.ws.handler.Handler> ls = bindingProvider.getBinding().getHandlerChain(); ls.add(new SOAPLoggingHandler()); bindingProvider.getBinding().setHandlerChain(handlerChain); //create request for creating trouble ticket CreateTroubleTicketByValueRequest request = createTroubleTicketValueRequest(); //invoke the createTroubleTicketByValue API CreateTroubleTicketByValueResponse response = port.createTroubleTicketByValue(request); } catch (Exception e) { e.printStackTrace(); } } public static void disableCertificateValidation() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} }}; // Ignore differences between given hostname and certificate hostname HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Exception e) {} } private static CreateTroubleTicketByValueRequest createTroubleTicketValueRequest() { TroubleTicketValue value = new ObjectFactory().createTroubleTicketValue(); //set the values in TroubleTicketValue object CreateTroubleTicketByValueRequest request = new ObjectFactory().createCreateTroubleTicketByValueRequest(); request.setTroubleTicketValue(value); return request; } }
Example–SOAPLoggingHandler.java
import java.io.ByteArrayOutputStream; import java.util.Set; import java.util.logging.Logger; import javax.xml.namespace.QName; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> { private static Logger logger = Logger.getLogger(SOAPLoggingHandler.class.getName()); public boolean handleMessage(SOAPMessageContext context) { Boolean outGoingMsg = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); SOAPMessage soapMsg = context.getMessage(); if(soapMsg != null && soapMsg.getSOAPPart() != null) { SOAPEnvelope soapEnv; try { soapEnv = soapMsg.getSOAPPart().getEnvelope(); SOAPHeader soapHeader = soapEnv.getHeader(); if (soapHeader == null) { soapHeader = soapEnv.addHeader(); } addAuthentication(soapHeader); } catch (SOAPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (outGoingMsg) System.out.println("########outgoing soap message######"); else System.out.println("########incoming soap message############"); logSoapMessage(context); return true; } public boolean handleFault(SOAPMessageContext context) { System.out.println("########Fault soap message######"); logSoapMessage(context); return true; } public void close(MessageContext context) { } public void logSoapMessage(SOAPMessageContext context) { try { SOAPMessage msg = context.getMessage(); ByteArrayOutputStream bas = new ByteArrayOutputStream(); msg.writeTo(bas); System.out.println(bas); } catch (Exception e) { System.out.println("Error while writing SOAP message to debug log " + e); } } public Set<QName> getHeaders() { return null; } private void addAuthentication(SOAPHeader header) { try { SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis- open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); SOAPElement username = usernameToken.addChildElement("Username", "wsse"); username.addTextNode("***"); SOAPElement password = usernameToken.addChildElement("Password", "wsse"); password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); password.addTextNode("***"); } catch (Exception e) { e.printStackTrace(); } } }