javax.xml.soap.SOAPConnection Java Examples

The following examples show how to use javax.xml.soap.SOAPConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractWsSender.java    From freehealth-connector with GNU Affero General Public License v3.0 7 votes vote down vote up
protected GenericResponse call(GenericRequest genericRequest) throws TechnicalConnectorException {
   SOAPConnection conn = null;
   Handler[] chain = genericRequest.getHandlerchain();

   GenericResponse var6;
   try {
      SOAPMessageContext request = this.createSOAPMessageCtx(genericRequest);
      request.putAll(genericRequest.getRequestMap());
      request.put("javax.xml.ws.handler.message.outbound", true);
      executeHandlers(chain, request);
      conn = scf.createConnection();
      SOAPMessageContext reply = createSOAPMessageCtx(conn.call(request.getMessage(), generateEndpoint(request)));
      reply.putAll(genericRequest.getRequestMap());
      reply.put("javax.xml.ws.handler.message.outbound", false);
      ArrayUtils.reverse(chain);
      executeHandlers(chain, reply);
      var6 = new GenericResponse(reply.getMessage());
   } catch (Exception var10) {
      throw translate(var10);
   } finally {
      ConnectorIOUtils.closeQuietly((Object)conn);
   }

   return var6;
}
 
Example #2
Source File: AbstractWsSender.java    From freehealth-connector with GNU Affero General Public License v3.0 7 votes vote down vote up
protected GenericResponse call(GenericRequest genericRequest) throws TechnicalConnectorException {
   SOAPConnection conn = null;
   Handler[] chain = genericRequest.getHandlerchain();

   GenericResponse genericResponse;
   try {
      SOAPMessageContext request = this.createSOAPMessageCtx(genericRequest);
      request.putAll(genericRequest.getRequestMap());
      request.put("javax.xml.ws.handler.message.outbound", true);
      executeHandlers(chain, request);
      conn = scf.createConnection();
      SOAPMessageContext reply = createSOAPMessageCtx(conn.call(request.getMessage(), generateEndpoint(request)));
      reply.putAll(genericRequest.getRequestMap());
      reply.put("javax.xml.ws.handler.message.outbound", false);
      ArrayUtils.reverse(chain);
   executeHandlers(chain, reply);
      genericResponse = new GenericResponse(reply.getMessage(), request.getMessage());
   } catch (Exception var10) {
      throw translate(var10);
   } finally {
      ConnectorIOUtils.closeQuietly((Object)conn);
   }

   return genericResponse;
}
 
Example #3
Source File: AbstractWsSender.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
protected GenericResponse call(GenericRequest genericRequest) throws TechnicalConnectorException {
   SOAPConnection conn = null;
   Handler[] chain = genericRequest.getHandlerchain();

   GenericResponse var6;
   try {
      SOAPMessageContext request = this.createSOAPMessageCtx(genericRequest);
      request.putAll(genericRequest.getRequestMap());
      request.put("javax.xml.ws.handler.message.outbound", true);
      executeHandlers(chain, request);
      conn = scf.createConnection();
      SOAPMessageContext reply = createSOAPMessageCtx(conn.call(request.getMessage(), generateEndpoint(request)));
      reply.putAll(genericRequest.getRequestMap());
      reply.put("javax.xml.ws.handler.message.outbound", false);
      ArrayUtils.reverse(chain);
      executeHandlers(chain, reply);
      var6 = new GenericResponse(reply.getMessage());
   } catch (Exception var10) {
      throw translate(var10);
   } finally {
      ConnectorIOUtils.closeQuietly((Object)conn);
   }

   return var6;
}
 
Example #4
Source File: ProviderRPCClientServerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSWA() throws Exception {
    SOAPFactory soapFac = SOAPFactory.newInstance();
    MessageFactory msgFac = MessageFactory.newInstance();
    SOAPConnectionFactory conFac = SOAPConnectionFactory.newInstance();
    SOAPMessage msg = msgFac.createMessage();

    QName sayHi = new QName("http://apache.org/hello_world_rpclit", "sayHiWAttach");
    msg.getSOAPBody().addChildElement(soapFac.createElement(sayHi));
    AttachmentPart ap1 = msg.createAttachmentPart();
    ap1.setContent("Attachment content", "text/plain");
    msg.addAttachmentPart(ap1);
    AttachmentPart ap2 = msg.createAttachmentPart();
    ap2.setContent("Attachment content - Part 2", "text/plain");
    msg.addAttachmentPart(ap2);
    msg.saveChanges();

    SOAPConnection con = conFac.createConnection();
    URL endpoint = new URL("http://localhost:" + PORT
                           + "/SOAPServiceProviderRPCLit/SoapPortProviderRPCLit1");
    SOAPMessage response = con.call(msg, endpoint);
    QName sayHiResp = new QName("http://apache.org/hello_world_rpclit", "sayHiResponse");
    assertNotNull(response.getSOAPBody().getChildElements(sayHiResp));
    assertEquals(2, response.countAttachments());
}
 
Example #5
Source File: AbstractWsSender.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
protected GenericResponse call(GenericRequest genericRequest) throws TechnicalConnectorException {
   SOAPMessageContext request = this.createSOAPMessageCtx(genericRequest);
   request.putAll(genericRequest.getRequestMap());
   request.put("javax.xml.ws.handler.message.outbound", true);
   Handler<?>[] chain = genericRequest.getHandlerchain();
   SOAPConnection conn = null;
   SOAPMessageContext reply = null;

   try {
      URL endpoint = generateEndpoint(request);
      executeHandlers(chain, request);
      SOAPMessage msgToSend = request.getMessage();
      conn = scf.createConnection();
      reply = createSOAPMessageCtx(conn.call(msgToSend, endpoint));
   } catch (UnsupportedOperationException var13) {
      throw translate(var13);
   } catch (SOAPException var14) {
      throw translate(var14);
   } catch (MalformedURLException var15) {
      throw translate(var15);
   } finally {
      ConnectorIOUtils.closeQuietly((Object)conn);
   }

   reply.put("javax.xml.ws.handler.message.outbound", false);
   executeHandlers(chain, reply);
   return new GenericResponse(reply.getMessage());
}
 
Example #6
Source File: AbstractWsSender.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
protected GenericResponse call(GenericRequest genericRequest) throws TechnicalConnectorException {
   SOAPMessageContext request = this.createSOAPMessageCtx(genericRequest);
   request.putAll(genericRequest.getRequestMap());
   request.put("javax.xml.ws.handler.message.outbound", true);
   Handler<?>[] chain = genericRequest.getHandlerchain();
   SOAPConnection conn = null;
   SOAPMessageContext reply = null;

   try {
      URL endpoint = generateEndpoint(request);
      executeHandlers(chain, request);
      SOAPMessage msgToSend = request.getMessage();
      conn = scf.createConnection();
      reply = createSOAPMessageCtx(conn.call(msgToSend, endpoint));
   } catch (UnsupportedOperationException var13) {
      throw translate(var13);
   } catch (SOAPException var14) {
      throw translate(var14);
   } catch (MalformedURLException var15) {
      throw translate(var15);
   } finally {
      ConnectorIOUtils.closeQuietly((Object)conn);
   }

   reply.put("javax.xml.ws.handler.message.outbound", false);
   executeHandlers(chain, reply);
   return new GenericResponse(reply.getMessage());
}
 
Example #7
Source File: SignCode.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private String makeSigningRequest(List<File> filesToSign) throws SOAPException, IOException {
    log("Constructing the code signing request");

    SOAPMessage message = SOAP_MSG_FACTORY.createMessage();
    SOAPBody body = populateEnvelope(message, NS);

    SOAPElement requestSigning = body.addChildElement("requestSigning", NS);
    SOAPElement requestSigningRequest =
            requestSigning.addChildElement("requestSigningRequest", NS);

    addCredentials(requestSigningRequest, this.userName, this.password, this.partnerCode);

    SOAPElement applicationName =
            requestSigningRequest.addChildElement("applicationName", NS);
    applicationName.addTextNode(this.applicationName);

    SOAPElement applicationVersion =
            requestSigningRequest.addChildElement("applicationVersion", NS);
    applicationVersion.addTextNode(this.applicationVersion);

    SOAPElement signingServiceName =
            requestSigningRequest.addChildElement("signingServiceName", NS);
    signingServiceName.addTextNode(this.signingService);

    List<String> fileNames = getFileNames(filesToSign);

    SOAPElement commaDelimitedFileNames =
            requestSigningRequest.addChildElement("commaDelimitedFileNames", NS);
    commaDelimitedFileNames.addTextNode(StringUtils.join(fileNames));

    SOAPElement application =
            requestSigningRequest.addChildElement("application", NS);
    application.addTextNode(getApplicationString(fileNames, filesToSign));

    // Send the message
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = soapConnectionFactory.createConnection();

    log("Sending signing request to server and waiting for response");
    SOAPMessage response = connection.call(message, SIGNING_SERVICE_URL);

    if (debug) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2 * 1024);
        response.writeTo(baos);
        log(baos.toString("UTF-8"));
    }

    log("Processing response");
    SOAPElement responseBody = response.getSOAPBody();

    // Should come back signed
    NodeList bodyNodes = responseBody.getChildNodes();
    NodeList requestSigningResponseNodes = bodyNodes.item(0).getChildNodes();
    NodeList returnNodes = requestSigningResponseNodes.item(0).getChildNodes();

    String signingSetID = null;
    String signingSetStatus = null;

    for (int i = 0; i < returnNodes.getLength(); i++) {
        Node returnNode = returnNodes.item(i);
        if (returnNode.getLocalName().equals("signingSetID")) {
            signingSetID = returnNode.getTextContent();
        } else if (returnNode.getLocalName().equals("signingSetStatus")) {
            signingSetStatus = returnNode.getTextContent();
        }
    }

    if (!signingService.contains("TEST") && !"SIGNED".equals(signingSetStatus) ||
            signingService.contains("TEST") && !"INITIALIZED".equals(signingSetStatus) ) {
        throw new BuildException("Signing failed. Status was: " + signingSetStatus);
    }

    return signingSetID;
}
 
Example #8
Source File: SignCode.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void downloadSignedFiles(List<File> filesToSign, String id)
        throws SOAPException, IOException {

    log("Downloading signed files. The signing set ID is: " + id);

    SOAPMessage message = SOAP_MSG_FACTORY.createMessage();
    SOAPBody body = populateEnvelope(message, NS);

    SOAPElement getSigningSetDetails = body.addChildElement("getSigningSetDetails", NS);
    SOAPElement getSigningSetDetailsRequest =
            getSigningSetDetails.addChildElement("getSigningSetDetailsRequest", NS);

    addCredentials(getSigningSetDetailsRequest, this.userName, this.password, this.partnerCode);

    SOAPElement signingSetID =
            getSigningSetDetailsRequest.addChildElement("signingSetID", NS);
    signingSetID.addTextNode(id);

    SOAPElement returnApplication =
            getSigningSetDetailsRequest.addChildElement("returnApplication", NS);
    returnApplication.addTextNode("true");

    // Send the message
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = soapConnectionFactory.createConnection();

    log("Requesting signed files from server and waiting for response");
    SOAPMessage response = connection.call(message, SIGNING_SERVICE_URL);

    log("Processing response");
    SOAPElement responseBody = response.getSOAPBody();

    // Check for success

    // Extract the signed file(s) from the ZIP
    NodeList bodyNodes = responseBody.getChildNodes();
    NodeList getSigningSetDetailsResponseNodes = bodyNodes.item(0).getChildNodes();
    NodeList returnNodes = getSigningSetDetailsResponseNodes.item(0).getChildNodes();

    String result = null;
    String data = null;

    for (int i = 0; i < returnNodes.getLength(); i++) {
        Node returnNode = returnNodes.item(i);
        if (returnNode.getLocalName().equals("result")) {
            result = returnNode.getChildNodes().item(0).getTextContent();
        } else if (returnNode.getLocalName().equals("signingSet")) {
            data = returnNode.getChildNodes().item(1).getTextContent();
        }
    }

    if (!"0".equals(result)) {
        throw new BuildException("Download failed. Result code was: " + result);
    }

    extractFilesFromApplicationString(data, filesToSign);
}
 
Example #9
Source File: SignCodeMojo.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
private String makeSigningRequest(SignedFiles signedFiles) throws SOAPException, IOException, MojoExecutionException {
    log("Constructing the code signing request");

    SOAPMessage message = SOAP_MSG_FACTORY.createMessage();
    SOAPBody body = populateEnvelope(message, NS);

    SOAPElement requestSigning = body.addChildElement("requestSigning", NS);
    SOAPElement requestSigningRequest =
            requestSigning.addChildElement("requestSigningRequest", NS);

    addCredentials(requestSigningRequest, this.userName, this.password, this.partnerCode);

    SOAPElement applicationName =
            requestSigningRequest.addChildElement("applicationName", NS);
    applicationName.addTextNode(this.applicationName);

    SOAPElement applicationVersion =
            requestSigningRequest.addChildElement("applicationVersion", NS);
    applicationVersion.addTextNode(this.applicationVersion);

    SOAPElement signingServiceName =
            requestSigningRequest.addChildElement("signingServiceName", NS);
    signingServiceName.addTextNode(this.signingService);

    SOAPElement commaDelimitedFileNames =
            requestSigningRequest.addChildElement("commaDelimitedFileNames", NS);
    commaDelimitedFileNames.addTextNode(signedFiles.getCommaSeparatedUploadFileNames());

    SOAPElement application =
            requestSigningRequest.addChildElement("application", NS);
    application.addTextNode(signedFiles.getApplicationString());

    // Send the message
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = soapConnectionFactory.createConnection();

    log("Sending signing request to server and waiting for response");
    SOAPMessage response = connection.call(message, SIGNING_SERVICE_URL);

    if ( getLog().isDebugEnabled()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2 * 1024);
        response.writeTo(baos);
        getLog().debug(baos.toString("UTF-8"));
    }

    log("Processing response");
    SOAPElement responseBody = response.getSOAPBody();

    // Should come back signed
    NodeList bodyNodes = responseBody.getChildNodes();
    NodeList requestSigningResponseNodes = bodyNodes.item(0).getChildNodes();
    NodeList returnNodes = requestSigningResponseNodes.item(0).getChildNodes();

    String signingSetID = null;
    String signingSetStatus = null;
    StringBuilder errors = new StringBuilder();

    for (int i = 0; i < returnNodes.getLength(); i++) {
        Node returnNode = returnNodes.item(i);
        if (returnNode.getLocalName().equals("signingSetID")) {
            signingSetID = returnNode.getTextContent();
        } else if (returnNode.getLocalName().equals("signingSetStatus")) {
            signingSetStatus = returnNode.getTextContent();
        } else if (returnNode.getLocalName().equals("result") ) {
            final NodeList returnChildNodes = returnNode.getChildNodes();
            for (int j = 0; j < returnChildNodes.getLength(); j++ ) {
                if ( returnChildNodes.item(j).getLocalName().equals("errors") ) {
                    extractErrors(returnChildNodes.item(j), errors);
                }
            }
        }
    }

    if (!signingService.contains("TEST") && !"SIGNED".equals(signingSetStatus) ||
            signingService.contains("TEST") && !"INITIALIZED".equals(signingSetStatus) ) {
        throw new BuildException("Signing failed. Status was: " + signingSetStatus + " . Reported errors: " + errors + ".");
    }

    return signingSetID;
}
 
Example #10
Source File: SignCodeMojo.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
private void downloadSignedFiles(SignedFiles signedFiles, String id)
        throws SOAPException, IOException, BuildException {

    log("Downloading signed files. The signing set ID is: " + id);

    SOAPMessage message = SOAP_MSG_FACTORY.createMessage();
    SOAPBody body = populateEnvelope(message, NS);

    SOAPElement getSigningSetDetails = body.addChildElement("getSigningSetDetails", NS);
    SOAPElement getSigningSetDetailsRequest =
            getSigningSetDetails.addChildElement("getSigningSetDetailsRequest", NS);

    addCredentials(getSigningSetDetailsRequest, this.userName, this.password, this.partnerCode);

    SOAPElement signingSetID =
            getSigningSetDetailsRequest.addChildElement("signingSetID", NS);
    signingSetID.addTextNode(id);

    SOAPElement returnApplication =
            getSigningSetDetailsRequest.addChildElement("returnApplication", NS);
    returnApplication.addTextNode("true");

    // Send the message
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = soapConnectionFactory.createConnection();

    log("Requesting signed files from server and waiting for response");
    SOAPMessage response = connection.call(message, SIGNING_SERVICE_URL);

    log("Processing response");
    SOAPElement responseBody = response.getSOAPBody();

    // Check for success

    // Extract the signed file(s) from the ZIP
    NodeList bodyNodes = responseBody.getChildNodes();
    NodeList getSigningSetDetailsResponseNodes = bodyNodes.item(0).getChildNodes();
    NodeList returnNodes = getSigningSetDetailsResponseNodes.item(0).getChildNodes();

    String result = null;
    String data = null;

    for (int i = 0; i < returnNodes.getLength(); i++) {
        Node returnNode = returnNodes.item(i);
        if (returnNode.getLocalName().equals("result")) {
            result = returnNode.getChildNodes().item(0).getTextContent();
        } else if (returnNode.getLocalName().equals("signingSet")) {
            data = returnNode.getChildNodes().item(1).getTextContent();
        }
    }

    if (!"0".equals(result)) {
        throw new BuildException("Download failed. Result code was: " + result);
    }

    signedFiles.extractFilesFromApplicationString(data);
}
 
Example #11
Source File: CompilatioAPIUtil.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters, String secretKey,
		final int timeout) throws TransientSubmissionException, SubmissionException {

	SOAPConnectionFactory soapConnectionFactory;
	Document xmlDocument = null;
	try {
		soapConnectionFactory = SOAPConnectionFactory.newInstance();

		SOAPConnection soapConnection = soapConnectionFactory.createConnection();

		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage soapMessage = messageFactory.createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action"));
		parameters.remove("action");
		// api key
		SOAPElement soapBodyKey = soapBodyAction.addChildElement("key");
		soapBodyKey.addTextNode(secretKey);

		Set<Entry<String, String>> ets = parameters.entrySet();
		Iterator<Entry<String, String>> it = ets.iterator();
		while (it.hasNext()) {
			Entry<String, String> param = it.next();
			SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey());
			soapBodyElement.addTextNode(param.getValue());
		}
		
		URL endpoint = new URL(null, apiURL, new URLStreamHandler() {
			@Override
			protected URLConnection openConnection(URL url) throws IOException {
				URL target = new URL(url.toString());
				URLConnection connection = target.openConnection();
				// Connection settings
				connection.setConnectTimeout(timeout);
				connection.setReadTimeout(timeout);
				return(connection);
			}
		});
		
		SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint);

		// loading the XML document
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		soapResponse.writeTo(out);
		DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
		builderfactory.setNamespaceAware(true);

		DocumentBuilder builder = builderfactory.newDocumentBuilder();
		xmlDocument = builder.parse(new InputSource(new StringReader(out.toString())));
		soapConnection.close();

	} catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException | SAXException e) {
		log.error(e.getLocalizedMessage(), e);
	}
	return xmlDocument;

}
 
Example #12
Source File: SAAJTestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
/**
 * The main goal of this test is to exercise the SAAJ classes and make sure
 * that no exceptions are thrown. We build a SOAP message, we send it to a
 * mock SOAP server, which simply echoes the message back to us, and then we
 * check to make sure that the returned message is the same as the sent
 * message. We perform that check for equality in a very shallow way because
 * we are not seriously concerned about the possibility that the SOAP message
 * might be garbled. The check for equality should be thought of as only a
 * sanity check.
 */
private void testSAAJ(String protocol) throws Exception {
  // Create the message
  MessageFactory factory = MessageFactory.newInstance(protocol);
  SOAPMessage requestMessage = factory.createMessage();

  // Add a header
  SOAPHeader header = requestMessage.getSOAPHeader();
  QName headerName = new QName("http://ws-i.org/schemas/conformanceClaim/", "Claim", "wsi");
  SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
  headerElement.addAttribute(new QName("conformsTo"), "http://ws-i.org/profiles/basic/1.1/");

  // Add a body
  QName bodyName = new QName("http://wombat.ztrade.com", "GetLastTradePrice", "m");
  SOAPBody body = requestMessage.getSOAPBody();
  SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
  QName name = new QName("symbol");
  SOAPElement symbol = bodyElement.addChildElement(name);
  symbol.addTextNode("SUNW");

  // Add an attachment
  AttachmentPart attachment = requestMessage.createAttachmentPart();
  String stringContent =
      "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
  attachment.setContent(stringContent, "text/plain");
  attachment.setContentId("update_address");
  requestMessage.addAttachmentPart(attachment);

  // Add another attachment
  URL url = new URL("http://greatproducts.com/gizmos/img.jpg");
  // URL url = new URL("file:///etc/passwords");
  DataHandler dataHandler = new DataHandler(url);
  AttachmentPart attachment2 = requestMessage.createAttachmentPart(dataHandler);
  attachment2.setContentId("attached_image");
  requestMessage.addAttachmentPart(attachment2);

  // Send the message
  SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = soapConnectionFactory.createConnection();
  URL endpoint = new URL("http://wombat.ztrade.com/quotes");

  // Get the response. Our mock url-fetch handler will echo back the request
  SOAPMessage responseMessage = connection.call(requestMessage, endpoint);
  connection.close();

  assertEquals(requestMessage, responseMessage);
}
 
Example #13
Source File: CompilatioAPIUtil.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters, String secretKey,
		final int timeout) throws TransientSubmissionException, SubmissionException {

	SOAPConnectionFactory soapConnectionFactory;
	Document xmlDocument = null;
	try {
		soapConnectionFactory = SOAPConnectionFactory.newInstance();

		SOAPConnection soapConnection = soapConnectionFactory.createConnection();

		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage soapMessage = messageFactory.createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action"));
		parameters.remove("action");
		// api key
		SOAPElement soapBodyKey = soapBodyAction.addChildElement("key");
		soapBodyKey.addTextNode(secretKey);

		Set<Entry<String, String>> ets = parameters.entrySet();
		Iterator<Entry<String, String>> it = ets.iterator();
		while (it.hasNext()) {
			Entry<String, String> param = it.next();
			SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey());
			soapBodyElement.addTextNode(param.getValue());
		}
		
		URL endpoint = new URL(null, apiURL, new URLStreamHandler() {
			@Override
			protected URLConnection openConnection(URL url) throws IOException {
				URL target = new URL(url.toString());
				URLConnection connection = target.openConnection();
				// Connection settings
				connection.setConnectTimeout(timeout);
				connection.setReadTimeout(timeout);
				return(connection);
			}
		});
		
		SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint);

		// loading the XML document
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		soapResponse.writeTo(out);
		DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
		builderfactory.setNamespaceAware(true);

		DocumentBuilder builder = builderfactory.newDocumentBuilder();
		xmlDocument = builder.parse(new InputSource(new StringReader(out.toString())));
		soapConnection.close();

	} catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException | SAXException e) {
		log.error(e.getLocalizedMessage(), e);
	}
	return xmlDocument;

}
 
Example #14
Source File: SoapConnectionFactoryImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public SOAPConnection createConnection() throws SOAPException {
    return getSOAPConnectionFactory().createConnection();
}