Search in sources :

Example 36 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class RMManagerTest method testRecoverReliableClientEndpointWithAttachment.

@Test
public void testRecoverReliableClientEndpointWithAttachment() throws NoSuchMethodException, IOException {
    Method method = RMManager.class.getDeclaredMethod("createReliableEndpoint", new Class[] { Endpoint.class });
    manager = control.createMock(RMManager.class, new Method[] { method });
    manager.setReliableEndpointsMap(new HashMap<Endpoint, RMEndpoint>());
    Endpoint endpoint = control.createMock(Endpoint.class);
    EndpointInfo ei = control.createMock(EndpointInfo.class);
    ServiceInfo si = control.createMock(ServiceInfo.class);
    BindingInfo bi = control.createMock(BindingInfo.class);
    InterfaceInfo ii = control.createMock(InterfaceInfo.class);
    setUpEndpointForRecovery(endpoint, ei, si, bi, ii);
    Conduit conduit = control.createMock(Conduit.class);
    SourceSequence ss = control.createMock(SourceSequence.class);
    DestinationSequence ds = control.createMock(DestinationSequence.class);
    RMMessage m1 = new RMMessage();
    InputStream fis = getClass().getResourceAsStream("persistence/SerializedRMMessage.txt");
    CachedOutputStream cos = new CachedOutputStream();
    IOUtils.copyAndCloseInput(fis, cos);
    cos.flush();
    m1.setContent(cos);
    m1.setTo("toAddress");
    m1.setMessageNumber(Long.valueOf(10));
    m1.setContentType(MULTIPART_TYPE);
    Capture<Message> mc = Capture.newInstance();
    setUpRecoverReliableEndpointWithAttachment(endpoint, conduit, ss, ds, m1, mc);
    control.replay();
    manager.recoverReliableEndpoint(endpoint, conduit);
    control.verify();
    Message msg = mc.getValue();
    assertNotNull(msg);
    assertNotNull(msg.getExchange());
    assertSame(msg, msg.getExchange().getOutMessage());
    CachedOutputStream cos1 = (CachedOutputStream) msg.get(RMMessageConstants.SAVED_CONTENT);
    assertStartsWith(cos1.getInputStream(), "<soap:Envelope");
    assertEquals(1, msg.getAttachments().size());
}
Also used : RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) Message(org.apache.cxf.message.Message) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Conduit(org.apache.cxf.transport.Conduit) BindingInfo(org.apache.cxf.service.model.BindingInfo) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) Test(org.junit.Test)

Example 37 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class RMManagerTest method setUpRecoverReliableEndpoint.

void setUpRecoverReliableEndpoint(Endpoint endpoint, Conduit conduit, SourceSequence ss, DestinationSequence ds, RMMessage m) throws IOException {
    RMStore store = control.createMock(RMStore.class);
    RetransmissionQueue oqueue = control.createMock(RetransmissionQueue.class);
    RedeliveryQueue iqueue = control.createMock(RedeliveryQueue.class);
    manager.setStore(store);
    manager.setRetransmissionQueue(oqueue);
    manager.setRedeliveryQueue(iqueue);
    Collection<SourceSequence> sss = new ArrayList<>();
    if (null != ss) {
        sss.add(ss);
    }
    EasyMock.expect(store.getSourceSequences("{S}s.{P}p@cxf")).andReturn(sss);
    if (null == ss) {
        return;
    }
    Collection<DestinationSequence> dss = new ArrayList<>();
    if (null != ds) {
        dss.add(ds);
    }
    EasyMock.expect(store.getDestinationSequences("{S}s.{P}p@cxf")).andReturn(dss);
    if (null == ds) {
        return;
    }
    Collection<RMMessage> ms = new ArrayList<>();
    if (null != m) {
        ms.add(m);
    }
    Identifier id = new Identifier();
    id.setValue("S1");
    EasyMock.expect(ss.getIdentifier()).andReturn(id).times(null == m ? 1 : 2);
    EasyMock.expect(ss.getProtocol()).andReturn(ProtocolVariation.RM10WSA200408).anyTimes();
    EasyMock.expect(store.getMessages(id, true)).andReturn(ms);
    RMEndpoint rme = control.createMock(RMEndpoint.class);
    EasyMock.expect(manager.createReliableEndpoint(endpoint)).andReturn(rme);
    Source source = control.createMock(Source.class);
    EasyMock.expect(rme.getSource()).andReturn(source).anyTimes();
    Destination destination = control.createMock(Destination.class);
    EasyMock.expect(rme.getDestination()).andReturn(destination);
    destination.addSequence(ds, false);
    EasyMock.expectLastCall();
    Service service = control.createMock(Service.class);
    EasyMock.expect(endpoint.getService()).andReturn(service).anyTimes();
    Binding binding = control.createMock(Binding.class);
    EasyMock.expect(endpoint.getBinding()).andReturn(binding).anyTimes();
    EasyMock.expect(ss.isLastMessage()).andReturn(true).anyTimes();
    EasyMock.expect(ss.getCurrentMessageNr()).andReturn(Long.valueOf(10)).anyTimes();
    if (null == m) {
        return;
    }
    EasyMock.expect(m.getMessageNumber()).andReturn(Long.valueOf(10)).times(2);
    if (null == conduit) {
        EasyMock.expect(m.getTo()).andReturn("toAddress");
    }
    InputStream is = new ByteArrayInputStream(new byte[0]);
    CachedOutputStream cos = new CachedOutputStream();
    IOUtils.copy(is, cos);
    cos.flush();
    is.close();
    EasyMock.expect(m.getContent()).andReturn(cos).anyTimes();
    oqueue.addUnacknowledged(EasyMock.isA(Message.class));
    EasyMock.expectLastCall();
    oqueue.start();
    EasyMock.expectLastCall();
    iqueue.start();
    EasyMock.expectLastCall();
}
Also used : Binding(org.apache.cxf.binding.Binding) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) Message(org.apache.cxf.message.Message) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) RMStore(org.apache.cxf.ws.rm.persistence.RMStore) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Identifier(org.apache.cxf.ws.rm.v200702.Identifier) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 38 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class PersistenceUtilsTest method testDecodeRMContentWithAttachment.

@Test
public void testDecodeRMContentWithAttachment() throws Exception {
    InputStream is = getClass().getResourceAsStream("SerializedRMMessage.txt");
    CachedOutputStream cos = new CachedOutputStream();
    IOUtils.copyAndCloseInput(is, cos);
    cos.flush();
    RMMessage msg = new RMMessage();
    msg.setContent(cos);
    msg.setContentType(MULTIPART_TYPE);
    Message messageImpl = new MessageImpl();
    PersistenceUtils.decodeRMContent(msg, messageImpl);
    assertEquals(1, messageImpl.getAttachments().size());
    CachedOutputStream cos1 = (CachedOutputStream) messageImpl.get(RMMessageConstants.SAVED_CONTENT);
    assertStartsWith(cos1.getInputStream(), "<soap:Envelope");
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MessageImpl(org.apache.cxf.message.MessageImpl) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 39 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class JwsWriterInterceptor method aroundWriteTo.

@Override
public void aroundWriteTo(WriterInterceptorContext ctx) throws IOException, WebApplicationException {
    if (ctx.getEntity() == null) {
        ctx.proceed();
        return;
    }
    JwsHeaders headers = new JwsHeaders();
    JwsSignatureProvider sigProvider = getInitializedSigProvider(headers);
    setContentTypeIfNeeded(headers, ctx);
    if (!encodePayload) {
        headers.setPayloadEncodingStatus(false);
    }
    protectHttpHeadersIfNeeded(ctx, headers);
    OutputStream actualOs = ctx.getOutputStream();
    if (useJwsOutputStream) {
        JwsSignature jwsSignature = sigProvider.createJwsSignature(headers);
        JoseUtils.traceHeaders(headers);
        JwsOutputStream jwsStream = new JwsOutputStream(actualOs, jwsSignature, true);
        byte[] headerBytes = StringUtils.toBytesUTF8(writer.toJson(headers));
        Base64UrlUtility.encodeAndStream(headerBytes, 0, headerBytes.length, jwsStream);
        jwsStream.write(new byte[] { '.' });
        Base64UrlOutputStream base64Stream = null;
        if (encodePayload) {
            base64Stream = new Base64UrlOutputStream(jwsStream);
            ctx.setOutputStream(base64Stream);
        } else {
            ctx.setOutputStream(jwsStream);
        }
        ctx.proceed();
        setJoseMediaType(ctx);
        if (base64Stream != null) {
            base64Stream.flush();
        }
        jwsStream.flush();
    } else {
        CachedOutputStream cos = new CachedOutputStream();
        ctx.setOutputStream(cos);
        ctx.proceed();
        JwsCompactProducer p = new JwsCompactProducer(headers, new String(cos.getBytes(), StandardCharsets.UTF_8));
        setJoseMediaType(ctx);
        writeJws(p, sigProvider, actualOs);
    }
}
Also used : JwsOutputStream(org.apache.cxf.rs.security.jose.jws.JwsOutputStream) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsSignature(org.apache.cxf.rs.security.jose.jws.JwsSignature) Base64UrlOutputStream(org.apache.cxf.common.util.Base64UrlOutputStream) JwsOutputStream(org.apache.cxf.rs.security.jose.jws.JwsOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) JwsCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsCompactProducer) Base64UrlOutputStream(org.apache.cxf.common.util.Base64UrlOutputStream) JwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 40 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class DestinationSequence method acknowledge.

public void acknowledge(Message message) throws SequenceFault {
    RMProperties rmps = RMContextUtils.retrieveRMProperties(message, false);
    SequenceType st = rmps.getSequence();
    long messageNumber = st.getMessageNumber();
    LOG.fine("Acknowledging message: " + messageNumber);
    if (0L != lastMessageNumber && messageNumber > lastMessageNumber) {
        RMConstants consts = getProtocol().getConstants();
        SequenceFaultFactory sff = new SequenceFaultFactory(consts);
        throw sff.createSequenceTerminatedFault(st.getIdentifier(), false);
    }
    monitor.acknowledgeMessage();
    boolean updated = false;
    synchronized (this) {
        boolean done = false;
        int i = 0;
        for (; i < acknowledgement.getAcknowledgementRange().size(); i++) {
            AcknowledgementRange r = acknowledgement.getAcknowledgementRange().get(i);
            if (r.getLower().compareTo(messageNumber) <= 0 && r.getUpper().compareTo(messageNumber) >= 0) {
                done = true;
                break;
            }
            long diff = r.getLower() - messageNumber;
            if (diff == 1L) {
                r.setLower(messageNumber);
                updated = true;
                done = true;
            } else if (diff > 0L) {
                break;
            } else if (messageNumber - r.getUpper() == 1L) {
                r.setUpper(messageNumber);
                updated = true;
                done = true;
                break;
            }
        }
        if (!done) {
            // need new acknowledgement range
            AcknowledgementRange range = new AcknowledgementRange();
            range.setLower(messageNumber);
            range.setUpper(messageNumber);
            updated = true;
            acknowledgement.getAcknowledgementRange().add(i, range);
            if (acknowledgement.getAcknowledgementRange().size() > 1) {
                // acknowledge out-of-order at first opportunity
                scheduleImmediateAcknowledgement();
            }
        }
        mergeRanges();
    }
    if (updated) {
        RMStore store = destination.getManager().getStore();
        if (null != store && !MessageUtils.getContextualBoolean(message, Message.ROBUST_ONEWAY)) {
            try {
                RMMessage msg = new RMMessage();
                CachedOutputStream cos = (CachedOutputStream) message.get(RMMessageConstants.SAVED_CONTENT);
                msg.setMessageNumber(st.getMessageNumber());
                msg.setCreatedTime(rmps.getCreatedTime());
                // in case no attachments are available, cos can be saved directly
                if (message.getAttachments() == null) {
                    msg.setContent(cos);
                    msg.setContentType((String) message.get(Message.CONTENT_TYPE));
                } else {
                    InputStream is = cos.getInputStream();
                    PersistenceUtils.encodeRMContent(msg, message, is);
                }
                store.persistIncoming(this, msg);
            } catch (IOException e) {
                throw new Fault(e);
            }
        }
    }
    deliveringMessageNumbers.add(messageNumber);
    RMEndpoint reliableEndpoint = destination.getReliableEndpoint();
    RMConfiguration cfg = reliableEndpoint.getConfiguration();
    if (null == rmps.getCloseSequence()) {
        scheduleAcknowledgement(cfg.getAcknowledgementIntervalTime());
    }
    long inactivityTimeout = cfg.getInactivityTimeoutTime();
    scheduleSequenceTermination(inactivityTimeout);
}
Also used : RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) InputStream(java.io.InputStream) RMStore(org.apache.cxf.ws.rm.persistence.RMStore) Fault(org.apache.cxf.interceptor.Fault) SequenceType(org.apache.cxf.ws.rm.v200702.SequenceType) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) AcknowledgementRange(org.apache.cxf.ws.rm.v200702.SequenceAcknowledgement.AcknowledgementRange)

Aggregations

CachedOutputStream (org.apache.cxf.io.CachedOutputStream)105 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 Test (org.junit.Test)24 Message (org.apache.cxf.message.Message)22 OutputStream (java.io.OutputStream)18 Fault (org.apache.cxf.interceptor.Fault)18 MessageImpl (org.apache.cxf.message.MessageImpl)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)10 PrintWriter (java.io.PrintWriter)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 StreamSource (javax.xml.transform.stream.StreamSource)6 Endpoint (org.apache.cxf.endpoint.Endpoint)6 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)6 RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Source (javax.xml.transform.Source)5 ArrayList (java.util.ArrayList)4