[jboss-cvs] jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/handler ...
Chris Laprun
chris.laprun at jboss.com
Thu Aug 24 20:56:28 EDT 2006
User: claprun
Date: 06/08/24 20:56:28
Modified: wsrp/src/main/org/jboss/portal/wsrp/handler Tag:
JBoss_Portal_Branch_2_4
RequestHeaderClientHandler.java
Log:
- Re-wrote cookie/session management. Now handles cookies outside of initCookie calls. Still needs more testing.
- Added ProducerSessionInformationTestCase and other-tests in build.xml for WSRP.
- Minor improvements.
Revision Changes Path
No revision
No revision
1.7.2.1 +100 -23 jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/handler/RequestHeaderClientHandler.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RequestHeaderClientHandler.java
===================================================================
RCS file: /cvsroot/jboss/jboss-portal/wsrp/src/main/org/jboss/portal/wsrp/handler/RequestHeaderClientHandler.java,v
retrieving revision 1.7
retrieving revision 1.7.2.1
diff -u -b -r1.7 -r1.7.2.1
--- RequestHeaderClientHandler.java 10 Aug 2006 19:56:48 -0000 1.7
+++ RequestHeaderClientHandler.java 25 Aug 2006 00:56:28 -0000 1.7.2.1
@@ -21,15 +21,21 @@
*/
package org.jboss.portal.wsrp.handler;
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.cookie.MalformedCookieException;
+import org.apache.commons.httpclient.cookie.RFC2109Spec;
import org.jboss.portal.wsrp.consumer.ProducerSessionInformation;
import javax.xml.namespace.QName;
+import javax.xml.rpc.Stub;
import javax.xml.rpc.handler.GenericHandler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPMessage;
+import java.net.MalformedURLException;
+import java.net.URL;
/**
* A request handler that uses a thread local to setup cookies on the wire.
@@ -41,6 +47,7 @@
public class RequestHeaderClientHandler extends GenericHandler
{
private static final ThreadLocal local = new ThreadLocal();
+ private static final RFC2109Spec cookieParser = new RFC2109Spec();
public void init(HandlerInfo info)
{
@@ -53,14 +60,14 @@
public boolean handleRequest(MessageContext msgContext)
{
- CurrentInfo info = (CurrentInfo)local.get();
+ CurrentInfo info = getCurrentInfo(false);
if (info == null)
{
return super.handleRequest(msgContext);
}
ProducerSessionInformation sessionInfo = info.sessionInfo;
- if (sessionInfo == null || !sessionInfo.isUserCookiesInitialized())
+ if (sessionInfo == null)
{
return super.handleRequest(msgContext);
}
@@ -68,7 +75,7 @@
SOAPMessageContext smc = (SOAPMessageContext)msgContext;
SOAPMessage message = smc.getMessage();
MimeHeaders mimeHeaders = message.getMimeHeaders();
- String cookie;
+ StringBuffer cookie = new StringBuffer(64);
if (sessionInfo.isPerGroupCookies())
{
if (info.groupId == null)
@@ -76,44 +83,45 @@
throw new IllegalStateException("Was expecting a current group Id...");
}
- cookie = sessionInfo.getGroupCookieFor(info.groupId);
- }
- else
+ String groupCookie = sessionInfo.getGroupCookieFor(info.groupId);
+ if (groupCookie != null)
{
- cookie = sessionInfo.getUserCookie();
+ cookie.append(groupCookie).append(";");
}
-
- mimeHeaders.setHeader("Cookie", cookie);
-
- return super.handleRequest(msgContext);
}
- public boolean handleResponse(MessageContext msgContext)
- {
- CurrentInfo info = (CurrentInfo)local.get();
- if (info == null)
+ String userCookie = sessionInfo.getUserCookie();
+ if (userCookie != null)
{
- return super.handleResponse(msgContext);
+ cookie.append(userCookie);
}
- ProducerSessionInformation sessionInfo = info.sessionInfo;
- if (sessionInfo == null || sessionInfo.isUserCookiesInitialized())
+ if (cookie.length() != 0)
{
- return super.handleResponse(msgContext);
+ mimeHeaders.setHeader("Cookie", cookie.toString());
+ }
+
+ return super.handleRequest(msgContext);
}
+ public boolean handleResponse(MessageContext msgContext)
+ {
SOAPMessageContext smc = (SOAPMessageContext)msgContext;
SOAPMessage message = smc.getMessage();
MimeHeaders mimeHeaders = message.getMimeHeaders();
-
String[] cookieValues = mimeHeaders.getHeader("Set-Cookie");
+
if (cookieValues != null)
{
if (cookieValues.length > 1)
{
throw new IllegalArgumentException("Too many cookieValues headers!");
}
- String cookie = cookieValues[0];
+
+ Cookie[] cookies = extractCookies((String)msgContext.getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY), cookieValues);
+
+ CurrentInfo info = getCurrentInfo(true);
+ ProducerSessionInformation sessionInfo = info.sessionInfo;
if (sessionInfo.isPerGroupCookies())
{
@@ -122,17 +130,58 @@
throw new IllegalStateException("Was expecting a current group Id...");
}
- sessionInfo.addGroupCookie(info.groupId, cookie);
+ sessionInfo.setGroupCookieFor(info.groupId, cookies);
}
else
{
- sessionInfo.setUserCookie(cookie);
+ sessionInfo.setUserCookie(cookies);
}
}
return super.handleResponse(msgContext);
}
+ private Cookie[] extractCookies(String endpointAddress, String[] cookieValues)
+ {
+ if (endpointAddress == null)
+ {
+ throw new NullPointerException("Was expecting an endpoint address but none was provided in the MessageContext");
+ }
+
+ URL hostURL;
+ try
+ {
+ hostURL = new URL(endpointAddress);
+ }
+ catch (MalformedURLException e)
+ {
+ // should not happen
+ throw new IllegalArgumentException(endpointAddress + " is not a valid URL for the endpoint address.");
+ }
+
+ String cookie = cookieValues[0];
+ Cookie[] cookies;
+ try
+ {
+ String host = hostURL.getHost();
+ int port = hostURL.getPort();
+ String path = hostURL.getPath();
+ boolean secure = hostURL.getProtocol().endsWith("s"); // todo: is that correct?
+
+ cookies = cookieParser.parse(host, port, path, secure, cookie);
+
+ for (int i = 0; i < cookies.length; i++)
+ {
+ cookieParser.validate(host, port, path, secure, cookies[i]);
+ }
+ }
+ catch (MalformedCookieException e)
+ {
+ throw new IllegalArgumentException("Malformed cookie: " + cookie);
+ }
+ return cookies;
+ }
+
public static void setCurrentInfo(String groupId, ProducerSessionInformation sessionInformation)
{
local.set(new CurrentInfo(groupId, sessionInformation));
@@ -143,6 +192,23 @@
local.set(null);
}
+ public static ProducerSessionInformation getCurrentProducerSessionInformation()
+ {
+ return getCurrentProducerSessionInformation(false);
+ }
+
+ public static ProducerSessionInformation getCurrentProducerSessionInformation(boolean create)
+ {
+ CurrentInfo info = getCurrentInfo(create);
+
+ if (info != null)
+ {
+ return info.sessionInfo;
+ }
+
+ return null;
+ }
+
public static void setCurrentGroupId(String groupId)
{
CurrentInfo currentInfo = (CurrentInfo)local.get();
@@ -153,6 +219,17 @@
currentInfo.groupId = groupId;
}
+ private static CurrentInfo getCurrentInfo(boolean createIfNeeded)
+ {
+ CurrentInfo info = (CurrentInfo)local.get();
+ if (info == null && createIfNeeded)
+ {
+ info = new CurrentInfo(null, new ProducerSessionInformation());
+ local.set(info);
+ }
+ return info;
+ }
+
static class CurrentInfo
{
public CurrentInfo(String groupId, ProducerSessionInformation sessionInfo)
More information about the jboss-cvs-commits
mailing list