Search in sources :

Example 16 with Header

use of org.apache.cxf.headers.Header in project cxf by apache.

the class MAPCodec method unmarshalMAPs.

/**
 * Decode the MAPs from protocol-specific headers.
 *
 * @param message the SOAP message
 * @param the decoded MAPs
 * @exception SOAPFaultException if decoded MAPs are invalid
 */
public AddressingProperties unmarshalMAPs(SoapMessage message) {
    // REVISIT generate MessageAddressingHeaderRequired fault if an
    // expected header is missing
    AddressingProperties maps = null;
    Unmarshaller unmarshaller = null;
    try {
        List<Header> header = message.getHeaders();
        if (header != null) {
            LOG.log(Level.FINE, "Inbound WS-Addressing headers");
            Set<Element> referenceParameterHeaders = null;
            QName invalidCardinalityQName = null;
            Iterator<Header> iter = header.iterator();
            while (iter.hasNext()) {
                Header hdr = iter.next();
                if (hdr.getObject() instanceof Element) {
                    Element headerElement = (Element) hdr.getObject();
                    String headerURI = headerElement.getNamespaceURI();
                    // headers.
                    if (org.apache.cxf.ws.addressing.VersionTransformer.isSupported(headerURI)) {
                        if (unmarshaller == null) {
                            JAXBContext jaxbContext = org.apache.cxf.ws.addressing.VersionTransformer.getExposedJAXBContext(headerURI);
                            unmarshaller = jaxbContext.createUnmarshaller();
                            unmarshaller.setEventHandler(null);
                        }
                        if (maps == null) {
                            maps = new AddressingProperties();
                            maps.exposeAs(headerURI);
                        }
                        String localName = headerElement.getLocalName();
                        if (Names.WSA_MESSAGEID_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getMessageID() != null ? Names.WSA_MESSAGEID_QNAME : null;
                            maps.setMessageID(decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller));
                        } else if (Names.WSA_TO_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getTo() != null ? Names.WSA_TO_QNAME : null;
                            AttributedURIType addr = decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller);
                            maps.setTo(EndpointReferenceUtils.getEndpointReference(addr));
                        } else if (Names.WSA_FROM_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getFrom() != null ? Names.WSA_FROM_QNAME : null;
                            maps.setFrom(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
                        } else if (Names.WSA_REPLYTO_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getReplyTo() != null ? Names.WSA_REPLYTO_QNAME : null;
                            maps.setReplyTo(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
                        } else if (Names.WSA_FAULTTO_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getFaultTo() != null ? Names.WSA_FAULTTO_QNAME : null;
                            maps.setFaultTo(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
                        } else if (Names.WSA_RELATESTO_NAME.equals(localName)) {
                            maps.setRelatesTo(decodeAsNative(headerURI, RelatesToType.class, headerElement, unmarshaller));
                        } else if (Names.WSA_ACTION_NAME.equals(localName)) {
                            invalidCardinalityQName = maps.getAction() != null ? Names.WSA_ACTION_QNAME : null;
                            maps.setAction(decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller));
                        }
                    } else if (null != headerElement.getAttribute(IS_REFERENCE_PARAM_ATTR_NAME)) {
                        if (null == referenceParameterHeaders) {
                            referenceParameterHeaders = new HashSet<>();
                        }
                        referenceParameterHeaders.add(headerElement);
                    } else if (headerURI.contains(Names.WSA_NAMESPACE_PATTERN)) {
                        LOG.log(Level.WARNING, "UNSUPPORTED_VERSION_MSG", headerURI);
                    }
                }
            }
            if (maps != null && !MessageUtils.isRequestor(message) && maps.getReplyTo() == null) {
                // per spec, if unspecified, replyTo is anon
                AttributedURIType address = ContextUtils.getAttributedURI(Names.WSA_ANONYMOUS_ADDRESS);
                EndpointReferenceType replyTo = ContextUtils.WSA_OBJECT_FACTORY.createEndpointReferenceType();
                replyTo.setAddress(address);
                maps.setReplyTo(replyTo);
            }
            if (null != referenceParameterHeaders && null != maps) {
                decodeReferenceParameters(referenceParameterHeaders, maps, unmarshaller);
            }
            if (invalidCardinalityQName != null) {
                storeInvalidCardinalityFault(message, invalidCardinalityQName);
            }
        }
    } catch (JAXBException je) {
        LOG.log(Level.WARNING, "SOAP_HEADER_DECODE_FAILURE_MSG", je);
    } finally {
        JAXBUtils.closeUnmarshaller(unmarshaller);
    }
    return maps;
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) JAXBException(javax.xml.bind.JAXBException) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) JAXBContext(javax.xml.bind.JAXBContext) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Header(org.apache.cxf.headers.Header) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) Unmarshaller(javax.xml.bind.Unmarshaller) HashSet(java.util.HashSet)

Example 17 with Header

use of org.apache.cxf.headers.Header in project cxf by apache.

the class DecoupledFaultHandlerTest method testOnewayFault.

@Test
public void testOnewayFault() {
    DecoupledFaultHandler handler = new DecoupledFaultHandler() {

        protected Destination createDecoupledDestination(Exchange exchange, EndpointReferenceType epr) {
            assertEquals("http://bar", epr.getAddress().getValue());
            return EasyMock.createMock(Destination.class);
        }
    };
    SoapMessage message = new SoapMessage(new MessageImpl());
    QName qname = new QName("http://cxf.apache.org/mustunderstand", "TestMU");
    message.getHeaders().add(new Header(qname, new Object()));
    AddressingProperties maps = new AddressingProperties();
    EndpointReferenceType faultTo = new EndpointReferenceType();
    faultTo.setAddress(new AttributedURIType());
    faultTo.getAddress().setValue("http://bar");
    maps.setFaultTo(faultTo);
    message.put(ContextUtils.getMAPProperty(false, false, false), maps);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    exchange.setInMessage(message);
    exchange.setOneWay(true);
    handler.handleFault(message);
    assertTrue(message.getHeaders().isEmpty());
    assertFalse(exchange.isOneWay());
    assertSame(message, exchange.getOutMessage());
    assertNotNull(exchange.getDestination());
}
Also used : Exchange(org.apache.cxf.message.Exchange) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Test(org.junit.Test)

Example 18 with Header

use of org.apache.cxf.headers.Header in project cxf by apache.

the class SamlTokenInterceptor method processToken.

protected void processToken(SoapMessage message) {
    Header h = findSecurityHeader(message, false);
    if (h == null) {
        return;
    }
    Element el = (Element) h.getObject();
    Element child = DOMUtils.getFirstElement(el);
    while (child != null) {
        if ("Assertion".equals(child.getLocalName()) && (WSS4JConstants.SAML_NS.equals(child.getNamespaceURI()) || WSS4JConstants.SAML2_NS.equals(child.getNamespaceURI()))) {
            try {
                List<WSSecurityEngineResult> samlResults = processToken(child, message);
                if (samlResults != null) {
                    List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
                    if (results == null) {
                        results = new ArrayList<>();
                        message.put(WSHandlerConstants.RECV_RESULTS, results);
                    }
                    boolean signed = false;
                    for (WSSecurityEngineResult result : samlResults) {
                        SamlAssertionWrapper wrapper = (SamlAssertionWrapper) result.get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
                        if (wrapper.isSigned()) {
                            signed = true;
                            break;
                        }
                    }
                    assertTokens(message, SPConstants.SAML_TOKEN, signed);
                    Integer key = WSConstants.ST_UNSIGNED;
                    if (signed) {
                        key = WSConstants.ST_SIGNED;
                    }
                    WSHandlerResult rResult = new WSHandlerResult(null, samlResults, Collections.singletonMap(key, samlResults));
                    results.add(0, rResult);
                    // Check version against policy
                    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
                    for (AssertionInfo ai : PolicyUtils.getAllAssertionsByLocalname(aim, SPConstants.SAML_TOKEN)) {
                        SamlToken samlToken = (SamlToken) ai.getAssertion();
                        for (WSSecurityEngineResult result : samlResults) {
                            SamlAssertionWrapper assertionWrapper = (SamlAssertionWrapper) result.get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
                            if (!checkVersion(aim, samlToken, assertionWrapper)) {
                                ai.setNotAsserted("Wrong SAML Version");
                            }
                            TLSSessionInfo tlsInfo = message.get(TLSSessionInfo.class);
                            Certificate[] tlsCerts = null;
                            if (tlsInfo != null) {
                                tlsCerts = tlsInfo.getPeerCertificates();
                            }
                            if (!DOMSAMLUtil.checkHolderOfKey(assertionWrapper, null, tlsCerts)) {
                                ai.setNotAsserted("Assertion fails holder-of-key requirements");
                                continue;
                            }
                            if (!DOMSAMLUtil.checkSenderVouches(assertionWrapper, tlsCerts, null, null)) {
                                ai.setNotAsserted("Assertion fails sender-vouches requirements");
                                continue;
                            }
                        }
                    }
                    if (signed) {
                        Principal principal = (Principal) samlResults.get(0).get(WSSecurityEngineResult.TAG_PRINCIPAL);
                        SecurityContext sc = message.get(SecurityContext.class);
                        if (sc == null || sc.getUserPrincipal() == null) {
                            message.put(SecurityContext.class, new DefaultSecurityContext(principal, null));
                        }
                    }
                }
            } catch (WSSecurityException ex) {
                throw WSS4JUtils.createSoapFault(message, message.getVersion(), ex);
            }
        }
        child = DOMUtils.getNextElement(child);
    }
}
Also used : DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) SamlToken(org.apache.wss4j.policy.model.SamlToken) Element(org.w3c.dom.Element) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap) Header(org.apache.cxf.headers.Header) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) TLSSessionInfo(org.apache.cxf.security.transport.TLSSessionInfo) Principal(java.security.Principal) Certificate(java.security.cert.Certificate)

Example 19 with Header

use of org.apache.cxf.headers.Header in project cxf by apache.

the class UsernameTokenInterceptor method addToken.

protected void addToken(SoapMessage message) {
    UsernameToken tok = assertTokens(message);
    Header h = findSecurityHeader(message, true);
    Element el = (Element) h.getObject();
    Document doc = el.getOwnerDocument();
    WSSecUsernameToken utBuilder = addUsernameToken(message, doc, tok);
    if (utBuilder == null) {
        AssertionInfoMap aim = message.get(AssertionInfoMap.class);
        Collection<AssertionInfo> ais = PolicyUtils.getAllAssertionsByLocalname(aim, SPConstants.USERNAME_TOKEN);
        for (AssertionInfo ai : ais) {
            if (ai.isAsserted()) {
                ai.setAsserted(false);
            }
        }
        return;
    }
    utBuilder.prepare();
    el.appendChild(utBuilder.getUsernameTokenElement());
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) Header(org.apache.cxf.headers.Header) Element(org.w3c.dom.Element) UsernameToken(org.apache.wss4j.policy.model.UsernameToken) WSSecUsernameToken(org.apache.wss4j.dom.message.WSSecUsernameToken) Document(org.w3c.dom.Document) WSSecUsernameToken(org.apache.wss4j.dom.message.WSSecUsernameToken) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 20 with Header

use of org.apache.cxf.headers.Header in project cxf by apache.

the class RMSoapInInterceptor method unmarshalRMProperties.

/**
 * Decode the RM properties from protocol-specific headers.
 *
 * @param message the SOAP message
 * @return the RM properties
 */
public RMProperties unmarshalRMProperties(SoapMessage message) {
    RMProperties rmps = (RMProperties) message.get(RMContextUtils.getRMPropertiesKey(false));
    if (rmps == null) {
        rmps = new RMProperties();
    }
    List<Header> headers = message.getHeaders();
    if (headers != null) {
        decodeHeaders(message, headers, rmps);
    }
    return rmps;
}
Also used : Header(org.apache.cxf.headers.Header) RMProperties(org.apache.cxf.ws.rm.RMProperties)

Aggregations

Header (org.apache.cxf.headers.Header)56 Element (org.w3c.dom.Element)25 ArrayList (java.util.ArrayList)19 JAXBException (javax.xml.bind.JAXBException)19 QName (javax.xml.namespace.QName)19 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)18 JAXBElement (javax.xml.bind.JAXBElement)14 OutofBandHeader (org.apache.cxf.outofband.header.OutofBandHeader)13 Node (org.w3c.dom.Node)11 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)10 JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)10 AddressingProperties (org.apache.cxf.ws.addressing.AddressingProperties)8 List (java.util.List)7 MessageContext (javax.xml.ws.handler.MessageContext)7 SoapVersion (org.apache.cxf.binding.soap.SoapVersion)7 Fault (org.apache.cxf.interceptor.Fault)6 Message (org.apache.cxf.message.Message)6 Test (org.junit.Test)6 Exchange (org.apache.cxf.message.Exchange)5 MessageContentsList (org.apache.cxf.message.MessageContentsList)5