Example usage for javax.xml.soap SOAPFactory newInstance

List of usage examples for javax.xml.soap SOAPFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.soap SOAPFactory newInstance.

Prototype

public static SOAPFactory newInstance() throws SOAPException 

Source Link

Document

Creates a new SOAPFactory object that is an instance of the default implementation (SOAP 1.1).

Usage

From source file:org.wso2.carbon.mdm.mobileservices.windowspc.services.wstep.util.MessageHandler.java

/**
 * This method adds Timestamp for SOAP header, and adds Content-length for HTTP header for
 * avoiding HTTP chunking./*w ww.  j a v a 2  s .  c o m*/
 *
 * @param context
 */
@Override
public boolean handleMessage(SOAPMessageContext context) {

    Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outBoundProperty) {

        SOAPMessage message = context.getMessage();
        SOAPHeader header = null;
        SOAPEnvelope envelope = null;

        try {
            header = message.getSOAPHeader();
            envelope = message.getSOAPPart().getEnvelope();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        if (header == null) {
            try {
                header = envelope.addHeader();
            } catch (SOAPException e) {
                Response.serverError().build();
            }
        }
        SOAPFactory soapFactory = null;

        try {
            soapFactory = SOAPFactory.newInstance();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNamesSecurity = new QName(Constants.CertificateEnrollment.WS_SECURITY_TARGET_NAMESPACE,
                Constants.CertificateEnrollment.SECURITY);

        SOAPHeaderElement Security = null;

        try {
            Security = header.addHeaderElement(qNamesSecurity);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        Name attributeName = null;
        try {
            attributeName = soapFactory.createName(Constants.CertificateEnrollment.TIMESTAMP_ID,
                    Constants.CertificateEnrollment.TIMESTAMP_U,
                    Constants.CertificateEnrollment.WSS_SECURITY_UTILITY);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNameTimestamp = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.TIMESTAMP);
        SOAPHeaderElement timestamp = null;

        try {
            timestamp = header.addHeaderElement(qNameTimestamp);
            timestamp.addAttribute(attributeName, Constants.CertificateEnrollment.TIMESTAMP_0);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        DateTime dateTime = new DateTime();
        DateTime expiredDateTime = dateTime.plusMinutes(5);
        String createdISOTime = dateTime.toString(ISODateTimeFormat.dateTime());
        String expiredISOTime = expiredDateTime.toString(ISODateTimeFormat.dateTime());
        createdISOTime = createdISOTime.substring(0, createdISOTime.length() - 6);
        createdISOTime = createdISOTime + "Z";
        expiredISOTime = expiredISOTime.substring(0, expiredISOTime.length() - 6);
        expiredISOTime = expiredISOTime + "Z";

        QName qNameCreated = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.CREATED);
        SOAPHeaderElement SOAPHeaderCreated = null;

        try {
            SOAPHeaderCreated = header.addHeaderElement(qNameCreated);
            SOAPHeaderCreated.addTextNode(createdISOTime);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNameExpires = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.EXPIRES);
        SOAPHeaderElement SOAPHeaderExpires = null;

        try {
            SOAPHeaderExpires = header.addHeaderElement(qNameExpires);
            SOAPHeaderExpires.addTextNode(expiredISOTime);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        try {
            timestamp.addChildElement(SOAPHeaderCreated);
            timestamp.addChildElement(SOAPHeaderExpires);
            Security.addChildElement(timestamp);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        try {
            message.saveChanges();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            message.writeTo(outputStream);
        } catch (IOException e) {
            Response.serverError().build();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        String messageString = null;
        try {
            messageString = new String(outputStream.toByteArray(), Constants.CertificateEnrollment.UTF_8);
        } catch (UnsupportedEncodingException e) {
            Response.serverError().build();
        }

        Map<String, List<String>> headers = (Map<String, List<String>>) context
                .get(MessageContext.HTTP_REQUEST_HEADERS);
        headers = new HashMap<String, List<String>>();
        headers.put(Constants.CertificateEnrollment.CONTENT_LENGTH,
                Arrays.asList(String.valueOf(messageString.length())));
        context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

    }
    return true;
}