Java Code Examples for javax.ws.rs.container.ContainerRequestContext#getEntityStream()

The following examples show how to use javax.ws.rs.container.ContainerRequestContext#getEntityStream() . 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: DefaultFormEntityProvider.java    From krazo with Apache License 2.0 6 votes vote down vote up
@Override
public Form getForm(ContainerRequestContext context) throws IOException {
    final InputStream is = context.getEntityStream();

    // Ensure stream can be restored for next interceptor
    InputStream bufferedStream;
    if (is.markSupported()) {
        bufferedStream = is;
    } else {
        bufferedStream = new BufferedInputStream(is);
    }
    bufferedStream.mark(Integer.MAX_VALUE);

    final MediaType contentType = context.getMediaType();

    final String charset = contentType.getParameters().get("charset");
    final String entity = toString(bufferedStream, charset != null ? charset : DEFAULT_CHARSET);

    final Form form = parseForm(entity);

    bufferedStream.reset();
    context.setEntityStream(bufferedStream);

    return form;

}
 
Example 2
Source File: MCRResourceAccessFilter.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    // TODO due to ContainerRequest.getEntity() consumes InputStream, we need to keep a copy of it in memory
    try (InputStream in = requestContext.getEntityStream()) {
        ByteArrayOutputStream out = new ByteArrayOutputStream(64 * 1024);
        IOUtils.copy(in, out);
        byte[] entity = out.toByteArray();
        //restore input
        requestContext.setEntityStream(new ByteArrayInputStream(entity));
        boolean hasPermission = accessChecker.isPermitted(requestContext);
        if (!hasPermission) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        //restore input
        requestContext.setEntityStream(new ByteArrayInputStream(entity));
    } catch (IOException e) {
        throw new WebApplicationException(e);
    }
}
 
Example 3
Source File: RequestLoggingFilter.java    From pnc with Apache License 2.0 6 votes vote down vote up
private String getEntityBody(ContainerRequestContext requestContext) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = requestContext.getEntityStream();

    final StringBuilder b = new StringBuilder();
    try {
        IOUtils.copy(in, out);

        byte[] requestEntity = out.toByteArray();
        if (requestEntity.length == 0) {
            b.append("\n");
        } else {
            b.append(new String(requestEntity)).append("\n");
        }
        requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));

    } catch (IOException e) {
        logger.error("Error logging REST request.", e);
    }
    return b.toString();
}