Java Code Examples for org.apache.cxf.binding.soap.SoapMessage#getContent()

The following examples show how to use org.apache.cxf.binding.soap.SoapMessage#getContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SOAPHandlerFaultInInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handleMessage(SoapMessage message) {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    if (getInvoker(message).getProtocolHandlers().isEmpty()) {
        return;
    }

    checkUnderstoodHeaders(message);
    MessageContext context = createProtocolMessageContext(message);
    HandlerChainInvoker invoker = getInvoker(message);
    invoker.setProtocolMessageContext(context);

    if (!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
        handleAbort(message, context);
    }

    SOAPMessage msg = message.getContent(SOAPMessage.class);
    if (msg != null) {
        XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(msg);
        message.setContent(XMLStreamReader.class, xmlReader);
    }

}
 
Example 2
Source File: ReadHeadersInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc}*/
public void handleMessage(SoapMessage message) throws Fault {
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    if (xmlReader != null) {
        try {
            while (xmlReader.hasNext()) {
                if (xmlReader.next() == XMLStreamConstants.END_DOCUMENT) {
                    return;
                }
            }
        } catch (XMLStreamException e) {
            throw new SoapFault(e.getMessage(), e,
                                message.getVersion().getSender());
        }
    }
}
 
Example 3
Source File: WSS4JOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage mc) throws Fault {
    if (mc.getContent(SOAPMessage.class) == null) {
        saajOut.handleMessage(mc);
    }

    // If a custom Id has been set, then change the Id for the internal interceptor as well, as otherwise
    // we can't add two WSS4JOutInterceptor instances to the interceptor chain.
    if (!WSS4JOutInterceptor.class.getName().equals(getId())) {
        Random random = new Random();
        int randomInt = random.nextInt();
        ending.setId(WSS4JOutInterceptorInternal.class.getName() + "_" + randomInt);
    }
    mc.getInterceptorChain().add(ending);
}
 
Example 4
Source File: WSS4JFaultCodeTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Test for WSS4JInInterceptor when it receives a message with no security header.
 */
@Test
public void testNoSecurity() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    SoapMessage msg = getSoapMessageForDom(doc);
    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION, ConfigurationConstants.ENCRYPTION);
    inHandler.setProperty(ConfigurationConstants.DEC_PROP_FILE, "insecurity.properties");
    inHandler.setProperty(ConfigurationConstants.PW_CALLBACK_CLASS, TestPwdCallback.class.getName());

    inmsg.put(SecurityConstants.RETURN_SECURITY_ERROR, Boolean.TRUE);

    try {
        inHandler.handleMessage(inmsg);
        fail("Expected failure on an message with no security header");
    } catch (SoapFault fault) {
        assertTrue(fault.getReason().startsWith(
            "An error was discovered processing the <wsse:Security> header"));
        QName faultCode = new QName(WSS4JConstants.WSSE_NS, "InvalidSecurity");
        assertEquals(fault.getFaultCode(), faultCode);
    }
}
 
Example 5
Source File: SoapFaultSerializerTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSoap11Out() throws Exception {
    String faultString = "Hadrian caused this Fault!";
    SoapFault fault = new SoapFault(faultString, Soap11.getInstance().getSender());

    SoapMessage m = new SoapMessage(new MessageImpl());
    m.setExchange(new ExchangeImpl());
    m.setContent(Exception.class, fault);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
    writer.writeStartDocument();
    writer.writeStartElement("Body");

    m.setContent(XMLStreamWriter.class, writer);

    Soap11FaultOutInterceptorInternal.INSTANCE.handleMessage(m);

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.close();

    Document faultDoc = StaxUtils.read(new ByteArrayInputStream(out.toByteArray()));
    assertValid("//s:Fault/faultcode[text()='ns1:Client']", faultDoc);
    assertValid("//s:Fault/faultstring[text()='" + faultString + "']", faultDoc);

    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(out.toByteArray()));
    m.setContent(XMLStreamReader.class, reader);

    reader.nextTag();

    Soap11FaultInInterceptor inInterceptor = new Soap11FaultInInterceptor();
    inInterceptor.handleMessage(m);

    SoapFault fault2 = (SoapFault)m.getContent(Exception.class);
    assertNotNull(fault2);
    assertEquals(fault.getMessage(), fault2.getMessage());
    assertEquals(Soap11.getInstance().getSender(), fault2.getFaultCode());
}
 
Example 6
Source File: WSS4JFaultCodeTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignedEncryptedSOAP12Fault() throws Exception {
    Document doc = readDocument("wsse-response-fault.xml");

    SoapMessage msg = getSoapMessageForDom(doc, SOAPConstants.SOAP_1_2_PROTOCOL);
    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION,
                          ConfigurationConstants.SIGNATURE + " "  + ConfigurationConstants.ENCRYPTION);
    inHandler.setProperty(ConfigurationConstants.DEC_PROP_FILE, "insecurity.properties");
    inHandler.setProperty(ConfigurationConstants.SIG_VER_PROP_FILE, "insecurity.properties");
    inHandler.setProperty(ConfigurationConstants.PW_CALLBACK_CLASS, TestPwdCallback.class.getName());
    inHandler.setProperty(
        ConfigurationConstants.PW_CALLBACK_CLASS,
        "org.apache.cxf.ws.security.wss4j.TestPwdCallback"
    );

    inHandler.handleMessage(inmsg);
    // StaxUtils.print(saajMsg.getSOAPPart());
}
 
Example 7
Source File: PolicyBasedWSS4JOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage mc) throws Fault {
    boolean enableStax =
        MessageUtils.getContextualBoolean(mc, SecurityConstants.ENABLE_STREAMING_SECURITY);
    if (!enableStax) {
        if (mc.getContent(SOAPMessage.class) == null) {
            saajOut.handleMessage(mc);
        }
        mc.put(SECURITY_PROCESSED, Boolean.TRUE);
        mc.getInterceptorChain().add(ending);
    }
}
 
Example 8
Source File: WSS4JOutInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage mc) throws Fault {
    //must turn off mtom when using WS-Sec so binary is inlined so it can
    //be properly signed/encrypted/etc...
    if (!mtomEnabled) {
        mc.put(org.apache.cxf.message.Message.MTOM_ENABLED, false);
    }
    
    if (mc.getContent(SOAPMessage.class) == null) {
        saajOut.handleMessage(mc);
    }
    
    mc.getInterceptorChain().add(ending);
}
 
Example 9
Source File: WSS4JInInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
public final boolean isGET(SoapMessage message) {
    String method = (String)message.get(SoapMessage.HTTP_REQUEST_METHOD);
    boolean isGet = 
        "GET".equals(method) && message.getContent(XMLStreamReader.class) == null;
    if (isGet) {
        //make sure we skip the URIMapping as we cannot apply security requirements to that
        message.put(URIMappingInterceptor.URIMAPPING_SKIP, Boolean.TRUE);
    }
    return isGet;
}
 
Example 10
Source File: SonosFaultInterceptor.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = (Fault) message.getContent(Exception.class);
    LOG.warn("Error with Soap message", fault);

    if (fault.getCause() instanceof SonosSoapFault) {
        SonosSoapFault cause = (SonosSoapFault) fault.getCause();
        fault.setFaultCode(new QName(cause.getFaultCode()));
        fault.setMessage(cause.getFaultCode());

        Document document = DOMUtils.createDocument();
        Element details = document.createElement("detail");
        fault.setDetail(details);

        if (cause instanceof TokenRefreshRequired) {
            try {
                marshaller.marshal(((TokenRefreshRequired) cause).getRefreshTokens(), details);
            } catch (JAXBException e) {
                LOG.warn("Could not marshal Sonos refresh tokens", e);
            }
        } else {
            details.appendChild(document.createElement("ExceptionInfo"));

            Element sonosError = document.createElement("SonosError");
            sonosError.setTextContent(String.valueOf(cause.getSonosError()));
            details.appendChild(sonosError);
        }
    }
}
 
Example 11
Source File: WSS4JOutInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage mc) throws Fault {
    //must turn off mtom when using WS-Sec so binary is inlined so it can
    //be properly signed/encrypted/etc...
    if (!mtomEnabled) {
        mc.put(org.apache.cxf.message.Message.MTOM_ENABLED, false);
    }
    
    if (mc.getContent(SOAPMessage.class) == null) {
        saajOut.handleMessage(mc);
    }
    
    mc.getInterceptorChain().add(ending);
}
 
Example 12
Source File: CheckFaultInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage message) {
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    if (xmlReader == null) {
        return;
    }
    try {
        // advance to first tag.
        int x = xmlReader.getEventType();
        while (x != XMLStreamConstants.START_ELEMENT
            && x != XMLStreamConstants.END_ELEMENT
            && xmlReader.hasNext()) {
            x = xmlReader.next();
        }
        if (!xmlReader.hasNext()) {
            //end of document, just return
            return;
        }
    } catch (XMLStreamException e) {
        throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e,
                            message.getVersion().getSender());
    }
    if (message.getVersion().getFault().equals(xmlReader.getName()) && isRequestor(message)) {
        Endpoint ep = message.getExchange().getEndpoint();
        message.getInterceptorChain().abort();
        if (ep.getInFaultObserver() != null) {
            ep.getInFaultObserver().onMessage(message);
        }
    }
}
 
Example 13
Source File: WSS4JOutInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage mc) throws Fault {
    //must turn off mtom when using WS-Sec so binary is inlined so it can
    //be properly signed/encrypted/etc...
    if (!mtomEnabled) {
        mc.put(org.apache.cxf.message.Message.MTOM_ENABLED, false);
    }
    
    if (mc.getContent(SOAPMessage.class) == null) {
        saajOut.handleMessage(mc);
    }
    
    mc.getInterceptorChain().add(ending);
}
 
Example 14
Source File: SOAPHandlerInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(SoapMessage message) {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    if (getInvoker(message).getProtocolHandlers().isEmpty()) {
        return;
    }

    checkUnderstoodHeaders(message);

    if (getInvoker(message).isOutbound()) {
        if (!chainAlreadyContainsSAAJ(message)) {
            SAAJ_OUT.handleMessage(message);
        }
        message.getInterceptorChain().add(ending);
    } else {
        boolean isFault = handleMessageInternal(message);
        SOAPMessage msg = message.getContent(SOAPMessage.class);
        if (msg != null) {
            XMLStreamReader xmlReader = createXMLStreamReaderFromSOAPMessage(msg);
            message.setContent(XMLStreamReader.class, xmlReader);
            // replace headers
            try {
                SAAJInInterceptor.replaceHeaders(msg, message);
            } catch (SOAPException e) {
                e.printStackTrace();
            }
        }
        if (isFault) {
            Endpoint ep = message.getExchange().getEndpoint();
            message.getInterceptorChain().abort();
            if (ep.getInFaultObserver() != null) {
                ep.getInFaultObserver().onMessage(message);

            }
        }
    }
}
 
Example 15
Source File: EjbInterceptor.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void reserialize(SoapMessage message) throws Exception {
    SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
    if (soapMessage == null) {
        return;
    }

    DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart());
    XMLStreamReader xmlReader = StaxUtils.createXMLStreamReader(bodySource);
    message.setContent(XMLStreamReader.class, xmlReader);
}
 
Example 16
Source File: WSS4JInInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
private SOAPMessage getSOAPMessage(SoapMessage msg) {
    SAAJInInterceptor.INSTANCE.handleMessage(msg);
    return msg.getContent(SOAPMessage.class);
}
 
Example 17
Source File: WSS4JInOutTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomProcessorObject() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = getSoapMessageForDom(doc);

    msg.put(ConfigurationConstants.ACTION, ConfigurationConstants.SIGNATURE);
    msg.put(ConfigurationConstants.SIG_PROP_FILE, "outsecurity.properties");
    msg.put(ConfigurationConstants.USER, "myalias");
    msg.put("password", "myAliasPassword");

    handler.handleMessage(msg);

    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    assertValid("//wsse:Security", doc);
    assertValid("//wsse:Security/ds:Signature", doc);

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    final Map<String, Object> properties = new HashMap<>();
    final Map<QName, Object> customMap = new HashMap<>();
    customMap.put(
        new QName(
            WSS4JConstants.SIG_NS,
            WSS4JConstants.SIG_LN
        ),
        CustomProcessor.class
    );
    properties.put(
        WSS4JInInterceptor.PROCESSOR_MAP,
        customMap
    );
    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(properties);

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION, ConfigurationConstants.SIGNATURE);

    inHandler.handleMessage(inmsg);

    List<WSHandlerResult> results = getResults(inmsg);
    assertTrue(results != null && results.size() == 1);
    List<WSSecurityEngineResult> signatureResults =
        results.get(0).getActionResults().get(WSConstants.SIGN);
    assertTrue(signatureResults.size() == 1);

    Object obj = signatureResults.get(0).get("foo");
    assertNotNull(obj);
    assertEquals(obj.getClass().getName(), CustomProcessor.class.getName());
}
 
Example 18
Source File: WSS4JFaultCodeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Test that an action mismatch gets mapped to a proper fault code
 */
@Test
public void testActionMismatch() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = getSoapMessageForDom(doc);

    msg.put(ConfigurationConstants.ACTION, ConfigurationConstants.TIMESTAMP);

    handler.handleMessage(msg);

    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    assertValid("//wsse:Security", doc);

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION,
        ConfigurationConstants.TIMESTAMP + " " + ConfigurationConstants.USERNAME_TOKEN);
    inHandler.setProperty(ConfigurationConstants.PW_CALLBACK_CLASS, TestPwdCallback.class.getName());

    inmsg.put(SecurityConstants.RETURN_SECURITY_ERROR, Boolean.TRUE);

    try {
        inHandler.handleMessage(inmsg);
        fail("Expected failure on an action mismatch");
    } catch (SoapFault fault) {
        assertTrue(fault.getReason().startsWith(
            "An error was discovered processing the <wsse:Security> header"));
        QName faultCode = new QName(WSS4JConstants.WSSE_NS, "InvalidSecurity");
        assertEquals(fault.getFaultCode(), faultCode);
    }
}
 
Example 19
Source File: WSS4JFaultCodeTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Test that an invalid Timestamp gets mapped to a proper fault code
 */
@Test
public void testInvalidTimestamp() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = getSoapMessageForDom(doc);

    msg.put(ConfigurationConstants.ACTION, ConfigurationConstants.TIMESTAMP);
    msg.put(ConfigurationConstants.TTL_TIMESTAMP, "1");

    handler.handleMessage(msg);

    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    assertValid("//wsse:Security", doc);

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION, ConfigurationConstants.TIMESTAMP);
    inHandler.setProperty(ConfigurationConstants.TTL_TIMESTAMP, "1");
    inmsg.put(SecurityConstants.RETURN_SECURITY_ERROR, Boolean.TRUE);

    try {
        //
        // Sleep for over a second to make the timestamp invalid
        //
        Thread.sleep(1250);
        inHandler.handleMessage(inmsg);
        fail("Expected failure on an invalid Timestamp");
    } catch (SoapFault fault) {
        assertTrue(fault.getReason().contains("Invalid timestamp"));
        QName faultCode = new QName(WSS4JConstants.WSSE_NS, "MessageExpired");
        assertEquals(fault.getFaultCode(), faultCode);
    }
}
 
Example 20
Source File: WSS4JInOutTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomProcessor() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = getSoapMessageForDom(doc);

    msg.put(ConfigurationConstants.ACTION, ConfigurationConstants.SIGNATURE);
    msg.put(ConfigurationConstants.SIG_PROP_FILE, "outsecurity.properties");
    msg.put(ConfigurationConstants.USER, "myalias");
    msg.put("password", "myAliasPassword");

    handler.handleMessage(msg);

    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();

    assertValid("//wsse:Security", doc);
    assertValid("//wsse:Security/ds:Signature", doc);

    byte[] docbytes = getMessageBytes(doc);
    doc = StaxUtils.read(new ByteArrayInputStream(docbytes));

    final Map<String, Object> properties = new HashMap<>();
    properties.put(
        WSS4JInInterceptor.PROCESSOR_MAP,
        createCustomProcessorMap()
    );
    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(properties);

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(ConfigurationConstants.ACTION, WSHandlerConstants.NO_SECURITY);

    inHandler.handleMessage(inmsg);

    List<WSHandlerResult> results = getResults(inmsg);
    assertTrue(results != null && results.size() == 1);
    List<WSSecurityEngineResult> signatureResults =
        results.get(0).getActionResults().get(WSConstants.SIGN);
    assertTrue(signatureResults == null || signatureResults.isEmpty());
}