You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2002/07/24 00:35:37 UTC
cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/client ConnectionHelper.java JdkConnectionHelper.java
vmassol 2002/07/23 15:35:36
Modified: framework/src/java/share/org/apache/cactus Cookie.java
WebResponse.java
framework/src/java/share/org/apache/cactus/client
ConnectionHelper.java JdkConnectionHelper.java
Log:
refactoring. Moved Cookie static helper methods to the Cookie class
Revision Changes Path
1.3 +107 -1 jakarta-cactus/framework/src/java/share/org/apache/cactus/Cookie.java
Index: Cookie.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/Cookie.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Cookie.java 21 Jul 2002 12:09:16 -0000 1.2
+++ Cookie.java 23 Jul 2002 22:35:36 -0000 1.3
@@ -56,6 +56,9 @@
*/
package org.apache.cactus;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import java.io.Serializable;
import java.util.Date;
@@ -71,6 +74,11 @@
public class Cookie implements Serializable
{
/**
+ * The logger
+ */
+ private static final Log LOGGER = LogFactory.getLog(Cookie.class);
+
+ /**
* The cookie name
*/
private String name;
@@ -362,6 +370,104 @@
buffer.append("expiryDate = [" + getExpiryDate() + "]");
return buffer.toString();
+ }
+
+ /**
+ * Returns the domain that will be used to send the cookies. If a host
+ * was specified using <code>setURL()</code> then the domain will be
+ * this host. Otherwise it will be the real redirector host.
+ *
+ * @param theRequest the request containing all data to pass to the server
+ * redirector.
+ * @param theRealHost the real host to which we are connecting to. We will
+ * use it if no simulation host has been specified.
+ * @return the cookie domain to use
+ */
+ public static String getCookieDomain(WebRequest theRequest,
+ String theRealHost)
+ {
+ String domain;
+ ServletURL url = theRequest.getURL();
+
+ if ((url != null) && (url.getHost() != null)) {
+ domain = url.getHost();
+ } else {
+ domain = theRealHost;
+ }
+
+ LOGGER.debug("Cookie validation domain = [" + domain + "]");
+
+ return domain;
+ }
+
+ /**
+ * Returns the port that will be used to send the cookies. If a port
+ * was specified using <code>setURL()</code> then the port sent will be
+ * this port. Otherwise it will be the real redirector port.
+ *
+ * @param theRequest the request containing all data to pass to the server
+ * redirector.
+ * @param theRealPort the real port to which we are connecting to. We will
+ * use it if no simulation port has been specified.
+ * @return the cookie domain to use
+ */
+ public static int getCookiePort(WebRequest theRequest, int theRealPort)
+ {
+ int port;
+ ServletURL url = theRequest.getURL();
+
+ if ((url != null) && (url.getHost() != null)) {
+ port = url.getPort();
+ } else {
+ port = theRealPort;
+ }
+
+ LOGGER.debug("Cookie validation port = [" + port + "]");
+
+ return port;
+ }
+
+ /**
+ * Returns the path that will be used to validate if a cookie will be
+ * sent or not. The algorithm is as follows : if the cookie path is not
+ * set (i.e. null) then the cookie is always sent (provided the domain
+ * is right). If the cookie path is set, the cookie is sent only if
+ * the request path starts with the same string as the cookie path. If
+ * <code>setURL()</code> has been called, return the path it has been
+ * set to (context + servletPath + pathInfo). Otherwise return the
+ * real redirector path.
+ *
+ * @param theRequest the request containing all data to pass to the server
+ * redirector.
+ * @param theRealPath the real path to which we are connecting to. We will
+ * use it if no simulation path has been specified.
+ * @return the path to use to decide if a cookie will get sent
+ */
+ public static String getCookiePath(WebRequest theRequest,
+ String theRealPath)
+ {
+ String path;
+ ServletURL url = theRequest.getURL();
+
+ if ((url != null) && (url.getPath() != null)) {
+ path = url.getPath();
+ } else {
+ String file = theRealPath;
+ if (file != null) {
+ int q = file.lastIndexOf('?');
+ if (q != -1) {
+ path = file.substring(0, q);
+ } else {
+ path = file;
+ }
+ } else {
+ path = null;
+ }
+ }
+
+ LOGGER.debug("Cookie validation pah = [" + path + "]");
+
+ return path;
}
}
1.6 +7 -8 jakarta-cactus/framework/src/java/share/org/apache/cactus/WebResponse.java
Index: WebResponse.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/WebResponse.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- WebResponse.java 23 Jul 2002 22:07:52 -0000 1.5
+++ WebResponse.java 23 Jul 2002 22:35:36 -0000 1.6
@@ -68,7 +68,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.cactus.client.JdkConnectionHelper;
import org.apache.cactus.util.ChainedRuntimeException;
import org.apache.cactus.util.IoUtil;
@@ -246,12 +245,12 @@
org.apache.commons.httpclient.Cookie[] cookies;
try {
cookies = org.apache.commons.httpclient.Cookie.parse(
- JdkConnectionHelper.getDomain(getWebRequest(),
- getConnection()),
- JdkConnectionHelper.getPort(getWebRequest(),
- getConnection()),
- JdkConnectionHelper.getPath(getWebRequest(),
- getConnection()),
+ Cookie.getCookieDomain(getWebRequest(),
+ getConnection().getURL().getHost()),
+ Cookie.getCookiePort(getWebRequest(),
+ getConnection().getURL().getPort()),
+ Cookie.getCookiePath(getWebRequest(),
+ getConnection().getURL().getFile()),
new Header(headerName, headerValue));
} catch (HttpException e) {
throw new ChainedRuntimeException(
1.2 +2 -1 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ConnectionHelper.java
Index: ConnectionHelper.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ConnectionHelper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConnectionHelper.java 23 Jul 2002 22:03:53 -0000 1.1
+++ ConnectionHelper.java 23 Jul 2002 22:35:36 -0000 1.2
@@ -81,4 +81,5 @@
* @exception Throwable if an unexpected error occured
*/
HttpURLConnection connect(WebRequest theRequest) throws Throwable;
+
}
1.2 +10 -111 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/JdkConnectionHelper.java
Index: JdkConnectionHelper.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/JdkConnectionHelper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JdkConnectionHelper.java 23 Jul 2002 22:03:53 -0000 1.1
+++ JdkConnectionHelper.java 23 Jul 2002 22:35:36 -0000 1.2
@@ -72,8 +72,8 @@
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
-import org.apache.cactus.ServletURL;
import org.apache.cactus.WebRequest;
+import org.apache.cactus.Cookie;
import org.apache.cactus.client.authentication.AbstractAuthentication;
import org.apache.cactus.util.ChainedRuntimeException;
@@ -365,8 +365,8 @@
// If no domain has been specified, use a default one
String domain;
if (cactusCookie.getDomain() == null) {
- domain = JdkConnectionHelper.getDomain(theRequest,
- theConnection);
+ domain = Cookie.getCookieDomain(theRequest,
+ theConnection.getURL().getHost());
} else {
domain = cactusCookie.getDomain();
}
@@ -374,8 +374,8 @@
// If not path has been specified , use a default one
String path;
if (cactusCookie.getPath() == null) {
- path = JdkConnectionHelper.getPath(theRequest,
- theConnection);
+ path = Cookie.getCookiePath(theRequest,
+ theConnection.getURL().getFile());
} else {
path = cactusCookie.getPath();
}
@@ -395,8 +395,10 @@
// and create the cookie header to send
Header cookieHeader =
org.apache.commons.httpclient.Cookie.createCookieHeader(
- JdkConnectionHelper.getDomain(theRequest, theConnection),
- JdkConnectionHelper.getPath(theRequest, theConnection),
+ Cookie.getCookieDomain(theRequest,
+ theConnection.getURL().getHost()),
+ Cookie.getCookiePath(theRequest,
+ theConnection.getURL().getFile()),
httpclientCookies);
LOGGER.debug("Cookie string = [" + cookieHeader.getValue()
@@ -405,109 +407,6 @@
theConnection.setRequestProperty("Cookie",
cookieHeader.getValue());
}
- }
-
- /**
- * Returns the domain that will be used to send the cookies. If a host
- * was specified using <code>setURL()</code> then the domain will be
- * this host. Otherwise it will be the redirector host.
- *
- * @param theRequest the request containing all data to pass to the server
- * redirector.
- * @param theConnection the HTTP connection
- * @return the cookie domain to use
- */
- public static String getDomain(WebRequest theRequest,
- URLConnection theConnection)
- {
- String domain;
- ServletURL url = theRequest.getURL();
-
- if ((url != null) && (url.getHost() != null)) {
- domain = url.getHost();
- } else {
- domain = theConnection.getURL().getHost();
- }
-
- LOGGER.debug("Cookie validation domain = [" + domain + "]");
-
- return domain;
- }
-
- /**
- * Returns the domain that will be used to send the cookies. If a host
- * was specified using <code>setURL()</code> then the domain will be
- * this host. Otherwise it will be the redirector host.
- *
- * @param theRequest the request containing all data to pass to the server
- * redirector.
- * @param theConnection the HTTP connection
- * @return the cookie domain to use
- */
- public static int getPort(WebRequest theRequest,
- URLConnection theConnection)
- {
- int port;
- ServletURL url = theRequest.getURL();
-
- if ((url != null) && (url.getHost() != null)) {
- port = url.getPort();
- } else {
- port = theConnection.getURL().getPort();
- }
-
- LOGGER.debug("Cookie validation port = [" + port + "]");
-
- return port;
- }
-
- /**
- * Returns the path that will be used to validate if a cookie will be
- * sent or not. The algorithm is as follows : if the cookie path is not
- * set (i.e. null) then the cookie is always sent (provided the domain
- * is right). If the cookie path is set, the cookie is sent only if
- * the request path starts with the same string as the cookie path. If
- * <code>setURL()</code> has been called, return the path it has been
- * set to (context + servletPath + pathInfo). Otherwise return the
- * redirector path.
- *
- * @param theRequest the request containing all data to pass to the server
- * redirector.
- * @param theConnection the HTTP connection
- * @return the path to use to decide if a cookie will get sent
- */
- public static String getPath(WebRequest theRequest,
- URLConnection theConnection)
- {
- String path;
- ServletURL url = theRequest.getURL();
-
- if ((url != null) && (url.getPath() != null)) {
- path = url.getPath();
- } else {
-
- // We do not use the URL.getPath() API as it was only introduced
- // in JDK 1.3 and we want to retain compatibility with JDK 1.2.
- // Using JDK 1.3, we would have written :
- // path = theConnection.getURL().getPath();
-
- String file = theConnection.getURL().getFile();
- if (file != null) {
- int q = file.lastIndexOf('?');
- if (q != -1) {
- path = file.substring(0, q);
- } else {
- path = file;
- }
- } else {
- path = null;
- }
-
- }
-
- LOGGER.debug("Cookie validation pah = [" + path + "]");
-
- return path;
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>