Search in sources :

Example 31 with Conduit

use of org.apache.cxf.transport.Conduit in project cxf by apache.

the class RetransmissionQueueImpl method doResend.

private void doResend(SoapMessage message) {
    InputStream is = null;
    try {
        // initialize copied interceptor chain for message
        PhaseInterceptorChain retransmitChain = manager.getRetransmitChain(message);
        ProtocolVariation protocol = RMContextUtils.getProtocolVariation(message);
        Endpoint endpoint = manager.getReliableEndpoint(message).getEndpoint(protocol);
        PhaseChainCache cache = new PhaseChainCache();
        boolean after = true;
        if (retransmitChain == null) {
            // no saved retransmit chain, so construct one from scratch (won't work for WS-Security on server, so
            // need to fix)
            retransmitChain = buildRetransmitChain(endpoint, cache);
            after = false;
        }
        message.setInterceptorChain(retransmitChain);
        // clear flag for SOAP out interceptor so envelope will be written
        message.remove(SoapOutInterceptor.WROTE_ENVELOPE_START);
        // discard all saved content
        Set<Class<?>> formats = message.getContentFormats();
        List<CachedOutputStreamCallback> callbacks = null;
        for (Class<?> clas : formats) {
            Object content = message.getContent(clas);
            if (content != null) {
                LOG.info("Removing " + clas.getName() + " content of actual type " + content.getClass().getName());
                message.removeContent(clas);
                if (clas == OutputStream.class && content instanceof WriteOnCloseOutputStream) {
                    callbacks = ((WriteOnCloseOutputStream) content).getCallbacks();
                }
            }
        }
        // read SOAP headers from saved input stream
        CachedOutputStream cos = (CachedOutputStream) message.get(RMMessageConstants.SAVED_CONTENT);
        // CachedOutputStream is hold until delivering was successful
        cos.holdTempFile();
        // instance is needed to close input stream later on
        is = cos.getInputStream();
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is, StandardCharsets.UTF_8.name());
        message.getHeaders().clear();
        if (reader.getEventType() != XMLStreamConstants.START_ELEMENT && reader.nextTag() != XMLStreamConstants.START_ELEMENT) {
            throw new IllegalStateException("No document found");
        }
        readHeaders(reader, message);
        int event;
        while ((event = reader.nextTag()) != XMLStreamConstants.START_ELEMENT) {
            if (event == XMLStreamConstants.END_ELEMENT) {
                throw new IllegalStateException("No body content present");
            }
        }
        // set message addressing properties
        AddressingProperties maps = MAPCodec.getInstance(message.getExchange().getBus()).unmarshalMAPs(message);
        RMContextUtils.storeMAPs(maps, message, true, MessageUtils.isRequestor(message));
        AttributedURIType to = null;
        if (null != maps) {
            to = maps.getTo();
        }
        if (null == to) {
            LOG.log(Level.SEVERE, "NO_ADDRESS_FOR_RESEND_MSG");
            return;
        }
        if (RMUtils.getAddressingConstants().getAnonymousURI().equals(to.getValue())) {
            LOG.log(Level.FINE, "Cannot resend to anonymous target");
            return;
        }
        // initialize conduit for new message
        Conduit c = message.getExchange().getConduit(message);
        if (c == null) {
            c = buildConduit(message, endpoint, to);
        }
        c.prepare(message);
        // replace standard message marshaling with copy from saved stream
        ListIterator<Interceptor<? extends Message>> iterator = retransmitChain.getIterator();
        while (iterator.hasNext()) {
            Interceptor<? extends Message> incept = iterator.next();
            // remove JAX-WS interceptors which handle message modes and such
            if (incept.getClass().getName().startsWith("org.apache.cxf.jaxws.interceptors")) {
                retransmitChain.remove(incept);
            } else if (incept instanceof PhaseInterceptor && (((PhaseInterceptor<?>) incept).getPhase() == Phase.MARSHAL)) {
                // remove any interceptors from the marshal phase
                retransmitChain.remove(incept);
            }
        }
        retransmitChain.add(new CopyOutInterceptor(reader));
        // restore callbacks on output stream
        if (callbacks != null) {
            OutputStream os = message.getContent(OutputStream.class);
            if (os != null) {
                WriteOnCloseOutputStream woc;
                if (os instanceof WriteOnCloseOutputStream) {
                    woc = (WriteOnCloseOutputStream) os;
                } else {
                    woc = new WriteOnCloseOutputStream(os);
                    message.setContent(OutputStream.class, woc);
                }
                for (CachedOutputStreamCallback cb : callbacks) {
                    woc.registerCallback(cb);
                }
            }
        }
        // send the message
        message.put(RMMessageConstants.RM_RETRANSMISSION, Boolean.TRUE);
        if (after) {
            retransmitChain.doInterceptStartingAfter(message, RMCaptureOutInterceptor.class.getName());
        } else {
            retransmitChain.doIntercept(message);
        }
        if (LOG.isLoggable(Level.INFO)) {
            RMProperties rmps = RMContextUtils.retrieveRMProperties(message, true);
            SequenceType seq = rmps.getSequence();
            LOG.log(Level.INFO, "Retransmitted message " + seq.getMessageNumber() + " in sequence " + seq.getIdentifier().getValue());
            rmps = new RMProperties();
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "RESEND_FAILED_MSG", ex);
    } finally {
        // make sure to always close InputStreams of the CachedOutputStream to avoid leaving temp files undeleted
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) WriteOnCloseOutputStream(org.apache.cxf.io.WriteOnCloseOutputStream) CachedOutputStreamCallback(org.apache.cxf.io.CachedOutputStreamCallback) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) WriteOnCloseOutputStream(org.apache.cxf.io.WriteOnCloseOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) SequenceType(org.apache.cxf.ws.rm.v200702.SequenceType) RMCaptureOutInterceptor(org.apache.cxf.ws.rm.RMCaptureOutInterceptor) PhaseChainCache(org.apache.cxf.phase.PhaseChainCache) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) RMCaptureOutInterceptor(org.apache.cxf.ws.rm.RMCaptureOutInterceptor) AbstractOutDatabindingInterceptor(org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) SoapOutInterceptor(org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor) RMProperties(org.apache.cxf.ws.rm.RMProperties) ProtocolVariation(org.apache.cxf.ws.rm.ProtocolVariation) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) InputStream(java.io.InputStream) IOException(java.io.IOException) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) XMLStreamException(javax.xml.stream.XMLStreamException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) RMException(org.apache.cxf.ws.rm.RMException) Conduit(org.apache.cxf.transport.Conduit)

Example 32 with Conduit

use of org.apache.cxf.transport.Conduit in project cxf by apache.

the class ProxyTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    Method m = Proxy.class.getDeclaredMethod("createClient", new Class[] { Bus.class, Endpoint.class, ProtocolVariation.class, Conduit.class, org.apache.cxf.ws.addressing.EndpointReferenceType.class });
    Proxy proxy = EasyMock.createMockBuilder(Proxy.class).addMockedMethod(m).createMock(control);
    proxy.setReliableEndpoint(rme);
    RMManager manager = control.createMock(RMManager.class);
    EasyMock.expect(rme.getManager()).andReturn(manager).anyTimes();
    Bus bus = control.createMock(Bus.class);
    EasyMock.expect(manager.getBus()).andReturn(bus).anyTimes();
    Endpoint endpoint = control.createMock(Endpoint.class);
    EasyMock.expect(rme.getEndpoint(ProtocolVariation.RM10WSA200408)).andReturn(endpoint).anyTimes();
    BindingInfo bi = control.createMock(BindingInfo.class);
    EasyMock.expect(rme.getBindingInfo(ProtocolVariation.RM10WSA200408)).andReturn(bi).anyTimes();
    Conduit conduit = control.createMock(Conduit.class);
    EasyMock.expect(rme.getConduit()).andReturn(conduit).anyTimes();
    org.apache.cxf.ws.addressing.EndpointReferenceType replyTo = control.createMock(org.apache.cxf.ws.addressing.EndpointReferenceType.class);
    EasyMock.expect(rme.getReplyTo()).andReturn(replyTo).anyTimes();
    OperationInfo oi = control.createMock(OperationInfo.class);
    BindingOperationInfo boi = control.createMock(BindingOperationInfo.class);
    EasyMock.expect(bi.getOperation(oi)).andReturn(boi).anyTimes();
    Client client = control.createMock(Client.class);
    EasyMock.expect(client.getRequestContext()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(proxy.createClient(bus, endpoint, ProtocolVariation.RM10WSA200408, conduit, replyTo)).andReturn(client).anyTimes();
    Object[] args = new Object[] {};
    Map<String, Object> context = new HashMap<>();
    Object[] results = new Object[] { "a", "b", "c" };
    Exchange exchange = control.createMock(Exchange.class);
    EasyMock.expect(client.invoke(boi, args, context, exchange)).andReturn(results).anyTimes();
    control.replay();
    assertEquals("a", proxy.invoke(oi, ProtocolVariation.RM10WSA200408, args, context, exchange));
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) Bus(org.apache.cxf.Bus) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Exchange(org.apache.cxf.message.Exchange) Endpoint(org.apache.cxf.endpoint.Endpoint) Conduit(org.apache.cxf.transport.Conduit) BindingInfo(org.apache.cxf.service.model.BindingInfo) Client(org.apache.cxf.endpoint.Client) Test(org.junit.Test)

Example 33 with Conduit

use of org.apache.cxf.transport.Conduit in project cxf by apache.

the class ProxyTest method testRMClientGetConduit.

@Test
public void testRMClientGetConduit() throws Exception {
    Proxy proxy = new Proxy(rme);
    Bus bus = control.createMock(Bus.class);
    Endpoint endpoint = control.createMock(Endpoint.class);
    Conduit conduit = control.createMock(Conduit.class);
    ConduitSelector cs = control.createMock(ConduitSelector.class);
    EasyMock.expect(cs.selectConduit(EasyMock.isA(Message.class))).andReturn(conduit).anyTimes();
    control.replay();
    Proxy.RMClient client = proxy.new RMClient(bus, endpoint, cs);
    assertSame(conduit, client.getConduit());
    client.close();
}
Also used : Bus(org.apache.cxf.Bus) Endpoint(org.apache.cxf.endpoint.Endpoint) Conduit(org.apache.cxf.transport.Conduit) ConduitSelector(org.apache.cxf.endpoint.ConduitSelector) Test(org.junit.Test)

Example 34 with Conduit

use of org.apache.cxf.transport.Conduit in project cxf by apache.

the class RMEndpointTest method testInitialise.

@Test
public void testInitialise() throws NoSuchMethodException {
    Message m = new MessageImpl();
    Method m1 = RMEndpoint.class.getDeclaredMethod("createServices", new Class[] {});
    Method m2 = RMEndpoint.class.getDeclaredMethod("createEndpoints", new Class[] { org.apache.cxf.transport.Destination.class });
    Method m3 = RMEndpoint.class.getDeclaredMethod("setPolicies", new Class[] { Message.class });
    rme = EasyMock.createMockBuilder(RMEndpoint.class).addMockedMethods(m1, m2, m3).createMock(control);
    rme.createServices();
    EasyMock.expectLastCall();
    rme.createEndpoints(null);
    EasyMock.expectLastCall();
    rme.setPolicies(m);
    EasyMock.expectLastCall();
    Conduit c = control.createMock(Conduit.class);
    EndpointReferenceType epr = control.createMock(EndpointReferenceType.class);
    control.replay();
    rme.initialise(new RMConfiguration(), c, epr, null, m);
    assertSame(c, rme.getConduit());
    assertSame(epr, rme.getReplyTo());
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) Message(org.apache.cxf.message.Message) Conduit(org.apache.cxf.transport.Conduit) Method(java.lang.reflect.Method) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 35 with Conduit

use of org.apache.cxf.transport.Conduit in project cxf by apache.

the class RMManagerTest method testGetNewSequence.

@Test
public void testGetNewSequence() throws NoSuchMethodException, SequenceFault, RMException {
    Method m = RMManager.class.getDeclaredMethod("getSource", new Class[] { Message.class });
    manager = control.createMock(RMManager.class, new Method[] { m });
    Message message = control.createMock(Message.class);
    Exchange exchange = control.createMock(Exchange.class);
    EasyMock.expect(message.getContextualPropertyKeys()).andReturn(new HashSet<>()).anyTimes();
    EasyMock.expect(message.getExchange()).andReturn(exchange).anyTimes();
    EasyMock.expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    EasyMock.expect(exchange.getInMessage()).andReturn(null).anyTimes();
    EasyMock.expect(exchange.getOutFaultMessage()).andReturn(null).anyTimes();
    Conduit conduit = control.createMock(Conduit.class);
    EasyMock.expect(exchange.getConduit(message)).andReturn(conduit).anyTimes();
    Identifier inSid = control.createMock(Identifier.class);
    AddressingProperties maps = control.createMock(AddressingProperties.class);
    Source source = control.createMock(Source.class);
    EasyMock.expect(manager.getSource(message)).andReturn(source);
    EasyMock.expect(source.getCurrent(inSid)).andReturn(null);
    AttributedURIType uri = control.createMock(AttributedURIType.class);
    EasyMock.expect(maps.getTo()).andReturn(uri);
    EasyMock.expect(uri.getValue()).andReturn("http://localhost:9001/TestPort");
    EndpointReferenceType epr = RMUtils.createNoneReference();
    EasyMock.expect(maps.getReplyTo()).andReturn(epr);
    RMEndpoint rme = control.createMock(RMEndpoint.class);
    EasyMock.expect(source.getReliableEndpoint()).andReturn(rme).times(2);
    Proxy proxy = control.createMock(Proxy.class);
    EasyMock.expect(rme.getProxy()).andReturn(proxy);
    CreateSequenceResponseType createResponse = control.createMock(CreateSequenceResponseType.class);
    proxy.createSequence(EasyMock.isA(EndpointReferenceType.class), (RelatesToType) EasyMock.isNull(), EasyMock.eq(false), EasyMock.isA(ProtocolVariation.class), EasyMock.isA(Exchange.class), CastUtils.cast(EasyMock.isA(HashMap.class), String.class, Object.class));
    EasyMock.expectLastCall().andReturn(createResponse);
    Servant servant = control.createMock(Servant.class);
    EasyMock.expect(rme.getServant()).andReturn(servant);
    servant.createSequenceResponse(createResponse, ProtocolVariation.RM10WSA200408);
    EasyMock.expectLastCall();
    SourceSequence sseq = control.createMock(SourceSequence.class);
    EasyMock.expect(source.awaitCurrent(inSid)).andReturn(sseq);
    sseq.setTarget(EasyMock.isA(EndpointReferenceType.class));
    EasyMock.expectLastCall();
    control.replay();
    assertSame(sseq, manager.getSequence(inSid, message, maps));
    control.verify();
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) Message(org.apache.cxf.message.Message) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) Method(java.lang.reflect.Method) CreateSequenceResponseType(org.apache.cxf.ws.rm.v200702.CreateSequenceResponseType) Exchange(org.apache.cxf.message.Exchange) Identifier(org.apache.cxf.ws.rm.v200702.Identifier) Conduit(org.apache.cxf.transport.Conduit) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Conduit (org.apache.cxf.transport.Conduit)83 Test (org.junit.Test)36 Message (org.apache.cxf.message.Message)35 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)30 Exchange (org.apache.cxf.message.Exchange)28 IOException (java.io.IOException)18 MessageImpl (org.apache.cxf.message.MessageImpl)17 Endpoint (org.apache.cxf.endpoint.Endpoint)16 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)13 MessageObserver (org.apache.cxf.transport.MessageObserver)12 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)12 OutputStream (java.io.OutputStream)11 Bus (org.apache.cxf.Bus)11 Destination (org.apache.cxf.transport.Destination)11 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)10 InputStream (java.io.InputStream)9 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)9 ConduitInitiatorManager (org.apache.cxf.transport.ConduitInitiatorManager)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6