You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mo...@apache.org on 2002/10/02 20:32:39 UTC
cvs commit: jakarta-commons/latka/src/java/org/apache/commons/latka/xml RequestHandler.java
morgand 2002/10/02 11:32:39
Modified: latka/src/java/org/apache/commons/latka/http Request.java
RequestImpl.java
latka/src/java/org/apache/commons/latka/xml
RequestHandler.java
Log:
moved HttpClient redirect workaround from RequestHandler to
RequestImpl
Revision Changes Path
1.18 +20 -1 jakarta-commons/latka/src/java/org/apache/commons/latka/http/Request.java
Index: Request.java
===================================================================
RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/http/Request.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Request.java 4 Sep 2002 02:59:26 -0000 1.17
+++ Request.java 2 Oct 2002 18:32:39 -0000 1.18
@@ -80,7 +80,12 @@
public static final int HTTP_METHOD_HEAD = 2;
/**
- * Execute this HTTP request.
+ * Execute this HTTP request. In the event of a 301 or 302,
+ * this method may need to create a new Request,
+ * due to an idiosyncracy of
+ * HttpClient. In that case, the Response.getRequest()
+ * method will return the actual Request that was executed.
+ * This will probably be fixed at some point in the future.
*
* @return a Response object represnting the HTTP response to the request
*
@@ -185,6 +190,13 @@
void setCredentials(Credentials credentials);
/**
+ * Return the credentials for this request
+ *
+ * @return Request credentials
+ */
+ Credentials getCredentials();
+
+ /**
* Whether or not this request will instruct HttpClient
* to follow local redirects automatically.
*
@@ -216,5 +228,12 @@
* @param version HTTP version.
*/
void setVersion(String version);
+
+ /**
+ * Get the HTTP version to use in this request
+ *
+ * @return HTTP version for the request
+ */
+ String getVersion();
}
1.34 +73 -7 jakarta-commons/latka/src/java/org/apache/commons/latka/http/RequestImpl.java
Index: RequestImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/http/RequestImpl.java,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- RequestImpl.java 30 Sep 2002 23:35:54 -0000 1.33
+++ RequestImpl.java 2 Oct 2002 18:32:39 -0000 1.34
@@ -65,6 +65,7 @@
import java.io.InputStream;
import java.io.IOException;
import java.util.List;
+import java.util.LinkedList;
// latka imports
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.HttpClient;
@@ -104,6 +105,8 @@
protected int _method = -1;
/** http session the request is part of */
protected SessionImpl _session = null;
+ /** credentials for the request */
+ protected Credentials _credentials = null;
/** http method implementation */
protected HttpMethod _httpMethod = null;
/** URL being requested */
@@ -126,16 +129,23 @@
/** object used to perform the http requests */
protected HttpClient _httpClient = new HttpClient();
/** proxy used to perform the requests */
- private Proxy _proxy;
+ private Proxy _proxy = null;
/** log4j category used when logging messages */
protected static final Category _log =
Category.getInstance(RequestImpl.class);
-
/**
* HTTP Version 1.0
*/
private static final String HTTP_10 = "1.0";
+ /**
+ * HTTP Version 1.1
+ */
+ private static final String HTTP_11 = "1.1";
+
+ protected String _httpVersion = HTTP_11;
+
+ protected List _visitedURLs = new LinkedList();
/**
* Create a request for the specified URL, process using the supplied method
@@ -247,6 +257,11 @@
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
credentials.getUserName(), credentials.getPassword());
_session._state.setCredentials(null, creds);
+ _credentials = credentials;
+ }
+
+ public Credentials getCredentials() {
+ return _credentials;
}
/**
@@ -255,7 +270,7 @@
* @throws IOException when there are problems reading and writing
* @see Request#execute()
*/
- public Response execute() throws IOException {
+ protected Response executeRequestPerHost() throws IOException {
// set the request headers in HTTPClient
List headers = _requestHeaders.getHeaders();
@@ -332,6 +347,47 @@
}
/**
+ * Executes the request. In the event of a 301 or 302,
+ * this method may need to create a new Request,
+ * due to an idiosyncracy of
+ * HttpClient. In that case, the Response.getRequest()
+ * method will return the actual Request that was executed.
+ */
+ public Response execute() throws IOException {
+ Response response = executeRequestPerHost();
+
+ if (followRedirects() == false) {
+ return response;
+ }
+
+ Request lastRequest = this;
+ // execute the request until either we get a non-redirect response, or
+ // we visit a URL we have already visited
+ while (response.getStatusCode() == 301 || response.getStatusCode() == 302) {
+ // follow the redirect
+ URL url = new URL(response.getHeader("location"));
+
+ if (_visitedURLs.contains(url.toString())) {
+ return response;
+ }
+
+ Request request = _session.createRequest(lastRequest.getLabel(), url,
+ lastRequest.getMethod(), lastRequest.getVersion(), true, getProxy());
+ request.setParameters(lastRequest.getParameters());
+ request.setHeaders(lastRequest.getHeaders());
+ Credentials credentials = lastRequest.getCredentials();
+ if (credentials != null) {
+ request.setCredentials(credentials);
+ }
+ response = request.execute();
+ _visitedURLs.add(url.toString());
+ lastRequest = request;
+ }
+
+ return response;
+ }
+
+ /**
* Get the URL used for the request
* @return the {@link URL} of the request
* @see Request#getURL
@@ -520,5 +576,15 @@
*/
public void setVersion(String version) {
((HttpMethodBase) _httpMethod).setHttp11(!HTTP_10.equals(version));
+ _httpVersion = version;
+ }
+
+ /**
+ * Get the HTTP version to use in this request.
+ *
+ * @return HTTP version for the request
+ */
+ public String getVersion() {
+ return _httpVersion;
}
}
1.26 +6 -39 jakarta-commons/latka/src/java/org/apache/commons/latka/xml/RequestHandler.java
Index: RequestHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/RequestHandler.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- RequestHandler.java 16 Jul 2002 22:57:50 -0000 1.25
+++ RequestHandler.java 2 Oct 2002 18:32:39 -0000 1.26
@@ -66,8 +66,6 @@
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Properties;
import org.apache.commons.latka.LatkaProperties;
@@ -108,11 +106,7 @@
protected Request _request = null;
protected Response _response = null;
- protected String _version = "1.1";
protected boolean _requestExecuted = false;
- protected Credentials _credentials = null;
-
- protected List _visitedURLs = new LinkedList();
protected static final Category _log =
Category.getInstance(RequestHandler.class);
@@ -170,9 +164,9 @@
_log.info("request skipped");
}
} else if (localName.equals("credentials")) {
- _credentials = new CredentialsImpl(atts.getValue("userName"),
+ Credentials credentials = new CredentialsImpl(atts.getValue("userName"),
atts.getValue("password"));
- _request.setCredentials(_credentials);
+ _request.setCredentials(credentials);
} else if (localName.equals("requestHeader")) {
_log.info("adding request headers");
RequestHeaderHandler headerHandler = new RequestHeaderHandler(
@@ -231,7 +225,6 @@
}
}
-
/**
* Executes the request. In the event of a 301 or 302,
* this method may reassign the _request variable to
@@ -239,33 +232,7 @@
*/
protected Response executeRequest() throws IOException {
Response response = _request.execute();
-
- if (_request.followRedirects() == false) {
- return response;
- }
-
- // execute the request until either we get a non-redirect response, or
- // we visit a URL we have already visited
- while (response.getStatusCode() == 301 || response.getStatusCode() == 302) {
- // follow the redirect
- URL url = new URL(response.getHeader("location"));
-
- if (_visitedURLs.contains(url.toString())) {
- return response;
- }
-
- Request request = _session.createRequest(_request.getLabel(), url,
- _request.getMethod(), _version, true, getProxy());
- request.setParameters(_request.getParameters());
- request.setHeaders(_request.getHeaders());
- if (_credentials != null) {
- request.setCredentials(_credentials);
- }
- _request = request;
- response = _request.execute();
- _visitedURLs.add(url.toString());
- }
-
+ _request = response.getRequest();
return response;
}
@@ -337,8 +304,8 @@
followRedirects = false;
}
- _version = atts.getValue("version");
- return _session.createRequest(label, url, method, _version,
+ String version = atts.getValue("version");
+ return _session.createRequest(label, url, method, version,
followRedirects, getProxy());
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>