Search in sources :

Example 1 with JweEncryptionInput

use of org.apache.cxf.rs.security.jose.jwe.JweEncryptionInput 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();
    }
}
Also used : JweOutputStream(org.apache.cxf.rs.security.jose.jwe.JweOutputStream) JweEncryptionProvider(org.apache.cxf.rs.security.jose.jwe.JweEncryptionProvider) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) JweOutputStream(org.apache.cxf.rs.security.jose.jwe.JweOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) IOException(java.io.IOException) JweHeaders(org.apache.cxf.rs.security.jose.jwe.JweHeaders) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) JweEncryptionInput(org.apache.cxf.rs.security.jose.jwe.JweEncryptionInput) JweEncryptionOutput(org.apache.cxf.rs.security.jose.jwe.JweEncryptionOutput) JweException(org.apache.cxf.rs.security.jose.jwe.JweException) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) MediaType(javax.ws.rs.core.MediaType)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)1 MediaType (javax.ws.rs.core.MediaType)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 JweEncryptionInput (org.apache.cxf.rs.security.jose.jwe.JweEncryptionInput)1 JweEncryptionOutput (org.apache.cxf.rs.security.jose.jwe.JweEncryptionOutput)1 JweEncryptionProvider (org.apache.cxf.rs.security.jose.jwe.JweEncryptionProvider)1 JweException (org.apache.cxf.rs.security.jose.jwe.JweException)1 JweHeaders (org.apache.cxf.rs.security.jose.jwe.JweHeaders)1 JweOutputStream (org.apache.cxf.rs.security.jose.jwe.JweOutputStream)1