You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/10/10 11:40:24 UTC

svn commit: r1396521 - in /httpcomponents/httpcore/trunk: httpcore-ab/src/main/java/org/apache/http/benchmark/ httpcore/src/examples/org/apache/http/examples/ httpcore/src/main/java/org/apache/http/protocol/ httpcore/src/test/java/org/apache/http/tests...

Author: olegk
Date: Wed Oct 10 09:40:23 2012
New Revision: 1396521

URL: http://svn.apache.org/viewvc?rev=1396521&view=rev
Log:
Configuration API changes: added HttpCoreContext convenience class

Added:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpGet.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpPost.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalPoolingHttpGet.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpContext.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpClient.java

Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java Wed Oct 10 09:40:23 2012
@@ -45,10 +45,8 @@ import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.entity.ContentType;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HTTP;
-import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
@@ -67,7 +65,7 @@ import org.apache.http.protocol.RequestU
 class BenchmarkWorker implements Runnable {
 
     private final byte[] buffer = new byte[4096];
-    private final HttpContext context;
+    private final HttpCoreContext context;
     private final HttpProcessor httpProcessor;
     private final HttpRequestExecutor httpexecutor;
     private final ConnectionReuseStrategy connstrategy;
@@ -83,7 +81,7 @@ class BenchmarkWorker implements Runnabl
             final SocketFactory socketFactory,
             final Config config) {
         super();
-        this.context = new BasicHttpContext(null);
+        this.context = new HttpCoreContext();
         this.request = request;
         this.targetHost = targetHost;
         this.config = config;
@@ -115,9 +113,7 @@ class BenchmarkWorker implements Runnabl
         }
 
         // Populate the execution context
-        this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.targetHost);
-        this.context.setAttribute(ExecutionContext.HTTP_REQUEST, this.request);
+        this.context.setTarget(this.targetHost);
 
         stats.start();
         int count = config.getRequests();

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpGet.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpGet.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpGet.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpGet.java Wed Oct 10 09:40:23 2012
@@ -39,6 +39,7 @@ import org.apache.http.message.BasicHttp
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
@@ -69,16 +70,14 @@ public class ElementalHttpGet {
 
         HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
 
-        HttpContext context = new BasicHttpContext(null);
+        HttpCoreContext context = new HttpCoreContext();
         HttpHost host = new HttpHost("localhost", 8080);
+        context.setTarget(host);
+        context.setExpectContinue();
 
         DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
         ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
 
-        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
-        context.setAttribute(ExecutionContext.HTTP_EXPECT_CONT, true);
-
         try {
 
             String[] targets = {

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpPost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpPost.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpPost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpPost.java Wed Oct 10 09:40:23 2012
@@ -44,6 +44,7 @@ import org.apache.http.message.BasicHttp
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
@@ -74,17 +75,14 @@ public class ElementalHttpPost {
 
         HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
 
-        HttpContext context = new BasicHttpContext(null);
-
+        HttpCoreContext context = new HttpCoreContext();
         HttpHost host = new HttpHost("localhost", 8080);
+        context.setTarget(host);
+        context.setExpectContinue();
 
         DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
         ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
 
-        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
-        context.setAttribute(ExecutionContext.HTTP_EXPECT_CONT, true);
-
         try {
 
             HttpEntity[] requestBodies = {

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalPoolingHttpGet.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalPoolingHttpGet.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalPoolingHttpGet.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalPoolingHttpGet.java Wed Oct 10 09:40:23 2012
@@ -41,9 +41,7 @@ import org.apache.http.impl.pool.BasicCo
 import org.apache.http.impl.pool.BasicConnPool;
 import org.apache.http.impl.pool.BasicPoolEntry;
 import org.apache.http.message.BasicHttpRequest;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.ExecutionContext;
-import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
@@ -103,9 +101,8 @@ public class ElementalPoolingHttpGet {
                     BasicPoolEntry entry = future.get();
                     try {
                         HttpClientConnection conn = entry.getConnection();
-                        HttpContext context = new BasicHttpContext(null);
-                        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-                        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
+                        HttpCoreContext context = new HttpCoreContext();
+                        context.setTarget(this.target);
 
                         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
                         System.out.println(">> Request URI: " + request.getRequestLine().getUri());

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpContext.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpContext.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpContext.java Wed Oct 10 09:40:23 2012
@@ -27,18 +27,19 @@
 
 package org.apache.http.protocol;
 
-import java.util.HashMap;
-
 /**
  * HttpContext represents execution state of an HTTP process. It is a structure
- * that can be used to map an attribute name to an attribute value. Internally
- * HTTP context implementations are usually backed by a {@link HashMap}.
- * <p>
+ * that can be used to map an attribute name to an attribute value.
+ * <p/>
  * The primary purpose of the HTTP context is to facilitate information sharing
  * among various  logically related components. HTTP context can be used
  * to store a processing state for one message or several consecutive messages.
  * Multiple logically related messages can participate in a logical session
  * if the same context is reused between consecutive messages.
+ * <p>/
+ * IMPORTANT: Please note HTTP context implementation, even when thread safe,
+ * may not be used concurrently by multiple threads, as the context may contain
+ * thread unsafe attributes.
  *
  * @since 4.0
  */

Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java?rev=1396521&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java Wed Oct 10 09:40:23 2012
@@ -0,0 +1,89 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import org.apache.http.HttpConnection;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.util.Args;
+
+/**
+ * Implementation of {@link HttpContext} that provides convenience
+ * setters for user assignable attributes and getter for readable attributes.
+ *
+ * @since 4.3
+ */
+@NotThreadSafe
+public class HttpCoreContext extends BasicHttpContext implements ExecutionContext {
+
+    protected <T> T getAttribute(final String attribname, final Class<T> clazz) {
+        Args.notNull(clazz, "Attribute class");
+        Object obj = getAttribute(attribname);
+        if (obj == null) {
+            return null;
+        }
+        return clazz.cast(obj);
+    }
+
+    public <T extends HttpConnection> T getConnection(final Class<T> clazz) {
+        return getAttribute(HTTP_CONNECTION, clazz);
+    }
+
+    public boolean isExpectContinue() {
+        Boolean b = getAttribute(HTTP_EXPECT_CONT, Boolean.class);
+        return b != null ? b.booleanValue() : false;
+    }
+
+    public void setExpectContinue() {
+        setAttribute(HTTP_EXPECT_CONT, true);
+    }
+
+    public HttpRequest getRequest() {
+        return getAttribute(HTTP_REQUEST, HttpRequest.class);
+    }
+
+    public boolean isRequestSent() {
+        Boolean b = getAttribute(HTTP_REQ_SENT, Boolean.class);
+        return b != null ? b.booleanValue() : false;
+    }
+
+    public HttpResponse getResponse() {
+        return getAttribute(HTTP_RESPONSE, HttpResponse.class);
+    }
+
+    public void setTarget(final HttpHost host) {
+        setAttribute(HTTP_TARGET_HOST, host);
+    }
+
+    public HttpHost getTarget() {
+        return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpCoreContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpClient.java?rev=1396521&r1=1396520&r2=1396521&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpClient.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/testserver/HttpClient.java Wed Oct 10 09:40:23 2012
@@ -40,9 +40,8 @@ import org.apache.http.HttpRequestInterc
 import org.apache.http.HttpResponse;
 import org.apache.http.impl.DefaultBHttpClientConnection;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
@@ -57,7 +56,7 @@ public class HttpClient {
     private final HttpProcessor httpproc;
     private final HttpRequestExecutor httpexecutor;
     private final ConnectionReuseStrategy connStrategy;
-    private final HttpContext context;
+    private final HttpCoreContext context;
 
     private volatile int timeout;
 
@@ -66,7 +65,7 @@ public class HttpClient {
         this.httpproc = httpproc;
         this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
         this.httpexecutor = new HttpRequestExecutor();
-        this.context = new BasicHttpContext();
+        this.context = new HttpCoreContext();
     }
 
     public HttpClient() {
@@ -108,10 +107,7 @@ public class HttpClient {
             final HttpRequest request,
             final HttpHost targetHost,
             final HttpClientConnection conn) throws HttpException, IOException {
-        this.context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
-        this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
-        this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-
+        this.context.setTarget(targetHost);
         this.httpexecutor.preProcess(request, this.httpproc, this.context);
         HttpResponse response = this.httpexecutor.execute(request, conn, this.context);
         this.httpexecutor.postProcess(response, this.httpproc, this.context);