Example usage for javax.xml.soap MimeHeaders MimeHeaders

List of usage examples for javax.xml.soap MimeHeaders MimeHeaders

Introduction

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

Prototype

public MimeHeaders() 

Source Link

Document

Constructs a default MimeHeaders object initialized with an empty Vector object.

Usage

From source file:org.belio.service.gateway.SafcomGateway.java

private static SOAPMessage getSoapMessageFromString(String xml) throws SOAPException, IOException {
    MessageFactory factory = MessageFactory.newInstance();
    javax.xml.soap.SOAPMessage message = factory.createMessage(new MimeHeaders(),
            new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
    return message;
}

From source file:org.cerberus.service.soap.impl.SoapService.java

@Override
public SOAPMessage createSoapRequest(String envelope, String method)
        throws SOAPException, IOException, SAXException, ParserConfigurationException {
    String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);
    boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("SOAPAction", "\"" + method + "\"");
    headers.addHeader("Content-Type",
            is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE);

    InputStream input = new ByteArrayInputStream(unescapedEnvelope.getBytes("UTF-8"));
    MessageFactory messageFactory = MessageFactory
            .newInstance(is12SoapVersion ? SOAPConstants.SOAP_1_2_PROTOCOL : SOAPConstants.SOAP_1_1_PROTOCOL);
    return messageFactory.createMessage(headers, input);
}

From source file:org.eevolution.LMX.engine.vendor.LMXFoliosDigitalesService.java

public String getXMLSealed(final String respxml) throws AdempiereException, IOException, SOAPException {

    String[] respuesta = null;/*from  w  w w.jav  a2  s  . c o m*/

    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage(new MimeHeaders(),
            new ByteArrayInputStream(respxml.getBytes(Charset.forName("UTF-8"))));

    SOAPBody body = message.getSOAPBody();
    NodeList returnList = body.getElementsByTagName("TimbrarPruebaCFDIResult");
    NodeList innerRes = returnList.item(0).getChildNodes();

    boolean failed = false;

    respuesta = new String[innerRes.getLength()];
    String messageError = null;

    for (int i = 0; i < innerRes.getLength(); i++) {
        respuesta[i] = innerRes.item(i).getTextContent();

        if (i < 3 && !respuesta[i].equals("")) {
            failed = true;
            messageError = respuesta[i];
            //System.out.println(messageError);
        }
    }

    if (failed)
        throw new AdempiereException(messageError);

    respuesta[3] = StringEscapeUtils.unescapeXml(respuesta[3]);
    return respuesta[3];
}

From source file:org.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

private MimeHeaders parseMimeHeaders(InputStream inputStream) throws IOException {
    MimeHeaders mimeHeaders = new MimeHeaders();
    if (inputStream instanceof TransportInputStream) {
        TransportInputStream transportInputStream = (TransportInputStream) inputStream;
        for (Iterator<String> headerNames = transportInputStream.getHeaderNames(); headerNames.hasNext();) {
            String headerName = headerNames.next();
            for (Iterator<String> headerValues = transportInputStream.getHeaders(headerName); headerValues
                    .hasNext();) {/*from w  ww.j a  va2  s  .  co m*/
                String headerValue = headerValues.next();
                StringTokenizer tokenizer = new StringTokenizer(headerValue, ",");
                while (tokenizer.hasMoreTokens()) {
                    mimeHeaders.addHeader(headerName, tokenizer.nextToken().trim());
                }
            }
        }
    }
    return mimeHeaders;
}

From source file:org.springframework.ws.soap.saaj.support.SaajUtils.java

/**
 * Loads a SAAJ <code>SOAPMessage</code> from the given resource with a given message factory.
 *
 * @param resource       the resource to read from
 * @param messageFactory SAAJ message factory used to construct the message
 * @return the loaded SAAJ message//from ww w .  j  av a 2  s  .c om
 * @throws SOAPException if the message cannot be constructed
 * @throws IOException   if the input stream resource cannot be loaded
 */
public static SOAPMessage loadMessage(Resource resource, MessageFactory messageFactory)
        throws SOAPException, IOException {
    InputStream is = resource.getInputStream();
    try {
        MimeHeaders mimeHeaders = new MimeHeaders();
        mimeHeaders.addHeader(TransportConstants.HEADER_CONTENT_TYPE, "text/xml");
        mimeHeaders.addHeader(TransportConstants.HEADER_CONTENT_LENGTH,
                Long.toString(resource.getFile().length()));
        return messageFactory.createMessage(mimeHeaders, is);
    } finally {
        is.close();
    }
}

From source file:org.wso2.caching.receivers.CacheMessageReceiver.java

/**
 * This method will be called when the message is received to the MR
 * and this will serve the response from the cache
 *
 * @param messageCtx - MessageContext to be served
 * @throws AxisFault if there is any error in serving from the cache
 */// w  ww.  j a va2s.com
public void receive(MessageContext messageCtx) throws AxisFault {

    MessageContext outMsgContext = MessageContextBuilder.createOutMessageContext(messageCtx);

    if (outMsgContext != null) {
        OperationContext opCtx = outMsgContext.getOperationContext();

        if (opCtx != null) {
            opCtx.addMessageContext(outMsgContext);
            if (log.isDebugEnabled()) {
                log.debug("Serving from the cache...");
            }

            Object cachedObj = opCtx.getPropertyNonReplicable(CachingConstants.CACHED_OBJECT);
            if (cachedObj != null && cachedObj instanceof CachableResponse) {
                try {
                    MessageFactory mf = MessageFactory.newInstance();
                    SOAPMessage smsg;
                    if (messageCtx.isSOAP11()) {
                        smsg = mf.createMessage(new MimeHeaders(),
                                new ByteArrayInputStream(((CachableResponse) cachedObj).getResponseEnvelope()));
                        ((CachableResponse) cachedObj).setInUse(false);
                    } else {
                        MimeHeaders mimeHeaders = new MimeHeaders();
                        mimeHeaders.addHeader("Content-ID", IDGenerator.generateID());
                        mimeHeaders.addHeader("content-type", HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML);
                        smsg = mf.createMessage(mimeHeaders,
                                new ByteArrayInputStream(((CachableResponse) cachedObj).getResponseEnvelope()));
                        ((CachableResponse) cachedObj).setInUse(false);
                    }

                    if (smsg != null) {
                        org.apache.axiom.soap.SOAPEnvelope omSOAPEnv = SAAJUtil
                                .toOMSOAPEnvelope(smsg.getSOAPPart().getDocumentElement());
                        if (omSOAPEnv.getHeader() == null) {
                            SOAPFactory fac = getSOAPFactory(messageCtx);
                            fac.createSOAPHeader(omSOAPEnv);
                        }
                        outMsgContext.setEnvelope(omSOAPEnv);
                    } else {
                        handleException("Unable to serve from the cache : "
                                + "Couldn't build the SOAP response from the cached byte stream");
                    }

                } catch (SOAPException e) {
                    handleException("Unable to serve from the cache : "
                            + "Unable to get build the response from the byte stream", e);
                } catch (IOException e) {
                    handleException("Unable to serve from the cache : "
                            + "I/O Error in building the response envelope from the byte stream");
                }

                AxisEngine.send(outMsgContext);

            } else {
                handleException("Unable to find the response in the cache");
            }
        } else {
            handleException("Unable to serve from " + "the cache : OperationContext not found for processing");
        }
    } else {
        handleException("Unable to serve from " + "the cache : Unable to get the out message context");
    }
}

From source file:org.wso2.carbon.caching.module.receivers.CacheMessageReceiver.java

/**
 * This method will be called when the message is received to the MR
 * and this will serve the response from the cache
 *
 * @param messageCtx - MessageContext to be served
 * @throws AxisFault if there is any error in serving from the cache
 *//*  ww  w.ja  va2  s .c o m*/
public void receive(MessageContext messageCtx) throws AxisFault {

    MessageContext outMsgContext = MessageContextBuilder.createOutMessageContext(messageCtx);

    if (outMsgContext != null) {
        OperationContext opCtx = outMsgContext.getOperationContext();

        if (opCtx != null) {
            opCtx.addMessageContext(outMsgContext);
            if (log.isDebugEnabled()) {
                log.debug("Serving from the cache...");
            }

            Object cachedObj = opCtx.getPropertyNonReplicable(CachingConstants.CACHED_OBJECT);
            /**
             * This was introduced to  avoid cache being expired before this below code executed.
             */
            byte[] bt = (byte[]) opCtx.getPropertyNonReplicable(CachingConstants.CACHEENVELOPE);

            if (bt != null) {
                try {
                    MessageFactory mf = MessageFactory.newInstance();
                    SOAPMessage smsg;
                    if (messageCtx.isSOAP11()) {
                        smsg = mf.createMessage(new MimeHeaders(), new ByteArrayInputStream(bt));
                        ((CachableResponse) cachedObj).setInUse(false);
                    } else {
                        MimeHeaders mimeHeaders = new MimeHeaders();
                        mimeHeaders.addHeader("Content-ID", IDGenerator.generateID());
                        mimeHeaders.addHeader("content-type", HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML);
                        smsg = mf.createMessage(mimeHeaders, new ByteArrayInputStream(bt));
                        ((CachableResponse) cachedObj).setInUse(false);
                    }

                    if (smsg != null) {
                        org.apache.axiom.soap.SOAPEnvelope omSOAPEnv = SAAJUtil
                                .toOMSOAPEnvelope(smsg.getSOAPPart().getDocumentElement());
                        if (omSOAPEnv.getHeader() == null) {
                            SOAPFactory fac = getSOAPFactory(messageCtx);
                            fac.createSOAPHeader(omSOAPEnv);
                        }
                        outMsgContext.setEnvelope(omSOAPEnv);
                    } else {
                        handleException("Unable to serve from the cache : "
                                + "Couldn't build the SOAP response from the cached byte stream");
                    }

                } catch (SOAPException e) {
                    handleException("Unable to serve from the cache : "
                            + "Unable to get build the response from the byte stream", e);
                } catch (IOException e) {
                    handleException("Unable to serve from the cache : "
                            + "I/O Error in building the response envelope from the byte stream");
                }

                AxisEngine.send(outMsgContext);

            } else {
                handleException("Unable to find the response in the cache");
            }
        } else {
            handleException("Unable to serve from " + "the cache : OperationContext not found for processing");
        }
    } else {
        handleException("Unable to serve from " + "the cache : Unable to get the out message context");
    }
}

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./*w  w w  .j  av  a2  s .  c  om*/
 *
 * @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();
    }
}

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

/**
 * This method returns s SOAP message from the given Servlet Input Stream.
 * @param inputStream InputStream from the servlet Request
 * @return/*w  w  w. ja v a  2s.  c  o  m*/
 * @throws IOException
 * @throws SOAPException
 */
private SOAPMessage createSOAPMessagefromInputStream(InputStream inputStream)
        throws SOAPException, IOException {
    SOAPMessage soapMessage;
    MessageFactory messageFactory = MessageFactory.newInstance();
    soapMessage = messageFactory.createMessage(new MimeHeaders(), inputStream);
    return soapMessage;
}

From source file:org.zaproxy.zap.extension.soap.SOAPXMLInjectionActiveScanner.java

private boolean isSoapMessage(String content, String charset) {
    SOAPMessage soapMsg = null;/*  ww  w . j a va  2s .  c o m*/
    if (content.length() <= 0)
        return false;
    MessageFactory factory;
    try {
        factory = MessageFactory.newInstance();
        soapMsg = factory.createMessage(new MimeHeaders(),
                new ByteArrayInputStream(content.getBytes(Charset.forName(charset))));
        /* Content has been parsed correctly as SOAP content. */
        if (soapMsg != null)
            return true;
        else
            return false;
    } catch (Exception e) {
        /*
         * Error when trying to parse as SOAP content. It is considered as a non-SOAP
         * message.
         */
        return false;
    }
}