List of usage examples for org.apache.http.protocol HttpContext setAttribute
void setAttribute(String str, Object obj);
From source file:com.joyent.manta.http.ApacheHttpGetResponseEntityContentContinuator.java
/** * Get an {@link InputStream} which picks up starting {@code bytesRead} bytes from the beginning of the logical * object being downloaded. Implementations should compare headers across all requests and responses to ensure that * the object being downloaded has not changed between the initial and subsequent requests. * * @param ex the exception which occurred while downloading (either the first response or a continuation) * @param bytesRead byte offset at which the new stream should start * @return another stream which continues to deliver the bytes from the initial request * @throws HttpDownloadContinuationException if the provided {@link IOException} is not recoverable or the number of * retries has been reached, or there is an error * @throws HttpDownloadContinuationUnexpectedResponseException if the continuation response was incompatible or * indicated that the remote object has somehow changed *///from w ww.j av a 2 s . c o m @Override public InputStream buildContinuation(final IOException ex, final long bytesRead) throws IOException { requireNonNull(ex); if (!isRecoverable(ex)) { throw ex; } this.continuation++; if (this.maxContinuations != INFINITE_CONTINUATIONS && this.maxContinuations <= this.continuation) { throw new HttpDownloadContinuationException( String.format("Maximum number of continuations reached [%s], aborting auto-retry: %s", this.maxContinuations, ex.getMessage()), ex); } LOG.debug( "Attempting to build a continuation for " + "[{}] request " + "to path [{}] " + "to recover at byte offset {} " + "from exception {}", this.request.getMethod(), this.request.getRequestLine().getUri(), bytesRead, ex.getMessage()); // if an IOException occurs while reading EOF the user may ask us for a continuation // starting after the last valid byte. if (bytesRead == this.marker.getTotalRangeSize()) { if (this.metricRegistry != null) { this.metricRegistry.counter(METRIC_NAME_RECOVERED_EXCEPTION_PREFIX + ex.getClass().getSimpleName()) .inc(); this.metricRegistry.counter(METRIC_NAME_RECOVERED_EOF).inc(); } return ClosedInputStream.CLOSED_INPUT_STREAM; } try { this.marker.updateRangeStart(bytesRead); } catch (final IllegalArgumentException iae) { // we should wrap and rethrow this so that the caller doesn't get stuck in a loop throw new HttpDownloadContinuationException("Failed to update download continuation offset", iae); } this.request.setHeader(RANGE, this.marker.getCurrentRange().render()); // not yet trying to handle exceptions during request execution final HttpResponse response; try { final HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(CONTEXT_ATTRIBUTE_MANTA_RETRY_DISABLE, true); response = this.client.execute(this.request, httpContext); } catch (final IOException ioe) { throw new HttpDownloadContinuationException( "Exception occurred while attempting to build continuation: " + ioe.getMessage(), ioe); } final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != SC_PARTIAL_CONTENT) { throw new HttpDownloadContinuationUnexpectedResponseException(String .format("Invalid response code: expecting [%d], got [%d]", SC_PARTIAL_CONTENT, statusCode)); } try { validateResponseWithMarker(extractDownloadResponseFingerprint(response, false)); } catch (final HttpException he) { throw new HttpDownloadContinuationUnexpectedResponseException( "Continuation request failed validation: " + he.getMessage(), he); } final InputStream content; try { final HttpEntity entity = response.getEntity(); if (entity == null) { throw new HttpDownloadContinuationUnexpectedResponseException( "Entity missing from continuation response"); } content = entity.getContent(); if (content == null) { throw new HttpDownloadContinuationUnexpectedResponseException( "Entity content missing from continuation response"); } } catch (final UnsupportedOperationException | IOException uoe) { throw new HttpDownloadContinuationUnexpectedResponseException(uoe); } if (this.metricRegistry != null) { this.metricRegistry.counter(METRIC_NAME_RECOVERED_EXCEPTION_PREFIX + ex.getClass().getSimpleName()) .inc(); } LOG.debug("Successfully constructed continuation at byte offset {} to recover from {}", bytesRead, ex); return content; }
From source file:com.mnxfst.testing.activities.http.TestHTTPRequestActivity.java
public void testExecuteHTTPRequest() throws HttpException, IOException { HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, false); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost("www.heise.de", 80); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try {/*from w w w . ja v a 2 s .c o m*/ HttpEntity[] requestBodies = { new StringEntity("This is the first test request", "UTF-8"), new ByteArrayEntity("This is the second test request".getBytes("UTF-8")), new InputStreamEntity(new ByteArrayInputStream( "This is the third test request (will be chunked)".getBytes("UTF-8")), -1) }; HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); for (int i = 0; i < requestBodies.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/"); request.setEntity(requestBodies[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClient.java
/** * It creates an {@link HttpContext} object with a cookie store that will contain the cookies returned by the user in the {@link CloseableHttpResponse} that is received as parameter. *//*from w w w . jav a 2s. c o m*/ protected HttpContext createHttpContextWithCookies(CloseableHttpResponse loginResponse) { CookieStore cookieStore = new BasicCookieStore(); createAndAddCookiesOnStoreForHeaders(cookieStore, loginResponse.getAllHeaders()); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); return httpContext; }
From source file:gtu.youtube.JavaYoutubeVideoUrlHandler.java
public List<NameValuePair> getVideoInfo(String videoId, String format, String userAgent) { try {/* w w w . j a va 2 s. c om*/ List<NameValuePair> infoMap = new ArrayList<NameValuePair>(); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("video_id", videoId)); qparams.add(new BasicNameValuePair("fmt", "" + format)); URI uri = getUri("get_video_info", qparams); if (StringUtils.isBlank(userAgent)) { userAgent = DEFAULT_USER_AGENT; } CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); if (userAgent != null && userAgent.length() > 0) { httpget.setHeader("User-Agent", userAgent); } HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == 200) { InputStream instream = entity.getContent(); String videoInfo = getStringFromInputStream(DEFAULT_ENCODING, instream); URLEncodedUtils.parse(infoMap, new Scanner(videoInfo), DEFAULT_ENCODING); } return infoMap; } catch (Exception ex) { throw new RuntimeException("play Err : " + ex.getMessage(), ex); } }
From source file:org.mobicents.xcap.client.impl.XcapClientImpl.java
private XcapResponse execute(HttpUriRequest request, Header[] additionalRequestHeaders, Credentials credentials) throws IOException { if (additionalRequestHeaders != null) { for (Header header : additionalRequestHeaders) { request.addHeader(((HeaderImpl) header).getWrappedHeader()); }// ww w .j ava2 s .c o m } HttpContext httpContext = NULL_HTTP_CONTEXT; if (credentials != null) { httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, new SingleCredentialsProvider(((CredentialsImpl) credentials).getWrappedCredentials())); } final XcapResponseImpl response = client.execute(request, responseHandler, httpContext); if (log.isDebugEnabled()) { log.debug("Received:\n--BEGIN--\n" + response.toString() + "\n--END--"); } return response; }
From source file:com.joyent.manta.client.multipart.EncryptedMultipartManager.java
/** * Creates or enhances a request context with the flag that indicates it's an encrypted part upload. * * @param context an existing HttpContext or null * @return an HttpContext reflecting the use of encryption with a part upload *///from w ww . ja v a 2s. c o m private HttpContext buildRequestContext(final HttpContext context) { final HttpContext ctx; if (context != null) { ctx = context; } else { ctx = new BasicHttpContext(); } ctx.setAttribute(HttpContextRetryCancellation.CONTEXT_ATTRIBUTE_MANTA_RETRY_DISABLE, true); return ctx; }
From source file:org.apache.ambari.view.hive.client.HttpRequestInterceptorBase.java
@Override public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { try {//ww w . jav a2 s .com // If cookie based authentication is allowed, generate ticket only when necessary. // The necessary condition is either when there are no server side cookies in the // cookiestore which can be send back or when the server returns a 401 error code // indicating that the previous cookie has expired. if (isCookieEnabled) { httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); } // Generate the kerberos ticket under the following scenarios: // 1. Cookie Authentication is disabled OR // 2. The first time when the request is sent OR // 3. The server returns a 401, which sometimes means the cookie has expired // 4. The cookie is secured where as the client connect does not use SSL if (!isCookieEnabled || ((httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) == null && (cookieStore == null || (cookieStore != null && Utils.needToSendCredentials(cookieStore, cookieName, isSSL)))) || (httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) != null && httpContext .getAttribute(Utils.HIVE_SERVER2_RETRY_KEY).equals(Utils.HIVE_SERVER2_RETRY_TRUE)))) { addHttpAuthHeader(httpRequest, httpContext); } if (isCookieEnabled) { httpContext.setAttribute(Utils.HIVE_SERVER2_RETRY_KEY, Utils.HIVE_SERVER2_RETRY_FALSE); } // Insert the additional http headers if (additionalHeaders != null) { for (Map.Entry<String, String> entry : additionalHeaders.entrySet()) { httpRequest.addHeader(entry.getKey(), entry.getValue()); } } } catch (Exception e) { throw new HttpException(e.getMessage(), e); } }
From source file:org.Cherry.Modules.Web.Engine.RequestInterceptor.java
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException, InvalidCredentialsException { log.debug("Intercepted request [{}] with URI [{}] for context [{}].", request, request.getRequestLine().getUri(), context); final Boolean authenticated = authenticate(request, context); context.setAttribute(Authenticated, authenticated); Context.instance().getInvocationContext().set(Key.Authenticated, authenticated); if (request instanceof BasicHttpRequest) if (isDebugEnabled()) getTracerService().examine(request); }
From source file:gtu.youtube.JavaYoutubeDownloader.java
private void play(String videoId, int format, String encoding, String userAgent, File outputdir, String extension) throws Throwable { // ?Youtube?//from ww w . ja v a 2 s . c om JavaYoutubeVideoUrlHandler urlHandler = new JavaYoutubeVideoUrlHandler(videoId, String.valueOf(format), userAgent); // ? log.fine("Retrieving " + videoId); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("video_id", videoId)); qparams.add(new BasicNameValuePair("fmt", "" + format)); URI uri = getUri("get_video_info", qparams); CookieStore cookieStore = new BasicCookieStore(); if (true) { InputStream is = this.getClass().getResource("JavaYoutubeDownloader_cookies.txt").openStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); String cookieContent = baos.toString("UTF-8"); cookieStore = this.getCookieString(cookieContent); } HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); if (userAgent != null && userAgent.length() > 0) { httpget.setHeader("User-Agent", userAgent); } log.finer("Executing " + uri); HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == 200) { InputStream instream = entity.getContent(); String videoInfo = getStringFromInputStream(encoding, instream); System.out.println("videoInfo = " + videoInfo); if (videoInfo != null && videoInfo.length() > 0) { List<NameValuePair> infoMap = new ArrayList<NameValuePair>(); URLEncodedUtils.parse(infoMap, new Scanner(videoInfo), encoding); System.out.println("infoMap = " + infoMap); String downloadUrl = null; String filename = videoId; for (NameValuePair pair : infoMap) { String key = pair.getName(); String val = pair.getValue(); log.finest(key + "=" + val); // System.out.println(key + "\t" + val); if (key.equals("title")) { filename = val; } else if (key.equals("fmt_url_map")) { // String[] formats = commaPattern.split(val); boolean found = false; for (String fmt : formats) { String[] fmtPieces = pipePattern.split(fmt); if (fmtPieces.length == 2) { int pieceFormat = Integer.parseInt(fmtPieces[0]); log.fine("Available format=" + pieceFormat); if (pieceFormat == format) { // found what we want downloadUrl = fmtPieces[1]; System.out.println(">>> downloadUrl = " + downloadUrl); found = true; break; } } } if (!found) { log.warning( "Could not find video matching specified format, however some formats of the video do exist (use -verbose)."); } } } filename = cleanFilename(filename); if (filename.length() == 0) { filename = videoId; } else { filename += "_" + videoId; } filename += "." + extension; File outputfile = new File(outputdir, filename); // ?1 if (downloadUrl == null) { downloadUrl = customDownloadUrl_by_gtu001; } // ?2 if (downloadUrl == null) { JavaYoutubeVideoUrlHandler gtu001 = new JavaYoutubeVideoUrlHandler(videoId, "", userAgent); downloadUrl = gtu001.getUrl(format); } if (downloadUrl != null) { downloadWithHttpClient(userAgent, downloadUrl, outputfile); } else { log.severe("Could not find video"); } } else { log.severe("Did not receive content from youtube"); } } else { log.severe("Could not contact youtube: " + response.getStatusLine()); } }
From source file:com.ryan.ryanreader.cache.CacheDownload.java
private void downloadPost(final HttpClient httpClient) { final HttpPost httpPost = new HttpPost(initiator.url); try {/*from w w w . ja v a 2 s . com*/ httpPost.setEntity(new UrlEncodedFormEntity(initiator.postFields, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { BugReportActivity.handleGlobalError(initiator.context, e); return; } final HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, initiator.getCookies()); final HttpResponse response; final StatusLine status; try { response = httpClient.execute(httpPost, localContext); status = response.getStatusLine(); } catch (Throwable t) { t.printStackTrace(); notifyAllOnFailure(RequestFailureType.CONNECTION, t, null, "Unable to open a connection"); return; } if (status.getStatusCode() != 200) { notifyAllOnFailure(RequestFailureType.REQUEST, null, status, String.format("HTTP error %d (%s)", status.getStatusCode(), status.getReasonPhrase())); return; } final HttpEntity entity = response.getEntity(); if (entity == null) { notifyAllOnFailure(RequestFailureType.CONNECTION, null, status, "Did not receive a valid HTTP response"); return; } final InputStream is; try { is = entity.getContent(); } catch (Throwable t) { t.printStackTrace(); notifyAllOnFailure(RequestFailureType.CONNECTION, t, status, "Could not open an input stream"); return; } if (initiator.isJson) { final BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024); final JsonValue value; try { value = new JsonValue(bis); value.buildInNewThread(); } catch (Throwable t) { t.printStackTrace(); notifyAllOnFailure(RequestFailureType.PARSE, t, null, "Error parsing the JSON stream"); return; } synchronized (this) { this.value = value; notifyAllOnJsonParseStarted(value, RRTime.utcCurrentTimeMillis(), session); } try { value.join(); } catch (Throwable t) { t.printStackTrace(); notifyAllOnFailure(RequestFailureType.PARSE, t, null, "Error parsing the JSON stream"); return; } success = true; } else { throw new RuntimeException("POST requests must be for JSON values"); } }