Search in sources :

Example 56 with Header

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

the class OOBHeaderTest method checkReturnedOOBHeader.

private void checkReturnedOOBHeader(PutLastTradedPricePortType portType) {
    InvocationHandler handler = Proxy.getInvocationHandler(portType);
    BindingProvider bp = null;
    if (handler instanceof BindingProvider) {
        bp = (BindingProvider) handler;
        Map<String, Object> responseContext = bp.getResponseContext();
        OutofBandHeader hdrToTest = null;
        List<?> oobHdr = (List<?>) responseContext.get(Header.HEADER_LIST);
        if (oobHdr == null) {
            fail("Should have got List of out-of-band headers ..");
        }
        assertTrue("HeaderHolder list expected to conain 1 object received " + oobHdr.size(), oobHdr.size() == 1);
        if (oobHdr != null) {
            Iterator<?> iter = oobHdr.iterator();
            while (iter.hasNext()) {
                Object hdr = iter.next();
                if (hdr instanceof Header) {
                    Header hdr1 = (Header) hdr;
                    if (hdr1.getObject() instanceof Node) {
                        // System.out.println("Node conains : " + hdr1.getObject().toString());
                        try {
                            JAXBElement<?> job = (JAXBElement<?>) JAXBContext.newInstance(ObjectFactory.class).createUnmarshaller().unmarshal((Node) hdr1.getObject());
                            hdrToTest = (OutofBandHeader) job.getValue();
                        // System.out.println("oob-hdr contains : \nname = "
                        // + hdrToTest.getName()
                        // + "  \nvalue = " + hdrToTest.getValue()
                        // + " \natribute = " + hdrToTest.getHdrAttribute());
                        } catch (JAXBException ex) {
                            // 
                            ex.printStackTrace();
                        }
                    }
                }
            }
        }
        assertNotNull("out-of-band header should not be null", hdrToTest);
        assertTrue("Expected out-of-band Header name testOobReturnHeaderName recevied :" + hdrToTest.getName(), "testOobReturnHeaderName".equals(hdrToTest.getName()));
        assertTrue("Expected out-of-band Header value testOobReturnHeaderValue recevied :" + hdrToTest.getValue(), "testOobReturnHeaderValue".equals(hdrToTest.getValue()));
        assertTrue("Expected out-of-band Header attribute testReturnHdrAttribute recevied :" + hdrToTest.getHdrAttribute(), "testReturnHdrAttribute".equals(hdrToTest.getHdrAttribute()));
    }
}
Also used : Node(org.w3c.dom.Node) JAXBException(javax.xml.bind.JAXBException) BindingProvider(javax.xml.ws.BindingProvider) JAXBElement(javax.xml.bind.JAXBElement) InvocationHandler(java.lang.reflect.InvocationHandler) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) Header(org.apache.cxf.headers.Header) ObjectFactory(org.apache.cxf.outofband.header.ObjectFactory) OutofBandHeader(org.apache.cxf.outofband.header.OutofBandHeader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 57 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 58 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 59 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 60 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)

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