Example usage for org.apache.http.protocol HttpCoreContext HTTP_CONNECTION

List of usage examples for org.apache.http.protocol HttpCoreContext HTTP_CONNECTION

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpCoreContext HTTP_CONNECTION.

Prototype

String HTTP_CONNECTION

To view the source code for org.apache.http.protocol HttpCoreContext HTTP_CONNECTION.

Click Source Link

Usage

From source file:ste.web.http.api.BugFreeApiHandlerBase.java

@Before
public void setup() throws Exception {
    request = request(TEST_URI_PARAMETERS);
    context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    context.setAttribute(TEST_REQ_ATTR_NAME1, TEST_VALUE1);
    context.setAttribute(TEST_REQ_ATTR_NAME2, TEST_VALUE2);
    context.setAttribute(TEST_REQ_ATTR_NAME3, TEST_VALUE3);
    response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
    handler = new ApiHandler(new File(ROOT).getAbsolutePath());
}

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

public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {

    HttpClientConnection conn = (HttpClientConnection) context.getAttribute(HTTP_OUT_CONN);

    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
    context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);

    // Remove hop-by-hop headers
    /*request.removeHeaders(HTTP.CONTENT_LEN);
    request.removeHeaders(HTTP.TRANSFER_ENCODING);
    request.removeHeaders(HTTP.CONN_DIRECTIVE);
    request.removeHeaders("Keep-Alive");
    request.removeHeaders("Proxy-Authenticate");
    request.removeHeaders("TE");//from ww  w .j  a va2 s.c om
    request.removeHeaders("Trailers");
    request.removeHeaders("Upgrade");*/

    this.httpexecutor.preProcess(request, this.httpproc, context);

    LOGGER.debug(">> Request URI: " + request.getRequestLine().getUri());
    if (LOGGER.isTraceEnabled()) {
        for (Header header : request.getAllHeaders()) {
            LOGGER.trace(header.toString());
        }
    }

    HttpResponse targetResponse = this.httpexecutor.execute(request, conn, context);
    this.httpexecutor.postProcess(response, this.httpproc, context);

    // Remove hop-by-hop headers
    /*
    targetResponse.removeHeaders(HTTP.CONTENT_LEN);
    targetResponse.removeHeaders(HTTP.TRANSFER_ENCODING);
    targetResponse.removeHeaders(HTTP.CONN_DIRECTIVE);
    targetResponse.removeHeaders("Keep-Alive");
    targetResponse.removeHeaders("TE");
    targetResponse.removeHeaders("Trailers");
    targetResponse.removeHeaders("Upgrade");
    */
    response.setStatusLine(targetResponse.getStatusLine());
    response.setHeaders(targetResponse.getAllHeaders());
    response.setEntity(targetResponse.getEntity());

    LOGGER.debug("<< Response: " + response.getStatusLine());

    boolean keepalive = this.connStrategy.keepAlive(response, context);
    context.setAttribute(HTTP_CONN_KEEPALIVE, Boolean.valueOf(keepalive));
}

From source file:ste.web.http.beanshell.BugFreeBeanShellHandler.java

@Before
public void setUp() throws Exception {
    request = new BasicHttpRequest("GET", TEST_URI_PARAMETERS);
    context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    context.setAttribute(TEST_REQ_ATTR_NAME1, TEST_VALUE1);
    context.setAttribute(TEST_REQ_ATTR_NAME2, TEST_VALUE2);
    context.setAttribute(TEST_REQ_ATTR_NAME3, TEST_VALUE3);
    response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
    handler = new BeanShellHandler(new File(ROOT).getAbsolutePath());
}

From source file:ste.web.http.beanshell.BugFreeBeanShellUtils.java

/**
 * Test of setup method, of class BeanShellUtils.
 */// w w w  .ja  v a 2 s  .c  om
@Test
public void setup() throws Exception {
    BasicHttpRequest request = new BasicHttpRequest("get", TEST_URI_PARAMETERS);
    HttpSessionContext context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    context.setAttribute(TEST_REQ_ATTR_NAME1, TEST_VALUE1);
    context.setAttribute(TEST_REQ_ATTR_NAME2, TEST_VALUE2);
    context.setAttribute(TEST_REQ_ATTR_NAME3, TEST_VALUE3);

    Interpreter i = new Interpreter();
    BeanShellUtils.setup(i, request, RESPONSE_OK, context);

    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put(TEST_REQ_ATTR_NAME1, Arrays.asList(TEST_VALUE1));
    attributes.put(TEST_REQ_ATTR_NAME2, Arrays.asList(TEST_VALUE2));
    attributes.put(TEST_REQ_ATTR_NAME3, Arrays.asList(TEST_VALUE3));

    URI uri = new URI(request.getRequestLine().getUri());
    ste.web.beanshell.BugFreeBeanShellUtils.checkSetup(i, QueryString.parse(uri).getMap(), attributes);
}

From source file:ste.web.http.beanshell.BeanShellUtils.java

public static void setup(final Interpreter interpreter, final HttpRequest request, final HttpResponse response,
        final HttpSessionContext context) throws EvalError, IOException {
    ////from   ww w .j a  v  a2 s  . c  o m
    // Set attributes as script variables
    //
    for (String k : context.keySet()) {
        String key = normalizeVariableName(k);
        interpreter.set(key, context.getAttribute(k));
    }

    //
    // If the request contains url-encoded body, set the given parameters
    //
    Header[] headers = request.getHeaders(HttpHeaders.CONTENT_TYPE);
    if (headers.length > 0) {
        String contentType = headers[0].getValue();
        if (contentType.matches(ContentType.APPLICATION_FORM_URLENCODED.getMimeType() + "( *;.*)?")) {
            HttpEntityEnclosingRequest r = (HttpEntityEnclosingRequest) request;
            HttpEntity e = r.getEntity();

            QueryString qs = QueryString.parse(IOUtils.toString(e.getContent()));
            for (String n : qs.getNames()) {
                String name = normalizeVariableName(n);
                interpreter.set(name, qs.getValues(n).get(0));
            }
        }
    }

    //
    // Set request parameters as script variables. Note that parameters
    // override attributes (note that these override form content
    //
    try {
        QueryString qs = QueryString.parse(new URI(request.getRequestLine().getUri()));
        for (String n : qs.getNames()) {
            String name = normalizeVariableName(n);
            interpreter.set(name, qs.getValues(n).get(0));
        }
    } catch (URISyntaxException x) {
        //
        // nothing to do
        //
    }

    BasicHttpConnection connection = (BasicHttpConnection) context
            .getAttribute(HttpCoreContext.HTTP_CONNECTION);

    interpreter.set(VAR_REQUEST, request);
    interpreter.set(VAR_RESPONSE, response);
    interpreter.set(VAR_SESSION, context);
    interpreter.set(VAR_OUT, connection.getWriter());
    interpreter.set(VAR_LOG, log);
    if (HttpUtils.hasJSONBody(request) && (request instanceof HttpEntityEnclosingRequest)) {
        interpreter.set(VAR_BODY, getJSONBody(getEntityInputStream(request)));
    }
}

From source file:ste.web.http.beanshell.BugFreeBeanShellUtils.java

@Test
public void cleanup() throws Exception {
    BasicHttpRequest request = new BasicHttpRequest("get", TEST_URI_PARAMETERS);
    HttpSessionContext context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    context.setAttribute(TEST_REQ_ATTR_NAME1, TEST_VALUE1);

    Interpreter i = new Interpreter();
    BeanShellUtils.setup(i, request, RESPONSE_OK, context);
    BeanShellUtils.cleanup(i, request);//from w w w.j  a v  a  2 s  . c om

    //
    // We need to make sure that after the handling of the request,
    // parameters are not valid variable any more so to avoid that next
    // invocations will inherit them
    //
    checkCleanup(i, QueryString.parse(new URI(request.getRequestLine().getUri())).getNames());
}

From source file:info.bonjean.beluga.connection.BelugaHTTPClient.java

private BelugaHTTPClient() {
    BelugaConfiguration configuration = BelugaConfiguration.getInstance();
    HttpClientBuilder clientBuilder = HttpClients.custom();

    // timeout/*from   w  w w. j a v a  2s.  com*/
    RequestConfig config = RequestConfig.custom().setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT)
            .setConnectionRequestTimeout(TIMEOUT).build();
    clientBuilder.setDefaultRequestConfig(config);

    switch (configuration.getConnectionType()) {
    case PROXY_DNS:
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
        BelugaDNSResolver dnsOverrider = new BelugaDNSResolver(DNSProxy.PROXY_DNS);
        connectionManager = new PoolingHttpClientConnectionManager(registry, dnsOverrider);
        break;
    case HTTP_PROXY:
        HttpHost proxy = new HttpHost(configuration.getProxyHost(), configuration.getProxyPort(), "http");
        clientBuilder.setProxy(proxy);
        break;
    default:
    }

    // limit the pool size
    connectionManager.setDefaultMaxPerRoute(2);

    // add interceptor, currently for debugging only
    clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
            HttpInetConnection connection = (HttpInetConnection) context
                    .getAttribute(HttpCoreContext.HTTP_CONNECTION);
            log.debug("Remote address: " + connection.getRemoteAddress());
            // TODO: reimplement blacklisting for DNS proxy by maintaining a
            // map [DNS IP,RESOLVED IP] in the DNS resolver for reverse
            // lookup
        }
    });

    // finally create the HTTP client
    clientBuilder.setConnectionManager(connectionManager);
    httpClient = clientBuilder.build();
}

From source file:ste.web.http.api.BugFreeApiHandlerExec.java

@Test
public void running_multiple_thread_in_different_contexts() throws Exception {
    final HttpSessionContext CTX1 = new HttpSessionContext();
    final HttpSessionContext CTX2 = new HttpSessionContext();
    CTX1.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    CTX2.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    Thread t1 = new Thread(new Runnable() {
        @Override/*  w  w w  .ja v  a2  s  . c om*/
        public void run() {
            try {
                handler.handle(request("/api/app/get/multithreading"), response, CTX1);
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    });
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                handler.handle(request("/api/app/get/multithreading"), response, CTX2);
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    });
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    then(CTX1.get("view")).isEqualTo(t1.getName());
    then(CTX2.get("view")).isEqualTo(t2.getName());
}

From source file:ste.web.http.beanshell.BugFreeBeanShellUtils.java

@Test
public void bodyAsNotSpecifiedType() throws Exception {
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("get", TEST_URI_PARAMETERS);
    HttpSessionContext context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    request.setEntity(new StringEntity("one=1&two=2"));

    Interpreter i = new Interpreter();
    BeanShellUtils.setup(i, request, RESPONSE_OK, context);

    checkBodyAsNotSpecifiedType(i);//from w  w w .  ja v  a  2s  .co m
}

From source file:ste.web.http.beanshell.BugFreeBeanShellUtils.java

@Test
public void bodyAsJSONObject() throws Exception {
    final String TEST_LABEL1 = "label1";
    final String TEST_LABEL2 = "label2";
    final String TEST_VALUE1 = "a first label";
    final String TEST_VALUE2 = "a second label";

    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("get", TEST_URI_PARAMETERS);
    HttpSessionContext context = new HttpSessionContext();
    context.setAttribute(HttpCoreContext.HTTP_CONNECTION, getConnection());
    request.addHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE_JSON);
    request.setEntity(new StringEntity(String.format("{%s:'%s'}", TEST_LABEL1, TEST_VALUE1)));

    Interpreter i = new Interpreter();
    BeanShellUtils.setup(i, request, RESPONSE_OK, context);

    checkJSONObject(i, TEST_LABEL1, TEST_VALUE1);

    request.setEntity(new StringEntity(
            String.format("[{%s:'%s'}, {%s:'%s'}]", TEST_LABEL1, TEST_VALUE1, TEST_LABEL2, TEST_VALUE2)));

    BeanShellUtils.setup(i, request, RESPONSE_OK, context);

    checkJSONArray(i, new String[] { TEST_LABEL1, TEST_LABEL2 }, new String[] { TEST_VALUE1, TEST_VALUE2 });
}