Search in sources :

Example 11 with Header

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

the class SoapHeaderInterceptor method handleMessage.

public void handleMessage(Message m) throws Fault {
    SoapMessage message = (SoapMessage) m;
    SoapVersion soapVersion = message.getVersion();
    Exchange exchange = message.getExchange();
    MessageContentsList parameters = MessageContentsList.getContentsList(message);
    if (null == parameters) {
        parameters = new MessageContentsList();
    }
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    if (null == bop) {
        return;
    }
    if (bop.isUnwrapped()) {
        bop = bop.getWrappedOperation();
    }
    boolean client = isRequestor(message);
    BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
    if (bmi == null) {
        // one way operation.
        return;
    }
    List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
    if (headers == null || headers.isEmpty()) {
        return;
    }
    boolean supportsNode = this.supportsDataReader(message, Node.class);
    Service service = ServiceModelUtil.getService(message.getExchange());
    for (SoapHeaderInfo header : headers) {
        MessagePartInfo mpi = header.getPart();
        try {
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
                validateHeader(message, mpi, schema);
            }
        } catch (Fault f) {
            if (!isRequestor(message)) {
                f.setFaultCode(Fault.FAULT_CODE_CLIENT);
            }
            throw f;
        }
        if (mpi.getTypeClass() != null) {
            Header param = findHeader(message, mpi);
            Object object = null;
            if (param != null) {
                message.getHeaders().remove(param);
                if (param.getDataBinding() == null) {
                    Node source = (Node) param.getObject();
                    if (source instanceof Element) {
                        // need to remove these attributes as they
                        // would cause validation failures
                        Element el = (Element) source;
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
                    }
                    if (supportsNode) {
                        object = getNodeDataReader(message).read(mpi, source);
                    } else {
                        W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
                        try {
                            // advance into the first tag
                            reader.nextTag();
                        } catch (XMLStreamException e) {
                        // ignore
                        }
                        object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
                    }
                } else {
                    object = param.getObject();
                }
            }
            parameters.put(mpi, object);
        }
    }
    if (!parameters.isEmpty()) {
        message.setContent(List.class, parameters);
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapHeaderInfo(org.apache.cxf.binding.soap.model.SoapHeaderInfo) Schema(javax.xml.validation.Schema) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Header(org.apache.cxf.headers.Header) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 12 with Header

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

the class SAAJInInterceptor method replaceHeaders.

public static void replaceHeaders(SOAPMessage soapMessage, SoapMessage message) throws SOAPException {
    SOAPHeader header = SAAJUtils.getHeader(soapMessage);
    if (header == null) {
        return;
    }
    Element elem = DOMUtils.getFirstElement(header);
    elem = (Element) DOMUtils.getDomElement(elem);
    while (elem != null) {
        Bus b = message.getExchange() == null ? null : message.getExchange().getBus();
        HeaderProcessor p = null;
        if (b != null && b.getExtension(HeaderManager.class) != null) {
            p = b.getExtension(HeaderManager.class).getHeaderProcessor(elem.getNamespaceURI());
        }
        Object obj;
        DataBinding dataBinding = null;
        if (p == null || p.getDataBinding() == null) {
            obj = elem;
        } else {
            dataBinding = p.getDataBinding();
            obj = p.getDataBinding().createReader(Node.class).read(elem);
        }
        SoapHeader shead = new SoapHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()), obj, dataBinding);
        shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
        String mu = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameMustUnderstand());
        String act = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameRole());
        shead.setActor(act);
        shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
        Header oldHdr = message.getHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()));
        if (oldHdr != null) {
            message.getHeaders().remove(oldHdr);
        }
        message.getHeaders().add(shead);
        elem = DOMUtils.getNextElement(elem);
    }
}
Also used : Bus(org.apache.cxf.Bus) SOAPHeader(javax.xml.soap.SOAPHeader) Header(org.apache.cxf.headers.Header) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) QName(javax.xml.namespace.QName) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 13 with Header

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

the class ReadHeaderInterceptorTest method testHandleHeader.

@Test
public void testHandleHeader() {
    try {
        prepareSoapMessage("test-soap-header.xml");
    } catch (IOException ioe) {
        fail("Failed in creating soap message");
    }
    staxIntc.handleMessage(soapMessage);
    soapMessage.getInterceptorChain().doIntercept(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());
    for (int i = 0; i < headerChilds.size(); i++) {
        Element ele = headerChilds.get(i);
        if (ele.getLocalName().equals("reservation")) {
            Element reservation = ele;
            List<Element> reservationChilds = new ArrayList<>();
            Element elem = DOMUtils.getFirstElement(reservation);
            while (elem != null) {
                reservationChilds.add(elem);
                elem = DOMUtils.getNextElement(elem);
            }
            assertEquals(2, reservationChilds.size());
            assertEquals("reference", reservationChilds.get(0).getLocalName());
            assertEquals("uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d", reservationChilds.get(0).getTextContent());
            assertEquals("dateAndTime", reservationChilds.get(1).getLocalName());
            assertEquals("2001-11-29T13:20:00.000-05:00", reservationChilds.get(1).getTextContent());
        }
        if (ele.getLocalName().equals("passenger")) {
            Element passenger = ele;
            assertNotNull(passenger);
            Element child = DOMUtils.getFirstElement(passenger);
            assertNotNull("passenger should have a child element", child);
            assertEquals("name", child.getLocalName());
            assertEquals("Bob", child.getTextContent());
        }
    }
}
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)

Example 14 with Header

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

the class JAXWSMethodInvokerTest method testFaultHeadersCopy.

@Test
public void testFaultHeadersCopy() throws Throwable {
    ExceptionService serviceObject = new ExceptionService();
    Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
    Exchange ex = new ExchangeImpl();
    prepareInMessage(ex, true);
    Message msg = new MessageImpl();
    SoapMessage outMessage = new SoapMessage(msg);
    ex.setOutMessage(outMessage);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
        fail("Expected fault");
    } catch (Fault fault) {
        Message outMsg = ex.getOutMessage();
        Assert.assertNotNull(outMsg);
        @SuppressWarnings("unchecked") List<Header> headers = (List<Header>) outMsg.get(Header.HEADER_LIST);
        Assert.assertEquals(1, headers.size());
        Assert.assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
    }
}
Also used : Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) MessageContentsList(org.apache.cxf.message.MessageContentsList) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Exchange(org.apache.cxf.message.Exchange) Header(org.apache.cxf.headers.Header) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 15 with Header

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

the class MAPCodec method encodeReferenceParameters.

private void encodeReferenceParameters(AddressingProperties maps, SoapMessage msg, JAXBContext ctx) throws JAXBException {
    Element header = null;
    EndpointReferenceType toEpr = maps.getToEndpointReference();
    if (null != toEpr) {
        ReferenceParametersType params = toEpr.getReferenceParameters();
        if (null != params) {
            for (Object o : params.getAny()) {
                if (o instanceof Element || o instanceof JAXBElement) {
                    if (header == null) {
                        header = getHeaderFactory().getHeader(msg.getVersion());
                    }
                    JAXBElement<?> jaxbEl = null;
                    if (o instanceof Element) {
                        Element e = (Element) o;
                        Node importedNode = header.getOwnerDocument().importNode(e, true);
                        header.appendChild(importedNode);
                    } else {
                        jaxbEl = (JAXBElement<?>) o;
                        ctx.createMarshaller().marshal(jaxbEl, header);
                    }
                    Element lastAdded = (Element) header.getLastChild();
                    header.removeChild(lastAdded);
                    addIsReferenceParameterMarkerAttribute(lastAdded, maps.getNamespaceURI());
                    Header holder = new Header(new QName(lastAdded.getNamespaceURI(), lastAdded.getLocalName()), lastAdded);
                    msg.getHeaders().add(holder);
                } else {
                    LOG.log(Level.WARNING, "IGNORE_NON_ELEMENT_REF_PARAM_MSG", o);
                }
            }
        }
    }
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ReferenceParametersType(org.apache.cxf.ws.addressing.ReferenceParametersType) JAXBElement(javax.xml.bind.JAXBElement)

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