Search in sources :

Example 41 with MessageFactory

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

the class LanguageIdentifierClientHTTP method guessQueryLanguage.

// NOTE (rwesten): I rather do the error handling in the EnhancementEngine!
public List<GuessedLanguage> guessQueryLanguage(String text) throws IOException, SOAPException {
    if (text == null || text.isEmpty()) {
        // no language
        return Collections.emptyList();
    }
    // create the POST request
    HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
    // write content
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    writer.write(SOAP_PREFIX);
    writer.write("<lan:guessQueryLanguage><textToGuess>");
    StringEscapeUtils.escapeXml(writer, text);
    writer.write("</textToGuess></lan:guessQueryLanguage>");
    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(stream);
    // Set contents of message
    soapPart.setContent(source);
    SOAPBody soapBody = message.getSOAPBody();
    List<GuessedLanguage> guesses = new Vector<GuessedLanguage>();
    NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
    for (int i = 0; i < nlist.getLength(); i++) {
        try {
            Element result = (Element) nlist.item(i);
            String lang = result.getAttribute("language");
            double d = Double.parseDouble(result.getAttribute("guessConfidence"));
            guesses.add(new GuessedLanguage(lang, d));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return guesses;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) 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) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) 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)

Example 42 with MessageFactory

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

the class LanguageIdentifierClientHTTP method guessLanguage.

// NOTE (rwesten): I rather do the error handling in the EnhancementEngine!
public List<GuessedLanguage> guessLanguage(String text) throws IOException, SOAPException {
    if (text == null || text.isEmpty()) {
        // no text -> no language
        return Collections.emptyList();
    }
    // create the POST request
    HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
    // write content
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    writer.write(SOAP_PREFIX);
    writer.write("<lan:guessLanguage><textToGuess>");
    StringEscapeUtils.escapeXml(writer, text);
    writer.write("</textToGuess></lan:guessLanguage>");
    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(stream);
    // Set contents of message
    soapPart.setContent(source);
    SOAPBody soapBody = message.getSOAPBody();
    List<GuessedLanguage> guesses = new Vector<GuessedLanguage>();
    NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
    for (int i = 0; i < nlist.getLength(); i++) {
        try {
            Element result = (Element) nlist.item(i);
            String lang = result.getAttribute("language");
            double d = Double.parseDouble(result.getAttribute("guessConfidence"));
            guesses.add(new GuessedLanguage(lang, d));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return guesses;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) 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) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) 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)

Example 43 with MessageFactory

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

the class X509TokenTest method testAsymmetricIssuerSerialDispatchMessage.

@org.junit.Test
public void testAsymmetricIssuerSerialDispatchMessage() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = X509TokenTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = X509TokenTest.class.getResource("DoubleItX509.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItAsymmetricIssuerSerialOperationPort");
    Dispatch<SOAPMessage> disp = service.createDispatch(portQName, SOAPMessage.class, Mode.MESSAGE);
    updateAddressPort(disp, test.getPort());
    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(disp);
    }
    Document xmlDocument = DOMUtils.newDocument();
    final String ns = "http://www.example.org/schema/DoubleIt";
    Element requestElement = xmlDocument.createElementNS(ns, "tns:DoubleIt");
    requestElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:tns", ns);
    Element dataElement = xmlDocument.createElementNS(null, "numberToDouble");
    dataElement.appendChild(xmlDocument.createTextNode("25"));
    requestElement.appendChild(dataElement);
    xmlDocument.appendChild(requestElement);
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage request = factory.createMessage();
    request.getSOAPBody().appendChild(request.getSOAPPart().adoptNode(requestElement));
    // We need to set the wsdl operation name here, or otherwise the policy layer won't pick
    // up the security policy attached at the operation level
    // this can be done in one of three ways:
    // 1) set the WSDL_OPERATION context property
    // QName wsdlOperationQName = new QName(NAMESPACE, "DoubleIt");
    // disp.getRequestContext().put(MessageContext.WSDL_OPERATION, wsdlOperationQName);
    // 2) Set the "find.dispatch.operation" to TRUE to have  CXF explicitly try and determine it from the payload
    disp.getRequestContext().put("find.dispatch.operation", Boolean.TRUE);
    // 3) Turn on WS-Addressing as that will force #2
    // TODO - add code for this, really is adding WS-Addressing feature to the createDispatch call above
    SOAPMessage resp = disp.invoke(request);
    Node nd = resp.getSOAPBody().getFirstChild();
    XPathUtils xp = new XPathUtils(Collections.singletonMap("ns2", ns));
    Object o = xp.getValue("//ns2:DoubleItResponse/doubledNumber", DOMUtils.getDomElement(nd), XPathConstants.STRING);
    assertEquals(StaxUtils.toString(nd), "50", o);
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Service(javax.xml.ws.Service) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) XPathUtils(org.apache.cxf.helpers.XPathUtils)

Example 44 with MessageFactory

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

the class RMSoapOutInterceptorTest method setupOutboundFaultMessage.

private SoapMessage setupOutboundFaultMessage() throws Exception {
    Exchange ex = new ExchangeImpl();
    Message message = new MessageImpl();
    RMProperties rmps = new RMProperties();
    rmps.exposeAs(RM10Constants.NAMESPACE_URI);
    RMContextUtils.storeRMProperties(message, rmps, false);
    AddressingProperties maps = new AddressingProperties();
    RMContextUtils.storeMAPs(maps, message, false, false);
    ex.setInMessage(message);
    message = new MessageImpl();
    SoapMessage soapMessage = new SoapMessage(message);
    ex.setOutFaultMessage(soapMessage);
    soapMessage.setExchange(ex);
    MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
    SOAPMessage soap = factory.createMessage();
    soap.getSOAPBody().addFault();
    soapMessage.setContent(SOAPMessage.class, soap);
    return soapMessage;
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) MessageFactory(javax.xml.soap.MessageFactory) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) MessageImpl(org.apache.cxf.message.MessageImpl) SOAPMessage(javax.xml.soap.SOAPMessage) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) RMProperties(org.apache.cxf.ws.rm.RMProperties) SoapMessage(org.apache.cxf.binding.soap.SoapMessage)

Example 45 with MessageFactory

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

the class ProviderHeaderClientServerTest method testRPCInHeader.

@Test
public void testRPCInHeader() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/soapheader_rpc.wsdl");
    assertNotNull(wsdl);
    SOAPRPCHeaderService service = new SOAPRPCHeaderService(wsdl, new QName("http://apache.org/header_test/rpc", "SOAPRPCHeaderService"));
    assertNotNull(service);
    Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://apache.org/header_test/rpc", "SoapRPCHeaderPort"), javax.xml.soap.SOAPMessage.class, Service.Mode.MESSAGE);
    MessageFactory factory = MessageFactory.newInstance();
    InputStream is = getClass().getClassLoader().getResourceAsStream("./soapheader_rpc_provider/sayHelloMsg.xml");
    SOAPMessage inMessage = factory.createMessage(null, is);
    SOAPMessage response = dispatch.invoke(inMessage);
    is.close();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    response.writeTo(bout);
    assertTrue(new String(bout.toByteArray()).contains("part/header"));
}
Also used : SOAPRPCHeaderService(org.apache.header_test.rpc.SOAPRPCHeaderService) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) Test(org.junit.Test) AbstractJaxWsTest(org.apache.cxf.jaxws.AbstractJaxWsTest)

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