Search in sources :

Example 56 with SOAPMessage

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();
    }
}
Also used : FactorySelector(org.apache.axiom.ts.saaj.FactorySelector) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) MimeHeaders(javax.xml.soap.MimeHeaders) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 57 with SOAPMessage

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);
    }
}
Also used : WSSecurityUtil(org.apache.wss4j.dom.util.WSSecurityUtil) StringUtils(org.apache.commons.lang.StringUtils) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) MetadataConstants(org.apache.cxf.ws.addressing.policy.MetadataConstants) SOAPException(javax.xml.soap.SOAPException) STSClientConfiguration(ddf.security.sts.client.configuration.STSClientConfiguration) LoggerFactory(org.slf4j.LoggerFactory) XMLUtils(org.codice.ddf.platform.util.XMLUtils) SoapBindingConstants(org.apache.cxf.binding.soap.SoapBindingConstants) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) AbstractHTTPDestination(org.apache.cxf.transport.http.AbstractHTTPDestination) SOAPElement(javax.xml.soap.SOAPElement) HttpServletRequest(javax.servlet.http.HttpServletRequest) WSS4JInInterceptor(org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap) Fault(org.apache.cxf.interceptor.Fault) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) PolicyBasedWSS4JInInterceptor(org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JInInterceptor) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) EncryptionService(ddf.security.encryption.EncryptionService) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Phase(org.apache.cxf.phase.Phase) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) PolicyBasedWSS4JOutInterceptor(org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Logger(org.slf4j.Logger) Security(org.codice.ddf.security.common.Security) Message(org.apache.cxf.message.Message) WSHandlerConstants(org.apache.wss4j.dom.handler.WSHandlerConstants) Set(java.util.Set) Subject(ddf.security.Subject) UUID(java.util.UUID) SecurityConstants(org.apache.cxf.ws.security.SecurityConstants) TimeUnit(java.util.concurrent.TimeUnit) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Element(org.w3c.dom.Element) MessageUtils(org.apache.cxf.message.MessageUtils) AbstractWSS4JInterceptor(org.apache.cxf.ws.security.wss4j.AbstractWSS4JInterceptor) CacheBuilder(com.google.common.cache.CacheBuilder) SOAPMessage(javax.xml.soap.SOAPMessage) Cache(com.google.common.cache.Cache) SecurityManager(ddf.security.service.SecurityManager) SOAPFactory(javax.xml.soap.SOAPFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 58 with SOAPMessage

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;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) InputStreamReader(java.io.InputStreamReader) 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 59 with SOAPMessage

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();
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) HashSet(java.util.HashSet)

Example 60 with SOAPMessage

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));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SOAPPart(javax.xml.soap.SOAPPart) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) Test(org.junit.Test)

Aggregations

SOAPMessage (javax.xml.soap.SOAPMessage)219 SOAPException (javax.xml.soap.SOAPException)87 SOAPBody (javax.xml.soap.SOAPBody)47 Test (org.junit.Test)46 InputStream (java.io.InputStream)45 QName (javax.xml.namespace.QName)45 Element (org.w3c.dom.Element)44 IOException (java.io.IOException)40 MessageFactory (javax.xml.soap.MessageFactory)40 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)30 SOAPElement (javax.xml.soap.SOAPElement)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)25 XMLStreamReader (javax.xml.stream.XMLStreamReader)25 Node (org.w3c.dom.Node)24 Document (org.w3c.dom.Document)22 URL (java.net.URL)21 SOAPPart (javax.xml.soap.SOAPPart)21 Exchange (org.apache.cxf.message.Exchange)19 MessageImpl (org.apache.cxf.message.MessageImpl)19