@Override public Continuation<ClientRequest> apply(ClientRequest requestContext) { for (ClientRequestFilter filter : requestFilters) { try { filter.filter(requestContext); final Response abortResponse = requestContext.getAbortResponse(); if (abortResponse != null) { throw new AbortException(new ClientResponse(requestContext, abortResponse)); } } catch (IOException ex) { throw new ProcessingException(ex); } } return Continuation.of(requestContext, getDefaultNext()); } }
/** * {@inheritDoc} */ @Override public ClientResponse apply(ClientRequest jerseyRequest) { try { final HttpUriRequest apacheRequest = buildApacheRequest(jerseyRequest); final CloseableHttpResponse apacheResponse = client.execute(apacheRequest); final StatusLine statusLine = apacheResponse.getStatusLine(); final String reasonPhrase = Strings.nullToEmpty(statusLine.getReasonPhrase()); final Response.StatusType status = Statuses.from(statusLine.getStatusCode(), reasonPhrase); final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest); for (Header header : apacheResponse.getAllHeaders()) { jerseyResponse.getHeaders().computeIfAbsent(header.getName(), k -> new ArrayList<>()) .add(header.getValue()); } final HttpEntity httpEntity = apacheResponse.getEntity(); jerseyResponse.setEntityStream(httpEntity != null ? httpEntity.getContent() : new ByteArrayInputStream(new byte[0])); return jerseyResponse; } catch (Exception e) { throw new ProcessingException(e); } }
@SuppressWarnings("MagicNumber") private ClientResponse tryFollowRedirects(boolean followRedirects, ClientResponse response, ClientRequest request) { if (!followRedirects) { return response; } while (true) { switch (response.getStatus()) { case 303: case 302: case 307: request = new ClientRequest(request); request.setUri(response.getLocation()); if (response.getStatus() == 303) { request.setMethod("GET"); } response = apply(request); break; default: return response; } } }
private ClientResponse _apply(final ClientRequest request) { checkNotClosed(); final ClientResponse response = new ClientResponse(LOOPBACK_STATUS, request); // Headers. response.headers(HeaderUtils.asStringHeaders(request.getHeaders())); // Entity. if (request.hasEntity()) { response.setEntityStream(new ByteArrayInputStream(bufferEntity(request))); } return response; }
private ClientResponse createClientResponse(final ClientRequest clientRequest, final InMemoryResponseWriter responseWriter) { final ClientResponse clientResponse = new ClientResponse(responseWriter.getStatusInfo(), clientRequest); clientResponse.getHeaders().putAll(responseWriter.getHeaders()); clientResponse.setEntityStream(new ByteArrayInputStream(responseWriter.getEntity())); return clientResponse; }
private ClientResponse _apply(final ClientRequest request) throws IOException { final HttpURLConnection uc; uc = this.connectionFactory.getConnection(request.getUri().toURL()); uc.setDoInput(true); final String httpMethod = request.getMethod(); if (request.resolveProperty(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, setMethodWorkaround)) { setRequestMethodViaJreBugWorkaround(uc, httpMethod); } else { resolvedRequestUri = uc.getURL().toURI(); } catch (URISyntaxException e) { throw new ProcessingException(e); ClientResponse responseContext = new ClientResponse(status, request, resolvedRequestUri); responseContext.headers( uc.getHeaderFields() .entrySet() Map.Entry::getValue)) ); responseContext.setEntityStream(getInputStream(uc));
@Override public void completed(final ClientResponse response, final RequestScope scope) { if (responseFuture.isCancelled()) { response.close(); failed(new ProcessingException( new CancellationException(LocalizationMessages.ERROR_REQUEST_CANCELLED()))); return; } final T result; if (callbackParamClass == Response.class) { result = callbackParamClass.cast(new InboundJaxrsResponse(response, scope)); responseFuture.complete(result); callback.completed(result); } else if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { result = response.readEntity(new GenericType<T>(callbackParamType)); responseFuture.complete(result); callback.completed(result); } else { failed(convertToException(new InboundJaxrsResponse(response, scope))); } }
private <T> T translate(final ClientResponse response, final RequestScope scope, final GenericType<T> responseType) throws ProcessingException { if (responseType.getRawType() == Response.class) { //noinspection unchecked return (T) new InboundJaxrsResponse(response, scope); } if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { try { return response.readEntity(responseType); } catch (final ProcessingException ex) { throw new ResponseProcessingException( new InboundJaxrsResponse(response, scope), ex.getCause() != null ? ex.getCause() : ex); } catch (final WebApplicationException ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex); } catch (final Exception ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex); } } else { throw convertToException(new InboundJaxrsResponse(response, scope)); } }
@Override public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException { final HttpUriRequest request = getUriHttpRequest(clientRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request); clientRequest.resolveProperty(ApacheClientProperties.CREDENTIALS_PROVIDER, CredentialsProvider.class); if (credentialsProvider != null) { context.setCredentialsProvider(credentialsProvider); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName()); : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); final ClientResponse responseContext = new ClientResponse(status, clientRequest); final List<URI> redirectLocations = context.getRedirectLocations(); if (redirectLocations != null && !redirectLocations.isEmpty()) { responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1)); final MultivaluedMap<String, String> headers = responseContext.getHeaders(); for (final Header header : respHeaders) { final String headerName = header.getName(); responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response))); } catch (final IOException e) { LOGGER.log(Level.SEVERE, null, e); throw new ProcessingException(e);
@Override public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException { final Request jettyRequest = translateRequest(jerseyRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest); final ContentProvider entity = getBytesProvider(jerseyRequest); if (entity != null) { jettyRequest.content(entity); } try { final ContentResponse jettyResponse = jettyRequest.send(); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName()); final javax.ws.rs.core.Response.StatusType status = jettyResponse.getReason() == null ? Statuses.from(jettyResponse.getStatus()) : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason()); final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest); processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse); try { jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse)); } catch (final IOException e) { LOGGER.log(Level.SEVERE, null, e); } return jerseyResponse; } catch (final Exception e) { throw new ProcessingException(e); } }
@Override public ClientResponse apply(ClientRequest request) { RequestRunner<ClientRequest> requestRunner = new RequestRunner<>(retryComponents, headerSetter, request.getUri(), request.getMethod()); while ( requestRunner.shouldContinue() ) { URI uri = requestRunner.prepareRequest(request); request.setUri(uri); try { ClientResponse response = connector.apply(request); if ( requestRunner.isSuccessResponse(response.getStatus()) ) { return response; } } catch ( Exception e ) { if ( !requestRunner.shouldBeRetried(e) ) { throw new ProcessingException(e); } } } throw new ProcessingException("Retries expired: " + requestRunner.getOriginalUri()); }
this.headers(OutboundJaxrsResponse.from(response).getContext().getStringHeaders()); setEntityStream(entityStream);
@Override @SuppressWarnings("unchecked") public MultivaluedMap<String, Object> getMetadata() { final MultivaluedMap<String, ?> headers = context.getHeaders(); return (MultivaluedMap<String, Object>) headers; }
private static ClientResponse translateResponse(final ClientRequest jerseyRequest, final org.eclipse.jetty.client.api.Response jettyResponse, final NonBlockingInputStream entityStream) { final ClientResponse jerseyResponse = new ClientResponse(Statuses.from(jettyResponse.getStatus()), jerseyRequest); processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse); jerseyResponse.setEntityStream(entityStream); return jerseyResponse; }
@Override public int getStatus() { return context.getStatus(); }
@Override public void close() throws ProcessingException { try { context.close(); } finally { if (requestContext != null) { requestContext.release(); } } }
@Override public URI getLocation() { return context.getLocation(); }
@SuppressWarnings("SimplifiableIfStatement") @Override public boolean shouldBeRetried(URI uri, int retryCount, ClientResponse response, Throwable exception, Mode mode) { if (retryCount >= maxRetries) { LOG.warning(String.format("Retries exceeded. retryCount: %d - maxRetries: %d", retryCount, maxRetries)); return false; } if ((response != null) && (mode == Mode.RETRY_ALL)) { if (response.getStatusInfo().getFamily() == Response.Status.Family.SERVER_ERROR) { LOG.info(String.format("Retrying request due to Status %d. retryCount: %d - request: %s", response.getStatus(), retryCount, uri)); return true; } } return shouldBeRetried(uri, exception, mode); }
@Override public StatusType getStatusInfo() { return context.getStatusInfo(); }
@Override public boolean bufferEntity() throws ProcessingException { return context.bufferEntity(); }