use of org.apache.cxf.rs.security.jose.jwe.JweEncryptionOutput 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.getContentEncryptionKey(), encryption.getIv());
} catch (IOException ex) {
LOG.warning("JWE encryption error");
throw new JweException(JweException.Error.CONTENT_ENCRYPTION_FAILURE, ex);
}
OutputStream wrappedStream = null;
JweOutputStream jweOutputStream = new JweOutputStream(actualOs, encryption.getCipher(), encryption.getAuthTagProducer());
wrappedStream = jweOutputStream;
if (encryption.isCompressionSupported()) {
wrappedStream = new DeflaterOutputStream(jweOutputStream);
}
ctx.setOutputStream(wrappedStream);
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();
}
}
Aggregations