private JwtHeadersWriter writer = new JwtTokenReaderWriter();
@Override
public void aroundWriteTo(WriterInterceptorContext ctx) throws IOException, WebApplicationException {
OutputStream actualOs = ctx.getOutputStream();
JweEncryptionProvider theEncryptionProvider = getInitializedEncryptionProvider();
String ctString = null;
if (contentTypeRequired) {
MediaType mt = ctx.getMediaType();
if (mt != null) {
ctString = JAXRSUtils.mediaTypeToString(mt);
}
}
if (useJweOutputStream) {
JweEncryption encryption = theEncryptionProvider.createJweEncryption(ctString);
try {
JweCompactProducer.startJweContent(actualOs,
encryption.getHeaders(),
writer,
encryption.getContentEncryptionKey(),
encryption.getIv());
} catch (IOException ex) {
throw new SecurityException(ex);
}
OutputStream jweStream = new JweOutputStream(actualOs, encryption.getCipher(),
encryption.getAuthTagLen());
if (encryption.isCompressionSupported()) {
jweStream = new DeflaterOutputStream(jweStream);
}
ctx.setOutputStream(jweStream);
ctx.proceed();
jweStream.flush();
} else {
CachedOutputStream cos = new CachedOutputStream();
ctx.setOutputStream(cos);
ctx.proceed();
String jweContent = theEncryptionProvider.encrypt(cos.getBytes(), ctString);
IOUtils.copy(new ByteArrayInputStream(jweContent.getBytes("UTF-8")), actualOs);
actualOs.flush();
}
}