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

The following are Jave code examples for showing how to use setEntityStream() of the javax.ws.rs.client.ClientResponseContext class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ameba   File: LoggingFilter.java   Source Code and License Vote up 7 votes
/**
 * {@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
Project: camel-alma   File: AlmaResponseFilter.java   Source Code and License Vote up 6 votes
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Response status: {} {}", responseContext.getStatus(), responseContext.getStatusInfo().toString());
        LOG.trace("Response headers: {}", responseContext.getHeaders());

        InputStream stream = responseContext.getEntityStream();
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = stream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        stream.close();
        LOG.trace("Response body: {}", result.toString("UTF-8"));

        responseContext.setEntityStream(new ByteArrayInputStream(result.toByteArray()));
    }
}
 
Example 3
Project: jive-sdk-java-jersey   File: StripAllowIllegalResourceCallFilter.java   Source Code and License Vote up 6 votes
private void stripResponse(ClientResponseContext responseContext) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = responseContext.getEntityStream();

        final StringBuilder b = new StringBuilder();
        try {
            if (in.available() > 0) {
                ReaderWriter.writeTo(in, out);
                StringBuffer sbuf = new StringBuffer(new String(out.toByteArray()));
                if (sbuf.indexOf(ALLOW_ILLEGAL_RESOURCE_CALL_PREFIX) == 0) {
                    if (log.isDebugEnabled()) { log.debug("Stripping "+ALLOW_ILLEGAL_RESOURCE_CALL_PREFIX); }
                    responseContext.setEntityStream(new ByteArrayInputStream(sbuf.substring(ALLOW_ILLEGAL_RESOURCE_CALL_PREFIX.length()).getBytes()));
                } else {
                    responseContext.setEntityStream(new ByteArrayInputStream(out.toByteArray()));
                } // end if
            } // end if
        } catch (IOException ex) {
            throw new ContainerException(ex);
        } // end try/catch

    }
 
Example 4
Project: jive-sdk-java-jersey   File: DebugClientResponseFilter.java   Source Code and License Vote up 6 votes
private void logResponse(ClientResponseContext responseContext) throws IOException {

        String responseBody = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = responseContext.getEntityStream();

        final StringBuilder b = new StringBuilder();
        try {
            if (in.available() > 0) {
                ReaderWriter.writeTo(in, out);
                responseContext.setEntityStream(new ByteArrayInputStream(out.toByteArray()));
                responseBody = new String(out.toByteArray());
            } // end if
        } catch (IOException ex) {
            throw new ContainerException(ex);
        } // end try/catch

        responseBody = (responseBody == null) ? "" : "\n"+responseBody;

        log.debug("\nClient Response:\nStatus: "+responseContext.getStatus()+ responseBody);

    }
 
Example 5
Project: smart-id-java-client   File: LoggingFilter.java   Source Code and License Vote up 5 votes
private void logResponseBody(ClientResponseContext responseContext) throws IOException {
  Charset charset = MessageUtils.getCharset(responseContext.getMediaType());
  InputStream entityStream = responseContext.getEntityStream();
  byte[] bodyBytes = readInputStreamBytes(entityStream);
  responseContext.setEntityStream(new ByteArrayInputStream(bodyBytes));
  logger.trace("Response body: " + new String(bodyBytes, charset));
}
 
Example 6
Project: robozonky   File: RoboZonkyFilter.java   Source Code and License Vote up 5 votes
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 7
Project: jahhan   File: CryptoFilter.java   Source Code and License Vote up 5 votes
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
	boolean needDecrypt = AuthenticationVariable.getAuthenticationVariable().isCrypt();
	URI uriInfo = requestContext.getUri();
	String path = uriInfo.getPath();
	boolean docRequest = path.endsWith("/swagger");
	if (needDecrypt && !(docRequest && BaseConfiguration.IS_DEBUG)) {
		InputStream entityStream = responseContext.getEntityStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(entityStream));
		String line = "";
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		String context = sb.toString();
		String key = AuthenticationVariable.getAuthenticationVariable().getKey();
		Assert.notNullString(key, "鉴权秘钥获取失败!", HttpStatus.SC_UNAUTHORIZED, JahhanErrorCode.NO_AUTHORITY);
		context = aesCrypto.decrypt(context, key);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			out.write(context.getBytes());
			ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
			responseContext.setEntityStream(in);
		} catch (Exception e) {
			JahhanException.throwException(JahhanErrorCode.UNKNOW_ERROR, "未知错误", e);
		} finally {
			out.close();
		}
	}
}
 
Example 8
Project: gitlab-plugin   File: ResteasyGitLabClientBuilder.java   Source Code and License Vote up 5 votes
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 9
Project: nsi-dds   File: RestClient.java   Source Code and License Vote up 5 votes
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException
{
    if (requestContext == null || responseContext == null || responseContext.getStatus() != Response.Status.FOUND.getStatusCode()) {
       return;
    }

    log.debug("Processing redirect for " + requestContext.getMethod() + " " + requestContext.getUri().toASCIIString() + " to " + responseContext.getLocation().toASCIIString());

    Client inClient = requestContext.getClient();
    Object entity = requestContext.getEntity();
    MultivaluedMap<String, Object> headers = requestContext.getHeaders();
    String method = requestContext.getMethod();
    Response resp;
    if ("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method)) {
        resp = inClient.target(responseContext.getLocation())
                .request(requestContext.getMediaType())
                .headers(headers)
                .method(requestContext.getMethod(), Entity.entity(new GenericEntity<JAXBElement<?>>((JAXBElement<?>)entity) {}, NsiConstants.NSI_DDS_V1_XML));
    }
    else {
        resp = inClient.target(responseContext.getLocation())
                .request(requestContext.getMediaType())
                .headers(headers)
                .method(requestContext.getMethod());
    }

    responseContext.setEntityStream((InputStream) resp.getEntity());
    responseContext.setStatusInfo(resp.getStatusInfo());
    responseContext.setStatus(resp.getStatus());
    responseContext.getHeaders().putAll(resp.getStringHeaders());

    log.debug("Processing redirect with result " + resp.getStatus());
}
 
Example 10
Project: docker-java   File: LoggingFilter.java   Source Code and License Vote up 5 votes
@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
Project: docker-java   File: FollowRedirectsFilter.java   Source Code and License Vote up 5 votes
@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
Project: raml-tester   File: JaxrsContextRamlResponse.java   Source Code and License Vote up 5 votes
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
Project: robots   File: FollowRedirectsFilter.java   Source Code and License Vote up 4 votes
private void populateResponseContext(ClientResponseContext responseContext, Response response) {
    responseContext.setEntityStream((InputStream) response.getEntity());
    responseContext.setStatusInfo(response.getStatusInfo());
    responseContext.setStatus(response.getStatus());
}