hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r552166 - in /jakarta/httpcomponents/httpclient/trunk/module-client/src: examples/org/apache/http/examples/client/ main/java/org/apache/http/client/ main/java/org/apache/http/client/protocol/ main/java/org/apache/http/impl/client/
Date Sat, 30 Jun 2007 15:57:38 GMT
Author: olegk
Date: Sat Jun 30 08:57:37 2007
New Revision: 552166

URL: http://svn.apache.org/viewvc?view=rev&rev=552166
Log:
* Renamed HttpClient#getContext() to HttpClient#getDefaultContext()
* Tweaked the way DefaualtHttpClient populates the default HTTP context returned with HttpClient#getDefaultContext()
* Added local HTTP context example

Added:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java
      - copied, changed from r552157, jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/MethodAbort.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
  (with props)
Removed:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/MethodAbort.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/HttpClient.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/HttpClientContext.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java

Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java
(from r552157, jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/MethodAbort.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java?view=diff&rev=552166&p1=jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/MethodAbort.java&r1=552157&p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java&r2=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/MethodAbort.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientAbortMethod.java
Sat Jun 30 08:57:37 2007
@@ -49,7 +49,7 @@
  *
  * @since 4.0
  */
-public class MethodAbort {
+public class ClientAbortMethod {
 
     public final static void main(String[] args) throws Exception {
 
@@ -57,7 +57,7 @@
         HttpParams params = new BasicHttpParams();
         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
         HttpProtocolParams.setContentCharset(params, "UTF-8");
-        HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
+        HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
         
         HttpClient httpclient = new DefaultHttpClient(params);
 

Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java?view=auto&rev=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
Sat Jun 30 08:57:37 2007
@@ -0,0 +1,101 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.examples.client;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpState;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.cookie.Cookie;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+
+/**
+ * This examples demonstrates the use of a local HTTP context
+ * populated with custom attributes.
+ *
+ * <!-- empty lines above to avoid 'svn diff' context problems -->
+ * @version $Revision$
+ *
+ * @since 4.0
+ */
+public class ClientCustomContext {
+
+    public final static void main(String[] args) throws Exception {
+
+        // Initialize default parameters
+        HttpParams params = new BasicHttpParams();
+        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
+        HttpProtocolParams.setContentCharset(params, "UTF-8");
+        HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
+        
+        HttpClient httpclient = new DefaultHttpClient(params);
+
+        // Create a local instance of HttpState
+        HttpState localState = new HttpState();
+        
+        // Obtain default HTTP context
+        HttpContext defaultContext = httpclient.getDefaultContext();
+        // Create local HTTP context
+        HttpContext localContent = new HttpClientContext(defaultContext);
+        // Bind custom HTTP state to the local context
+        localContent.setAttribute(HttpClientContext.HTTP_STATE, localState);
+        
+        HttpGet httpget = new HttpGet("http://www.google.com/"); 
+
+        System.out.println("executing request " + httpget.getURI());
+
+        // Pass local context as a parameter
+        HttpResponse response = httpclient.execute(httpget, localContent);
+        HttpEntity entity = response.getEntity();
+
+        System.out.println("----------------------------------------");
+        System.out.println(response.getStatusLine());
+        if (entity != null) {
+            System.out.println("Response content length: " + entity.getContentLength());
+            System.out.println("Chunked?: " + entity.isChunked());
+        }
+        Cookie[] cookies = localState.getCookies();
+        for (int i = 0; i < cookies.length; i++) {
+            System.out.println("Local cookie: " + cookies[i]);
+        }
+        System.out.println("----------------------------------------");
+    }
+    
+}
+

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/HttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/HttpClient.java?view=diff&rev=552166&r1=552165&r2=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/HttpClient.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/HttpClient.java
Sat Jun 30 08:57:37 2007
@@ -60,13 +60,13 @@
 
 
     /**
-     * Obtains the default context used by this client.
-     * This context will be used by default when executing requests
-     * with this client.
+     * Obtains the default context used by this client populated with
+     * default attributes. This context will be used by default when 
+     * executing requests with this client.
      *
      * @return  the default context
      */
-    HttpContext getContext()
+    HttpContext getDefaultContext()
         ;
 
 

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/HttpClientContext.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/HttpClientContext.java?view=diff&rev=552166&r1=552165&r2=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/HttpClientContext.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/protocol/HttpClientContext.java
Sat Jun 30 08:57:37 2007
@@ -36,7 +36,6 @@
 
 public class HttpClientContext extends HttpExecutionContext {
     
-    public static final String SCHEME_REGISTRY       = "http.scheme-registry"; 
     public static final String COOKIESPEC_REGISTRY   = "http.cookiespec-registry"; 
     public static final String AUTHSCHEME_REGISTRY   = "http.authscheme-registry"; 
     public static final String HTTP_STATE            = "http.state"; 

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java?view=diff&rev=552166&r1=552165&r2=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
Sat Jun 30 08:57:37 2007
@@ -51,12 +51,12 @@
 import org.apache.http.client.RedirectHandler;
 import org.apache.http.client.RoutedRequest;
 import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.cookie.CookieSpecRegistry;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.BasicHttpProcessor;
 import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpExecutionContext;
 import org.apache.http.protocol.HttpRequestInterceptorList;
 import org.apache.http.protocol.HttpResponseInterceptorList;
 
@@ -292,10 +292,11 @@
     }
 
 
-    public synchronized final HttpContext getContext() {
+    public synchronized final HttpContext getDefaultContext() {
         if (defaultContext == null) {
             defaultContext = createHttpContext();
         }
+        populateContext(defaultContext);
         return defaultContext;
     }
     
@@ -395,9 +396,7 @@
         
         synchronized (this) {
             if (context == null) {
-                context = new HttpExecutionContext(getContext());
-                // Populate the context for this request
-                populateContext(context);
+                context = new HttpClientContext(getDefaultContext());
             }
         }
         
@@ -428,9 +427,7 @@
         // all shared objects that are potentially threading unsafe.
         synchronized (this) {
             if (context == null) {
-                context = new HttpExecutionContext(getContext());
-                // Populate the context for this request
-                populateContext(context);
+                context = new HttpClientContext(getDefaultContext());
             }
             // Create a director for this request
             director = new DefaultClientRequestDirector(

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java?view=diff&rev=552166&r1=552165&r2=552166
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java
Sat Jun 30 08:57:37 2007
@@ -118,7 +118,7 @@
         HttpParams params = new BasicHttpParams();
         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
         HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
-        HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
+        HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0");
         HttpProtocolParams.setUseExpectContinue(params, true);
         return params;
     }
@@ -235,9 +235,6 @@
 
     protected void populateContext(final HttpContext context) {
         context.setAttribute(
-                HttpClientContext.SCHEME_REGISTRY, 
-                getConnectionManager().getSchemeRegistry());
-        context.setAttribute(
                 HttpClientContext.AUTHSCHEME_REGISTRY, 
                 getAuthSchemes());
         context.setAttribute(
@@ -266,13 +263,17 @@
 
         HttpHost proxy = (HttpHost) request.getParams().getParameter(
                 HttpClientParams.DEFAULT_PROXY);
-        
-        Scheme schm = getConnectionManager().getSchemeRegistry().
-            getScheme(target.getSchemeName());
-        // as it is typically used for TLS/SSL, we assume that
-        // a layered scheme implies a secure connection
-        HttpRoute route = new HttpRoute(target, null, proxy, schm.isLayered());
 
+        HttpRoute route;
+        if (proxy == null) {
+            route = new HttpRoute(target);
+        } else {
+            Scheme schm = getConnectionManager().getSchemeRegistry().
+            getScheme(target.getSchemeName());
+            // as it is typically used for TLS/SSL, we assume that
+            // a layered scheme implies a secure connection
+            route = new HttpRoute(target, null, proxy, schm.isLayered());
+        }
         return new RoutedRequest.Impl(request, route);
     }
 



Mime
View raw message