public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { if (command.getFailureCount() > retryCountLimit) return false; if (response.getStatusCode() == 400 && command.getCurrentRequest().getMethod().equals("PUT") && command.getCurrentRequest().getEndpoint().getPath().indexOf("sandboxes") != -1) { if (response.getPayload() != null) { String error = new String(closeClientButKeepContentStream(response)); if (error != null && error.indexOf("was not uploaded") != -1) { return backoffLimitedRetryHandler.shouldRetryRequest(command, response); } } } return false; }
public void handleError(HttpCommand command, HttpResponse from) { String content; try { content = from.getPayload() != null ? Strings2.toStringAndClose(from.getPayload().openStream()) : null; command.setException(new HttpResponseException(command, from, content)); } catch (IOException e) { command.setException(new HttpResponseException(command, from)); } finally { releasePayload(from); } } }
private String createStringToSign(HttpRequest request, long expires) { utils.logRequest(signatureLog, request, ">>"); StringBuilder buffer = new StringBuilder(); buffer.append(request.getMethod()).append("\n"); buffer.append(Strings.nullToEmpty(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_MD5))).append("\n"); buffer.append(Strings.nullToEmpty(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE))).append("\n"); buffer.append(String.valueOf(expires)).append("\n"); // TODO: extension headers buffer.append(request.getEndpoint().getPath()); return buffer.toString(); } }
@Override public Boolean createOrPropagate(Throwable t) throws Exception { if (contains404(checkNotNull(t, "throwable"))) return true; if (returnValueOnCodeOrNull(t, false, equalTo(409)) != null) return false; throw propagate(t); } }
public HTTPRequest filterLogAndConvertRe(HttpRequest request) { for (HttpRequestFilter filter : request.getFilters()) { request = filter.filter(request); } checkRequestHasContentLengthOrChunkedEncoding(request, "After filtering, the request has neither chunked encoding nor content length: " + request); logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine()); wirePayloadIfEnabled(wire, request); HTTPRequest nativeRequest = convertToGaeRequest.apply(request); utils.logRequest(headerLog, request, ">>"); return nativeRequest; }
@Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { boolean retry = false; // default try { byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; if (response.getStatusCode() == 401 || (message != null && message.indexOf(IP_MISMATCH) != -1)) { logger.debug("invalidating session"); sessionCache.invalidateAll(); retry = super.shouldRetryRequest(command, response); } return retry; } finally { releasePayload(response); } }
public HttpRequest filter(HttpRequest input) throws HttpException { HttpRequest request = input.toBuilder().endpoint(input.getEndpoint().toString().replace("%3F", "?")).build(); String contentHash = hashBody(request.getPayload()); Multimap<String, String> headers = ArrayListMultimap.create(); headers.put("X-Ops-Content-Hash", contentHash); String timestamp = timeStampProvider.get(); String toSign = createStringToSign(request.getMethod(), hashPath(request.getEndpoint().getPath()), contentHash, timestamp); headers.put("X-Ops-Userid", creds.get().identity); headers.put("X-Ops-Sign", SIGNING_DESCRIPTION); request = calculateAndReplaceAuthorizationHeaders(request, toSign); headers.put("X-Ops-Timestamp", timestamp); utils.logRequest(signatureLog, request, "<<"); return request.toBuilder().replaceHeaders(headers).build(); }
public MutableContentMetadata apply(BlobMetadata base) { MutableBlobMetadataImpl to = new MutableBlobMetadataImpl(); HttpUtils.copy(base.getContentMetadata(), to.getContentMetadata()); return new DelegatingMutableContentMetadata(base.getUri(), base.getName(), base.getUri() != null ? base.getUri() .getPath() : null, to.getContentMetadata()); }
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: break; case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; } command.setException(exception); } }
@Override public void handleError(HttpCommand command, HttpResponse response) { byte[] data = closeClientButKeepContentStream(response); : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); Exception exception = new HttpResponseException(command, response, message); if (response.getPayload() != null && response.getPayload().getContentMetadata().getContentType().startsWith(VCloudDirectorMediaType.ERROR)) { try { Error error = JAXB.unmarshal(new ByteArrayInputStream(data), Error.class); command.setException(exception);
@Override public T apply(final HttpResponse from) { InputStream xml = from.getPayload().getInput(); try { return apply(xml); } catch (Exception e) { StringBuilder message = new StringBuilder(); message.append("Error parsing input"); logger.error(e, message.toString()); throw new HttpResponseException(message.toString() + "\n" + from, null, from, e); } finally { releasePayload(from); } }
public HttpRequest filter(HttpRequest request) throws HttpException { checkNotNull(request, "request must be present"); Multimap<String, String> decodedParams = queryParser().apply(request.getEndpoint().getRawQuery()); addSigningParams(decodedParams); String stringToSign = createStringToSign(request, decodedParams); String signature = sign(stringToSign); addSignature(decodedParams, signature); request = request.toBuilder().endpoint(uriBuilder(request.getEndpoint()).query(decodedParams).build()).build(); utils.logRequest(signatureLog, request, "<<"); return request; }
@Override public URI apply(HttpResponse from) { if (from.getStatusCode() == 201 && request.getPayload().getContentMetadata().getContentLength() == 0) { releasePayload(from); return null; } return super.apply(from); } }
public MutableBlobMetadata apply(HttpResponse from) { checkNotNull(from, "request"); checkState(name != null, "name must be initialized by now"); MutableBlobMetadata to = metadataFactory.get(); to.setName(name); to.setUri(endpoint); if (from.getPayload() != null) HttpUtils.copy(from.getPayload().getContentMetadata(), to.getContentMetadata()); addETagTo(from, to); parseLastModifiedOrThrowException(from, to); addUserMetadataTo(from, to); return to; }
@Override public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { if (response.getStatusCode() == 400 || response.getStatusCode() == 403 || response.getStatusCode() == 409) { // Content can be null in the case of HEAD requests if (response.getPayload() != null) { closeClientButKeepContentStream(response); AWSError error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response); if (error != null) { return shouldRetryRequestOnError(command, response, error); } } } return false; }
/** * parses the http response body to create a new {@code <T>}. */ public T apply(HttpResponse from) { try { InputStream gson = from.getPayload().openStream(); return apply(gson); } catch (Exception e) { StringBuilder message = new StringBuilder(); message.append("Error parsing input: "); message.append(e.getMessage()); logger.error(e, message.toString()); throw new HttpResponseException(message.toString() + "\n" + from, null, from, e); } finally { releasePayload(from); } }
private HttpRequest createSoapRequest(HttpRequest request) { Payload oldPayload = request.getPayload(); ContentMetadata oldMetadata = oldPayload.getContentMetadata(); String body = SOAP_PREFIX.concat(oldPayload.getRawContent().toString()).concat(SOAP_SUFFIX); Payload newPayload = Payloads.newStringPayload(body); HttpUtils.copy(oldMetadata, newPayload.getContentMetadata()); newPayload.getContentMetadata().setContentLength(Long.valueOf(body.getBytes().length)); // resize, add prefix/suffix length return request.toBuilder().payload(newPayload).build(); }
@Test(timeOut = TEST_SAFE_TIMEOUT) public void testDoNotRetryIfNotReplayable() { // InputStream payloads are not replayable Payload payload = newInputStreamPayload(new ByteArrayInputStream(new byte[0])); HttpCommand command = new HttpCommand(HttpRequest.builder().method("GET").endpoint("http://localhost") .payload(payload).build()); HttpResponse response = HttpResponse.builder().statusCode(429).build(); try { assertFalse(rateLimitRetryHandler.shouldRetryRequest(command, response)); } finally { releasePayload(command.getCurrentRequest()); } }
/** * parses the http response headers to create a new {@link AuthenticationResponse} object. */ public AuthenticationResponse apply(HttpResponse from) { releasePayload(from); Builder<String, URI> builder = ImmutableMap.builder(); for (Entry<String, String> entry : from.getHeaders().entries()) { if (entry.getKey().endsWith(URL_SUFFIX)) builder.put(entry.getKey(), getURI(entry.getValue())); } AuthenticationResponse response = new AuthenticationResponse(checkNotNull(from.getFirstHeaderOrNull(AUTH_TOKEN), AUTH_TOKEN), builder.build()); logger.debug("will connect to: ", response); return response; }
public String parseMessage(HttpResponse response) { if (response.getPayload() == null) return null; try { return toStringAndClose(response.getPayload().openStream()); } catch (IOException e) { throw new RuntimeException(e); } finally { releasePayload(response); } } }