public static String sendSOAP( EndpointReference soapEPR, String requestString, String action, String operation) throws Exception { ServiceClient sender = PmServiceClient.getServiceClient(); OperationClient operationClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); // creating message context MessageContext outMsgCtx = new MessageContext(); // assigning message context's option object into instance variable Options opts = outMsgCtx.getOptions(); // setting properties into option log.debug(soapEPR); opts.setTo(soapEPR); opts.setAction(action); opts.setTimeOutInMilliSeconds(180000); log.debug(requestString); SOAPEnvelope envelope = null; try { SOAPFactory fac = OMAbstractFactory.getSOAP11Factory(); envelope = fac.getDefaultEnvelope(); OMNamespace omNs = fac.createOMNamespace( "http://rpdr.partners.org/", //$NON-NLS-1$ "rpdr"); //$NON-NLS-1$ // creating the SOAP payload OMElement method = fac.createOMElement(operation, omNs); OMElement value = fac.createOMElement("RequestXmlString", omNs); // $NON-NLS-1$ value.setText(requestString); method.addChild(value); envelope.getBody().addChild(method); } catch (FactoryConfigurationError e) { log.error(e.getMessage()); throw new Exception(e); } outMsgCtx.setEnvelope(envelope); operationClient.addMessageContext(outMsgCtx); operationClient.execute(true); MessageContext inMsgtCtx = operationClient.getMessageContext("In"); // $NON-NLS-1$ SOAPEnvelope responseEnv = inMsgtCtx.getEnvelope(); OMElement soapResponse = responseEnv.getBody().getFirstElement(); // System.out.println("Sresponse: "+ soapResponse.toString()); OMElement soapResult = soapResponse.getFirstElement(); // System.out.println("Sresult: "+ soapResult.toString()); String i2b2Response = soapResult.getText(); log.debug(i2b2Response); return i2b2Response; }
public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException { Options options = new Options(); options.setTo(new EndpointReference(targetEPR)); options.setAction("urn:uploadFileUsingSwA"); options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); MessageContext mc = new MessageContext(); System.out.println("Sending file : " + fileName + " as SwA"); FileDataSource fileDataSource = new FileDataSource(new File(fileName)); DataHandler dataHandler = new DataHandler(fileDataSource); String attachmentID = mc.addAttachment(dataHandler); SOAPFactory factory = OMAbstractFactory.getSOAP11Factory(); SOAPEnvelope env = factory.getDefaultEnvelope(); OMNamespace ns = factory.createOMNamespace("http://services.samples/xsd", "m0"); OMElement payload = factory.createOMElement("uploadFileUsingSwA", ns); OMElement request = factory.createOMElement("request", ns); OMElement imageId = factory.createOMElement("imageId", ns); imageId.setText(attachmentID); request.addChild(imageId); payload.addChild(request); env.getBody().addChild(payload); mc.setEnvelope(env); mepClient.addMessageContext(mc); mepClient.execute(true); MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); SOAPBody body = response.getEnvelope().getBody(); String imageContentId = body.getFirstChildWithName( new QName("http://services.samples/xsd", "uploadFileUsingSwAResponse")) .getFirstChildWithName(new QName("http://services.samples/xsd", "response")) .getFirstChildWithName(new QName("http://services.samples/xsd", "imageId")) .getText(); Attachments attachment = response.getAttachmentMap(); dataHandler = attachment.getDataHandler(imageContentId); File tempFile = File.createTempFile("swa-", ".gif"); FileOutputStream fos = new FileOutputStream(tempFile); dataHandler.writeTo(fos); fos.flush(); fos.close(); System.out.println("Saved response to file : " + tempFile.getAbsolutePath()); return response; }
public static void transferFile(File file, String destinationFile) throws Exception { Options options = new Options(); options.setTo(targetEPR); options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE); options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); // Increase the time out when sending large attachments options.setTimeOutInMilliSeconds(10000); options.setTo(targetEPR); options.setAction("urn:uploadFile"); // assume the use runs this sample at // <axis2home>/samples/soapwithattachments/ dir ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem( "../../repository", null); ServiceClient sender = new ServiceClient(configContext, null); sender.setOptions(options); OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP); MessageContext mc = new MessageContext(); FileDataSource fileDataSource = new FileDataSource(file); // Create a dataHandler using the fileDataSource. Any implementation of // javax.activation.DataSource interface can fit here. DataHandler dataHandler = new DataHandler(fileDataSource); String attachmentID = mc.addAttachment(dataHandler); SOAPFactory fac = OMAbstractFactory.getSOAP11Factory(); SOAPEnvelope env = fac.getDefaultEnvelope(); OMNamespace omNs = fac.createOMNamespace("http://service.soapwithattachments.sample", "swa"); OMElement uploadFile = fac.createOMElement("uploadFile", omNs); OMElement nameEle = fac.createOMElement("name", omNs); nameEle.setText(destinationFile); OMElement idEle = fac.createOMElement("attchmentID", omNs); idEle.setText(attachmentID); uploadFile.addChild(nameEle); uploadFile.addChild(idEle); env.getBody().addChild(uploadFile); mc.setEnvelope(env); mepClient.addMessageContext(mc); mepClient.execute(true); MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); SOAPBody body = response.getEnvelope().getBody(); OMElement element = body.getFirstElement() .getFirstChildWithName( new QName("http://service.soapwithattachments.sample", "return")); System.out.println(element.getText()); }
/** * Use the provided ServiceClient instance to create an OperationClient identified by the * operation QName provided. * * @param sc * @param operation * @return */ private OperationClient createOperationClient(ServiceClient sc, QName operation) { if (sc == null) { throw ExceptionFactory.makeWebServiceException(Messages.getMessage("ICCreateOpClientErr1")); } if (operation == null) { throw ExceptionFactory.makeWebServiceException(Messages.getMessage("ICCreateOpClientErr2")); } if (log.isDebugEnabled()) { log.debug("Creating OperationClient for operation: " + operation); } try { OperationClient client = sc.createClient(operation); return client; } catch (AxisFault e) { throw ExceptionFactory.makeWebServiceException(e); } }
/** * This method creates 3 soap envelopes for 3 different client based sessions. Then it randomly * choose one envelope for each iteration and send it to the ESB. ESB should be configured with * session affinity load balancer and the SampleClientInitiatedSession dispatcher. This will * output request number, session number and the server ID for each iteration. So it can be * observed that one session number always associated with one server ID. */ private void sessionfullClient() { String synapsePort = "8280"; int iterations = 100; boolean infinite = true; String pPort = getProperty("port", synapsePort); String pIterations = getProperty("i", null); String addUrl = getProperty("addurl", null); String trpUrl = getProperty("trpurl", null); String prxUrl = getProperty("prxurl", null); String sleep = getProperty("sleep", null); String session = getProperty("session", null); long sleepTime = -1; if (sleep != null) { try { sleepTime = Long.parseLong(sleep); } catch (NumberFormatException ignored) { } } if (pPort != null) { try { Integer.parseInt(pPort); synapsePort = pPort; } catch (NumberFormatException e) { // run with default value } } if (pIterations != null) { try { iterations = Integer.parseInt(pIterations); if (iterations != -1) { infinite = false; } } catch (NumberFormatException e) { // run with default values } } Options options = new Options(); options.setTo( new EndpointReference("http://localhost:" + synapsePort + "/services/LBService1")); options.setAction("urn:sampleOperation"); options.setTimeOutInMilliSeconds(10000000); try { SOAPEnvelope env1 = buildSoapEnvelope("c1", "v1"); SOAPEnvelope env2 = buildSoapEnvelope("c2", "v1"); SOAPEnvelope env3 = buildSoapEnvelope("c3", "v1"); SOAPEnvelope[] envelopes = {env1, env2, env3}; ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem("client_repo", null); ServiceClient client = new ServiceClient(configContext, null); // set addressing, transport and proxy url if (addUrl != null && !"null".equals(addUrl)) { client.engageModule("addressing"); options.setTo(new EndpointReference(addUrl)); } if (trpUrl != null && !"null".equals(trpUrl)) { options.setProperty(Constants.Configuration.TRANSPORT_URL, trpUrl); } else { client.engageModule("addressing"); } if (prxUrl != null && !"null".equals(prxUrl)) { HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties(); try { URL url = new URL(prxUrl); proxyProperties.setProxyName(url.getHost()); proxyProperties.setProxyPort(url.getPort()); proxyProperties.setUserName(""); proxyProperties.setPassWord(""); proxyProperties.setDomain(""); options.setProperty(HTTPConstants.PROXY, proxyProperties); } catch (MalformedURLException e) { throw new AxisFault("Error creating proxy URL", e); } } client.setOptions(options); int i = 0; int sessionNumber = 0; String[] cookies = new String[3]; boolean httpSession = session != null && "http".equals(session); int cookieNumber = 0; while (i < iterations || infinite) { i++; if (sleepTime != -1) { try { Thread.sleep(sleepTime); } catch (InterruptedException ignored) { } } MessageContext messageContext = new MessageContext(); sessionNumber = getSessionTurn(envelopes.length); messageContext.setEnvelope(envelopes[sessionNumber]); cookieNumber = getSessionTurn(cookies.length); String cookie = cookies[cookieNumber]; if (httpSession) { setSessionID(messageContext, cookie); } try { OperationClient op = client.createClient(ServiceClient.ANON_OUT_IN_OP); op.addMessageContext(messageContext); op.execute(true); MessageContext responseContext = op.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); String receivedCookie = extractSessionID(responseContext); String receivedSetCookie = getSetCookieHeader(responseContext); if (httpSession) { if (receivedSetCookie != null && !"".equals(receivedSetCookie)) { cookies[cookieNumber] = receivedCookie; } } SOAPEnvelope responseEnvelope = responseContext.getEnvelope(); OMElement vElement = responseEnvelope.getBody().getFirstChildWithName(new QName("Value")); System.out.println( "Request: " + i + " with Session ID: " + (httpSession ? cookie : sessionNumber) + " ---- " + "Response : with " + (httpSession && receivedCookie != null ? (receivedSetCookie != null ? receivedSetCookie : receivedCookie) : " ") + " " + vElement.getText()); } catch (AxisFault axisFault) { System.out.println( "Request with session id " + (httpSession ? cookie : sessionNumber) + " " + "- Get a Fault : " + axisFault.getMessage()); } } } catch (AxisFault axisFault) { System.out.println(axisFault.getMessage()); } }