/** * Returns true if a failed request should be retried. * * @param originalRequest The original service request that is being executed. * @param method The current HTTP method being executed. * @param exception The client/service exception from the failed request. * @param requestCount The number of times the current request has been attempted. * @return True if the failed request should be retried. */ private boolean shouldRetry( AmazonWebServiceRequest originalRequest, HttpRequestBase method, AmazonClientException exception, int requestCount, RetryPolicy retryPolicy) { final int retries = requestCount - 1; int maxErrorRetry = config.getMaxErrorRetry(); // We should use the maxErrorRetry in // the RetryPolicy if either the user has not explicitly set it in // ClientConfiguration, or the RetryPolicy is configured to take // higher precedence. if (maxErrorRetry < 0 || !retryPolicy.isMaxErrorRetryInClientConfigHonored()) { maxErrorRetry = retryPolicy.getMaxErrorRetry(); } // Immediately fails when it has exceeds the max retry count. if (retries >= maxErrorRetry) return false; // Never retry on requests containing non-repeatable entity if (method instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) method).getEntity(); if (entity != null && !entity.isRepeatable()) { if (log.isDebugEnabled()) { log.debug("Entity not repeatable"); } return false; } } // Pass all the context information to the RetryCondition and let it // decide whether it should be retried. return retryPolicy.getRetryCondition().shouldRetry(originalRequest, exception, retries); }
static void enhanceEntity(final HttpEntityEnclosingRequest request) { final HttpEntity entity = request.getEntity(); if (entity != null && !entity.isRepeatable() && !isEnhanced(entity)) { final HttpEntity proxy = (HttpEntity) Proxy.newProxyInstance( HttpEntity.class.getClassLoader(), new Class<?>[] {HttpEntity.class}, new RequestEntityExecHandler(entity)); request.setEntity(proxy); } }
static boolean isRepeatable(final HttpRequest request) { if (request instanceof HttpEntityEnclosingRequest) { final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); if (entity != null) { if (isEnhanced(entity)) { final RequestEntityExecHandler handler = (RequestEntityExecHandler) Proxy.getInvocationHandler(entity); if (!handler.isConsumed()) { return true; } } return entity.isRepeatable(); } } return true; }
/** * Returns true if a failed request should be retried. * * @param method The current HTTP method being executed. * @param exception The exception from the failed request. * @param retries The number of times the current request has been attempted. * @return True if the failed request should be retried. */ private boolean shouldRetry(HttpRequestBase method, Exception exception, int retries) { if (retries >= config.getMaxErrorRetry()) return false; if (method instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) method).getEntity(); if (entity != null && !entity.isRepeatable()) { if (log.isDebugEnabled()) { log.debug("Entity not repeatable"); } return false; } } if (exception instanceof IOException) { if (log.isDebugEnabled()) { log.debug("Retrying on " + exception.getClass().getName() + ": " + exception.getMessage()); } return true; } if (exception instanceof AmazonServiceException) { AmazonServiceException ase = (AmazonServiceException) exception; /* * For 500 internal server errors and 503 service * unavailable errors, we want to retry, but we need to use * an exponential back-off strategy so that we don't overload * a server with a flood of retries. If we've surpassed our * retry limit we handle the error response as a non-retryable * error and go ahead and throw it back to the user as an exception. */ if (ase.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR || ase.getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { return true; } /* * Throttling is reported as a 400 error from newer services. To try * and smooth out an occasional throttling error, we'll pause and * retry, hoping that the pause is long enough for the request to * get through the next time. */ if (isThrottlingException(ase)) return true; } return false; }
/** Generates a cURL command equivalent to the given request. */ private static String toCurl(HttpUriRequest request) throws IOException { StringBuilder builder = new StringBuilder(); builder.append("curl "); for (Header header : request.getAllHeaders()) { builder.append("--header \""); builder.append(header.toString().trim()); builder.append("\" "); } URI uri = request.getURI(); // If this is a wrapped request, use the URI from the original // request instead. getURI() on the wrapper seems to return a // relative URI. We want an absolute URI. if (request instanceof RequestWrapper) { HttpRequest original = ((RequestWrapper) request).getOriginal(); if (original instanceof HttpUriRequest) { uri = ((HttpUriRequest) original).getURI(); } } builder.append("\""); builder.append(uri); builder.append("\""); if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity = entityRequest.getEntity(); if (entity != null && entity.isRepeatable()) { if (entity.getContentLength() < 1024) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); entity.writeTo(stream); String entityString = stream.toString(); // TODO: Check the content type, too. builder.append(" --data-ascii \"").append(entityString).append("\""); } else { builder.append(" [TOO MUCH DATA TO INCLUDE]"); } } } return builder.toString(); }
protected void processAndAddLastClientInvocation( GenericClient theClient, ResultType theResultType, ModelMap theModelMap, long theLatency, String outcomeDescription, CaptureInterceptor theInterceptor, HomeRequest theRequest) { try { HttpRequestBase lastRequest = theInterceptor.getLastRequest(); HttpResponse lastResponse = theInterceptor.getLastResponse(); String requestBody = null; String requestUrl = lastRequest != null ? lastRequest.getURI().toASCIIString() : null; String action = lastRequest != null ? lastRequest.getMethod() : null; String resultStatus = lastResponse != null ? lastResponse.getStatusLine().toString() : null; String resultBody = StringUtils.defaultString(theInterceptor.getLastResponseBody()); if (lastRequest instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) lastRequest).getEntity(); if (entity.isRepeatable()) { requestBody = IOUtils.toString(entity.getContent()); } } ContentType ct = lastResponse != null ? ContentType.get(lastResponse.getEntity()) : null; String mimeType = ct != null ? ct.getMimeType() : null; EncodingEnum ctEnum = EncodingEnum.forContentType(mimeType); String narrativeString = ""; StringBuilder resultDescription = new StringBuilder(); Bundle bundle = null; IBaseResource riBundle = null; FhirContext context = getContext(theRequest); if (ctEnum == null) { resultDescription.append("Non-FHIR response"); } else { switch (ctEnum) { case JSON: if (theResultType == ResultType.RESOURCE) { narrativeString = parseNarrative(theRequest, ctEnum, resultBody); resultDescription.append("JSON resource"); } else if (theResultType == ResultType.BUNDLE) { resultDescription.append("JSON bundle"); if (context.getVersion().getVersion().isRi()) { riBundle = context.newJsonParser().parseResource(resultBody); } else { bundle = context.newJsonParser().parseBundle(resultBody); } } break; case XML: default: if (theResultType == ResultType.RESOURCE) { narrativeString = parseNarrative(theRequest, ctEnum, resultBody); resultDescription.append("XML resource"); } else if (theResultType == ResultType.BUNDLE) { resultDescription.append("XML bundle"); if (context.getVersion().getVersion().isRi()) { riBundle = context.newXmlParser().parseResource(resultBody); } else { bundle = context.newXmlParser().parseBundle(resultBody); } } break; } } resultDescription.append(" (").append(resultBody.length() + " bytes)"); Header[] requestHeaders = lastRequest != null ? applyHeaderFilters(lastRequest.getAllHeaders()) : new Header[0]; Header[] responseHeaders = lastResponse != null ? applyHeaderFilters(lastResponse.getAllHeaders()) : new Header[0]; theModelMap.put("outcomeDescription", outcomeDescription); theModelMap.put("resultDescription", resultDescription.toString()); theModelMap.put("action", action); theModelMap.put("bundle", bundle); theModelMap.put("riBundle", riBundle); theModelMap.put("resultStatus", resultStatus); theModelMap.put("requestUrl", requestUrl); theModelMap.put("requestUrlText", formatUrl(theClient.getUrlBase(), requestUrl)); String requestBodyText = format(requestBody, ctEnum); theModelMap.put("requestBody", requestBodyText); String resultBodyText = format(resultBody, ctEnum); theModelMap.put("resultBody", resultBodyText); theModelMap.put("resultBodyIsLong", resultBodyText.length() > 1000); theModelMap.put("requestHeaders", requestHeaders); theModelMap.put("responseHeaders", responseHeaders); theModelMap.put("narrative", narrativeString); theModelMap.put("latencyMs", theLatency); } catch (Exception e) { ourLog.error("Failure during processing", e); theModelMap.put("errorMsg", "Error during processing: " + e.getMessage()); } }
public boolean isRepeatable() { return delegate.isRepeatable(); }
<T> T invokeClient( FhirContext theContext, IClientResponseHandler<T> binding, BaseHttpClientInvocation clientInvocation, EncodingEnum theEncoding, Boolean thePrettyPrint, boolean theLogRequestAndResponse) { if (!myDontValidateConformance) { myFactory.validateServerBaseIfConfiguredToDoSo(myUrlBase, myClient, this); } // TODO: handle non 2xx status codes by throwing the correct exception, // and ensure it's passed upwards HttpRequestBase httpRequest; HttpResponse response; try { Map<String, List<String>> params = createExtraParams(); if (theEncoding == EncodingEnum.XML) { params.put(Constants.PARAM_FORMAT, Collections.singletonList("xml")); } else if (theEncoding == EncodingEnum.JSON) { params.put(Constants.PARAM_FORMAT, Collections.singletonList("json")); } if (thePrettyPrint == Boolean.TRUE) { params.put( Constants.PARAM_PRETTY, Collections.singletonList(Constants.PARAM_PRETTY_VALUE_TRUE)); } EncodingEnum encoding = getEncoding(); if (theEncoding != null) { encoding = theEncoding; } httpRequest = clientInvocation.asHttpRequest(myUrlBase, params, encoding, thePrettyPrint); if (theLogRequestAndResponse) { ourLog.info("Client invoking: {}", httpRequest); if (httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity(); if (entity.isRepeatable()) { String content = IOUtils.toString(entity.getContent()); ourLog.info("Client request body: {}", content); } } } for (IClientInterceptor nextInterceptor : myInterceptors) { nextInterceptor.interceptRequest(httpRequest); } response = myClient.execute(httpRequest); for (IClientInterceptor nextInterceptor : myInterceptors) { nextInterceptor.interceptResponse(response); } } catch (DataFormatException e) { throw new FhirClientConnectionException(e); } catch (IOException e) { throw new FhirClientConnectionException(e); } try { String mimeType; if (Constants.STATUS_HTTP_204_NO_CONTENT == response.getStatusLine().getStatusCode()) { mimeType = null; } else { ContentType ct = ContentType.get(response.getEntity()); mimeType = ct != null ? ct.getMimeType() : null; } Map<String, List<String>> headers = new HashMap<String, List<String>>(); if (response.getAllHeaders() != null) { for (Header next : response.getAllHeaders()) { String name = next.getName().toLowerCase(); List<String> list = headers.get(name); if (list == null) { list = new ArrayList<String>(); headers.put(name, list); } list.add(next.getValue()); } } if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) { String body = null; Reader reader = null; try { reader = createReaderFromResponse(response); body = IOUtils.toString(reader); } catch (Exception e) { ourLog.debug("Failed to read input stream", e); } finally { IOUtils.closeQuietly(reader); } String message = "HTTP " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase(); BaseOperationOutcome oo = null; if (Constants.CT_TEXT.equals(mimeType)) { message = message + ": " + body; } else { EncodingEnum enc = EncodingEnum.forContentType(mimeType); if (enc != null) { IParser p = enc.newParser(theContext); try { // TODO: handle if something other than OO comes back oo = (BaseOperationOutcome) p.parseResource(body); if (oo.getIssueFirstRep().getDetailsElement().isEmpty() == false) { message = message + ": " + oo.getIssueFirstRep().getDetailsElement().getValue(); } } catch (Exception e) { ourLog.debug("Failed to process OperationOutcome response"); } } } keepResponseAndLogIt(theLogRequestAndResponse, response, body); BaseServerResponseException exception = BaseServerResponseException.newInstance( response.getStatusLine().getStatusCode(), message); exception.setOperationOutcome(oo); if (body != null) { exception.setResponseBody(body); } throw exception; } if (binding instanceof IClientResponseHandlerHandlesBinary) { IClientResponseHandlerHandlesBinary<T> handlesBinary = (IClientResponseHandlerHandlesBinary<T>) binding; if (handlesBinary.isBinary()) { InputStream reader = response.getEntity().getContent(); try { if (ourLog.isTraceEnabled() || myKeepResponses || theLogRequestAndResponse) { byte[] responseBytes = IOUtils.toByteArray(reader); if (myKeepResponses) { myLastResponse = response; myLastResponseBody = null; } String message = "HTTP " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase(); if (theLogRequestAndResponse) { ourLog.info("Client response: {} - {} bytes", message, responseBytes.length); } else { ourLog.trace("Client response: {} - {} bytes", message, responseBytes.length); } reader = new ByteArrayInputStream(responseBytes); } return handlesBinary.invokeClient( mimeType, reader, response.getStatusLine().getStatusCode(), headers); } finally { IOUtils.closeQuietly(reader); } } } Reader reader = createReaderFromResponse(response); if (ourLog.isTraceEnabled() || myKeepResponses || theLogRequestAndResponse) { String responseString = IOUtils.toString(reader); keepResponseAndLogIt(theLogRequestAndResponse, response, responseString); reader = new StringReader(responseString); } try { return binding.invokeClient( mimeType, reader, response.getStatusLine().getStatusCode(), headers); } finally { IOUtils.closeQuietly(reader); } } catch (IllegalStateException e) { throw new FhirClientConnectionException(e); } catch (IOException e) { throw new FhirClientConnectionException(e); } finally { if (response instanceof CloseableHttpResponse) { try { ((CloseableHttpResponse) response).close(); } catch (IOException e) { ourLog.debug("Failed to close response", e); } } } }
@Override public boolean isRepeatable() { return src.isRepeatable(); }
@Override public boolean isRepeatable() { return httpEntity.isRepeatable(); }