Example usage for javax.xml.soap MessageFactory newInstance

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

Introduction

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

Prototype

public static MessageFactory newInstance(String protocol) throws SOAPException 

Source Link

Document

Creates a new MessageFactory object that is an instance of the specified implementation.

Usage

From source file:org.soapfromhttp.service.CallSOAP.java

/**
 * Contruction dynamique de la requte SOAP
 *
 * @param pBody//from  w w w.j a v a  2s  .c om
 * @param method
 * @return SOAPMessage
 * @throws SOAPException
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private SOAPMessage createSOAPRequest(final String pBody, final String method)
        throws SOAPException, IOException, SAXException, ParserConfigurationException {

    // Prcise la version du protocole SOAP  utiliser (ncessaire pour les appels de WS Externe)
    MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

    SOAPMessage soapMessage = messageFactory.createMessage();

    MimeHeaders headers = soapMessage.getMimeHeaders();

    // Prcise la mthode du WSDL  interroger
    headers.addHeader("SOAPAction", method);
    // Encodage UTF-8
    headers.addHeader("Content-Type", "text/xml;charset=UTF-8");

    final SOAPBody soapBody = soapMessage.getSOAPBody();

    // convert String into InputStream - traitement des caracres escaps > < ... (contraintes de l'affichage IHM)
    //InputStream is = new ByteArrayInputStream(HtmlUtils.htmlUnescape(pBody).getBytes());
    InputStream is = new ByteArrayInputStream(pBody.getBytes());
    DocumentBuilder builder = null;

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    // Important  laisser sinon KO
    builderFactory.setNamespaceAware(true);
    try {
        builder = builderFactory.newDocumentBuilder();

        Document document = builder.parse(is);

        soapBody.addDocument(document);
    } catch (ParserConfigurationException e) {
        MyLogger.log(CallSOAP.class.getName(), Level.ERROR, e.toString());
    } finally {
        is.close();
        if (builder != null) {
            builder.reset();
        }
    }
    soapMessage.saveChanges();

    return soapMessage;
}

From source file:com.inbravo.scribe.rest.service.crm.ms.auth.SOAPExecutor.java

/**
 * Executes SOAP message/* w  w w.  j  ava2s  .co  m*/
 * 
 * @param endpointUrl SOAP endpoint
 * @param request SOAP request
 * @return SOAP response message
 * @throws SOAPException in case of a SOAP issue
 * @throws IOException in case of an IO issue
 */
public final SOAPMessage execute(final String endpointUrl, final String request) throws Exception {

    if (logger.isDebugEnabled()) {
        logger.debug("----Inside execute endpointUrl: " + endpointUrl + " & request: " + request);
    }

    /* Create SOAP message */
    final SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
            .createMessage(new MimeHeaders(), new ByteArrayInputStream(request.getBytes()));

    /* Execute the url */
    return this.execute(endpointUrl, message);
}

From source file:edu.duke.cabig.c3pr.webservice.integration.StudyImportExportWebServiceTest.java

private SOAPMessage prepareRequestEnvelope(String xmlFile, String wrapperElement)
        throws SOAPException, DOMException, IOException, SAXException, ParserConfigurationException {
    MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
    SOAPMessage reqMsg = mf.createMessage();
    SOAPPart part = reqMsg.getSOAPPart();
    SOAPEnvelope env = part.getEnvelope();
    SOAPBody body = env.getBody();

    SOAPElement operation = body.addChildElement(wrapperElement, "stud", SERVICE_NS);
    operation.appendChild(env.getOwnerDocument().importNode(getSOAPBodyFromXML(xmlFile), true));
    reqMsg.saveChanges();/*from   w ww. j  a  va  2 s . c  o m*/
    return reqMsg;
}

From source file:io.hummer.util.ws.WebServiceClient.java

private SOAPMessage createSOAPMessage(Element request, List<Element> headers, String protocol)
        throws Exception {
    MessageFactory mf = MessageFactory.newInstance(protocol);
    SOAPMessage message = mf.createMessage();
    SOAPBody body = message.getSOAPBody();

    // check if we have a complete soap:Envelope as request..
    String ns = request.getNamespaceURI();
    if (request.getTagName().contains("Envelope")) {
        if (ns.equals("http://schemas.xmlsoap.org/soap/envelope/"))
            message = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(
                    new MimeHeaders(), new ByteArrayInputStream(xmlUtil.toString(request).getBytes()));
        if (ns.equals("http://www.w3.org/2003/05/soap-envelope"))
            message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(
                    new MimeHeaders(), new ByteArrayInputStream(xmlUtil.toString(request).getBytes()));

    } else {//from w  w  w. j  ava  2  s  .  c  o  m
        xmlUtil.appendChild(body, request);
    }
    for (Element h : headers) {
        xmlUtil.appendChild(message.getSOAPHeader(), h);
    }
    for (Element h : eprParamsAndProps) {
        xmlUtil.appendChild(message.getSOAPHeader(), h);
    }
    xmlUtil.appendChild(message.getSOAPHeader(), xmlUtil.toElement(
            "<wsa:To xmlns:wsa=\"" + EndpointReference.NS_WS_ADDRESSING + "\">" + endpointURL + "</wsa:To>"));
    message.saveChanges();
    return message;
}

From source file:com.centurylink.mdw.hub.servlet.SoapServlet.java

protected MessageFactory getSoapMessageFactory(String soapVersion) throws SOAPException {
    return MessageFactory.newInstance(soapVersion);
}

From source file:com.ibm.soatf.component.soap.SOAPComponent.java

private void checkSOAPMessage(boolean ok) throws SoapComponentException {
    ProgressMonitor.init(2, "Loading message from file...");
    String filename = new StringBuilder(serviceName).append(NAME_DELIMITER).append(operationName)
            .append(NAME_DELIMITER).append(RESPONSE_FILE_SUFFIX).toString();
    final File file = new File(workingDir, filename);
    InputStream is = null;//  w  ww  . j  ava  2s. com
    try {
        final byte[] xmlMessage = FileUtils.readFileToByteArray(file);
        is = new ByteArrayInputStream(xmlMessage);
        SOAPMessage response = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL)
                .createMessage(new MimeHeaders(), is);
        ProgressMonitor.increment("Checking for fault...");
        response.removeAllAttachments();
        SOAPEnvelope envp = response.getSOAPPart().getEnvelope();
        SOAPBody someBody = envp.getBody();
        if (ok) {
            if (someBody.getFault() == null) {
                cor.addMsg("soap body is OK");
                cor.markSuccessful();
            } else {
                final String msg = "found soap fault in response body:\n" + new String(xmlMessage);
                cor.addMsg(msg);
                throw new SoapComponentException(msg);
            }
        } else {
            if (someBody.getFault() != null) {
                cor.addMsg("found soap fault in response body");
                cor.markSuccessful();
            } else {
                final String msg = "response body doesn't contain soap fault:\n" + new String(xmlMessage);
                cor.addMsg(msg);
                throw new SoapComponentException(msg);
            }
        }
    } catch (IOException | SOAPException ex) {
        throw new SoapComponentException("error while trying to parse response", ex);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
                logger.debug("Not able to close input stream. ", ex);
            }
        }
    }
}

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.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

public void afterPropertiesSet() {
    if (messageFactory == null) {
        try {/* w w  w . j  a v a2 s.c  o  m*/
            if (SaajUtils.getSaajVersion() >= SaajUtils.SAAJ_13) {
                if (!StringUtils.hasLength(messageFactoryProtocol)) {
                    messageFactoryProtocol = SOAPConstants.SOAP_1_1_PROTOCOL;
                }
                if (logger.isInfoEnabled()) {
                    logger.info("Creating SAAJ 1.3 MessageFactory with " + messageFactoryProtocol);
                }
                messageFactory = MessageFactory.newInstance(messageFactoryProtocol);
            } else if (SaajUtils.getSaajVersion() == SaajUtils.SAAJ_12) {
                logger.info("Creating SAAJ 1.2 MessageFactory");
                messageFactory = MessageFactory.newInstance();
            } else if (SaajUtils.getSaajVersion() == SaajUtils.SAAJ_11) {
                logger.info("Creating SAAJ 1.1 MessageFactory");
                messageFactory = MessageFactory.newInstance();
            } else {
                throw new IllegalStateException(
                        "SaajSoapMessageFactory requires SAAJ 1.1, which was not found on the classpath");
            }
        } catch (NoSuchMethodError ex) {
            throw new SoapMessageCreationException(
                    "Could not create SAAJ MessageFactory. Is the version of the SAAJ specification interfaces ["
                            + SaajUtils.getSaajVersionString()
                            + "] the same as the version supported by the application server?",
                    ex);
        } catch (SOAPException ex) {
            throw new SoapMessageCreationException("Could not create SAAJ MessageFactory: " + ex.getMessage(),
                    ex);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Using MessageFactory class [" + messageFactory.getClass().getName() + "]");
    }
}

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

private static boolean isSaaj13() {
    try {// w ww  . j a  va  2 s  .  c  o m
        ClassUtils.forName(SAAJ_13_CLASS_NAME, SaajUtils.class.getClassLoader());
        MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        return true;
    } catch (ClassNotFoundException ex) {
        return false;
    } catch (NoSuchMethodError ex) {
        return false;
    } catch (SOAPException ex) {
        return false;
    }
}

From source file:test.integ.be.agiv.security.Axis2Test.java

@Test
public void testWSSecurityHandler() throws Exception {
    // setup/*from  w  w w .  jav a  2  s. c  om*/
    WSSecurityHandler testedInstance = new WSSecurityHandler();

    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.TRUE);
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.token")).andStubReturn(null);
    String testUsername = "username-" + UUID.randomUUID().toString();
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.username"))
            .andStubReturn(testUsername);
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.password"))
            .andStubReturn("password");
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.key")).andStubReturn(null);
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.certificate"))
            .andStubReturn(null);

    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(null,
            new ByteArrayInputStream(
                    "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><Body>test</Body></Envelope>"
                            .getBytes()));

    LOG.debug("SOAP message: " + toString(soapMessage.getSOAPPart()));
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    testedInstance.handleMessage(mockContext);

    // verify
    EasyMock.verify(mockContext);
    LOG.debug("SOAP message after handleMessage: " + toString(soapMessage.getSOAPPart()));
}