use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.
the class RMTxStoreTestBase method createRMMessage.
private RMMessage createRMMessage(Long mn, String to) throws IOException {
RMMessage msg = control.createMock(RMMessage.class);
EasyMock.expect(msg.getMessageNumber()).andReturn(mn).anyTimes();
EasyMock.expect(msg.getTo()).andReturn(to).anyTimes();
EasyMock.expect(msg.getContentType()).andReturn("text/xml").anyTimes();
EasyMock.expect(msg.getCreatedTime()).andReturn(TIME);
byte[] value = ("Message " + mn.longValue()).getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(value);
CachedOutputStream cos = new CachedOutputStream();
IOUtils.copy(bais, cos);
cos.flush();
bais.close();
EasyMock.expect(msg.getContent()).andReturn(cos).anyTimes();
return msg;
}
use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.
the class PersistenceUtilsTest method testEncodeDecodeRMContent.
@Test
public void testEncodeDecodeRMContent() throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(SOAP_PART.getBytes());
RMMessage rmmsg = new RMMessage();
Message messageImpl = new MessageImpl();
messageImpl.put(Message.CONTENT_TYPE, "text/xml");
// add attachments
addAttachment(messageImpl);
// serialize
PersistenceUtils.encodeRMContent(rmmsg, messageImpl, bis);
Message messageImplRestored = new MessageImpl();
PersistenceUtils.decodeRMContent(rmmsg, messageImplRestored);
assertEquals(1, messageImplRestored.getAttachments().size());
CachedOutputStream cos = (CachedOutputStream) messageImplRestored.get(RMMessageConstants.SAVED_CONTENT);
assertStartsWith(cos.getInputStream(), SOAP_PART);
}
use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.
the class RMMessageTest method testContentCachedOutputStream.
@Test
public void testContentCachedOutputStream() throws Exception {
RMMessage msg = new RMMessage();
CachedOutputStream co = new CachedOutputStream();
co.write(DATA);
msg.setContent(co);
byte[] msgbytes = IOUtils.readBytesFromStream(msg.getContent().getInputStream());
assertArrayEquals(DATA, msgbytes);
co.close();
}
use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.
the class JweWriterInterceptor method aroundWriteTo.
@Override
public void aroundWriteTo(WriterInterceptorContext ctx) throws IOException, WebApplicationException {
if (ctx.getEntity() == null) {
ctx.proceed();
return;
}
OutputStream actualOs = ctx.getOutputStream();
JweHeaders jweHeaders = new JweHeaders();
JweEncryptionProvider theEncryptionProvider = getInitializedEncryptionProvider(jweHeaders);
String ctString = null;
MediaType contentMediaType = ctx.getMediaType();
if (contentTypeRequired && contentMediaType != null) {
if ("application".equals(contentMediaType.getType())) {
ctString = contentMediaType.getSubtype();
} else {
ctString = JAXRSUtils.mediaTypeToString(contentMediaType);
}
}
if (ctString != null) {
jweHeaders.setContentType(ctString);
}
protectHttpHeadersIfNeeded(ctx, jweHeaders);
if (useJweOutputStream) {
JweEncryptionOutput encryption = theEncryptionProvider.getEncryptionOutput(new JweEncryptionInput(jweHeaders));
JoseUtils.traceHeaders(encryption.getHeaders());
try {
JweCompactBuilder.startJweContent(actualOs, encryption.getHeaders(), encryption.getEncryptedContentEncryptionKey(), encryption.getIv());
} catch (IOException ex) {
LOG.warning("JWE encryption error");
throw new JweException(JweException.Error.CONTENT_ENCRYPTION_FAILURE, ex);
}
JweOutputStream jweOutputStream = new JweOutputStream(actualOs, encryption.getCipher(), encryption.getAuthTagProducer());
ctx.setOutputStream(encryption.isCompressionSupported() ? new DeflaterOutputStream(jweOutputStream) : jweOutputStream);
ctx.proceed();
setJoseMediaType(ctx);
jweOutputStream.finalFlush();
} else {
CachedOutputStream cos = new CachedOutputStream();
ctx.setOutputStream(cos);
ctx.proceed();
String jweContent = theEncryptionProvider.encrypt(cos.getBytes(), jweHeaders);
JoseUtils.traceHeaders(jweHeaders);
setJoseMediaType(ctx);
IOUtils.copy(new ByteArrayInputStream(StringUtils.toBytesUTF8(jweContent)), actualOs);
actualOs.flush();
}
}
use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.
the class JweJsonWriterInterceptor method aroundWriteTo.
@Override
public void aroundWriteTo(WriterInterceptorContext ctx) throws IOException, WebApplicationException {
if (ctx.getEntity() == null) {
ctx.proceed();
return;
}
OutputStream actualOs = ctx.getOutputStream();
JweHeaders sharedProtectedHeaders = new JweHeaders();
List<String> propLocs = getPropertyLocations();
List<JweHeaders> perRecipientUnprotectedHeaders = new ArrayList<>(propLocs.size());
for (int i = 0; i < propLocs.size(); i++) {
perRecipientUnprotectedHeaders.add(new JweHeaders());
}
List<JweEncryptionProvider> providers = getInitializedEncryptionProviders(propLocs, sharedProtectedHeaders, perRecipientUnprotectedHeaders);
String ctString = null;
MediaType contentMediaType = ctx.getMediaType();
if (contentTypeRequired && contentMediaType != null) {
if ("application".equals(contentMediaType.getType())) {
ctString = contentMediaType.getSubtype();
} else {
ctString = JAXRSUtils.mediaTypeToString(contentMediaType);
}
}
if (ctString != null) {
sharedProtectedHeaders.setContentType(ctString);
}
protectHttpHeadersIfNeeded(ctx, sharedProtectedHeaders);
if (useJweOutputStream) {
// TODO
} else {
CachedOutputStream cos = new CachedOutputStream();
ctx.setOutputStream(cos);
ctx.proceed();
JweJsonProducer producer = new JweJsonProducer(sharedProtectedHeaders, cos.getBytes());
String jweContent = producer.encryptWith(providers, perRecipientUnprotectedHeaders);
setJoseMediaType(ctx);
IOUtils.copy(new ByteArrayInputStream(StringUtils.toBytesUTF8(jweContent)), actualOs);
actualOs.flush();
}
}
Aggregations