@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()); } }
@Override public void filter(ClientRequestContext request, ClientResponseContext response) { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "Got response from GitLab:\nURL: {0}\nStatus: {1} {2}\nResponse headers: [\n{3}\n]\nResponse body: {4}", LoggerUtil.toArray(request.getUri(), response.getStatus(), response.getStatusInfo(), toString(response.getHeaders()), getPrettyPrintResponseBody(response))); } }
@Override public StatusType getStatusInfo() { return responseContext.getStatusInfo(); }
@Override public StatusType getStatusInfo() { return responseContext.getStatusInfo(); }
@Override public StatusType getStatusInfo() { return responseContext.getStatusInfo(); }
@Override public StatusType getStatusInfo() { return responseContext.getStatusInfo(); }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getStatus() != Response.Status.OK.getStatusCode() && responseContext.hasEntity()) { try { final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final Error error = (Error) unmarshaller.unmarshal(responseContext.getEntityStream()); final Response.ResponseBuilder builder = Response.status(responseContext.getStatusInfo()); builder.entity(error); // copy response headers for (Map.Entry<String, List<String>> header : responseContext.getHeaders().entrySet()) { builder.header(header.getKey(), header.getValue()); } throw new LinkedInException(error, builder.build()); } catch (JAXBException e) { // log and ignore LOG.warn("Unable to parse LinkedIn error: {}", e.getMessage(), e); } } } }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { StringBuilder response = new StringBuilder(); response.append("HTTP response\n"); response.append(Integer.toString(responseContext.getStatus())).append(" ").append(responseContext.getStatusInfo().getReasonPhrase()).append("\n"); appendHeaders(response, responseContext.getHeaders()); if (responseContext.hasEntity()) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); IOUtils.copy(responseContext.getEntityStream(), stream); byte[] responseBytes = stream.toByteArray(); response.append(new String(responseBytes)); response.append("\n"); responseContext.setEntityStream(new ByteArrayInputStream(responseBytes)); } log.debug(response.toString()); }
@Override public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException { StringBuilder debugMsgBuilder = new StringBuilder(); debugMsgBuilder.append("ClientRequest: ").append(request.getMethod()).append("\t"); debugMsgBuilder.append(request.getUri().toASCIIString()).append("\t"); appendObjectHeaders(debugMsgBuilder, "> ", request.getHeaders().entrySet()); appendStringHeaders(debugMsgBuilder, "< ", response.getHeaders().entrySet()); debugMsgBuilder.append(response.getStatusInfo()).append(", length=").append(response.getLength()).append(" "); Long requestStartTime = (Long) request.getProperty(PROPERTY_NANOS); if (requestStartTime != null) { debugMsgBuilder .append((System.nanoTime() - requestStartTime) / MILLISECONDS_PER_NANOSECOND) .append(" ms\t"); } LOG.debug(debugMsgBuilder.toString()); RequestLog.stopTiming(CLIENT_TOTAL_TIMER); }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { // 304 (not modified) is not redirect so we need to be explicit what // status codes we want to target here if (responseContext.getStatus() == 301 || responseContext.getStatus() == 302) { URI redirectTarget = responseContext.getLocation(); throw new IllegalStateException( String.format( "Received status %s. Redirected to %s. Check your server URL (e.g. used http instead of https)", responseContext.getStatusInfo(), redirectTarget)); } } }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { // 304 (not modified) is not redirect so we need to be explicit what // status codes we want to target here if (responseContext.getStatus() == 301 || responseContext.getStatus() == 302) { URI redirectTarget = responseContext.getLocation(); throw new IllegalStateException( String.format( "Received status %s. Redirected to %s. Check your server URL (e.g. used http instead of https)", responseContext.getStatusInfo(), redirectTarget)); } } }
@Override public void filter(final ClientRequestContext clientRequestContext, final ClientResponseContext clientResponseContext) throws IOException { logger.debug("HTTP {} Response from {}: {} {}.", clientRequestContext.getMethod(), clientRequestContext.getUri(), clientResponseContext.getStatus(), clientResponseContext.getStatusInfo().getReasonPhrase()); final String responseEntity = getResponseEntity(clientResponseContext); if (clientResponseContext.getStatus() == 400 && responseEntity.contains("invalid_token")) { // Zonky is dumb and throws 400 when it should throw 401 clientResponseContext.setStatus(401); } responseHeaders = clientResponseContext.getHeaders().entrySet().stream() .filter(e -> !e.getValue().isEmpty()) .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0), (a, b) -> a, TreeMap::new)); } }
private static String formatMessage(ClientRequestContext request, ClientResponseContext response) { String method = request.getMethod(); String url = request.getUri().toString(); int statusCode = response.getStatus(); String statusText = response.getStatusInfo().getReasonPhrase(); return String.format("%s %s: %d %s", method, url, statusCode, statusText); } }
@Override public void filter(final ClientRequestContext clientRequestContext, final ClientResponseContext clientResponseContext) throws IOException { logger.debug("HTTP {} Response from {}: {} {}.", clientRequestContext.getMethod(), clientRequestContext.getUri(), clientResponseContext.getStatus(), clientResponseContext.getStatusInfo().getReasonPhrase()); final String responseEntity = getResponseEntity(clientResponseContext); if (clientResponseContext.getStatus() == 400 && responseEntity.contains("invalid_token")) { // Zonky is dumb and throws 400 when it should throw 401 clientResponseContext.setStatus(401); } responseHeaders = clientResponseContext.getHeaders().entrySet().stream() .filter(e -> !e.getValue().isEmpty()) .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0), (a, b) -> a, TreeMap::new)); } }
@Override @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST") public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getStatusInfo().getFamily().equals( Response.Status.Family.SUCCESSFUL) && !isContentTypeCompatible(responseContext.getMediaType())) { log.error(ERROR_MSG); String snippet = String.format( "Wrong content type received: [%s]", responseContext.getMediaType()); log.error(snippet); throw new IllegalStateException(snippet); } } }
@Override @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST") public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getStatusInfo().getFamily().equals( Response.Status.Family.SUCCESSFUL) && !isContentTypeCompatible(responseContext.getMediaType())) { log.error(ERROR_MSG); String snippet = String.format( "Wrong content type received: [%s]", responseContext.getMediaType()); log.error(snippet); throw new IllegalStateException(snippet); } } }
private void logResponse(ClientRequestContext request, ClientResponseContext response) { LOG.info("{} {}: {} {}", request.getMethod(), request.getUri(), response.getStatus(), response.getStatusInfo().getReasonPhrase()); }
@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()); } }
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (!logHttp && !log.isTraceEnabled()) { return; } // log after a response has been received log("<< REST Response: " + responseContext.getStatus() + ":" + responseContext.getStatusInfo()); for (String key : responseContext.getHeaders().keySet()) { log("<< Header: " + key + " = " + responseContext.getHeaders().get(key)); } log(">> Media Type: " + responseContext.getMediaType()); if (responseContext.hasEntity()) { StringBuilder sb = new StringBuilder(); // replace the response's entity stream with a copy so that we can // log the original stream's contents responseContext.setEntityStream( copyStream(responseContext.getEntityStream(), sb)); log(">> Entity:\n" + sb); } } }
@Test void changes400to401() throws IOException { final int expectedCode = 400; final ClientRequestContext ctx = mock(ClientRequestContext.class); final ClientResponseContext ctx2 = mock(ClientResponseContext.class); final ZonkyApiToken token = new ZonkyApiToken("", "", 299); when(ctx2.hasEntity()).thenReturn(true); when(ctx2.getHeaders()).thenReturn(new MultivaluedMapImpl<>()); when(ctx2.getEntityStream()).thenReturn(c(token)); when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode)); when(ctx2.getStatus()).thenReturn(expectedCode); final RoboZonkyFilter filter = new AuthenticatedFilter(() -> token); filter.filter(ctx, ctx2); verify(ctx2, times(1)).setStatus(401); } }