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 2007/05/07 22:08:32 UTC

svn commit: r535975 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http: client/methods/HttpUriRequest.java client/protocol/RequestAddCookies.java impl/client/RequestWrapper.java

Author: olegk
Date: Mon May  7 13:08:31 2007
New Revision: 535975

URL: http://svn.apache.org/viewvc?view=rev&rev=535975
Log:
Changelog:
* Added an extended version of the HttpRequest interface that provides convenience methods to access request properties such as parsed request URI and method type.
* Added a wrapper class for HttpRequests that can be used to change properties of the current request without modifying the original request object.

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpUriRequest.java   (with props)
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/RequestWrapper.java   (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/protocol/RequestAddCookies.java

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpUriRequest.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpUriRequest.java?view=auto&rev=535975
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpUriRequest.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpUriRequest.java Mon May  7 13:08:31 2007
@@ -0,0 +1,59 @@
+/*
+ * $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.client.methods;
+
+import java.net.URI;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpVersion;
+
+/**
+ * Extended version of the {@link HttpRequest} interface that provides 
+ * convenience methods to access request properties such as request URI
+ * and method type.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * <!-- empty lines to avoid svn diff problems -->
+ * @version   $Revision$
+ *
+ * @since 4.0
+ */
+public interface HttpUriRequest extends HttpRequest {
+    
+    String getMethod();
+    
+    HttpVersion getHttpVersion();
+
+    URI getURI();
+    
+}

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

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

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

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/protocol/RequestAddCookies.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/protocol/RequestAddCookies.java?view=diff&rev=535975&r1=535974&r2=535975
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/protocol/RequestAddCookies.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/protocol/RequestAddCookies.java Mon May  7 13:08:31 2007
@@ -46,6 +46,7 @@
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.ProtocolException;
 import org.apache.http.client.HttpState;
+import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.cookie.Cookie;
@@ -119,11 +120,15 @@
         }
         
         URI requestURI;
-        try {
-            requestURI = new URI(request.getRequestLine().getUri());
-        } catch (URISyntaxException ex) {
-            throw new ProtocolException("Invalid request URI: " + 
-                    request.getRequestLine().getUri(), ex);
+        if (request instanceof HttpUriRequest) {
+            requestURI = ((HttpUriRequest) request).getURI();
+        } else {
+            try {
+                requestURI = new URI(request.getRequestLine().getUri());
+            } catch (URISyntaxException ex) {
+                throw new ProtocolException("Invalid request URI: " + 
+                        request.getRequestLine().getUri(), ex);
+            }
         }
         
         CookieOrigin cookieOrigin = new CookieOrigin(

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/RequestWrapper.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/RequestWrapper.java?view=auto&rev=535975
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/RequestWrapper.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/RequestWrapper.java Mon May  7 13:08:31 2007
@@ -0,0 +1,137 @@
+/*
+ * $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.impl.client;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpVersion;
+import org.apache.http.RequestLine;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.message.AbstractHttpMessage;
+import org.apache.http.message.BasicRequestLine;
+import org.apache.http.params.HttpProtocolParams;
+
+/**
+ * A wrapper class for {@link HttpRequest}s that can be used to change
+ * properties of the current request without modifying the original
+ * object.
+ * </p>
+ * This class is also capable of resetting the request headers to
+ * the state of the original request.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
+class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
+    
+    private final HttpRequest original;
+
+    private URI uri;
+    private String method;
+    private HttpVersion version;
+    
+    public RequestWrapper(final HttpRequest request) throws URISyntaxException {
+        super();
+        if (request == null) {
+            throw new IllegalArgumentException("HTTP request may not be null");
+        }
+        this.original = request;
+        // Make a copy of original headers
+        setHeaders(request.getAllHeaders());
+        setParams(request.getParams());
+        // Make a copy of the original URI 
+        if (request instanceof HttpUriRequest) {
+            this.uri = ((HttpUriRequest) request).getURI();
+            this.method = ((HttpUriRequest) request).getMethod();
+            this.version = null;
+        } else {
+            RequestLine requestLine = request.getRequestLine();
+            this.uri = new URI(requestLine.getUri());
+            this.method = requestLine.getMethod();
+            this.version = request.getHttpVersion();
+        }
+    }
+
+    public String getMethod() {
+        return this.method;
+    }
+
+    public void setMethod(final String method) {
+        if (method == null) {
+            throw new IllegalArgumentException("Method name may not be null");
+        }
+        this.method = method;
+    }
+
+    public HttpVersion getHttpVersion() {
+        if (this.version != null) {
+            return this.version;
+        } else {
+            return HttpProtocolParams.getVersion(getParams());
+        }
+    }
+
+    public void setVersion(final HttpVersion version) {
+        this.version = version;
+    }
+
+    public URI getURI() {
+        return this.uri;
+    }
+    
+    public void setURI(final URI uri) {
+        this.uri = uri;
+    }
+
+    public RequestLine getRequestLine() {
+        String method = getMethod();
+        HttpVersion ver = getHttpVersion();
+        URI uri = getURI();
+        String uritext;
+        if (uri != null) {
+            uritext = uri.toASCIIString();
+        } else {
+            uritext = "/";
+        }
+        return new BasicRequestLine(method, uritext, ver);
+    }
+
+    public void resetHeaders() {
+        setHeaders(this.original.getAllHeaders());
+    }
+    
+}

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

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

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