List of usage examples for org.apache.http.impl.client RequestWrapper RequestWrapper
public RequestWrapper(final HttpRequest request) throws ProtocolException
From source file:com.apigee.sdk.apm.http.impl.client.cache.ConditionalRequestBuilder.java
/** * 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. * /*from www . j a v a 2 s. c om*/ * @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; }
From source file:org.deviceconnect.message.intent.impl.client.IntentRequestDirector.java
@Override public HttpResponse execute(final HttpHost target, final HttpRequest request, final HttpContext context) throws HttpException, IOException { RequestWrapper origWrapper = new RequestWrapper(request); HttpRoute origRoute = determineRoute(target, origWrapper, context); Object userToken = context.getAttribute(ClientContext.USER_TOKEN); long timeout = HttpConnectionParams.getConnectionTimeout(params); ClientConnectionRequest connRequest = connManager.requestConnection(origRoute, userToken); ManagedClientConnection managedConn; try {/* w ww . ja v a 2 s. com*/ managedConn = connRequest.getConnection(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new IOException(e); } if (request.getFirstHeader(HttpHeaders.HOST) == null) { request.setHeader(HttpHeaders.HOST, "org.deviceconnect.manager/.DConnectBroadcastReceiver"); } // requestExec.preProcess(request, httpProcessor, context); HttpResponse response = requestExec.execute(request, managedConn, context); // requestExec.postProcess(response, httpProcessor, context); return response; }
From source file:com.clarionmedia.infinitum.http.rest.impl.CachingEnabledRestfulClient.java
@Override public RestResponse executeGet(String uri) { try {/*from www . j ava 2 s. co m*/ RequestWrapper request = new RequestWrapper(new HttpGet(uri)); return executeRequest(new HashableHttpRequest(request)); } catch (ProtocolException e) { throw new InfinitumRuntimeException("Unable to execute request", e); } }
From source file:com.apigee.sdk.apm.http.impl.client.cache.ConditionalRequestBuilder.java
/** * 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/*w w w . j a va 2 s .com*/ * * @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; }
From source file:nl.esciencecenter.octopus.webservice.mac.MacSchemeTest.java
@Test public void testAuthenticateWrapped() throws URISyntaxException, ProtocolException { // set date to known value scheme.setDate(new Date(0)); // make nonce creation predictable // by mocking random generator Random rand = mock(Random.class); scheme.setRandom(rand);//from w w w. j av a 2 s. co m String id = "eyJzYWx0IjogIjU3MjY0NCIsICJleHBpcmVzIjogMTM4Njc3MjEwOC4yOTIyNTUsICJ1c2VyaWQiOiAiam9ibWFuYWdlciJ9KBJRMeTW2G9I6jlYwRj6j8koAek="; String key = "_B1YfcqEYpZxyTx_-411-QdBOSI="; MacCredential credentials = new MacCredential(id, key, new URI("http://localhost")); HttpGet request = new HttpGet("http://localhost"); RequestWrapper wrappedRequest = new RequestWrapper(request); Header header = scheme.authenticate(credentials, wrappedRequest, null); String headerValue = "MAC id=\"eyJzYWx0IjogIjU3MjY0NCIsICJleHBpcmVzIjogMTM4Njc3MjEwOC4yOTIyNTUsICJ1c2VyaWQiOiAiam9ibWFuYWdlciJ9KBJRMeTW2G9I6jlYwRj6j8koAek=\","; headerValue += "ts=\"0\","; headerValue += "nonce=\"0\","; headerValue += "mac=\"cDuU6wfugKOnoAaEEEUz22J/kTc=\""; assertEquals("Authorization", header.getName()); assertEquals(headerValue, header.getValue()); }
From source file:com.clarionmedia.infinitum.http.rest.impl.CachingEnabledRestfulClient.java
@Override public RestResponse executeGet(String uri, Map<String, String> headers) { HttpGet httpGet = new HttpGet(uri); for (Entry<String, String> header : headers.entrySet()) { httpGet.addHeader(header.getKey(), header.getValue()); }//from ww w .j a v a 2 s . c o m try { RequestWrapper request = new RequestWrapper(httpGet); return executeRequest(new HashableHttpRequest(request)); } catch (ProtocolException e) { throw new InfinitumRuntimeException("Unable to execute request", e); } }
From source file:com.google.android.net.GoogleHttpClient.java
/** * Wraps the request making it mutable./*from ww w. j av a2 s . c om*/ */ private static RequestWrapper wrapRequest(HttpUriRequest request) throws IOException { try { // We have to wrap it with the right type. Some code performs // instanceof checks. RequestWrapper wrapped; if (request instanceof HttpEntityEnclosingRequest) { wrapped = new EntityEnclosingRequestWrapper((HttpEntityEnclosingRequest) request); } else { wrapped = new RequestWrapper(request); } // Copy the headers from the original request into the wrapper. wrapped.resetHeaders(); return wrapped; } catch (ProtocolException e) { throw new ClientProtocolException(e); } }
From source file:com.clarionmedia.infinitum.http.rest.impl.CachingEnabledRestfulClient.java
@Override public RestResponse executePost(String uri, String messageBody, String contentType) { HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("content-type", contentType); try {//from w ww.jav a 2s .c o m httpPost.setEntity(new StringEntity(messageBody, "UTF-8")); } catch (UnsupportedEncodingException e) { mLogger.error("Unable to send POST request (could not encode message body)", e); return null; } try { RequestWrapper request = new RequestWrapper(httpPost); return executeRequest(new HashableHttpRequest(request)); } catch (ProtocolException e) { throw new InfinitumRuntimeException("Unable to execute request", e); } }
From source file:com.apigee.sdk.apm.http.impl.client.cache.ConditionalRequestBuilder.java
/** * 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.//from w w w . j a va2 s.c om * * @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; }
From source file:com.clarionmedia.infinitum.http.rest.impl.CachingEnabledRestfulClient.java
@Override public RestResponse executePost(String uri, String messageBody, String contentType, Map<String, String> headers) { HttpPost httpPost = new HttpPost(uri); for (Entry<String, String> header : headers.entrySet()) { httpPost.addHeader(header.getKey(), header.getValue()); }//from w w w .ja va 2s . c om httpPost.addHeader("content-type", contentType); try { httpPost.setEntity(new StringEntity(messageBody, "UTF-8")); } catch (UnsupportedEncodingException e) { mLogger.error("Unable to send POST request (could not encode message body)", e); return null; } try { RequestWrapper request = new RequestWrapper(httpPost); return executeRequest(new HashableHttpRequest(request)); } catch (ProtocolException e) { throw new InfinitumRuntimeException("Unable to execute request", e); } }