Search in sources :

Example 6 with PhaseManagerImpl

use of org.apache.cxf.bus.managers.PhaseManagerImpl in project cxf by apache.

the class ColocUtilTest method testGetInInterceptorChain.

@Test
public void testGetInInterceptorChain() throws Exception {
    PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
    SortedSet<Phase> list = phaseMgr.getInPhases();
    ColocUtil.setPhases(list, Phase.SETUP, Phase.POST_LOGICAL);
    Endpoint ep = control.createMock(Endpoint.class);
    Service srv = control.createMock(Service.class);
    Exchange ex = new ExchangeImpl();
    ex.put(Bus.class, bus);
    ex.put(Endpoint.class, ep);
    ex.put(Service.class, srv);
    EasyMock.expect(bus.getExtension(PhaseManager.class)).andReturn(phaseMgr);
    EasyMock.expect(ep.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    EasyMock.expect(ep.getService()).andReturn(srv).atLeastOnce();
    EasyMock.expect(srv.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    EasyMock.expect(bus.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    control.replay();
    InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, list);
    control.verify();
    assertNotNull("Should have chain instance", chain);
    Iterator<Interceptor<? extends Message>> iter = chain.iterator();
    assertEquals("Should not have interceptors in chain", false, iter.hasNext());
    assertNotNull("OutFaultObserver should be set", chain.getFaultObserver());
}
Also used : Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Phase(org.apache.cxf.phase.Phase) Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Interceptor(org.apache.cxf.interceptor.Interceptor) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 7 with PhaseManagerImpl

use of org.apache.cxf.bus.managers.PhaseManagerImpl in project cxf by apache.

the class ColocOutInterceptorTest method testInvokeInboundChain.

@Test
public void testInvokeInboundChain() {
    // Reset Exchange on msg
    msg.setExchange(null);
    Bus bus = setupBus();
    colocOut.setBus(bus);
    PhaseManager pm = new PhaseManagerImpl();
    EasyMock.expect(bus.getExtension(PhaseManager.class)).andReturn(pm).times(2);
    Endpoint ep = control.createMock(Endpoint.class);
    Binding bd = control.createMock(Binding.class);
    Service srv = control.createMock(Service.class);
    ex.setInMessage(msg);
    ex.put(Bus.class, bus);
    ex.put(Endpoint.class, ep);
    ex.put(Service.class, srv);
    EasyMock.expect(ep.getBinding()).andReturn(bd);
    EasyMock.expect(bd.createMessage()).andReturn(new MessageImpl());
    EasyMock.expect(ep.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    EasyMock.expect(ep.getService()).andReturn(srv).atLeastOnce();
    EasyMock.expect(srv.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    EasyMock.expect(bus.getInInterceptors()).andReturn(new ArrayList<Interceptor<? extends Message>>()).atLeastOnce();
    control.replay();
    colocOut.invokeInboundChain(ex, ep);
    Message inMsg = ex.getInMessage();
    assertNotSame(msg, inMsg);
    assertEquals("Requestor role should be set to true.", Boolean.TRUE, inMsg.get(Message.REQUESTOR_ROLE));
    assertEquals("Inbound Message should be set to true.", Boolean.TRUE, inMsg.get(Message.INBOUND_MESSAGE));
    assertNotNull("Inbound Message should have interceptor chain set.", inMsg.getInterceptorChain());
    assertEquals("Client Invoke state should be FINISHED", Boolean.TRUE, ex.get(ClientImpl.FINISHED));
    control.verify();
}
Also used : Binding(org.apache.cxf.binding.Binding) Bus(org.apache.cxf.Bus) PhaseManager(org.apache.cxf.phase.PhaseManager) Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 8 with PhaseManagerImpl

use of org.apache.cxf.bus.managers.PhaseManagerImpl in project cxf by apache.

the class ColocUtilTest method testSetColocInPhases.

@Test
public void testSetColocInPhases() throws Exception {
    PhaseManagerImpl phaseMgr = new PhaseManagerImpl();
    SortedSet<Phase> list = phaseMgr.getInPhases();
    int size1 = list.size();
    ColocUtil.setPhases(list, Phase.USER_LOGICAL, Phase.INVOKE);
    assertNotSame("The list size should not be same", size1, list.size());
    assertEquals("Expecting Phase.USER_LOGICAL", list.first().getName(), Phase.USER_LOGICAL);
    assertEquals("Expecting Phase.POST_INVOKE", list.last().getName(), Phase.INVOKE);
}
Also used : Phase(org.apache.cxf.phase.Phase) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Endpoint(org.apache.cxf.endpoint.Endpoint) Test(org.junit.Test)

Example 9 with PhaseManagerImpl

use of org.apache.cxf.bus.managers.PhaseManagerImpl in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.

@Test
public void testChangeSOAPHeaderOutBound() 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();
                    Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
                    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
                    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();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    message.setContent(OutputStream.class, originalEmptyOs);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                Object[] headerInfo = prepareSOAPHeader();
                StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
    Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    originalEmptyOs.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) 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) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Iterator(java.util.Iterator) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) 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) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Example 10 with PhaseManagerImpl

use of org.apache.cxf.bus.managers.PhaseManagerImpl in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPBodyOutBound.

// SAAJ tree is created from DOMXMLStreamWriter. Any changes to SOAPMessage should be streamed back to
// outputStream
@Test
public void testChangeSOAPBodyOutBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outboundProperty.booleanValue()) {
                try {
                    smc.setMessage(prepareSOAPMessage("resources/greetMeRpcLitRespChanged.xml"));
                } 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();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(originalEmptyOs);
    message.setContent(XMLStreamWriter.class, writer);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    writer.flush();
    // Verify SOAPMessage
    SOAPMessage resultedMessage = message.getContent(SOAPMessage.class);
    assertNotNull(resultedMessage);
    SOAPBody bodyNew = resultedMessage.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"));
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    Iterator<?> outIt = bodyElementNew.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "out"));
    Element outElement = (SOAPElement) outIt.next();
    assertNotNull(outElement);
    Element elem3Element = DOMUtils.findAllElementsByTagNameNS(outElement, "http://apache.org/hello_world_rpclit/types", "elem3").get(0);
    assertEquals("100", elem3Element.getTextContent());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) Exchange(org.apache.cxf.message.Exchange) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) SOAPBody(javax.xml.soap.SOAPBody) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Aggregations

PhaseManagerImpl (org.apache.cxf.bus.managers.PhaseManagerImpl)11 Test (org.junit.Test)9 Endpoint (org.apache.cxf.endpoint.Endpoint)6 Exchange (org.apache.cxf.message.Exchange)6 Phase (org.apache.cxf.phase.Phase)6 ArrayList (java.util.ArrayList)5 Message (org.apache.cxf.message.Message)5 MessageImpl (org.apache.cxf.message.MessageImpl)5 Bus (org.apache.cxf.Bus)4 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)4 PhaseManager (org.apache.cxf.phase.PhaseManager)4 Service (org.apache.cxf.service.Service)4 Binding (org.apache.cxf.binding.Binding)3 Interceptor (org.apache.cxf.interceptor.Interceptor)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 QName (javax.xml.namespace.QName)2 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)2 SOAPElement (javax.xml.soap.SOAPElement)2