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());
}
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();
}
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");
}
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);
}
}
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);
}
Aggregations