Search in sources :

Example 81 with WrappedMessageContext

use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.

the class SAMLTokenValidatorCachedRealmTest method createValidatorParameters.

private TokenValidatorParameters createValidatorParameters() throws WSSecurityException {
    TokenValidatorParameters parameters = new TokenValidatorParameters();
    TokenRequirements tokenRequirements = new TokenRequirements();
    tokenRequirements.setTokenType(STSConstants.STATUS);
    parameters.setTokenRequirements(tokenRequirements);
    KeyRequirements keyRequirements = new KeyRequirements();
    parameters.setKeyRequirements(keyRequirements);
    parameters.setPrincipal(new CustomTokenPrincipal("alice"));
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
    parameters.setMessageContext(msgCtx);
    // Add STSProperties object
    StaticSTSProperties stsProperties = new StaticSTSProperties();
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    stsProperties.setEncryptionCrypto(crypto);
    stsProperties.setSignatureCrypto(crypto);
    stsProperties.setEncryptionUsername("myservicekey");
    stsProperties.setSignatureUsername("mystskey");
    stsProperties.setCallbackHandler(new PasswordCallbackHandler());
    stsProperties.setIssuer("STS-2");
    parameters.setStsProperties(stsProperties);
    parameters.setTokenStore(tokenStore);
    return parameters;
}
Also used : CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Crypto(org.apache.wss4j.common.crypto.Crypto) TokenRequirements(org.apache.cxf.sts.request.TokenRequirements) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) KeyRequirements(org.apache.cxf.sts.request.KeyRequirements) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 82 with WrappedMessageContext

use of org.apache.cxf.jaxws.context.WrappedMessageContext in project camel by apache.

the class CxfProducer method prepareRequest.

protected Map<String, Object> prepareRequest(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) throws Exception {
    // create invocation context
    WrappedMessageContext requestContext = new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);
    camelExchange.setProperty(Message.MTOM_ENABLED, String.valueOf(endpoint.isMtomEnabled()));
    // set data format mode in exchange
    DataFormat dataFormat = endpoint.getDataFormat();
    camelExchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, dataFormat);
    LOG.trace("Set Camel Exchange property: {}={}", DataFormat.class.getName(), dataFormat);
    if (endpoint.getMergeProtocolHeaders()) {
        camelExchange.setProperty(CxfConstants.CAMEL_CXF_PROTOCOL_HEADERS_MERGED, Boolean.TRUE);
    }
    // set data format mode in the request context
    requestContext.put(DataFormat.class.getName(), dataFormat);
    // don't let CXF ClientImpl close the input stream 
    if (dataFormat.dealias() == DataFormat.RAW) {
        cxfExchange.put(Client.KEEP_CONDUIT_ALIVE, true);
        LOG.trace("Set CXF Exchange property: {}={}", Client.KEEP_CONDUIT_ALIVE, true);
    }
    // bind the request CXF exchange
    endpoint.getCxfBinding().populateCxfRequestFromExchange(cxfExchange, camelExchange, requestContext);
    // add appropriate cookies from the cookie store to the protocol headers
    if (endpoint.getCookieHandler() != null) {
        try {
            Map<String, List<String>> transportHeaders = CastUtils.cast((Map<?, ?>) requestContext.get(Message.PROTOCOL_HEADERS));
            boolean added;
            if (transportHeaders == null) {
                transportHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
                added = true;
            } else {
                added = false;
            }
            transportHeaders.putAll(endpoint.getCookieHandler().loadCookies(camelExchange, endpoint.getRequestUri(camelExchange)));
            if (added && transportHeaders.size() > 0) {
                requestContext.put(Message.PROTOCOL_HEADERS, transportHeaders);
            }
        } catch (IOException e) {
            LOG.warn("Cannot load cookies", e);
        }
    }
    // Remove protocol headers from scopes.  Otherwise, response headers can be
    // overwritten by request headers when SOAPHandlerInterceptor tries to create
    // a wrapped message context by the copyScoped() method.
    requestContext.getScopes().remove(Message.PROTOCOL_HEADERS);
    return requestContext.getWrappedMap();
}
Also used : WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) List(java.util.List) IOException(java.io.IOException)

Example 83 with WrappedMessageContext

use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.

the class ContextPropertiesMappingTest method testCreateWebServiceContextWithInAttachments.

@Test
public void testCreateWebServiceContextWithInAttachments() {
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    Collection<Attachment> attachments = new LinkedList<>();
    DataSource source = new ByteDataSource(new byte[0], "text/xml");
    DataHandler handler1 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part1", handler1));
    DataHandler handler2 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part2", handler2));
    inMessage.setAttachments(attachments);
    inMessage.putAll(message);
    exchange.setInMessage(inMessage);
    exchange.setOutMessage(new MessageImpl());
    MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
    Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
    assertNotNull("inbound attachments object must be initialized", inAttachments);
    assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
    Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) inAttachments);
    assertEquals("two inbound attachments expected", 2, dataHandlers.size());
    assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
    // should do as it's the same instance
    assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
    assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
    assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) LinkedList(java.util.LinkedList) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource) Exchange(org.apache.cxf.message.Exchange) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 84 with WrappedMessageContext

use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.

the class HandlerChainInvokerTest method setUp.

@Before
public void setUp() {
    AbstractHandlerBase.clear();
    @SuppressWarnings("rawtypes") List<Handler> handlers = new ArrayList<>();
    for (int i = 0; i < logicalHandlers.length; i++) {
        logicalHandlers[i] = new TestLogicalHandler();
        handlers.add(logicalHandlers[i]);
    }
    for (int i = 0; i < protocolHandlers.length; i++) {
        protocolHandlers[i] = new TestProtocolHandler();
        handlers.add(protocolHandlers[i]);
    }
    invoker = new HandlerChainInvoker(handlers);
    message = new MessageImpl();
    Exchange e = new ExchangeImpl();
    message.setExchange(e);
    lmc = new LogicalMessageContextImpl(message);
    pmc = new WrappedMessageContext(message);
/*
        payload = new DOMSource();
        message.setContent(Source.class, payload);*/
}
Also used : ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) Exchange(org.apache.cxf.message.Exchange) LogicalMessageContextImpl(org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Before(org.junit.Before)

Example 85 with WrappedMessageContext

use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.

the class HandlerChainInvoker method setFaultMessage.

/*
     * When the message direction is reversed, if the message is not already a
     * fault message then it is replaced with a fault message
     */
private void setFaultMessage(MessageContext mc, Exception exception) {
    Message msg = ((WrappedMessageContext) mc).getWrappedMessage();
    msg.setContent(Exception.class, exception);
    msg.removeContent(XMLStreamReader.class);
    msg.removeContent(Source.class);
    try {
        SoapVersion version = null;
        if (msg instanceof SoapMessage) {
            version = ((SoapMessage) msg).getVersion();
        }
        SOAPMessage soapMessage = SAAJFactoryResolver.createMessageFactory(version).createMessage();
        msg.setContent(SOAPMessage.class, soapMessage);
        SOAPBody body = SAAJUtils.getBody(soapMessage);
        SOAPFault soapFault = body.addFault();
        if (exception instanceof SOAPFaultException) {
            SOAPFaultException sf = (SOAPFaultException) exception;
            soapFault.setFaultString(sf.getFault().getFaultString());
            SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
            soapFault.setFaultActor(sf.getFault().getFaultActor());
            if (sf.getFault().hasDetail()) {
                Node nd = soapMessage.getSOAPPart().importNode(sf.getFault().getDetail(), true);
                nd = nd.getFirstChild();
                soapFault.addDetail();
                while (nd != null) {
                    soapFault.getDetail().appendChild(nd);
                    nd = nd.getNextSibling();
                }
            }
        } else if (exception instanceof Fault) {
            SoapFault sf = SoapFault.createFault((Fault) exception, ((SoapMessage) msg).getVersion());
            soapFault.setFaultString(sf.getReason());
            SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
            if (sf.hasDetails()) {
                soapFault.addDetail();
                Node nd = soapMessage.getSOAPPart().importNode(sf.getDetail(), true);
                nd = nd.getFirstChild();
                while (nd != null) {
                    soapFault.getDetail().appendChild(nd);
                    nd = nd.getNextSibling();
                }
            }
        } else {
            SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandlerFault"));
            soapFault.setFaultString(exception.getMessage());
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    // do nothing
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Fault(org.apache.cxf.interceptor.Fault) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SOAPFault(javax.xml.soap.SOAPFault)

Aggregations

WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)167 MessageImpl (org.apache.cxf.message.MessageImpl)152 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)137 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)115 Crypto (org.apache.wss4j.common.crypto.Crypto)113 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)107 JAXBElement (javax.xml.bind.JAXBElement)93 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)93 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)86 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)83 Element (org.w3c.dom.Element)65 ServiceMBean (org.apache.cxf.sts.service.ServiceMBean)61 StaticService (org.apache.cxf.sts.service.StaticService)61 RequestSecurityTokenResponseCollectionType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType)56 KeyRequirements (org.apache.cxf.sts.request.KeyRequirements)54 TokenRequirements (org.apache.cxf.sts.request.TokenRequirements)54 EncryptionProperties (org.apache.cxf.sts.service.EncryptionProperties)53 SecurityContext (org.apache.cxf.security.SecurityContext)51 Principal (java.security.Principal)49 RequestedSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType)49