Java Code Examples for javax.ws.rs.ext.WriterInterceptorContext#getEntity()

The following examples show how to use javax.ws.rs.ext.WriterInterceptorContext#getEntity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamingWriterInterceptor.java    From ameba with MIT License 6 votes vote down vote up
/**
 * <p>applyStreaming.</p>
 *
 * @param requestContext a {@link javax.ws.rs.container.ContainerRequestContext} object.
 * @param context a {@link javax.ws.rs.ext.WriterInterceptorContext} object.
 * @throws java.io.IOException if any.
 */
protected void applyStreaming(ContainerRequestContext requestContext, WriterInterceptorContext context)
        throws IOException {

    Object entity = context.getEntity();
    StreamingProcess<Object> process = MessageHelper.getStreamingProcess(context.getEntity(), manager);

    if (process != null) {
        ContainerResponseContext responseContext =
                (ContainerResponseContext) requestContext.getProperty(RESP_PROP_N);
        responseContext.setStatusInfo(Response.Status.PARTIAL_CONTENT);
        context.getHeaders().putSingle(ACCEPT_RANGES, BYTES_RANGE);
        context.setType(StreamingOutput.class);
        context.setEntity(new MediaStreaming(
                        entity,
                requestContext.getHeaderString(MediaStreaming.RANGE),
                        process,
                        context.getMediaType(),
                        context.getHeaders()
                )
        );
    }
}
 
Example 2
Source File: ContentLengthWriterInterceptor.java    From ameba with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    if (!context.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
        Object entity = context.getEntity();
        StreamingProcess<Object> process = MessageHelper.getStreamingProcess(entity, manager);

        if (process != null) {
            long length = process.length(entity);

            if (length != -1)
                context.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, length);
        }
    }

    context.proceed();
}
 
Example 3
Source File: StreamingWriterInterceptor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>isWritable.</p>
 *
 * @param context a {@link javax.ws.rs.ext.WriterInterceptorContext} object.
 * @return a boolean.
 */
protected boolean isWritable(WriterInterceptorContext context) {
    return context.getEntity() != null
            && !Boolean.FALSE.equals(requestProvider.get().getProperty(MessageHelper.STREAMING_RANGE_ENABLED))
            && context.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)
            && !(context.getEntity() instanceof MediaStreaming);
}
 
Example 4
Source File: CaptchaWriterInterceptor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    Object entity = context.getEntity();
    if (entity instanceof Images.Captcha || entity instanceof Captcha) {
        context.setMediaType(IMG_TYPE);
        context.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, IMG_TYPE);
        if (entity instanceof Captcha) {
            Captcha captcha = (Captcha) entity;
            context.setType(BufferedImage.class);
            context.setEntity(captcha.getImage());
        }
    }
    context.proceed();
}
 
Example 5
Source File: TemplateMethodInterceptor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void aroundWriteTo(final WriterInterceptorContext context) throws IOException, WebApplicationException {
    final Object entity = context.getEntity();

    if (!(entity instanceof Viewable) && resourceInfoProvider.get().getResourceMethod() != null) {
        final Template template = TemplateHelper.getTemplateAnnotation(context.getAnnotations());
        if (template != null) {
            context.setType(Viewable.class);
            context.setEntity(new Viewable(template.name(), entity));
        }
    }

    context.proceed();
}
 
Example 6
Source File: CreateSignatureInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException {
    // Only sign the request if we have a Body.
    if (context.getEntity() != null) {
        // skip digest if already set
        if (addDigest
            && context.getHeaders().keySet().stream().noneMatch(DIGEST_HEADER_NAME::equalsIgnoreCase)) {
            addDigest(context);
        } else {
            sign(context);
            context.proceed();
        }
    }
}
 
Example 7
Source File: JAXRSHTTPSignatureTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private boolean shouldAddDigest(WriterInterceptorContext context) {
    return null != context.getEntity()
        && context.getHeaders().keySet().stream().noneMatch(DIGEST_HEADER_NAME::equalsIgnoreCase);
}
 
Example 8
Source File: JweWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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);
        }
        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();
    }
}
 
Example 9
Source File: JweJsonWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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();
    }
}
 
Example 10
Source File: JwsWriterInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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);
    }
}