Java Code Examples for javax.ws.rs.client.ClientResponseContext#setEntityStream()

The following examples show how to use javax.ws.rs.client.ClientResponseContext#setEntityStream() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: ameba   File: LoggingFilter.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
        throws IOException {
    final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
    final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

    StringBuilder b = (StringBuilder) requestContext.getProperty(LOGGER_BUFFER_PROPERTY);
    if (b == null) {
        b = new StringBuilder();
        requestContext.setProperty(LOGGER_BUFFER_PROPERTY, b);
    }

    printResponseLine(b, "Client response received", id, responseContext.getStatus());
    printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());

    if (printEntity && responseContext.hasEntity() && isSupportPrintType(responseContext.getMediaType())) {
        responseContext.setEntityStream(logInboundEntity(b, responseContext.getEntityStream(),
                MessageUtils.getCharset(responseContext.getMediaType())));
    }

    log(b);
}
 
Example 2
Source Project: gitlab4j-api   File: MaskingLoggingFilter.java    License: MIT License 6 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {

    if (!logger.isLoggable(level)) {
        return;
    }

    final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
    final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

    final StringBuilder sb = new StringBuilder();
    printResponseLine(sb, "Received server response", id, responseContext.getStatus());
    printHeaders(sb, id, RESPONSE_PREFIX, responseContext.getHeaders());
 
    if (responseContext.hasEntity() && maxEntitySize > 0) {
        responseContext.setEntityStream(logResponseEntity(sb, responseContext.getEntityStream(), 
                MessageUtils.getCharset(responseContext.getMediaType())));
    }

    log(sb);
}
 
Example 3
Source Project: cxf   File: JweJsonClientResponseFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext req, ClientResponseContext res) throws IOException {
    if (isMethodWithNoContent(req.getMethod())
        || isStatusCodeWithNoContent(res.getStatus())
        || isCheckEmptyStream() && !res.hasEntity()) {
        return;
    }
    final byte[] encryptedContent = IOUtils.readBytesFromStream(res.getEntityStream());
    if (encryptedContent.length == 0) {
        return;
    }
    JweDecryptionOutput out = decrypt(encryptedContent);
    byte[] bytes = out.getContent();
    res.setEntityStream(new ByteArrayInputStream(bytes));
    res.getHeaders().putSingle("Content-Length", Integer.toString(bytes.length));
    String ct = JoseUtils.checkContentType(out.getHeaders().getContentType(), getDefaultMediaType());
    if (ct != null) {
        res.getHeaders().putSingle("Content-Type", ct);
    }
    if (super.isValidateHttpHeaders()) {
        super.validateHttpHeadersIfNeeded(res.getHeaders(), out.getHeaders());
    }
}
 
Example 4
Source Project: cxf   File: JweClientResponseFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext req, ClientResponseContext res) throws IOException {
    if (isMethodWithNoContent(req.getMethod())
            || isStatusCodeWithNoContent(res.getStatus())
            || isCheckEmptyStream() && !res.hasEntity()) {
        return;
    }
    final byte[] encryptedContent = IOUtils.readBytesFromStream(res.getEntityStream());
    if (encryptedContent.length == 0) {
        return;
    }
    JweDecryptionOutput out = decrypt(encryptedContent);
    byte[] bytes = out.getContent();
    res.setEntityStream(new ByteArrayInputStream(bytes));
    res.getHeaders().putSingle("Content-Length", Integer.toString(bytes.length));
    String ct = JoseUtils.checkContentType(out.getHeaders().getContentType(), getDefaultMediaType());
    if (ct != null) {
        res.getHeaders().putSingle("Content-Type", ct);
    }
    if (super.isValidateHttpHeaders()) {
        super.validateHttpHeadersIfNeeded(res.getHeaders(), out.getHeaders());
    }
}
 
Example 5
Source Project: cxf   File: JwsJsonClientResponseFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext req, ClientResponseContext res) throws IOException {
    if (isMethodWithNoContent(req.getMethod())
        || isStatusCodeWithNoContent(res.getStatus())
        || isCheckEmptyStream() && !res.hasEntity()) {
        return;
    }
    final String content = IOUtils.readStringFromStream(res.getEntityStream());
    if (StringUtils.isEmpty(content)) {
        return;
    }
    JwsSignatureVerifier theSigVerifier = getInitializedSigVerifier();
    JwsJsonConsumer c = new JwsJsonConsumer(content);
    validate(c, theSigVerifier);
    byte[] bytes = c.getDecodedJwsPayloadBytes();
    res.setEntityStream(new ByteArrayInputStream(bytes));
    res.getHeaders().putSingle("Content-Length", Integer.toString(bytes.length));

    // the list is guaranteed to be non-empty
    JwsJsonSignatureEntry sigEntry = c.getSignatureEntries().get(0);
    String ct = JoseUtils.checkContentType(sigEntry.getUnionHeader().getContentType(), getDefaultMediaType());
    if (ct != null) {
        res.getHeaders().putSingle("Content-Type", ct);
    }
}
 
Example 6
Source Project: allure-java   File: AllureJaxRs.java    License: Apache License 2.0 5 votes vote down vote up
private String getBody(final ClientResponseContext responseContext) throws IOException {
    try (InputStream stream = responseContext.getEntityStream();
         ByteArrayOutputStream result = new ByteArrayOutputStream()) {
        final byte[] buffer = new byte[1024];

        int length = stream.read(buffer);
        while (length != -1) {
            result.write(buffer, 0, length);
            length = stream.read(buffer);
        }
        responseContext.setEntityStream(new ByteArrayInputStream(result.toByteArray()));
        return result.toString(StandardCharsets.UTF_8.toString());
    }
}
 
Example 7
Source Project: robozonky   File: RoboZonkyFilter.java    License: Apache License 2.0 5 votes vote down vote up
private String getResponseEntity(final ClientResponseContext clientResponseContext) throws IOException {
    if (shouldLogEntity(clientResponseContext)) {
        final InterceptingInputStream s = new InterceptingInputStream(clientResponseContext.getEntityStream());
        clientResponseContext.setEntityStream(s);
        logger.debug("Response body is: {}", s.getContents());
        return s.getContents();
    } else {
        return "";
    }
}
 
Example 8
Source Project: logbook   File: RemoteResponse.java    License: MIT License 5 votes vote down vote up
@Override
public State buffer(
        final ClientResponseContext context) throws IOException {

    final byte[] body = toByteArray(context.getEntityStream());
    context.setEntityStream(new ByteArrayInputStream(body));
    return new Buffering(body);
}
 
Example 9
private String getResponseBody(ClientResponseContext context) {
    try (InputStream entityStream = context.getEntityStream()) {
        if (entityStream != null) {
            byte[] bytes = IOUtils.toByteArray(entityStream);
            context.setEntityStream(new ByteArrayInputStream(bytes));
            return new String(bytes);
        }
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Failure during reading the response body", e);
        context.setEntityStream(new ByteArrayInputStream(new byte[0]));
    }
    return "";
}
 
Example 10
Source Project: docker-java   File: LoggingFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
        throws IOException {
    final long id = aid.incrementAndGet();
    final StringBuilder b = new StringBuilder();

    printResponseLine(b, "Client response received", id, responseContext.getStatus());
    printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());

    if (printEntity && responseContext.hasEntity()) {
        responseContext.setEntityStream(logInboundEntity(b, responseContext.getEntityStream()));
    }

    log(b);
}
 
Example 11
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    if (!responseContext.getStatusInfo().getFamily().equals(Response.Status.Family.REDIRECTION)) {
        return;
    }

    Response resp = requestContext.getClient().target(responseContext.getLocation()).request()
            .method(requestContext.getMethod());
    responseContext.setEntityStream((InputStream) resp.getEntity());
    responseContext.setStatusInfo(resp.getStatusInfo());
    responseContext.setStatus(resp.getStatus());
}
 
Example 12
public JaxrsContextRamlResponse(ClientResponseContext context) {
    this.context = context;
    try {
        content = IoUtils.readIntoByteArray(context.getEntityStream());
        context.setEntityStream(new ByteArrayInputStream(content));
    } catch (IOException e) {
        throw new RamlCheckerException("Could not get response content", e);
    }
}
 
Example 13
Source Project: cxf   File: JwsClientResponseFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext req, ClientResponseContext res) throws IOException {
    if (isMethodWithNoContent(req.getMethod())
        || isStatusCodeWithNoContent(res.getStatus())
        || isCheckEmptyStream() && !res.hasEntity()) {
        return;
    }
    final String content = IOUtils.readStringFromStream(res.getEntityStream());
    if (StringUtils.isEmpty(content)) {
        return;
    }
    JwsCompactConsumer p = new JwsCompactConsumer(content);
    JwsSignatureVerifier theSigVerifier = getInitializedSigVerifier(p.getJwsHeaders());
    if (!p.verifySignatureWith(theSigVerifier)) {
        throw new JwsException(JwsException.Error.INVALID_SIGNATURE);
    }

    byte[] bytes = p.getDecodedJwsPayloadBytes();
    res.setEntityStream(new ByteArrayInputStream(bytes));
    res.getHeaders().putSingle("Content-Length", Integer.toString(bytes.length));
    String ct = JoseUtils.checkContentType(p.getJwsHeaders().getContentType(), getDefaultMediaType());
    if (ct != null) {
        res.getHeaders().putSingle("Content-Type", ct);
    }

    if (super.isValidateHttpHeaders()) {
        super.validateHttpHeadersIfNeeded(res.getHeaders(), p.getJwsHeaders());
    }
}
 
Example 14
Source Project: cxf   File: VerifySignatureClientFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) {
    byte[] messageBody = verifyDigest(responseContext.getHeaders(), responseContext.getEntityStream());
    if (messageBody != null) {
        responseContext.setEntityStream(new ByteArrayInputStream(messageBody));
    }

    verifySignature(responseContext.getHeaders(), "", "");
}