Example usage for javax.xml.soap SOAPHeader addNamespaceDeclaration

List of usage examples for javax.xml.soap SOAPHeader addNamespaceDeclaration

Introduction

In this page you can find the example usage for javax.xml.soap SOAPHeader addNamespaceDeclaration.

Prototype

public SOAPElement addNamespaceDeclaration(String prefix, String uri) throws SOAPException;

Source Link

Document

Adds a namespace declaration with the specified prefix and URI to this SOAPElement object.

Usage

From source file:com.amazon.advertising.api.common.HmacSecurityHandler.java

public void invoke(MessageContext mc) throws AxisFault {
    String actionUri = mc.getSOAPActionURI();
    String tokens[] = actionUri.split("/");
    String action = tokens[tokens.length - 1];

    String timestamp = getTimestamp();
    String signature;// w  ww  .ja  v a2s .c  o m
    try {
        signature = this.calculateSignature(action, timestamp);
    } catch (Exception e) {
        throw AxisFault.makeFault(e);
    }

    try {
        SOAPMessageContext smc = (SOAPMessageContext) mc;
        SOAPMessage message = smc.getMessage();
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPHeader header = envelope.getHeader();
        header.addNamespaceDeclaration(AWS_SECURITY_NS_PREFIX, AWS_SECURITY_NS);

        Name akidName = envelope.createName("AWSAccessKeyId", AWS_SECURITY_NS_PREFIX, AWS_SECURITY_NS);
        Name tsName = envelope.createName("Timestamp", AWS_SECURITY_NS_PREFIX, AWS_SECURITY_NS);
        Name sigName = envelope.createName("Signature", AWS_SECURITY_NS_PREFIX, AWS_SECURITY_NS);

        SOAPHeaderElement akidElement = header.addHeaderElement(akidName);
        SOAPHeaderElement tsElement = header.addHeaderElement(tsName);
        SOAPHeaderElement sigElement = header.addHeaderElement(sigName);

        akidElement.addTextNode(awsAccessKeyId);
        tsElement.addTextNode(timestamp);
        sigElement.addTextNode(signature);
    } catch (SOAPException e) {
        throw AxisFault.makeFault(e);
    }
}

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

/**
 * {@inheritDoc}//from   ww w .ja v  a 2s  . c om
 */
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;
}