Search in sources :

Example 46 with Header

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

the class BinarySecurityTokenInterceptor 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 (WSS4JConstants.BINARY_TOKEN_LN.equals(child.getLocalName()) && WSS4JConstants.WSSE_NS.equals(child.getNamespaceURI())) {
            try {
                List<WSSecurityEngineResult> bstResults = processToken(child, message);
                if (bstResults != null) {
                    List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
                    if (results == null) {
                        results = new ArrayList<>();
                        message.put(WSHandlerConstants.RECV_RESULTS, results);
                    }
                    WSHandlerResult rResult = new WSHandlerResult(null, bstResults, Collections.singletonMap(WSConstants.BST, bstResults));
                    results.add(0, rResult);
                    assertTokens(message);
                    Principal principal = (Principal) bstResults.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) Header(org.apache.cxf.headers.Header) Element(org.w3c.dom.Element) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) Principal(java.security.Principal)

Example 47 with Header

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

the class WSDiscoveryClient method addAddressing.

private void addAddressing(BindingProvider p, boolean addSeq, String action) {
    AddressingProperties addrProperties = new AddressingProperties();
    if (action != null) {
        AttributedURIType act = new AttributedURIType();
        act.setValue(action);
        addrProperties.setAction(act);
    }
    if (adHoc) {
        EndpointReferenceType to = new EndpointReferenceType();
        addrProperties.exposeAs(version.getAddressingNamespace());
        AttributedURIType epr = new AttributedURIType();
        epr.setValue(version.getToAddress());
        to.setAddress(epr);
        addrProperties.setTo(to);
        if (addSeq) {
            AppSequenceType s = new AppSequenceType();
            s.setInstanceId(instanceId);
            s.setMessageNumber(msgId.getAndIncrement());
            JAXBElement<AppSequenceType> seq = new ObjectFactory().createAppSequence(s);
            Header h = new Header(seq.getName(), seq, new JAXBDataBinding(getJAXBContext()));
            List<Header> headers = new ArrayList<>();
            headers.add(h);
            p.getRequestContext().put(Header.HEADER_LIST, headers);
        }
    } else {
        addrProperties.exposeAs(version.getAddressingNamespace());
    }
    p.getRequestContext().put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES, addrProperties);
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) ObjectFactory(org.apache.cxf.ws.discovery.wsdl.ObjectFactory) Header(org.apache.cxf.headers.Header) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) ArrayList(java.util.ArrayList) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) AppSequenceType(org.apache.cxf.ws.discovery.wsdl.AppSequenceType)

Example 48 with Header

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

the class SoapHeaderInterceptor method validateHeader.

private void validateHeader(final SoapMessage message, MessagePartInfo mpi, Schema schema) {
    Header param = findHeader(message, mpi);
    if (param != null && param.getDataBinding() == null) {
        Node source = (Node) param.getObject();
        if (!(source instanceof Element)) {
            return;
        }
        if (schema != null) {
            final Element el = (Element) source;
            DOMSource ds = new DOMSource(el);
            try {
                Validator v = schema.newValidator();
                ErrorHandler errorHandler = new ErrorHandler() {

                    public void warning(SAXParseException exception) throws SAXException {
                    }

                    public void error(SAXParseException exception) throws SAXException {
                        String msg = exception.getMessage();
                        if (msg.contains(el.getLocalName()) && (msg.contains(":" + message.getVersion().getAttrNameRole()) || msg.contains(":" + message.getVersion().getAttrNameMustUnderstand()))) {
                            return;
                        }
                        throw exception;
                    }

                    public void fatalError(SAXParseException exception) throws SAXException {
                        throw exception;
                    }
                };
                v.setErrorHandler(errorHandler);
                v.validate(ds);
            } catch (SAXException e) {
                throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
            } catch (IOException e) {
                throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
            }
        }
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DOMSource(javax.xml.transform.dom.DOMSource) Header(org.apache.cxf.headers.Header) SAXParseException(org.xml.sax.SAXParseException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 49 with Header

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

the class SoapOutInterceptor method writeSoapEnvelopeStart.

private void writeSoapEnvelopeStart(final SoapMessage message) {
    final SoapVersion soapVersion = message.getVersion();
    try {
        XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class);
        String soapPrefix = xtw.getPrefix(soapVersion.getNamespace());
        if (StringUtils.isEmpty(soapPrefix)) {
            soapPrefix = "soap";
        }
        if (message.hasAdditionalEnvNs()) {
            Map<String, String> nsMap = message.getEnvelopeNs();
            for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                if (soapVersion.getNamespace().equals(entry.getValue())) {
                    soapPrefix = entry.getKey();
                }
            }
            xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
            xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
            xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
            for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                if (!soapVersion.getNamespace().equals(entry.getValue())) {
                    xtw.writeNamespace(entry.getKey(), entry.getValue());
                }
            }
        } else {
            xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
            xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
            String s2 = xtw.getPrefix(soapVersion.getNamespace());
            if (StringUtils.isEmpty(s2) || soapPrefix.equals(s2)) {
                xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
            } else {
                soapPrefix = s2;
            }
        }
        boolean preexistingHeaders = message.hasHeaders();
        if (preexistingHeaders) {
            xtw.writeStartElement(soapPrefix, soapVersion.getHeader().getLocalPart(), soapVersion.getNamespace());
            List<Header> hdrList = message.getHeaders();
            for (Header header : hdrList) {
                XMLStreamWriter writer = xtw;
                if (xtw instanceof W3CDOMStreamWriter) {
                    Element nd = ((W3CDOMStreamWriter) xtw).getCurrentNode();
                    if (header.getObject() instanceof Element && nd.isSameNode(((Element) header.getObject()).getParentNode())) {
                        continue;
                    }
                }
                if (header instanceof SoapHeader) {
                    SoapHeader soapHeader = (SoapHeader) header;
                    writer = new SOAPHeaderWriter(xtw, soapHeader, soapVersion, soapPrefix);
                }
                DataBinding b = header.getDataBinding();
                if (b == null) {
                    HeaderProcessor hp = bus.getExtension(HeaderManager.class).getHeaderProcessor(header.getName().getNamespaceURI());
                    if (hp != null) {
                        b = hp.getDataBinding();
                    }
                }
                if (b != null) {
                    MessagePartInfo part = new MessagePartInfo(header.getName(), null);
                    part.setConcreteName(header.getName());
                    b.createWriter(XMLStreamWriter.class).write(header.getObject(), part, writer);
                } else {
                    Element node = (Element) header.getObject();
                    StaxUtils.copy(node, writer);
                }
            }
        }
        boolean endedHeader = handleHeaderPart(preexistingHeaders, message, soapPrefix);
        if (preexistingHeaders && !endedHeader) {
            xtw.writeEndElement();
        }
        xtw.writeStartElement(soapPrefix, soapVersion.getBody().getLocalPart(), soapVersion.getNamespace());
    // Interceptors followed such as Wrapped/RPC/Doc Interceptor will write SOAP body
    } catch (XMLStreamException e) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) Element(org.w3c.dom.Element) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) HeaderManager(org.apache.cxf.headers.HeaderManager) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Header(org.apache.cxf.headers.Header) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Map(java.util.Map)

Example 50 with Header

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

the class SAAJInInterceptorTest method testHandleHeader.

@Test
public void testHandleHeader() {
    try {
        prepareSoapMessage("../test-soap-header.xml");
    } catch (IOException ioe) {
        fail("Failed in creating soap message");
    }
    staxIntc.handleMessage(soapMessage);
    rhi.handleMessage(soapMessage);
    sbi.handleMessage(soapMessage);
    saajIntc.handleMessage(soapMessage);
    // check the xmlReader should be placed on the first entry of the body
    // element
    XMLStreamReader xmlReader = soapMessage.getContent(XMLStreamReader.class);
    assertEquals("check the first entry of body", "itinerary", xmlReader.getLocalName());
    List<Header> eleHeaders = soapMessage.getHeaders();
    List<Element> headerChilds = new ArrayList<>();
    Iterator<Header> iter = eleHeaders.iterator();
    while (iter.hasNext()) {
        Header hdr = iter.next();
        if (hdr.getObject() instanceof Element) {
            headerChilds.add((Element) hdr.getObject());
        }
    }
    assertEquals(2, headerChilds.size());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Header(org.apache.cxf.headers.Header) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Header (org.apache.cxf.headers.Header)78 QName (javax.xml.namespace.QName)29 Element (org.w3c.dom.Element)27 ArrayList (java.util.ArrayList)25 JAXBException (javax.xml.bind.JAXBException)24 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)18 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)17 Node (org.w3c.dom.Node)16 JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)15 JAXBElement (javax.xml.bind.JAXBElement)14 List (java.util.List)13 OutofBandHeader (org.apache.cxf.outofband.header.OutofBandHeader)13 MessageContext (javax.xml.ws.handler.MessageContext)9 Test (org.junit.Test)9 AddressingProperties (org.apache.cxf.ws.addressing.AddressingProperties)8 SoapVersion (org.apache.cxf.binding.soap.SoapVersion)7 Message (org.apache.cxf.message.Message)7 HashMap (java.util.HashMap)6 Fault (org.apache.cxf.interceptor.Fault)6 Exchange (org.apache.cxf.message.Exchange)6