org.apache.http.impl.client.RequestWrapper Java Examples

The following examples show how to use org.apache.http.impl.client.RequestWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Http2Curl.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String inferUri(HttpRequest request) {
  String inferredUri = request.getRequestLine().getUri();
  if (!isValidUrl(inferredUri)) { // Missing schema and domain name
    String host = getHost(request);
    String inferredScheme = "http";
    if (host.endsWith(":443")) {
      inferredScheme = "https";
    } else if ((request instanceof RequestWrapper) || (request instanceof HttpRequestWrapper)) {
      if (getOriginalRequestUri(request).startsWith("https")) {
        // This is for original URL, so if during redirects we go out of HTTPs, this might be a wrong guess
        inferredScheme = "https";
      }
    }

    if ("CONNECT".equals(request.getRequestLine().getMethod())) {
      inferredUri = String.format("%s://%s", inferredScheme, host);
    } else {
      inferredUri =
          String.format("%s://%s/%s", inferredScheme, host, inferredUri)
              .replaceAll("(?<!http(s)?:)//", "/");
    }
  }
  return inferredUri;
}
 
Example #2
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
private void requestDidNotExpect100ContinueButResponseIsOne(
		HttpRequest request, HttpResponse response)
		throws ClientProtocolException {
	if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) {
		return;
	}

	if (!requestWasWrapped(request)) {
		return;
	}

	ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request);

	if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) {
		return;
	}

	if (originalRequestDidNotExpectContinue((RequestWrapper) request)) {
		throw new ClientProtocolException(
				"The incoming request did not contain a "
						+ "100-continue header, but the response was a Status 100, continue.");

	}
}
 
Example #3
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 6 votes vote down vote up
protected void rewriteRequestURI(
        final RequestWrapper request,
        final HttpRoute route) throws ProtocolException {
    try {

        URI uri = request.getURI();
        if (route.getProxyHost() != null && !route.isTunnelled()) {
            // Make sure the request URI is absolute
            if (!uri.isAbsolute()) {
                HttpHost target = route.getTargetHost();
                uri = URIUtils.rewriteURI(uri, target);
                request.setURI(uri);
            }
        } else {
            // Make sure the request URI is relative
            if (uri.isAbsolute()) {
                uri = URIUtils.rewriteURI(uri, null);
                request.setURI(uri);
            }
        }

    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid URI: "
        		+ request.getRequestLine().getUri(), ex);
    }
}
 
Example #4
Source File: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private HttpRequest upgradeRequestTo(HttpRequest request,
		ProtocolVersion version) throws ProtocolException {
	RequestWrapper newRequest = new RequestWrapper(request);
	newRequest.setProtocolVersion(version);

	return newRequest;
}
 
Example #5
Source File: ApacheHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
private void setRequestUri(HttpRequest request, URI uri) {
    if (request instanceof HttpRequestWrapper) {
        setRequestUri(((HttpRequestWrapper) request).getOriginal(), uri);
    } else if (request instanceof RequestWrapper) {
        setRequestUri(((RequestWrapper) request).getOriginal(), uri);
    } else {
        ((HttpRequestBase) request).setURI(uri);
    }
}
 
Example #6
Source File: ConditionalRequestBuilder.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * When a {@link HttpCacheEntry} does not exist for a specific
 * {@link HttpRequest} we attempt to see if an existing
 * {@link HttpCacheEntry} is appropriate by building a conditional
 * {@link HttpRequest} using the variants' ETag values. If no such values
 * exist, the request is unmodified
 * 
 * @param request
 *            the original request from the caller
 * @param cacheEntry
 *            the entry that needs to be revalidated
 * @return the wrapped request
 * @throws ProtocolException
 *             when I am unable to build a new origin request.
 */
public HttpRequest buildConditionalRequestFromVariants(HttpRequest request,
		Set<HttpCacheEntry> variantEntries) throws ProtocolException {
	RequestWrapper wrapperRequest = new RequestWrapper(request);
	wrapperRequest.resetHeaders();

	// we do not support partial content so all etags are used
	StringBuilder etags = new StringBuilder();
	boolean first = true;
	for (HttpCacheEntry entry : variantEntries) {
		Header etagHeader = entry.getFirstHeader(HeaderConstants.ETAG);
		if (etagHeader != null) {
			if (first) {
				etags.append(etagHeader.getValue());
				first = false;
			} else {
				etags.append(",").append(etagHeader.getValue());
			}
		}
	}
	// if first is still true than no variants had a cache entry, return
	// unmodified wrapped request
	if (first) {
		return wrapperRequest;
	}

	wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH,
			etags.toString());

	return wrapperRequest;
}
 
Example #7
Source File: ConditionalRequestBuilder.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
 * to an {@link HttpRequest} we will attempt to revalidate the entry with
 * the origin. Build the origin {@link HttpRequest} here and return it.
 * 
 * @param request
 *            the original request from the caller
 * @param cacheEntry
 *            the entry that needs to be revalidated
 * @return the wrapped request
 * @throws ProtocolException
 *             when I am unable to build a new origin request.
 */
public HttpRequest buildConditionalRequest(HttpRequest request,
		HttpCacheEntry cacheEntry) throws ProtocolException {
	RequestWrapper wrapperRequest = new RequestWrapper(request);
	wrapperRequest.resetHeaders();
	Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
	if (eTag != null) {
		wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH,
				eTag.getValue());
	}
	Header lastModified = cacheEntry
			.getFirstHeader(HeaderConstants.LAST_MODIFIED);
	if (lastModified != null) {
		wrapperRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE,
				lastModified.getValue());
	}
	boolean mustRevalidate = false;
	for (Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
		for (HeaderElement elt : h.getElements()) {
			if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE
					.equalsIgnoreCase(elt.getName())
					|| HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE
							.equalsIgnoreCase(elt.getName())) {
				mustRevalidate = true;
				break;
			}
		}
	}
	if (mustRevalidate) {
		wrapperRequest.addHeader("Cache-Control", "max-age=0");
	}
	return wrapperRequest;

}
 
Example #8
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private boolean originalRequestDidNotExpectContinue(RequestWrapper request) {

		try {
			HttpEntityEnclosingRequest original = (HttpEntityEnclosingRequest) request
					.getOriginal();

			return !original.expectContinue();
		} catch (ClassCastException ex) {
			return false;
		}
	}
 
Example #9
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private void transferEncodingIsNotReturnedTo1_0Client(HttpRequest request,
		HttpResponse response) {
	if (!requestWasWrapped(request)) {
		return;
	}

	ProtocolVersion originalProtocol = getOriginalRequestProtocol((RequestWrapper) request);

	if (originalProtocol.compareToVersion(HttpVersion.HTTP_1_1) >= 0) {
		return;
	}

	removeResponseTransferEncoding(response);
}
 
Example #10
Source File: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private HttpRequest downgradeRequestTo(HttpRequest request,
		ProtocolVersion version) throws ProtocolException {
	RequestWrapper newRequest = new RequestWrapper(request);
	newRequest.setProtocolVersion(version);

	return newRequest;
}
 
Example #11
Source File: SolrHttpRequestRetryHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected boolean requestIsAborted(final HttpRequest request) {
  HttpRequest req = request;
  if (request instanceof RequestWrapper) { // does not forward request to original
    req = ((RequestWrapper) request).getOriginal();
  }
  return (req instanceof HttpUriRequest && ((HttpUriRequest) req).isAborted());
}
 
Example #12
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 5 votes vote down vote up
private RequestWrapper wrapRequest(
        final HttpRequest request) throws ProtocolException {
    if (request instanceof HttpEntityEnclosingRequest) {
        return new EntityEnclosingRequestWrapper(
                (HttpEntityEnclosingRequest) request);
    } else {
        return new RequestWrapper(
                request);
    }
}
 
Example #13
Source File: DefaultHttpRequestRetryHandler.java    From android-lite-http with Apache License 2.0 5 votes vote down vote up
@Deprecated
protected boolean requestIsAborted(final HttpRequest request) {
    if (request == null) return false;
    HttpRequest req = request;
    if (request instanceof RequestWrapper) { // does not forward request to
        // original
        req = ((RequestWrapper) request).getOriginal();
    }
    return (req instanceof HttpUriRequest && ((HttpUriRequest) req).isAborted());
}
 
Example #14
Source File: Http2Curl.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static String getOriginalRequestUri(HttpRequest request) {
  if (request instanceof HttpRequestWrapper) {
    return ((HttpRequestWrapper) request).getOriginal().getRequestLine().getUri();
  } else if (request instanceof RequestWrapper) {
    return ((RequestWrapper) request).getOriginal().getRequestLine().getUri();
  } else {
    throw new IllegalArgumentException("Unsupported request class type: " + request.getClass());
  }
}
 
Example #15
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 4 votes vote down vote up
/**
 * Execute request and retry in case of a recoverable I/O failure
 */
private HttpResponse tryExecute(
        final RoutedRequest req, final HttpContext context) throws HttpException, IOException {
    RequestWrapper wrapper = req.getRequest();
    HttpRoute route = req.getRoute();
    HttpResponse response = null;

    Exception retryReason = null;
    for (;;) {
        // Increment total exec count (with redirects)
        execCount++;
        // Increment exec count for this particular request
        wrapper.incrementExecCount();
        if (!wrapper.isRepeatable()) {
        	
            if (retryReason != null) {
                throw new NonRepeatableRequestException("Cannot retry request " +
                    "with a non-repeatable request entity.  The cause lists the " +
                    "reason the original request failed." + retryReason);
            } else {
                throw new NonRepeatableRequestException("Cannot retry request " +
                        "with a non-repeatable request entity.");
            }
        }

        try {
            if (!managedConn.isOpen()) {
                // If we have a direct route to the target host
                // just re-open connection and re-try the request
                if (!route.isTunnelled()) {
                	if (DEBUG) {
                		Logger.debug("Reopening the direct connection.");
                	}
                    managedConn.open(route, context, params);
                } else {
                    // otherwise give up
                	if (DEBUG) {
                		Logger.debug("Proxied connection. Need to start over.");
                	}
                    break;
                }
            }

            response = requestExec.execute(wrapper, managedConn, context);
            break;

        } catch (IOException ex) {
            try {
                managedConn.close();
            } catch (IOException ignore) {
            }
            if (retryHandler.retryRequest(ex, wrapper.getExecCount(), context)) {
                retryReason = ex;
            } else {
                throw ex;
            }
        }
    }
    return response;
}
 
Example #16
Source File: AndroidHttpClient.java    From android-download-manager with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken)
		throws IOException {
	StringBuilder builder = new StringBuilder();

	builder.append("curl ");

	for (Header header : request.getAllHeaders()) {
		if (!logAuthToken
				&& (header.getName().equals("Authorization") || header
						.getName().equals("Cookie"))) {
			continue;
		}
		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();
}
 
Example #17
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
private ProtocolVersion getOriginalRequestProtocol(RequestWrapper request) {
	return request.getOriginal().getProtocolVersion();
}
 
Example #18
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
private boolean requestWasWrapped(HttpRequest request) {
	return request instanceof RequestWrapper;
}
 
Example #19
Source File: AndroidHttpClient.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header: request.getAllHeaders()) {
        if (!logAuthToken
                && (header.getName().equals("Authorization") ||
                    header.getName().equals("Cookie"))) {
            continue;
        }
        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();
}
 
Example #20
Source File: AndroidHttpClient.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header: request.getAllHeaders()) {
        if (!logAuthToken
                && (header.getName().equals("Authorization") ||
                    header.getName().equals("Cookie"))) {
            continue;
        }
        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();
}
 
Example #21
Source File: ConditionalRequestBuilder.java    From apigee-android-sdk with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a request to unconditionally validate a cache entry with the
 * origin. In certain cases (due to multiple intervening caches) our cache
 * may actually receive a response to a normal conditional validation where
 * the Date header is actually older than that of our current cache entry.
 * In this case, the protocol recommendation is to retry the validation and
 * force syncup with the origin.
 * 
 * @param request
 *            client request we are trying to satisfy
 * @param entry
 *            existing cache entry we are trying to validate
 * @return an unconditional validation request
 * @throws ProtocolException
 */
public HttpRequest buildUnconditionalRequest(HttpRequest request,
		HttpCacheEntry entry) throws ProtocolException {
	RequestWrapper wrapped = new RequestWrapper(request);
	wrapped.resetHeaders();
	wrapped.addHeader("Cache-Control", "no-cache");
	wrapped.addHeader("Pragma", "no-cache");
	wrapped.removeHeaders("If-Range");
	wrapped.removeHeaders("If-Match");
	wrapped.removeHeaders("If-None-Match");
	wrapped.removeHeaders("If-Unmodified-Since");
	wrapped.removeHeaders("If-Modified-Since");
	return wrapped;
}