You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2001/10/15 15:09:08 UTC
cvs commit: xml-cocoon2/src/org/apache/cocoon/environment/commandline CommandLineRequest.java CommandLineSession.java
cziegeler 01/10/15 06:09:08
Modified: src/org/apache/cocoon/environment/commandline Tag:
cocoon_20_branch CommandLineRequest.java
CommandLineSession.java
Log:
Better session handling for commandline interface
Revision Changes Path
No revision
No revision
1.2.2.7 +160 -22 xml-cocoon2/src/org/apache/cocoon/environment/commandline/CommandLineRequest.java
Index: CommandLineRequest.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/environment/commandline/CommandLineRequest.java,v
retrieving revision 1.2.2.6
retrieving revision 1.2.2.7
diff -u -r1.2.2.6 -r1.2.2.7
--- CommandLineRequest.java 2001/10/11 08:56:10 1.2.2.6
+++ CommandLineRequest.java 2001/10/15 13:09:08 1.2.2.7
@@ -20,7 +20,7 @@
* Creates a specific servlet request simulation from command line usage.
*
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
- * @version CVS $Revision: 1.2.2.6 $ $Date: 2001/10/11 08:56:10 $
+ * @version CVS $Revision: 1.2.2.7 $ $Date: 2001/10/15 13:09:08 $
*/
/*
@@ -59,26 +59,43 @@
private Map parameters;
private Map headers;
private String characterEncoding = null;
- private CommandLineSession session = new CommandLineSession();
- public CommandLineRequest(Environment env, String contextPath, String servletPath, String pathInfo) {
+ public CommandLineRequest(Environment env,
+ String contextPath,
+ String servletPath,
+ String pathInfo) {
this(env, contextPath, servletPath, pathInfo, null, null, null);
}
- public CommandLineRequest(Environment env, String contextPath, String servletPath, String pathInfo, Map attributes) {
+ public CommandLineRequest(Environment env,
+ String contextPath,
+ String servletPath,
+ String pathInfo,
+ Map attributes) {
this(env, contextPath, servletPath, pathInfo, attributes, null, null);
}
- public CommandLineRequest(Environment env, String contextPath, String servletPath, String pathInfo, Map attributes, Map parameters) {
+ public CommandLineRequest(Environment env,
+ String contextPath,
+ String servletPath,
+ String pathInfo,
+ Map attributes,
+ Map parameters) {
this(env, contextPath, servletPath, pathInfo, attributes, parameters, null);
}
- public CommandLineRequest(Environment env, String contextPath, String servletPath, String pathInfo, Map attributes, Map parameters, Map headers) {
+ public CommandLineRequest(Environment env,
+ String contextPath,
+ String servletPath,
+ String pathInfo,
+ Map attributes,
+ Map parameters,
+ Map headers) {
this.env = env;
this.contextPath = contextPath;
this.servletPath = servletPath;
this.pathInfo = pathInfo;
- this.attributes = attributes;
+ this.attributes = (attributes == null ? new HashMap() : attributes);
this.parameters = parameters;
this.headers = headers;
}
@@ -103,16 +120,16 @@
public String getPathTranslated() { return null; } // FIXME (SM) this is legal but should we do something more?
public Object getAttribute(String name) {
- return (attributes != null) ? attributes.get(name) : null;
+ return this.attributes.get(name);
}
public Enumeration getAttributeNames() {
- return (attributes != null) ? new IteratorWrapper(attributes.keySet().iterator()) : null;
+ return new IteratorWrapper(this.attributes.keySet().iterator());
}
public void setAttribute(String name, Object value) {
- if (attributes != null) attributes.put(name, value);
+ this.attributes.put(name, value);
}
public void removeAttribute(String name) {
- if (attributes != null) attributes.remove(name);
+ this.attributes.remove(name);
}
public String getParameter(String name) {
@@ -121,12 +138,16 @@
public Enumeration getParameterNames() {
return (parameters != null) ? new IteratorWrapper(parameters.keySet().iterator()) : null;
}
+
public String[] getParameterValues(String name) {
- Object [] obj = parameters.values().toArray();
- String [] str = new String [obj.length];
- for (int i = 0; i < obj.length; i++)
- str[i] = (String)obj[i];
- return str;
+ if (parameters != null) {
+ Object [] obj = parameters.values().toArray();
+ String [] str = new String [obj.length];
+ for (int i = 0; i < obj.length; i++)
+ str[i] = (String)obj[i];
+ return str;
+ }
+ return null;
}
public String getHeader(String name) {
@@ -169,14 +190,131 @@
public Map getCookieMap() {
return Collections.unmodifiableMap(new HashMap());
}
+
+ /**
+ *
+ * Returns the current session associated with this request,
+ * or if the request does not have a session, creates one.
+ *
+ * @return the <code>Session</code> associated
+ * with this request
+ *
+ * @see #getSession(boolean)
+ *
+ */
public Session getSession() {
- return session;
+ return this.getSession(true);
+ }
+
+ /**
+ *
+ * Returns the current <code>Session</code>
+ * associated with this request or, if if there is no
+ * current session and <code>create</code> is true, returns
+ * a new session.
+ *
+ * <p>If <code>create</code> is <code>false</code>
+ * and the request has no valid <code>Session</code>,
+ * this method returns <code>null</code>.
+ *
+ * <p>To make sure the session is properly maintained,
+ * you must call this method before
+ * the response is committed.
+ *
+ *
+ *
+ *
+ * @param <code>true</code> to create
+ * a new session for this request if necessary;
+ * <code>false</code> to return <code>null</code>
+ * if there's no current session
+ *
+ *
+ * @return the <code>Session</code> associated
+ * with this request or <code>null</code> if
+ * <code>create</code> is <code>false</code>
+ * and the request has no valid session
+ *
+ * @see #getSession()
+ *
+ *
+ */
+ public Session getSession(boolean create) {
+ return CommandLineSession.getSession(create);
+ }
+
+ /**
+ *
+ * Returns the session ID specified by the client. This may
+ * not be the same as the ID of the actual session in use.
+ * For example, if the request specified an old (expired)
+ * session ID and the server has started a new session, this
+ * method gets a new session with a new ID. If the request
+ * did not specify a session ID, this method returns
+ * <code>null</code>.
+ *
+ *
+ * @return a <code>String</code> specifying the session
+ * ID, or <code>null</code> if the request did
+ * not specify a session ID
+ *
+ * @see #isRequestedSessionIdValid
+ *
+ */
+ public String getRequestedSessionId() {
+ return (CommandLineSession.getSession(false) != null) ?
+ CommandLineSession.getSession(false).getId() : null;
+ }
+
+ /**
+ *
+ * Checks whether the requested session ID is still valid.
+ *
+ * @return <code>true</code> if this
+ * request has an id for a valid session
+ * in the current session context;
+ * <code>false</code> otherwise
+ *
+ * @see #getRequestedSessionId
+ * @see #getSession
+ *
+ */
+ public boolean isRequestedSessionIdValid() {
+ return (CommandLineSession.getSession(false) != null);
+ }
+
+ /**
+ *
+ * Checks whether the requested session ID came in as a cookie.
+ *
+ * @return <code>true</code> if the session ID
+ * came in as a
+ * cookie; otherwise, <code>false</code>
+ *
+ *
+ * @see #getSession
+ *
+ */
+ public boolean isRequestedSessionIdFromCookie() {
+ return false;
+ }
+
+ /**
+ *
+ * Checks whether the requested session ID came in as part of the
+ * request URL.
+ *
+ * @return <code>true</code> if the session ID
+ * came in as part of a URL; otherwise,
+ * <code>false</code>
+ *
+ *
+ * @see #getSession
+ *
+ */
+ public boolean isRequestedSessionIdFromURL() {
+ return false;
}
- public Session getSession(boolean create) { return null; }
- public String getRequestedSessionId() { return null; }
- public boolean isRequestedSessionIdValid() { return false; }
- public boolean isRequestedSessionIdFromCookie() { return false; }
- public boolean isRequestedSessionIdFromURL() { return false; }
public Locale getLocale() { return Locale.getDefault(); }
public Enumeration getLocales() {
1.1.2.4 +22 -1 xml-cocoon2/src/org/apache/cocoon/environment/commandline/CommandLineSession.java
Index: CommandLineSession.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/environment/commandline/CommandLineSession.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- CommandLineSession.java 2001/10/11 08:56:10 1.1.2.3
+++ CommandLineSession.java 2001/10/15 13:09:08 1.1.2.4
@@ -18,7 +18,7 @@
* Command-line version of Http Session.
*
* @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
- * @version CVS $Revision: 1.1.2.3 $ $Date: 2001/10/11 08:56:10 $
+ * @version CVS $Revision: 1.1.2.4 $ $Date: 2001/10/15 13:09:08 $
*/
public final class CommandLineSession
implements Session {
@@ -68,10 +68,31 @@
public void invalidate() {
this.attributes.clear();
+ invalidateSession();
}
public boolean isNew() {
return false;
}
+
+ protected static CommandLineSession session;
+
+ /**
+ * Get the current session object - if available
+ */
+ public static Session getSession(boolean create) {
+ if (create == true && session == null) {
+ session = new CommandLineSession();
+ }
+ return session;
+ }
+
+ /**
+ * Invalidate the current session
+ */
+ public static void invalidateSession() {
+ session = null;
+ }
+
}
----------------------------------------------------------------------
In case of troubles, e-mail: webmaster@xml.apache.org
To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org