proposal for bean population
"Eelco Hillenius" <[email protected]>
| Newsgroups | gmane.comp.web.maverick.general |
|---|---|
| Message-ID | <003301c300e0$5e5aa920$01a8a8c0@Eelco> |
Hi all, I propose the following for FormBeanUser, ThrowawayFormBeanUser and ThrowawayBean: Add another populate that get its map from the request attributes. This is usefull when linking forwards or includes. For example: I use a servlet filter for jaas based access control. In this filter I set the current request as a request attribute before using the request dispatcher to forward to my login command. Using my patch, the bean (be it a form or the command) is populated, and I can keep on passing this 'saved' last request between the view and the Maverick command until a login is succesfull without have to get attributes 'by hand' from the request. Find the proposed patches attached. Cheers, Eelco
ThrowawayFormBeanUser-patch.txt
(text/plain, 1.4 KB)
Index: ThrowawayFormBeanUser.java
===================================================================
RCS file: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/ctl/ThrowawayFormBeanUser.java,v
retrieving revision 1.3
diff -u -r1.3 ThrowawayFormBeanUser.java
--- ThrowawayFormBeanUser.java 19 Feb 2003 22:50:47 -0000 1.3
+++ ThrowawayFormBeanUser.java 12 Apr 2003 10:28:00 -0000
@@ -5,6 +5,12 @@
package org.infohazard.maverick.ctl;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
import org.apache.commons.beanutils.BeanUtils;
/**
@@ -35,11 +41,28 @@
this.formBean = this.makeFormBean();
BeanUtils.populate(this.formBean, this.getCtx().getRequest().getParameterMap());
+ BeanUtils.populate(formBean, getAttributeMap(this.getCtx().getRequest()));
BeanUtils.populate(this.formBean, this.getCtx().getControllerParams());
this.getCtx().setModel(this.formBean);
return this.perform();
+ }
+
+ /**
+ * builds a map of the request attributes
+ * @param request
+ * @return
+ */
+ protected Map getAttributeMap(HttpServletRequest request) {
+
+ Map attribs = new HashMap();
+ Enumeration names = request.getAttributeNames();
+ while(names.hasMoreElements()) {
+ String name = (String)names.nextElement();
+ attribs.put(name, request.getAttribute(name));
+ }
+ return attribs;
}
/**
FormBeanUser-patch.txt
(text/plain, 1.5 KB)
Index: FormBeanUser.java
===================================================================
RCS file: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/ctl/FormBeanUser.java,v
retrieving revision 1.5
diff -u -r1.5 FormBeanUser.java
--- FormBeanUser.java 19 Feb 2003 22:50:47 -0000 1.5
+++ FormBeanUser.java 12 Apr 2003 10:26:09 -0000
@@ -5,10 +5,15 @@
package org.infohazard.maverick.ctl;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
import org.infohazard.maverick.flow.*;
import org.apache.commons.beanutils.BeanUtils;
import org.jdom.Element;
import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
/**
* FormBeanUser is a base class for singleton controllers which use
@@ -54,6 +59,7 @@
Object formBean = this.makeFormBean(cctx);
BeanUtils.populate(formBean, cctx.getRequest().getParameterMap());
+ BeanUtils.populate(formBean, getAttributeMap(cctx.getRequest()));
BeanUtils.populate(formBean, cctx.getControllerParams());
cctx.setModel(formBean);
@@ -68,6 +74,22 @@
{
throw new ServletException(ex);
}
+ }
+
+ /**
+ * builds a map of the request attributes
+ * @param request
+ * @return
+ */
+ protected Map getAttributeMap(HttpServletRequest request) {
+
+ Map attribs = new HashMap();
+ Enumeration names = request.getAttributeNames();
+ while(names.hasMoreElements()) {
+ String name = (String)names.nextElement();
+ attribs.put(name, request.getAttribute(name));
+ }
+ return attribs;
}
/**
ThrowawayBean-patch.txt
(text/plain, 1.3 KB)
Index: ThrowawayBean.java
===================================================================
RCS file: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/ctl/ThrowawayBean.java,v
retrieving revision 1.4
diff -u -r1.4 ThrowawayBean.java
--- ThrowawayBean.java 19 Feb 2003 22:50:47 -0000 1.4
+++ ThrowawayBean.java 12 Apr 2003 10:27:32 -0000
@@ -5,6 +5,12 @@
package org.infohazard.maverick.ctl;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
import org.apache.commons.beanutils.BeanUtils;
/**
@@ -48,8 +54,26 @@
protected final String rawPerform() throws Exception
{
BeanUtils.populate(this, this.getRequest().getParameterMap());
+ BeanUtils.populate(this, getAttributeMap(this.getCtx().getRequest()));
BeanUtils.populate(this, this.getCtx().getControllerParams());
return this.perform();
}
+
+ /**
+ * builds a map of the request attributes
+ * @param request
+ * @return
+ */
+ protected Map getAttributeMap(HttpServletRequest request) {
+
+ Map attribs = new HashMap();
+ Enumeration names = request.getAttributeNames();
+ while(names.hasMoreElements()) {
+ String name = (String)names.nextElement();
+ attribs.put(name, request.getAttribute(name));
+ }
+ return attribs;
+ }
+
}