Example usage for org.apache.http.protocol HttpContext setAttribute

List of usage examples for org.apache.http.protocol HttpContext setAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpContext setAttribute.

Prototype

void setAttribute(String str, Object obj);

Source Link

Usage

From source file:org.wso2.appserver.integration.tests.webapp.spring.SpringScopeTestCase.java

@Test(groups = "wso2.as", description = "Verfiy Spring Session scope")
public void testSpringSessionScope() throws Exception {
    String endpoint = webAppURL + "/" + webAppMode.getWebAppName() + "/scope/session";

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet(endpoint);
    HttpResponse response1 = httpClient.execute(httpget, httpContext);
    String responseMsg1 = new BasicResponseHandler().handleResponse(response1);
    HttpResponse response2 = httpClient.execute(httpget, httpContext);
    String responseMsg2 = new BasicResponseHandler().handleResponse(response2);
    httpClient.close();//from  w  ww  .  j a  v  a 2  s . c  o m

    assertTrue(responseMsg1.equalsIgnoreCase(responseMsg2), "Failed: Responses should be the same");

}

From source file:org.epop.dataprovider.HTMLPage.java

private void getCode(URI uri) throws ClientProtocolException, IOException, ParserConfigurationException {

    // HttpGet httpget = new HttpGet(uri);
    // HttpClient httpclient = new DefaultHttpClient();
    // ResponseHandler<String> responseHandler = new BasicResponseHandler();
    // this.rawCode = httpclient.execute(httpget, responseHandler);
    ///*from w ww .  j a v a  2 s.  c  om*/
    // TagNode tagNode = new HtmlCleaner().clean(this.rawCode);
    // return new DomSerializer(new CleanerProperties()).createDOM(tagNode);

    HttpGet request = new HttpGet(uri);

    HttpContext HTTP_CONTEXT = new BasicHttpContext();
    HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT,
            "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
    request.setHeader("Referer", "http://www.google.com");
    request.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11");

    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request, HTTP_CONTEXT);

    if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 400) {
        throw new IOException(
                "bad response, error code = " + response.getStatusLine().getStatusCode() + " for " + uri);
    }

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        this.rawCode = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
    }

}

From source file:org.esigate.http.RedirectStrategy.java

@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
        throws ProtocolException {
    HttpUriRequest newRequest = super.getRedirect(request, response, context);
    context.setAttribute(LAST_REQUEST, newRequest);
    if (LOG.isInfoEnabled()) {
        LOG.info(request.getRequestLine() + " -> " + response.getStatusLine()
                + " -> automaticaly following redirect to " + newRequest.getRequestLine());
    }/*from ww  w  . j  ava2  s .  co  m*/
    return newRequest;
}

From source file:com.subgraph.vega.internal.http.requests.HttpRequestEngine.java

@Override
public IHttpResponse sendRequest(HttpUriRequest request, HttpContext context) throws RequestEngineException {
    final HttpContext requestContext = (context == null) ? (new BasicHttpContext()) : (context);
    requestContext.setAttribute(ClientContext.COOKIE_STORE, config.getCookieStore());
    Future<IHttpResponse> future = executor
            .submit(new RequestTask(client, rateLimit, request, requestContext, config, htmlParser));
    try {/* www. j av  a2  s  .  co  m*/
        return future.get();
    } catch (InterruptedException e) {
        logger.info("Request " + request.getURI() + " was interrupted before completion");
    } catch (ExecutionException e) {
        throw translateException(request, e.getCause());
    }
    return null;
}

From source file:org.apache.droids.protocol.http.DroidsHttpClient.java

@Override
protected HttpContext createHttpContext() {
    HttpContext context = new BasicHttpContext();
    context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
    context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
    return context;
}

From source file:org.aludratest.service.gui.web.selenium.httpproxy.RequestProcessorThread.java

/** The {@link Thread}'s worker method which processes the request. */
@Override//from w w w  . j av  a 2 s  .  co  m
public void run() {
    LOGGER.debug("New request processor thread");

    // Create context and bind connection objects to the execution context
    HttpContext context = new BasicHttpContext(null);
    context.setAttribute(HTTP_IN_CONN, this.inconn);
    context.setAttribute(HTTP_OUT_CONN, this.outconn);

    // checking request's keep-alive attribute
    Boolean keepAliveObj = (Boolean) context.getAttribute(HTTP_CONN_KEEPALIVE);
    boolean keepAlive = (keepAliveObj != null && keepAliveObj.booleanValue());

    // handle in/out character transfer according to keep-alive setting
    try {
        while (!Thread.interrupted()) {
            if (!this.inconn.isOpen()) {
                this.outconn.close();
                break;
            }
            LOGGER.debug("Handling request");

            this.httpservice.handleRequest(this.inconn, context);

            if (!keepAlive) {
                this.outconn.close();
                this.inconn.close();
                LOGGER.debug("Finishing request");
                break;
            }
        }
    } catch (ConnectionClosedException ex) {
        if (keepAlive && owner.isRunning()) {
            LOGGER.error("Client closed connection");
        } else {
            LOGGER.debug("Client closed connection");
        }
    } catch (IOException ex) {
        LOGGER.error("I/O error: " + ex.getMessage());
    } catch (HttpException ex) {
        LOGGER.error("Unrecoverable HTTP protocol violation: " + ex.getMessage());
    } finally {
        try {
            this.inconn.shutdown();
        } catch (IOException ignore) {
            // ignore possible exceptions
        }
        try {
            this.outconn.shutdown();
        } catch (IOException ignore) {
            // ignore possible exceptions
        }
        LOGGER.debug("Finished connection thread");
    }
}

From source file:org.apache.geode.tools.pulse.PulseDataExportTest.java

private HttpContext buildAuthenticatedHttpContext() throws Throwable {
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    return localContext;
}

From source file:org.sonatype.nexus.rest.client.internal.RestClientFactoryImpl.java

@Override
public Client create(final RestClientConfiguration configuration) {
    checkNotNull(configuration);/*from w  w w .j av a  2  s .co m*/

    try (TcclBlock tccl = TcclBlock.begin(ResteasyClientBuilder.class)) {
        HttpContext httpContext = new BasicHttpContext();
        if (configuration.getUseTrustStore()) {
            httpContext.setAttribute(SSLContextSelector.USE_TRUST_STORE, true);
        }
        HttpClient client;
        if (configuration.getHttpClient() != null) {
            client = checkNotNull(configuration.getHttpClient().get());
        } else {
            client = httpClient.get();
        }
        ClientHttpEngine httpEngine = new ApacheHttpClient4Engine(client, httpContext);

        ResteasyClientBuilder builder = new ResteasyClientBuilder().httpEngine(httpEngine);

        if (configuration.getCustomizer() != null) {
            configuration.getCustomizer().apply(builder);
        }

        return builder.build();
    }
}

From source file:com.woonoz.proxy.servlet.HttpRequestHandler.java

private void performHttpRequest(HttpRequestBase requestToServer, HttpServletResponse responseToClient,
        ServerHeadersHandler serverHeadersHandler) throws IOException, URISyntaxException {
    HttpContext context = new BasicHttpContext();
    context.setAttribute(HttpRequestHandler.class.getName(), this);
    HttpResponse responseFromServer = client.execute(requestToServer, context);
    logger.debug("Performed request: {} --> {}", requestToServer.getRequestLine(),
            responseFromServer.getStatusLine());
    responseToClient.setStatus(responseFromServer.getStatusLine().getStatusCode());
    copyHeaders(responseFromServer, responseToClient, serverHeadersHandler);
    HttpEntity entity = responseFromServer.getEntity();
    if (entity != null) {
        try {//  w  w w. j  av a  2s .  c o m
            entity.writeTo(responseToClient.getOutputStream());
        } finally {
            EntityUtils.consume(entity);
        }
    }
}

From source file:org.wso2.carbon.registry.notes.test.PublisherNotesTestCase.java

@Test(groups = "wso2.greg", description = "Add Schema without name", dependsOnMethods = "addNoteTestCase")
public void getNoteTestCase() throws Exception {
    Thread.sleep(60000);/*from   w  w  w .  java  2 s .  c  o  m*/
    String session_id = authenticateJaggeryAPI();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpClient client = new DefaultHttpClient();

    URIBuilder builder = new URIBuilder();
    builder.setScheme("https").setHost("localhost:10343").setPath("/publisher/apis/assets")
            .setParameter("type", "note").setParameter("q",
                    "overview_resourcepath\":\"/_system/governance/trunk/soapservices/4.5.0/SOAPService1\"");
    URI uri = builder.build();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = client.execute(httpget);
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
}