Search in sources :

Example 36 with Header

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

the class X509TokenTest method testKeyIdentifier2.

@org.junit.Test
public void testKeyIdentifier2() throws Exception {
    if (test.isStreaming()) {
        return;
    }
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = X509TokenTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = X509TokenTest.class.getResource("DoubleItOperations.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItKeyIdentifierPort2");
    DoubleItPortType2 x509Port = service.getPort(portQName, DoubleItPortType2.class);
    updateAddressPort(x509Port, PORT);
    List<Header> headers = new ArrayList<>();
    Header dummyHeader = new Header(new QName("uri:org.apache.cxf", "dummy"), "dummy-header", new JAXBDataBinding(String.class));
    headers.add(dummyHeader);
    ((BindingProvider) x509Port).getRequestContext().put(Header.HEADER_LIST, headers);
    int response = x509Port.doubleIt(25);
    assertEquals(50, response);
    int response2 = x509Port.doubleIt2(15);
    assertEquals(30, response2);
    ((java.io.Closeable) x509Port).close();
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) DoubleItPortType2(org.example.contract.doubleit.DoubleItPortType2) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Service(javax.xml.ws.Service) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Header(org.apache.cxf.headers.Header) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding)

Example 37 with Header

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

the class SimpleDocLitWrappedImpl method echoWithHeader.

public String echoWithHeader(String what) {
    List<Header> headers = new ArrayList<>();
    Header dummyHeader;
    try {
        dummyHeader = new Header(new QName("uri:org.apache.cxf", "dummy"), "decapitated", new JAXBDataBinding(String.class));
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    headers.add(dummyHeader);
    context.getMessageContext().put(Header.HEADER_LIST, headers);
    return what;
}
Also used : Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding)

Example 38 with Header

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

the class JAXWSMethodInvokerTest method prepareInMessage.

private Message prepareInMessage(Exchange ex, boolean copyHeadersByFault) throws ParserConfigurationException, SAXException, IOException {
    Message inMessage = new MessageImpl();
    inMessage.setExchange(ex);
    inMessage.put(JAXWSMethodInvoker.COPY_SOAP_HEADERS_BY_FAULT, Boolean.valueOf(copyHeadersByFault));
    List<Header> headers = new ArrayList<>();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document headerDoc = builder.parse(new ByteArrayInputStream("<test:testValue xmlns:test=\"test\"/>".getBytes()));
    Header testHeader = new Header(TEST_HEADER_NAME, headerDoc.getDocumentElement());
    headers.add(testHeader);
    inMessage.put(Header.HEADER_LIST, headers);
    ex.setInMessage(inMessage);
    return inMessage;
}
Also used : Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Header(org.apache.cxf.headers.Header) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 39 with Header

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

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderInBound.

@Test
public void testChangeSOAPHeaderInBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (!outboundProperty.booleanValue()) {
                    // change mustUnderstand to false
                    SOAPMessage message = smc.getMessage();
                    SOAPHeader soapHeader = message.getSOAPHeader();
                    Element headerElementNew = (Element) soapHeader.getFirstChild();
                    SoapVersion soapVersion = Soap11.getInstance();
                    Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
                    attr.setValue("false");
                    headerElementNew.setAttributeNodeNS(attr);
                }
            } catch (Exception e) {
                throw new Fault(e);
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    // This is to set direction to inbound
    expect(exchange.getOutMessage()).andReturn(null);
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
    message.setContent(XMLStreamReader.class, reader);
    Object[] headerInfo = prepareSOAPHeader();
    message.setContent(Node.class, headerInfo[0]);
    Node node = ((Element) headerInfo[1]).getFirstChild();
    message.getHeaders().add(new Header(new QName(node.getNamespaceURI(), node.getLocalName()), node));
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    Element headerElementNew = DOMUtils.getFirstElement(soapMessageNew.getSOAPHeader());
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    // Verify XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
    // Verify Header Element
    Iterator<Header> iter = message.getHeaders().iterator();
    Element requiredHeader = null;
    while (iter.hasNext()) {
        Header localHdr = iter.next();
        if (localHdr.getObject() instanceof Element) {
            Element elem = (Element) localHdr.getObject();
            if (elem.getNamespaceURI().equals("http://apache.org/hello_world_rpclit/types") && elem.getLocalName().equals("header1")) {
                requiredHeader = (Element) localHdr.getObject();
                break;
            }
        }
    }
    assertNotNull("Should have found header1", requiredHeader);
    assertEquals("false", requiredHeader.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) Attr(org.w3c.dom.Attr) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) Binding(javax.xml.ws.Binding) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPHeader(javax.xml.soap.SOAPHeader) Header(org.apache.cxf.headers.Header) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 40 with Header

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

the class RMSoapOutInterceptorTest method verifyHeaders.

private void verifyHeaders(SoapMessage message, String... names) {
    List<Header> headers = new ArrayList<Header>(message.getHeaders());
    for (String name : names) {
        boolean found = false;
        Iterator<Header> iter = headers.iterator();
        while (iter.hasNext()) {
            Header header = iter.next();
            Object obj = header.getObject();
            String namespace = header.getName().getNamespaceURI();
            String localName = header.getName().getLocalPart();
            if (obj instanceof Element) {
                Element elem = (Element) obj;
                namespace = elem.getNamespaceURI();
                localName = elem.getLocalName();
            }
            if (RM10Constants.NAMESPACE_URI.equals(namespace) && localName.equals(name)) {
                found = true;
                iter.remove();
                break;
            } else if (Names.WSA_NAMESPACE_NAME.equals(namespace) && localName.equals(name)) {
                found = true;
                iter.remove();
                break;
            }
        }
        assertTrue("Could not find header element " + name, found);
    }
    // no other headers should be present
    if (!headers.isEmpty()) {
        assertTrue("Unexpected header element " + headers.get(0).getName(), false);
    }
}
Also used : Header(org.apache.cxf.headers.Header) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

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