Search in sources :

Example 1 with JwsOutputStream

use of org.apache.cxf.rs.security.jose.jws.JwsOutputStream 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);
    }
}
Also used : JwsOutputStream(org.apache.cxf.rs.security.jose.jws.JwsOutputStream) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsSignature(org.apache.cxf.rs.security.jose.jws.JwsSignature) Base64UrlOutputStream(org.apache.cxf.common.util.Base64UrlOutputStream) JwsOutputStream(org.apache.cxf.rs.security.jose.jws.JwsOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) JwsCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsCompactProducer) Base64UrlOutputStream(org.apache.cxf.common.util.Base64UrlOutputStream) JwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Aggregations

OutputStream (java.io.OutputStream)1 Base64UrlOutputStream (org.apache.cxf.common.util.Base64UrlOutputStream)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 JwsCompactProducer (org.apache.cxf.rs.security.jose.jws.JwsCompactProducer)1 JwsHeaders (org.apache.cxf.rs.security.jose.jws.JwsHeaders)1 JwsOutputStream (org.apache.cxf.rs.security.jose.jws.JwsOutputStream)1 JwsSignature (org.apache.cxf.rs.security.jose.jws.JwsSignature)1 JwsSignatureProvider (org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider)1