use of javax.xml.soap.SOAPMessage in project webservices-axiom by apache.
the class TestExamineMustUnderstandHeaderElements method runTest.
@Override
protected void runTest() throws Throwable {
MessageFactory messageFactory = spec.getAdapter(FactorySelector.class).newMessageFactory(saajImplementation, dynamic);
MimeHeaders mimeHeaders = new MimeHeaders();
mimeHeaders.addHeader("Content-Type", spec.getContentType());
InputStream in = SOAPSampleSet.MUST_UNDERSTAND.getMessage(spec).getInputStream();
try {
SOAPMessage message = messageFactory.createMessage(mimeHeaders, in);
SOAPHeader header = message.getSOAPHeader();
Iterator<?> it = header.examineMustUnderstandHeaderElements(null);
assertTrue(it.hasNext());
assertTrue(it.next() instanceof SOAPHeaderElement);
assertFalse(it.hasNext());
} finally {
in.close();
}
}
use of javax.xml.soap.SOAPMessage in project ddf by codice.
the class GuestInterceptor method internalHandleMessage.
private void internalHandleMessage(SoapMessage message, SOAPMessage soapMessage) throws Fault {
//Check if security header exists; if not, execute GuestInterceptor logic
String actor = (String) getOption(WSHandlerConstants.ACTOR);
if (actor == null) {
actor = (String) message.getContextualProperty(SecurityConstants.ACTOR);
}
Element existingSecurityHeader = null;
try {
LOGGER.debug("Checking for security header.");
existingSecurityHeader = WSSecurityUtil.getSecurityHeader(soapMessage.getSOAPPart(), actor);
} catch (WSSecurityException e1) {
LOGGER.debug("Issue with getting security header", e1);
}
if (existingSecurityHeader != null) {
LOGGER.debug("SOAP message contains security header, no action taken by the GuestInterceptor.");
return;
}
LOGGER.debug("Current request has no security header, continuing with GuestInterceptor");
AssertionInfoMap assertionInfoMap = message.get(AssertionInfoMap.class);
boolean hasAddressingAssertion = assertionInfoMap.entrySet().stream().flatMap(p -> p.getValue().stream()).filter(info -> MetadataConstants.ADDRESSING_ASSERTION_QNAME.equals(info.getAssertion().getName())).findFirst().isPresent();
if (hasAddressingAssertion) {
createAddressing(message, soapMessage);
}
LOGGER.debug("Creating guest security token.");
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
SecurityToken securityToken = createSecurityToken(request.getRemoteAddr());
message.put(SecurityConstants.TOKEN, securityToken);
if (!MessageUtils.isRequestor(message)) {
try {
message.put(Message.REQUESTOR_ROLE, true);
policyBasedWss4jOutInterceptor.handleMessage(message);
} finally {
message.remove(Message.REQUESTOR_ROLE);
}
} else {
policyBasedWss4jOutInterceptor.handleMessage(message);
}
}
use of javax.xml.soap.SOAPMessage 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;
}
use of javax.xml.soap.SOAPMessage 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.SOAPMessage in project cxf by apache.
the class W3CDOMStreamReaderTest method testReader.
@Test
public void testReader() throws Exception {
ByteArrayInputStream is = new ByteArrayInputStream("<Test xmlns=\"http://example.org/types\"><argument>foobar</argument></Test>".getBytes("utf-8"));
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage();
SOAPPart part = msg.getSOAPPart();
Document doc = docBuilder.parse(is);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(part.getEnvelope());
XMLStreamReader reader = StaxUtils.createXMLStreamReader(new DOMSource(doc));
StaxUtils.copy(reader, writer);
assertTrue(StaxUtils.toString(writer.getDocument()).endsWith(RESULT));
}
Aggregations