Java Code Examples for javax.xml.soap.SOAPMessage#countAttachments()

The following examples show how to use javax.xml.soap.SOAPMessage#countAttachments() . 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: SAAJTestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private static void assertAttachmentsAreEqual(SOAPMessage expected, SOAPMessage actual)
    throws Exception {
  int expectedNumAttachments = expected.countAttachments();
  int actualNumAttachments = actual.countAttachments();
  if (expectedNumAttachments != actualNumAttachments) {
    throw new Exception(
        "expectedNumAttachments="
            + expectedNumAttachments
            + "actualNumAttachments="
            + actualNumAttachments);
  }
  Iterator<?> expectedAttachmentIterator = expected.getAttachments();
  Iterator<?> actualAttachmentIterator = actual.getAttachments();
  for (int attachmentIndex = 0; attachmentIndex < actualNumAttachments; attachmentIndex++) {
    AttachmentPart expectedAttachment = (AttachmentPart) expectedAttachmentIterator.next();
    AttachmentPart actualAttachment = (AttachmentPart) actualAttachmentIterator.next();
    assertEquals(expectedAttachment, actualAttachment, attachmentIndex);
  }
}
 
Example 2
Source File: ConsumerGateway.java    From xroad-rest-gateway with European Union Public License 1.1 5 votes vote down vote up
@Override
protected String deserializeResponseData(Node responseNode, SOAPMessage message) throws SOAPException {
    // Remove namespace if it's required
    handleNamespace(responseNode);

    // If message has attachments, return the first attachment
    if (message.countAttachments() > 0) {
        logger.debug("SOAP attachment detected. Use attachment as response data.");
        return SOAPHelper.toString((AttachmentPart) message.getAttachments().next());
    }
    // Convert response to string
    return SOAPHelper.toString(responseNode);
}
 
Example 3
Source File: ProviderGateway.java    From xroad-rest-gateway with European Union Public License 1.1 5 votes vote down vote up
protected boolean processAttachment(Map map, SOAPMessage message) {
    // If message has attachments, use the first attachment as
    // request body
    if (message.countAttachments() > 0 && map.containsKey(Constants.PARAM_REQUEST_BODY)) {
        logger.debug("SOAP attachment detected. Use attachment as request body.", Constants.PARAM_REQUEST_BODY);
        List<String> values = new ArrayList<>();
        values.add(SOAPHelper.toString((AttachmentPart) message.getAttachments().next()));
        map.put(Constants.PARAM_REQUEST_BODY, values);
        return true;
    } else {
        map.remove(Constants.PARAM_REQUEST_BODY);
        return false;
    }
}
 
Example 4
Source File: HWSoapMessageProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SOAPMessage invoke(SOAPMessage request) {
    SOAPMessage response = null;
    try {
        SOAPBody body = SAAJUtils.getBody(request);
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response = sayHiResponse;
            if (request.countAttachments() > 0) {
                MessageFactory factory = MessageFactory.newInstance();
                InputStream is = getClass().getResourceAsStream("resources/sayHiRpcLiteralResp.xml");
                response = factory.createMessage(null, is);
                is.close();
                Iterator<AttachmentPart> it = CastUtils.cast(request.getAttachments(),
                                                             AttachmentPart.class);
                while (it.hasNext()) {
                    response.addAttachmentPart(it.next());
                }
            }
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response = greetMeResponse;
        } else {
            response = request;
            //response.writeTo(System.out);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 5
Source File: ConsumerGateway.java    From xroad-rest-gateway with European Union Public License 1.1 4 votes vote down vote up
@Override
protected String deserializeResponseData(Node responseNode, SOAPMessage message) throws SOAPException {
    // Put all the response nodes to map so that we can easily access them
    Map<String, String> nodes = SOAPHelper.nodesToMap(responseNode.getChildNodes());

    /**
     * N.B.! If signature is present (B: encrypt then sign) and we want
     * to verify it before going ahead with processing, this is the
     * place to do it. The signed part of the message is accessed:
     * nodes.get(Constants.PARAM_ENCRYPTED)
     */
    // Decrypt session key using the private key
    Decrypter symmetricDecrypter = RESTGatewayUtil.getSymmetricDecrypter(this.asymmetricDecrypter, nodes.get(Constants.PARAM_KEY), nodes.get(Constants.PARAM_IV));

    // If message has attachments, return the first attachment
    if (message.countAttachments() > 0) {
        logger.debug("SOAP attachment detected. Use attachment as response data.");
        // Return decrypted data
        return symmetricDecrypter.decrypt(SOAPHelper.toString((AttachmentPart) message.getAttachments().next()));
    }
    /**
     * N.B.! If signature is present (A: sign then encrypt) and we want
     * to verify it before going ahead with processing, this is the
     * place to do it. The signed part of the message is accessed:
     * symmetricDecrypter.decrypt(nodes.get(Constants.PARAM_ENCRYPTED))
     * UTF-8 String wrapper must left out from signature verification.
     */

    // Decrypt the response. UTF-8 characters are not showing correctly
    // if we don't wrap the decrypted data into a new string and
    // explicitly tell that it's UTF-8 even if encrypter and
    // decrypter handle strings as UTF-8.
    String decryptedResponse = new String(symmetricDecrypter.decrypt(nodes.get(Constants.PARAM_ENCRYPTED)).getBytes(StandardCharsets.UTF_8));
    // Convert encrypted response to SOAP
    SOAPElement encryptionWrapper = SOAPHelper.xmlStrToSOAPElement(decryptedResponse);
    // Remove all the children under response node
    SOAPHelper.removeAllChildren(responseNode);
    // Remove the extra <encryptionWrapper> element between response node
    // and the actual response. After the modification all the response
    // elements are directly under response.
    SOAPHelper.moveChildren(encryptionWrapper, (SOAPElement) responseNode, !this.omitNamespace);
    // Clone response node because removing namespace from the original 
    // node causes null pointer exception in AbstractResponseDeserializer 
    // when wrappers are not used. Cloning the original node, removing
    // namespace from the clone and returning the clone prevents the
    // problem to occur.
    Node modifiedResponseNode = (Node) responseNode.cloneNode(true);
    // Remove namespace if it's required
    handleNamespace(modifiedResponseNode);
    // Return the response
    return SOAPHelper.toString(modifiedResponseNode);
}
 
Example 6
Source File: DispatchImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public T invoke(T obj, boolean isOneWay) {
    checkError();
    try {
        if (obj instanceof SOAPMessage) {
            SOAPMessage msg = (SOAPMessage)obj;
            if (msg.countAttachments() > 0) {
                client.getRequestContext().put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, Boolean.TRUE);
            }
        } else if (context != null) {
            Boolean unwrapProperty = obj instanceof JAXBElement ? Boolean.FALSE : Boolean.TRUE;
            getRequestContext().put("unwrap.jaxb.element", unwrapProperty);
        }
        QName opName = (QName)getRequestContext().get(MessageContext.WSDL_OPERATION);

        boolean hasOpName;
        if (opName == null) {
            hasOpName = false;
            opName = isOneWay ? INVOKE_ONEWAY_QNAME : INVOKE_QNAME;
        } else {
            hasOpName = true;
            BindingOperationInfo bop = client.getEndpoint().getBinding()
                                        .getBindingInfo().getOperation(opName);
            if (bop == null) {
                addInvokeOperation(opName, isOneWay);
            }
        }
        Holder<T> holder = new Holder<>(obj);
        opName = calculateOpName(holder, opName, hasOpName);

        Object[] ret = client.invokeWrapped(opName, holder.value);
        if (isOneWay || ret == null || ret.length == 0) {
            return null;
        }
        return (T)ret[0];
    } catch (IllegalEmptyResponseException ie) {
        return null;
    } catch (Exception ex) {
        throw mapException(ex);
    }
}