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