Search in sources :

Example 26 with SOAPPart

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

the class NERserviceClientHTTP method extractEntities.

public List<NamedEntity> extractEntities(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, -1);
    // write content
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    writer.write(SOAP_PREFIX);
    writer.write("<v0u0:processTextRequest><v0u0:text>");
    StringEscapeUtils.escapeXml(writer, text);
    writer.write("</v0u0:text><v0u0:language>" + lang + "</v0u0:language></v0u0:processTextRequest>");
    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();
    // extract the results
    List<NamedEntity> extractedNE = new Vector<NamedEntity>();
    NodeList nlist = soapBody.getElementsByTagName("result");
    for (int i = 0; i < nlist.getLength(); i++) {
        Element result = (Element) nlist.item(i);
        NamedEntity ne = new NamedEntity();
        ne.setType(result.getElementsByTagNameNS("*", "type").item(0).getTextContent());
        ne.setSource(result.getElementsByTagNameNS("*", "source").item(0).getTextContent());
        ne.setFormKind(result.getElementsByTagNameNS("*", "formKind").item(0).getTextContent());
        ne.setFrom(Long.parseLong(result.getElementsByTagNameNS("*", "from").item(0).getTextContent()));
        ne.setTo(Long.parseLong(result.getElementsByTagNameNS("*", "to").item(0).getTextContent()));
        extractedNE.add(ne);
    }
    return extractedNE;
}
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) 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 27 with SOAPPart

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

the class ClassificationClientHTTP method extractConcepts.

public List<Concept> extractConcepts(String text, String lang) throws IOException, SOAPException {
    if (text == null || text.isEmpty()) {
        // no text -> no classification
        return Collections.emptyList();
    }
    // create the POST request
    HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
    // "stream" the request content directly to the buffered writer
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
    writer.write(SOAP_PREFIX);
    writer.write("<clas:classify>");
    // TODO: should the user be configurable?
    writer.write("<clas:user>wiki</clas:user>");
    writer.write("<clas:model>");
    writer.write(lang);
    writer.write("</clas:model>");
    writer.write("<clas:text>");
    // write the escaped text directly to the request
    StringEscapeUtils.escapeXml(writer, text);
    writer.write("</clas:text>");
    writer.write("</clas:classify>");
    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);
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPMessage message = msgFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    StreamSource source = new StreamSource(stream);
    // Set contents of message
    soapPart.setContent(source);
    SOAPBody soapBody = message.getSOAPBody();
    List<Concept> extractedConcepts = new Vector<Concept>();
    NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
    for (int i = 0; i < nlist.getLength() && i < maxResultToReturn; i++) {
        Element result = (Element) nlist.item(i);
        // NOTE: (rwesten) implemented a mapping from the CELI classification
        // to the Stanbol fise:TopicEnhancements (STANBOL-617) that
        // * one fise:TopicAnnotation is generated per "model"
        // * the whole label string is used as fise:entity-label
        // * the uri of the most specific dbpedia ontology type (see
        // selectClassificationClass) is used as fise:entity-reference
        // This has the intuition that for users it is easier to grasp
        // the meaning of the whole lable, while for machines the link
        // to the most specific dbpedia ontology class is best suited.
        String model = result.getElementsByTagNameNS("*", "label").item(0).getTextContent();
        model = model.substring(1, model.length() - 1);
        IRI modelConcept = selectClassificationClass(model);
        String conf = result.getElementsByTagNameNS("*", "score").item(0).getTextContent();
        Double confidence = new Double(conf);
        extractedConcepts.add(new Concept(model, modelConcept, confidence));
    }
    return extractedConcepts;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) 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) 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 28 with SOAPPart

use of javax.xml.soap.SOAPPart 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 29 with SOAPPart

use of javax.xml.soap.SOAPPart 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 30 with SOAPPart

use of javax.xml.soap.SOAPPart in project Payara by payara.

the class BaseAuthConfig method getName.

private static Name getName(SOAPMessage message) {
    Name rvalue = null;
    SOAPPart soap = message.getSOAPPart();
    if (soap != null) {
        try {
            SOAPEnvelope envelope = soap.getEnvelope();
            if (envelope != null) {
                SOAPBody body = envelope.getBody();
                if (body != null) {
                    Iterator it = body.getChildElements();
                    while (it.hasNext()) {
                        Object o = it.next();
                        if (o instanceof SOAPElement) {
                            rvalue = ((SOAPElement) o).getElementName();
                            break;
                        }
                    }
                }
            }
        } catch (SOAPException se) {
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, "WSS: Unable to get SOAP envelope", se);
            }
        }
    }
    return rvalue;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Aggregations

SOAPPart (javax.xml.soap.SOAPPart)45 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPBody (javax.xml.soap.SOAPBody)28 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)26 SOAPException (javax.xml.soap.SOAPException)26 MessageFactory (javax.xml.soap.MessageFactory)17 SOAPElement (javax.xml.soap.SOAPElement)14 Iterator (java.util.Iterator)13 Element (org.w3c.dom.Element)13 StreamSource (javax.xml.transform.stream.StreamSource)12 QName (javax.xml.namespace.QName)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 Name (javax.xml.soap.Name)10 NodeList (org.w3c.dom.NodeList)9 BufferedWriter (java.io.BufferedWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 SOAPHeader (javax.xml.soap.SOAPHeader)6