Example usage for javax.xml.soap SOAPBodyElement getLocalName

List of usage examples for javax.xml.soap SOAPBodyElement getLocalName

Introduction

In this page you can find the example usage for javax.xml.soap SOAPBodyElement getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:com.offbynull.portmapper.upnpigd.UpnpIgdController.java

private Map<String, String> parseResponseXml(String expectedTagName, byte[] data) {
    try {/*w  w w  .j  av  a 2s  .c o m*/
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage soapMessage = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(data));

        if (soapMessage.getSOAPBody().hasFault()) {
            StringWriter writer = new StringWriter();
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                transformer.transform(new DOMSource(soapMessage.getSOAPPart()), new StreamResult(writer));
            } catch (IllegalArgumentException | TransformerException | TransformerFactoryConfigurationError e) {
                writer.append("Failed to dump fault: " + e);
            }

            throw new ResponseException(writer.toString());
        }

        Iterator<SOAPBodyElement> responseBlockIt = soapMessage.getSOAPBody()
                .getChildElements(new QName(serviceType, expectedTagName));
        if (!responseBlockIt.hasNext()) {
            throw new ResponseException(expectedTagName + " tag missing");
        }

        Map<String, String> ret = new HashMap<>();

        SOAPBodyElement responseNode = responseBlockIt.next();
        Iterator<SOAPBodyElement> responseChildrenIt = responseNode.getChildElements();
        while (responseChildrenIt.hasNext()) {
            SOAPBodyElement param = responseChildrenIt.next();
            String name = StringUtils.trim(param.getLocalName().trim());
            String value = StringUtils.trim(param.getValue().trim());

            ret.put(name, value);
        }

        return ret;
    } catch (IllegalArgumentException | IOException | SOAPException | DOMException e) {
        throw new IllegalStateException(e); // should never happen
    }
}

From source file:org.springframework.integration.sqs.AWSSecurityHandler.java

/**
 * {@inheritDoc}/*from ww  w . j a  v a  2  s.  c o  m*/
 */
public boolean handleMessage(final SOAPMessageContext context) {
    logMessage(context);
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (!outboundProperty) {
        return true;
    }

    /*
     * Example SOAP header from
     * http://docs.amazonwebservices.com/AWSSimpleQueueService
     * /2008-01-01/SQSDeveloperGuide
     * /MakingRequests_MakingSOAPRequestsArticle.html
     * 
     * <soapenv:Header
     * xmlns:aws="http://security.amazonaws.com/doc/2007-01-01/">
     * <aws:AWSAccessKeyId>1D9FVRAYCP1VJS767E02EXAMPLE</aws:AWSAccessKeyId>
     * <aws:Timestamp>2008-02-10T23:59:59Z</aws:Timestamp>
     * <aws:Signature>SZf1CHmQ/nrZbsrC13hCZS061ywsEXAMPLE</aws:Signature>
     * </soapenv:Header>
     */

    SOAPMessage aSOAPMessage = context.getMessage();
    try {
        SOAPEnvelope aEnvelope = aSOAPMessage.getSOAPPart().getEnvelope();
        SOAPHeader aHeader = aEnvelope.addHeader();
        String aTimestampStr = this.getTimestamp();
        // ADD AWS SECURITY HEADER ----------------------------------------
        aHeader.addNamespaceDeclaration(NAMESPACE_AWS_PREFIX, NAMESPACE_AWS);

        // ADD ACCESS KEY -------------------------------------------------
        Name aKeyName = aEnvelope.createName("AWSAccessKeyId", NAMESPACE_AWS_PREFIX, NAMESPACE_AWS);
        SOAPHeaderElement aKey = aHeader.addHeaderElement(aKeyName);
        aKey.addTextNode(s_key);

        // ADD TIMESTAMP --------------------------------------------------
        Name aTimestampName = aEnvelope.createName("Timestamp", NAMESPACE_AWS_PREFIX, NAMESPACE_AWS);
        SOAPHeaderElement aTimestamp = aHeader.addHeaderElement(aTimestampName);
        aTimestamp.addTextNode(aTimestampStr);

        // ADD SIGNATURE --------------------------------------------------
        Name aSignatureName = aEnvelope.createName("Signature", NAMESPACE_AWS_PREFIX, NAMESPACE_AWS);
        SOAPHeaderElement aSignature = aHeader.addHeaderElement(aSignatureName);

        SOAPBody aBody = aEnvelope.getBody();
        Iterator<?> aChildren = aBody.getChildElements();
        SOAPBodyElement aAction = (SOAPBodyElement) aChildren.next();
        if (aChildren.hasNext()) {
            throw new IllegalStateException(
                    "Unexpected number of actions in soap request. Cannot calculate signature.");
        }
        aSignature.addTextNode(this.calculateSignature(aAction.getLocalName(), aTimestampStr));
        aSOAPMessage.saveChanges();
        logMessage("Final out message: ", aSOAPMessage);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to add aws headers!", e);
    }
    return true;
}

From source file:org.wso2.carbon.identity.sso.saml.servlet.SAMLArtifactResolveServlet.java

/**
 * All requests are handled by this handleRequest method. Request should come with a soap envelop that
 * wraps an ArtifactResolve object. First we try to extract resolve object and if successful, call
 * handle artifact method.//from   w w  w .  jav  a 2s.  c  o  m
 *
 * @param req  HttpServletRequest object received.
 * @param resp HttpServletResponse object to be sent.
 * @throws ServletException
 * @throws IOException
 */
private void handleRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    try {
        ArtifactResolve artifactResolve = null;
        try {
            MessageFactory messageFactory = MessageFactory.newInstance();
            InputStream inStream = req.getInputStream();
            SOAPMessage soapMessage = messageFactory.createMessage(new MimeHeaders(), inStream);
            if (log.isDebugEnabled()) {
                OutputStream outputStream = new ByteArrayOutputStream();
                soapMessage.writeTo(outputStream);
                log.debug("SAML2 Artifact Resolve request received: " + outputStream.toString());
            }
            SOAPBody soapBody = soapMessage.getSOAPBody();
            Iterator iterator = soapBody.getChildElements();

            while (iterator.hasNext()) {
                SOAPBodyElement artifactResolveElement = (SOAPBodyElement) iterator.next();

                if (StringUtils.equals(SAMLConstants.SAML20P_NS, artifactResolveElement.getNamespaceURI())
                        && StringUtils.equals(ArtifactResolve.DEFAULT_ELEMENT_LOCAL_NAME,
                                artifactResolveElement.getLocalName())) {

                    DOMSource source = new DOMSource(artifactResolveElement);
                    StringWriter stringResult = new StringWriter();
                    TransformerFactory.newInstance().newTransformer().transform(source,
                            new StreamResult(stringResult));
                    artifactResolve = (ArtifactResolve) SAMLSSOUtil.unmarshall(stringResult.toString());
                }
            }
        } catch (SOAPException e) {
            throw new ServletException("Error while extracting SOAP body from the request.", e);
        } catch (TransformerException e) {
            throw new ServletException("Error while extracting ArtifactResponse from the request.", e);
        } catch (IdentityException e) {
            throw new ServletException("Error while unmarshalling ArtifactResponse  from the request.", e);
        }

        if (artifactResolve != null) {
            handleArtifact(req, resp, artifactResolve);
        } else {
            log.error("Invalid SAML Artifact Resolve request received.");
        }

    } finally {
        SAMLSSOUtil.removeSaaSApplicationThreaLocal();
        SAMLSSOUtil.removeUserTenantDomainThreaLocal();
        SAMLSSOUtil.removeTenantDomainFromThreadLocal();
    }
}