Search in sources :

Example 11 with MessageFactory

use of javax.xml.soap.MessageFactory in project jbossws-cxf by jbossws.

the class SOAPBindingTestCase method testNamespacedDocWrappedServiceMessageAccess.

@Test
@RunAsClient
public void testNamespacedDocWrappedServiceMessageAccess() throws Exception {
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();
    String purchaseNamespace = "http://namespace/purchase";
    String resultNamespace = "http://namespace/result";
    String stringNamespace = "http://namespace/string";
    String reqEnv = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" + " <env:Header/>" + " <env:Body>" + "  <ns1:SubmitNamespacedPO xmlns:ns1='" + targetNS + "'>" + "   <ns2:NamespacedPurchaseOrder xmlns:ns2='" + purchaseNamespace + "'>Ferrari</ns2:NamespacedPurchaseOrder>" + "   <ns3:NamespacedString xmlns:ns3='" + stringNamespace + "'>message</ns3:NamespacedString>" + "  </ns1:SubmitNamespacedPO>" + " </env:Body>" + "</env:Envelope>";
    SOAPMessage reqMsg = msgFactory.createMessage(null, new ByteArrayInputStream(reqEnv.getBytes()));
    URL epURL = new URL(baseURL + "/DocWrappedService");
    SOAPMessage resMsg = con.call(reqMsg, epURL);
    QName qname = new QName(targetNS, "SubmitNamespacedPOResponse");
    SOAPElement soapElement = (SOAPElement) resMsg.getSOAPBody().getChildElements(qname).next();
    soapElement = (SOAPElement) soapElement.getChildElements(new QName(resultNamespace, "NamespacedPurchaseOrderAck")).next();
    assertEquals("Ferrari", soapElement.getValue());
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) SOAPElement(javax.xml.soap.SOAPElement) SOAPConnection(javax.xml.soap.SOAPConnection) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Test(org.junit.Test) JBossWSTest(org.jboss.wsf.test.JBossWSTest)

Example 12 with MessageFactory

use of javax.xml.soap.MessageFactory in project jbossws-cxf by jbossws.

the class JBWS3084TestCase method doTestSoapConnection.

private void doTestSoapConnection(boolean disableChunking) throws Exception {
    SOAPFactory soapFac = SOAPFactory.newInstance();
    MessageFactory msgFac = MessageFactory.newInstance();
    SOAPConnectionFactory conFac = SOAPConnectionFactory.newInstance();
    SOAPMessage msg = msgFac.createMessage();
    if (disableChunking) {
        // this is the custom header checked by ServiceImpl
        msg.getMimeHeaders().addHeader("Transfer-Encoding-Disabled", "true");
        // this is a hint to SOAPConnection that the chunked encoding is not needed
        msg.getMimeHeaders().addHeader("Transfer-Encoding", "disabled");
    }
    QName sayHi = new QName("http://www.jboss.org/jbossws/saaj", "sayHello");
    msg.getSOAPBody().addChildElement(soapFac.createElement(sayHi));
    AttachmentPart ap1 = msg.createAttachmentPart();
    char[] content = new char[16 * 1024];
    Arrays.fill(content, 'A');
    ap1.setContent(new String(content), "text/plain");
    msg.addAttachmentPart(ap1);
    AttachmentPart ap2 = msg.createAttachmentPart();
    ap2.setContent("Attachment content - Part 2", "text/plain");
    msg.addAttachmentPart(ap2);
    msg.saveChanges();
    SOAPConnection con = conFac.createConnection();
    final String serviceURL = baseURL.toString();
    URL endpoint = new URL(serviceURL);
    SOAPMessage response = con.call(msg, endpoint);
    QName sayHiResp = new QName("http://www.jboss.org/jbossws/saaj", "sayHelloResponse");
    Iterator<?> sayHiRespIterator = response.getSOAPBody().getChildElements(sayHiResp);
    SOAPElement soapElement = (SOAPElement) sayHiRespIterator.next();
    assertNotNull(soapElement);
    assertEquals(2, response.countAttachments());
    String[] values = response.getMimeHeaders().getHeader("Transfer-Encoding-Disabled");
    if (disableChunking) {
        // this means that the ServiceImpl executed the code branch verifying
        // that chunking was disabled
        assertNotNull(values);
        assertTrue(values.length == 1);
    } else {
        assertNull(values);
    }
}
Also used : SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) SOAPConnection(javax.xml.soap.SOAPConnection) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPFactory(javax.xml.soap.SOAPFactory) URL(java.net.URL) SOAPElement(javax.xml.soap.SOAPElement)

Example 13 with MessageFactory

use of javax.xml.soap.MessageFactory in project polymap4-core by Polymap4.

the class SpnegoSOAPConnection method call.

@Override
public final SOAPMessage call(final SOAPMessage request, final Object endpoint) throws SOAPException {
    SOAPMessage message = null;
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        final MimeHeaders headers = request.getMimeHeaders();
        final String[] contentType = headers.getHeader("Content-Type");
        final String[] soapAction = headers.getHeader("SOAPAction");
        // build the Content-Type HTTP header parameter if not defined
        if (null == contentType) {
            final StringBuilder header = new StringBuilder();
            if (null == soapAction) {
                header.append("application/soap+xml; charset=UTF-8;");
            } else {
                header.append("text/xml; charset=UTF-8;");
            }
            // not defined as a MIME header but we need it as an HTTP header parameter
            this.conn.addRequestProperty("Content-Type", header.toString());
        } else {
            if (contentType.length > 1) {
                throw new IllegalArgumentException("Content-Type defined more than once.");
            }
            // user specified as a MIME header so add it as an HTTP header parameter
            this.conn.addRequestProperty("Content-Type", contentType[0]);
        }
        // specify SOAPAction as an HTTP header parameter
        if (null != soapAction) {
            if (soapAction.length > 1) {
                throw new IllegalArgumentException("SOAPAction defined more than once.");
            }
            this.conn.addRequestProperty("SOAPAction", soapAction[0]);
        }
        request.writeTo(bos);
        this.conn.connect(new URL(endpoint.toString()), bos);
        final MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        try {
            message = factory.createMessage(null, this.conn.getInputStream());
        } catch (IOException e) {
            message = factory.createMessage(null, this.conn.getErrorStream());
        }
    } catch (MalformedURLException e) {
        throw new SOAPException(e);
    } catch (IOException e) {
        throw new SOAPException(e);
    } catch (GSSException e) {
        throw new SOAPException(e);
    } catch (PrivilegedActionException e) {
        throw new SOAPException(e);
    } finally {
        try {
            bos.close();
        } catch (IOException ioe) {
            assert true;
        }
        this.close();
    }
    return message;
}
Also used : MalformedURLException(java.net.MalformedURLException) MessageFactory(javax.xml.soap.MessageFactory) PrivilegedActionException(java.security.PrivilegedActionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) MimeHeaders(javax.xml.soap.MimeHeaders) GSSException(org.ietf.jgss.GSSException) SOAPException(javax.xml.soap.SOAPException)

Example 14 with MessageFactory

use of javax.xml.soap.MessageFactory in project stanbol by apache.

the class LemmatizerClientHTTP method lemmatizeContents.

public String lemmatizeContents(String text, String lang) throws SOAPException, IOException {
    // create the POST request
    HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
    // write content
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    // write the SOAP envelope, header and start the body
    writer.write(SOAP_REQUEST_PREFIX);
    writer.write("<mor:inputText lang=\"");
    writer.write(lang);
    writer.write("\" text=\"");
    StringEscapeUtils.escapeXml(writer, text);
    writer.write("\"/>");
    writer.write(SOAP_REQUEST_SUFFIX);
    writer.close();
    // Call the service
    long start = System.currentTimeMillis();
    InputStream stream = con.getInputStream();
    log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
    // Create SoapMessage
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPMessage message = msgFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    // Load the SOAP text into a stream source
    StreamSource source = new StreamSource(stream);
    // Set contents of message
    soapPart.setContent(source);
    SOAPBody soapBody = message.getSOAPBody();
    StringBuilder buff = new StringBuilder();
    NodeList nlist = soapBody.getElementsByTagNameNS("*", "LexicalEntry");
    for (int i = 0; i < nlist.getLength(); i++) {
        Element result = (Element) nlist.item(i);
        NodeList lemmasList = result.getElementsByTagNameNS("*", "Lemma");
        if (lemmasList != null && lemmasList.getLength() > 0) {
            HashSet<String> lemmas = new HashSet<String>();
            for (int j = 0; j < lemmasList.getLength(); j++) {
                lemmas.add(lemmasList.item(j).getTextContent());
            }
            for (String lemma : lemmas) {
                buff.append(lemma).append(' ');
            }
        } else {
            buff.append(result.getAttributeNS("*", "WordForm")).append(' ');
        }
    }
    return buff.toString().trim();
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) HashSet(java.util.HashSet)

Example 15 with MessageFactory

use of javax.xml.soap.MessageFactory in project stanbol by apache.

the class SentimentAnalysisServiceClientHttp method extractSentimentExpressions.

public List<SentimentExpression> extractSentimentExpressions(String text, String lang) throws SOAPException, IOException {
    if (text == null || text.isEmpty()) {
        // no text -> no extractions
        return Collections.emptyList();
    }
    // create the POST request
    HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, connectionTimeout);
    // write content
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    writer.write(SOAP_PREFIX);
    writer.write("<sen:analyzeText><arg0><![CDATA[" + text + "]]></arg0><arg1>" + lang + "</arg1></sen:analyzeText>");
    writer.write(SOAP_SUFFIX);
    writer.close();
    // Call the service
    long start = System.currentTimeMillis();
    InputStream stream = con.getInputStream();
    log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
    // Create SoapMessage and parse the results
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPMessage message = msgFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    // Load the SOAP text into a stream source
    StreamSource source = new StreamSource(new InputStreamReader(stream, UTF8));
    // Set contents of message
    soapPart.setContent(source);
    SOAPBody soapBody = message.getSOAPBody();
    // extract the results
    List<SentimentExpression> sentExpressions = new Vector<SentimentExpression>();
    NodeList nlist = soapBody.getElementsByTagName("relation");
    String snippetStr = null;
    for (int i = 0; i < nlist.getLength(); i++) {
        Element relation = (Element) nlist.item(i);
        String sentimentType = relation.getAttribute("type");
        int startSnippet = Integer.parseInt(relation.getAttribute("start"));
        int endSnippet = Integer.parseInt(relation.getAttribute("end"));
        NodeList snippet = relation.getElementsByTagName("snippet");
        if (snippet.getLength() > 0) {
            snippetStr = snippet.item(0).getTextContent();
        }
        List<String> arguments = new Vector<String>();
        NodeList argsList = relation.getElementsByTagName("arguments");
        for (int x = 0; x < argsList.getLength(); x++) {
            NodeList lemmas = ((Element) argsList.item(x)).getElementsByTagName("lemma");
            for (int y = 0; y < lemmas.getLength(); y++) {
                Element lemma = (Element) lemmas.item(y);
                String lemmaStr = lemma.getAttribute("lemma");
                if (lemmaStr != null && lemmaStr.length() > 0)
                    arguments.add(lemmaStr);
            }
        }
        SentimentExpression expr = new SentimentExpression(sentimentType, snippetStr, startSnippet, endSnippet, arguments);
        sentExpressions.add(expr);
    }
    return sentExpressions;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

Aggregations

MessageFactory (javax.xml.soap.MessageFactory)70 SOAPMessage (javax.xml.soap.SOAPMessage)65 URL (java.net.URL)24 InputStream (java.io.InputStream)22 QName (javax.xml.namespace.QName)22 SOAPException (javax.xml.soap.SOAPException)22 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 SOAPBody (javax.xml.soap.SOAPBody)18 SOAPPart (javax.xml.soap.SOAPPart)17 StreamSource (javax.xml.transform.stream.StreamSource)14 SOAPConnection (javax.xml.soap.SOAPConnection)13 IOException (java.io.IOException)11 SOAPElement (javax.xml.soap.SOAPElement)11 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)10 JBossWSTest (org.jboss.wsf.test.JBossWSTest)10 NodeList (org.w3c.dom.NodeList)9 AttachmentPart (javax.xml.soap.AttachmentPart)8 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)8 Element (org.w3c.dom.Element)8