use of javax.xml.soap.SOAPBody 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;
}
use of javax.xml.soap.SOAPBody 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;
}
use of javax.xml.soap.SOAPBody 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;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class HWSAXSourceMessageProvider method invoke.
public SAXSource invoke(SAXSource request) {
QName qn = (QName) ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
if (qn == null) {
throw new RuntimeException("No Operation Name");
}
SAXSource response = new SAXSource();
try {
SOAPMessage msg = factory.createMessage();
msg.getSOAPPart().setContent(request);
SOAPBody body = msg.getSOAPBody();
Node n = body.getFirstChild();
while (n.getNodeType() != Node.ELEMENT_NODE) {
n = n.getNextSibling();
}
if (n.getLocalName().equals(sayHi.getLocalPart())) {
response.setInputSource(sayHiInputSource);
} else if (n.getLocalName().equals(greetMe.getLocalPart())) {
response.setInputSource(greetMeInputSource);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class HWSoapMessageDocProvider method invoke.
public SOAPMessage invoke(SOAPMessage request) {
QName qn = (QName) ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
if (qn == null) {
throw new RuntimeException("No Operation Name");
}
SOAPMessage response = null;
SOAPBody body = null;
try {
body = SAAJUtils.getBody(request);
} catch (SOAPException e) {
return null;
}
Node n = body.getFirstChild();
while (n.getNodeType() != Node.ELEMENT_NODE) {
n = n.getNextSibling();
}
if (n.getLocalName().equals(sayHi.getLocalPart())) {
response = sayHiResponse;
} else if (n.getLocalName().equals(greetMe.getLocalPart())) {
Element el = DOMUtils.getFirstElement(n);
String v = DOMUtils.getContent(el);
if (v.contains("Return sayHi")) {
response = sayHiResponse;
} else if (v.contains("exceed maxLength")) {
response = greetMeResponseExceedMaxLengthRestriction;
} else if (v.contains("throwFault")) {
try {
SOAPFactory f = SOAPFactory.newInstance();
SOAPFault soapFault = f.createFault();
soapFault.setFaultString("Test Fault String ****");
Detail detail = soapFault.addDetail();
detail = soapFault.getDetail();
QName qName = new QName("http://www.Hello.org/greeter", "TestFault", "ns");
DetailEntry de = detail.addDetailEntry(qName);
qName = new QName("http://www.Hello.org/greeter", "ErrorCode", "ns");
SOAPElement errorElement = de.addChildElement(qName);
errorElement.setTextContent("errorcode");
throw new SOAPFaultException(soapFault);
} catch (SOAPException ex) {
// ignore
}
} else {
response = greetMeResponse;
}
}
return response;
}
Aggregations