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;
}
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;
}
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);
}
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;
}
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"));
}
Aggregations