List of usage examples for org.apache.http.protocol ExecutionContext HTTP_REQUEST
String HTTP_REQUEST
To view the source code for org.apache.http.protocol ExecutionContext HTTP_REQUEST.
Click Source Link
From source file:com.seajas.search.contender.service.modifier.SourceElementModifierService.java
/** * Retrieve the result content for the given URI. * * @param encodingOverride/*from w ww.j av a 2 s .co m*/ * @param resultHeaders * @param userAgent * @return Content */ public Content getContent(final URI resultUri, final String encodingOverride, final Map<String, String> resultHeaders, final String userAgent) { URI uriAfterRedirects = null; // Retrieve the content Header contentType = null; try { InputStream inputStream; // Local file streams can only be read if the parent scheme is also local if (!resultUri.getScheme().equalsIgnoreCase("file")) { HttpGet method = new HttpGet(resultUri); if (resultHeaders != null) for (Entry<String, String> resultHeader : resultHeaders.entrySet()) method.setHeader(new BasicHeader(resultHeader.getKey(), resultHeader.getValue())); if (userAgent != null) method.setHeader(CoreProtocolPNames.USER_AGENT, userAgent); HttpContext context = new BasicHttpContext(); SizeRestrictedHttpResponse response = httpClient.execute(method, new SizeRestrictedResponseHandler(maximumContentLength, resultUri), context); if (response != null) { HttpUriRequest currentRequest = (HttpUriRequest) context .getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); try { uriAfterRedirects = new URI(currentHost.toURI()).resolve(currentRequest.getURI()); } catch (URISyntaxException e) { logger.error(String.format("Final URI '%s' is mysteriously invalid", currentHost.toURI()), e); } inputStream = new ByteArrayInputStream(response.getResponse()); contentType = response.getContentType(); } else return null; } else inputStream = new FileInputStream(resultUri.getPath()); // Convert the stream to a reset-able one ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(inputStream, outputStream); inputStream.close(); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); outputStream.close(); // Now determine the content type and create a reader in case of structured content Metadata metadata = new Metadata(); if (encodingOverride != null && contentType != null && StringUtils.hasText(contentType.getValue())) { MediaType type = MediaType.parse(contentType.getValue()); metadata.add(HttpHeaders.CONTENT_TYPE, type.getType() + "/" + type.getSubtype() + "; charset=" + encodingOverride); } else if (contentType != null && StringUtils.hasText(contentType.getValue())) metadata.add(HttpHeaders.CONTENT_TYPE, contentType.getValue()); else if (encodingOverride != null) metadata.add(HttpHeaders.CONTENT_ENCODING, encodingOverride); MediaType mediaType = autoDetectParser.getDetector().detect(inputStream, metadata); return new Content(new ByteArrayInputStream(outputStream.toByteArray()), mediaType.getBaseType() + "/" + mediaType.getSubtype(), contentType != null ? contentType.getValue() : null, uriAfterRedirects != null ? uriAfterRedirects : resultUri); } catch (IOException e) { logger.error("Could not retrieve the given URL", e); return null; } }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void test302CachedRedirectTarget() throws Exception { do {/*from w w w. jav a 2s .c om*/ HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); redirect.setHeader("Cache-Control", "public,max-age=3600"); responses.add(redirect); BasicHttpResponse doc = new BasicHttpResponse(_200); doc.setHeader("Cache-Control", "public,max-age=3600"); responses.add(doc); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }); } while (false); do { HttpContext localContext = new BasicHttpContext(); HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); redirect.setHeader("Cache-Control", "public,max-age=3600"); responses.add(redirect); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_302.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }, localContext); HttpHost host = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); URI root = new URI(host.getSchemeName(), null, host.getHostName(), -1, "/", null, null); assertEquals("http://example.com/302", root.resolve(req.getURI()).toASCIIString()); } while (false); }
From source file:com.akop.bach.parser.Parser.java
protected String getResponse(HttpUriRequest request, List<NameValuePair> inputs) throws IOException, ParserException { if (!request.containsHeader("Accept")) request.addHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*"); if (!request.containsHeader("Accept-Charset")) request.addHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); if (App.getConfig().logToConsole()) App.logv("Parser: Fetching %s", request.getURI()); long started = System.currentTimeMillis(); initRequest(request);/*ww w.jav a2 s .co m*/ try { synchronized (mHttpClient) { HttpContext context = new BasicHttpContext(); StringBuilder log = null; if (App.getConfig().logHttp()) { log = new StringBuilder(); log.append(String.format("URL: %s\n", request.getURI())); log.append("Headers: \n"); for (Header h : request.getAllHeaders()) log.append(String.format(" '%s': '%s'\n", h.getName(), h.getValue())); log.append("Cookies: \n"); for (Cookie c : mHttpClient.getCookieStore().getCookies()) log.append(String.format(" '%s': '%s'\n", c.getName(), c.getValue())); log.append("Query Elements: \n"); if (inputs != null) { for (NameValuePair p : inputs) log.append(String.format(" '%s': '%s'\n", p.getName(), p.getValue())); } else { log.append(" [empty]\n"); } } try { mLastResponse = mHttpClient.execute(request, context); } catch (SocketTimeoutException e) { throw new ParserException(mContext, R.string.error_timed_out); } try { if (mLastResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpUriRequest currentReq = (HttpUriRequest) context .getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); this.mLastUrl = currentHost.toURI() + currentReq.getURI(); } } catch (Exception e) { if (App.getConfig().logToConsole()) { App.logv("Unable to get last URL - see stack:"); e.printStackTrace(); } this.mLastUrl = null; } HttpEntity entity = mLastResponse.getEntity(); if (entity == null) return null; InputStream stream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream), 10000); StringBuilder builder = new StringBuilder(10000); try { int read; char[] buffer = new char[1000]; while ((read = reader.read(buffer)) >= 0) builder.append(buffer, 0, read); } catch (OutOfMemoryError e) { return null; } stream.close(); entity.consumeContent(); String response; try { response = builder.toString(); } catch (OutOfMemoryError e) { if (App.getConfig().logToConsole()) e.printStackTrace(); return null; } if (App.getConfig().logHttp()) { log.append(String.format("\nResponse: \n%s\n", response)); writeToFile( generateDatedFilename( "http-log-" + request.getURI().toString().replaceAll("[^A-Za-z0-9]", "_")), log.toString()); } return preparseResponse(response); } } finally { if (App.getConfig().logToConsole()) displayTimeTaken("Parser: Fetch took", started); } }
From source file:it.staiger.jmeter.protocol.http.sampler.HTTPHC4DynamicFilePost.java
@Override protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) { if (log.isDebugEnabled()) { log.debug("Start : sample " + url.toString()); log.debug("method " + method + " followingRedirect " + areFollowingRedirect + " depth " + frameDepth); }/*from ww w. j a v a 2 s . c o m*/ HTTPSampleResult res = createSampleResult(url, method); HttpClient httpClient = setupClient(url, res); HttpRequestBase httpRequest = null; try { URI uri = url.toURI(); if (method.equals(HTTPConstants.POST)) { httpRequest = new HttpPost(uri); } else { throw new IllegalArgumentException("Unexpected method: '" + method + "'"); } setupRequest(url, httpRequest, res); // can throw IOException } catch (Exception e) { res.sampleStart(); res.sampleEnd(); errorResult(e, res); return res; } HttpContext localContext = new BasicHttpContext(); res.sampleStart(); final CacheManager cacheManager = getCacheManager(); if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) { if (cacheManager.inCache(url)) { return updateSampleResultForResourceInCache(res); } } try { currentRequest = httpRequest; // attach POST data String postBody = sendPostData((HttpPost) httpRequest); res.setQueryString(postBody); // perform the sample HttpResponse httpResponse = executeRequest(httpClient, httpRequest, localContext, url); // parse response and fill the SampleResult with all info parseResponse(httpRequest, localContext, res, httpResponse); // Store any cookies received in the cookie manager: saveConnectionCookies(httpResponse, res.getURL(), getCookieManager()); // Save cache information if (cacheManager != null) { cacheManager.saveDetails(httpResponse, res); } // Follow redirects and download page resources if appropriate: res = resultProcessing(areFollowingRedirect, frameDepth, res); } catch (IOException e) { log.debug("IOException", e); if (res.getEndTime() == 0) { res.sampleEnd(); } // pick up headers if failed to execute the request if (res.getRequestHeaders() != null) { log.debug("Overwriting request old headers: " + res.getRequestHeaders()); } res.setRequestHeaders( getConnectionHeaders((HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST))); errorResult(e, res); return res; } catch (RuntimeException e) { log.debug("RuntimeException", e); if (res.getEndTime() == 0) { res.sampleEnd(); } errorResult(e, res); return res; } finally { currentRequest = null; } return res; }
From source file:org.apache.taverna.activities.rest.HTTPRequestHandler.java
/** * TODO - REDIRECTION output:: if there was no redirection, should just show * the actual initial URL?/* ww w .java 2 s . co m*/ * * @param httpRequest * @param acceptHeaderValue */ private static HTTPRequestResponse performHTTPRequest(ClientConnectionManager connectionManager, HttpRequestBase httpRequest, RESTActivityConfigurationBean configBean, Map<String, String> urlParameters, CredentialsProvider credentialsProvider) { // headers are set identically for all HTTP methods, therefore can do // centrally - here // If the user wants to set MIME type for the 'Accepts' header String acceptsHeaderValue = configBean.getAcceptsHeaderValue(); if ((acceptsHeaderValue != null) && !acceptsHeaderValue.isEmpty()) { httpRequest.setHeader(ACCEPT_HEADER_NAME, URISignatureHandler.generateCompleteURI(acceptsHeaderValue, urlParameters, configBean.getEscapeParameters())); } // See if user wanted to set any other HTTP headers ArrayList<ArrayList<String>> otherHTTPHeaders = configBean.getOtherHTTPHeaders(); if (!otherHTTPHeaders.isEmpty()) for (ArrayList<String> httpHeaderNameValuePair : otherHTTPHeaders) if (httpHeaderNameValuePair.get(0) != null && !httpHeaderNameValuePair.get(0).isEmpty()) { String headerParameterizedValue = httpHeaderNameValuePair.get(1); String headerValue = URISignatureHandler.generateCompleteURI(headerParameterizedValue, urlParameters, configBean.getEscapeParameters()); httpRequest.setHeader(httpHeaderNameValuePair.get(0), headerValue); } try { HTTPRequestResponse requestResponse = new HTTPRequestResponse(); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, null); ((DefaultHttpClient) httpClient).setCredentialsProvider(credentialsProvider); HttpContext localContext = new BasicHttpContext(); // Set the proxy settings, if any if (System.getProperty(PROXY_HOST) != null && !System.getProperty(PROXY_HOST).isEmpty()) { // Instruct HttpClient to use the standard // JRE proxy selector to obtain proxy information ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); httpClient.setRoutePlanner(routePlanner); // Do we need to authenticate the user to the proxy? if (System.getProperty(PROXY_USERNAME) != null && !System.getProperty(PROXY_USERNAME).isEmpty()) // Add the proxy username and password to the list of // credentials httpClient.getCredentialsProvider().setCredentials( new AuthScope(System.getProperty(PROXY_HOST), Integer.parseInt(System.getProperty(PROXY_PORT))), new UsernamePasswordCredentials(System.getProperty(PROXY_USERNAME), System.getProperty(PROXY_PASSWORD))); } // execute the request HttpResponse response = httpClient.execute(httpRequest, localContext); // record response code requestResponse.setStatusCode(response.getStatusLine().getStatusCode()); requestResponse.setReasonPhrase(response.getStatusLine().getReasonPhrase()); // record header values for Content-Type of the response requestResponse.setResponseContentTypes(response.getHeaders(CONTENT_TYPE_HEADER_NAME)); // track where did the final redirect go to (if there was any) HttpHost targetHost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest targetRequest = (HttpUriRequest) localContext .getAttribute(ExecutionContext.HTTP_REQUEST); requestResponse.setRedirectionURL("" + targetHost + targetRequest.getURI()); requestResponse.setRedirectionHTTPMethod(targetRequest.getMethod()); requestResponse.setHeaders(response.getAllHeaders()); /* read and store response body (check there is some content - negative length of content means unknown length; zero definitely means no content...)*/ // TODO - make sure that this test is sufficient to determine if // there is no response entity if (response.getEntity() != null && response.getEntity().getContentLength() != 0) requestResponse.setResponseBody(readResponseBody(response.getEntity())); // release resources (e.g. connection pool, etc) httpClient.getConnectionManager().shutdown(); return requestResponse; } catch (Exception ex) { return new HTTPRequestResponse(ex); } }
From source file:czd.lib.network.AsyncHttpClient.java
/** * Simple interface method, to enable or disable redirects. If you set manually RedirectHandler * on underlying HttpClient, effects of this method will be canceled. * * @param enableRedirects boolean//from ww w.j a v a2 s . c om */ public void setEnableRedirects(final boolean enableRedirects) { if (enableRedirects) { httpClient.setRedirectHandler(new DefaultRedirectHandler() { @Override public boolean isRedirectRequested(HttpResponse response, HttpContext context) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } int statusCode = response.getStatusLine().getStatusCode(); switch (statusCode) { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_SEE_OTHER: case HttpStatus.SC_TEMPORARY_REDIRECT: return true; default: return false; } } @Override public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } //get the location header to find out where to redirect to Header locationHeader = response.getFirstHeader("location"); if (locationHeader == null) { // got a redirect response, but no location header throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header"); } //HERE IS THE MODIFIED LINE OF CODE String location = locationHeader.getValue().replaceAll(" ", "%20"); URI uri; try { uri = new URI(location); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } HttpParams params = response.getParams(); // rfc2616 demands the location value be a complete URI // Location = "Location" ":" absoluteURI if (!uri.isAbsolute()) { if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) { throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"); } // Adjust location URI HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { throw new IllegalStateException("Target host not available " + "in the HTTP context"); } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); try { URI requestURI = new URI(request.getRequestLine().getUri()); URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); uri = URIUtils.resolve(absoluteRequestURI, uri); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) { RedirectLocations redirectLocations = (RedirectLocations) context .getAttribute("http.protocol.redirect-locations"); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); context.setAttribute("http.protocol.redirect-locations", redirectLocations); } URI redirectURI; if (uri.getFragment() != null) { try { HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); redirectURI = URIUtils.rewriteURI(uri, target, true); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } else { redirectURI = uri; } if (redirectLocations.contains(redirectURI)) { throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'"); } else { redirectLocations.add(redirectURI); } } return uri; } }); } }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
/** * ??<br>/*from ww w . ja v a 2 s .c o m*/ * ????? * * @throws Exception */ @Test public void testHttpRetry() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); // ?Handler HttpRequestRetryHandler mRequestRetryHandler = new HttpRequestRetryHandler() { // true? ? public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (executionCount > 5) { // ??? System.out.println("....."); return false; } if (exception instanceof NoHttpResponseException) { // ?? return true; } if (exception instanceof SSLHandshakeException) { // ???SSL? return false; } System.out.println("executionCount ==>" + executionCount); HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // ? return true; } return false; } }; // Http? httpClient.setHttpRequestRetryHandler(mRequestRetryHandler); HttpGet httpGet = new HttpGet(new URI(ERR_URL1)); HttpResponse httpResponse = httpClient.execute(httpGet); String content = parseEntity(httpResponse.getEntity()); System.out.println(content); }
From source file:com.hardincoding.sonar.subsonic.service.SubsonicMusicService.java
private void detectRedirect(String originalUrl, Context context, HttpContext httpContext) { HttpUriRequest request = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost host = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); // Sometimes the request doesn't contain the "http://host" part so we // must take from the HttpHost object. String redirectedUrl;//w w w. ja v a2s .c o m if (request.getURI().getScheme() == null) { redirectedUrl = host.toURI() + request.getURI(); } else { redirectedUrl = request.getURI().toString(); } redirectFrom = originalUrl.substring(0, originalUrl.indexOf("/rest/")); redirectTo = redirectedUrl.substring(0, redirectedUrl.indexOf("/rest/")); Log.i(TAG, redirectFrom + " redirects to " + redirectTo); redirectionLastChecked = System.currentTimeMillis(); redirectionNetworkType = getCurrentNetworkType(context); }