vgritsenko 2003/10/30 08:50:47
Modified: src/java/org/apache/cocoon/environment AbstractEnvironment.java src/java/org/apache/cocoon/environment/http HttpEnvironment.java Log: Move extractAction method from HttpEnvironment into AbstractEnvironment, add extractView method, add setAction and setView methods. This reduces code duplication with other environments. Revision Changes Path 1.23 +54 -1 cocoon-2.2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java Index: AbstractEnvironment.java =================================================================== RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- AbstractEnvironment.java 30 Oct 2003 12:20:45 -0000 1.22 +++ AbstractEnvironment.java 30 Oct 2003 16:50:46 -0000 1.23 @@ -57,6 +57,7 @@ import java.util.Map; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.cocoon.Constants; import org.apache.cocoon.util.BufferedOutputStream; import org.apache.commons.collections.iterators.IteratorEnumeration; @@ -112,6 +113,58 @@ this.action = action; this.objectModel = new HashMap(); } + + /** + * Allow implementations to set view later than in super() constructor. + * View can be set only once, and should be set in implementation's constructor. + */ + protected void setView(String view) { + if (this.view != null) { + throw new IllegalStateException("View was already set on this environment"); + } + this.view = view; + } + + /** + * Allow implementations to set action later than in super() constructor + * Action can be set only once, and should be set in implementation's constructor. + */ + protected void setAction(String action) { + if (this.action != null) { + throw new IllegalStateException("Action was already set on this environment"); + } + this.action = action; + } + + /** + * Helper method to extract the view name from the request. + */ + protected static String extractView(Request request) { + return request.getParameter(Constants.VIEW_PARAM); + } + + /** + * Helper method to extract the action name from the request. + */ + protected static String extractAction(Request req) { + String action = req.getParameter(Constants.ACTION_PARAM); + if (action != null) { + /* TC: still support the deprecated syntax */ + return action; + } else { + for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) { + String name = (String)e.nextElement(); + if (name.startsWith(Constants.ACTION_PARAM_PREFIX)) { + if (name.endsWith(".x") || name.endsWith(".y")) { + return name.substring(Constants.ACTION_PARAM_PREFIX.length(),name.length()-2); + } else { + return name.substring(Constants.ACTION_PARAM_PREFIX.length()); + } + } + } + return null; + } + } // Sitemap methods 1.16 +20 -42 cocoon-2.2/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java Index: HttpEnvironment.java =================================================================== RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- HttpEnvironment.java 30 Oct 2003 12:31:05 -0000 1.15 +++ HttpEnvironment.java 30 Oct 2003 16:50:47 -0000 1.16 @@ -53,18 +53,21 @@ import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; -import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.cocoon.Constants; -import org.apache.cocoon.environment.*; +import org.apache.cocoon.environment.AbstractEnvironment; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.PermanentRedirector; +import org.apache.cocoon.environment.Redirector; +import org.apache.cocoon.environment.Session; import org.apache.cocoon.util.NetUtils; /** - * @author ? + * + * @author <a herf="mailto:dev@cocoon.apache.org>Apache Cocoon Team</a> * @version CVS $Id$ */ public class HttpEnvironment extends AbstractEnvironment { @@ -89,16 +92,16 @@ * Constructs a HttpEnvironment object from a HttpServletRequest * and HttpServletResponse objects */ - public HttpEnvironment (String uri, - String root, - HttpServletRequest req, - HttpServletResponse res, - ServletContext servletContext, - HttpContext context, - String containerEncoding, - String defaultFormEncoding) + public HttpEnvironment(String uri, + String root, + HttpServletRequest req, + HttpServletResponse res, + ServletContext servletContext, + HttpContext context, + String containerEncoding, + String defaultFormEncoding) throws MalformedURLException, IOException { - super(uri, req.getParameter(Constants.VIEW_PARAM), extractAction(req)); + super(uri, null, null); this.request = new HttpRequest(req, this); this.request.setCharacterEncoding(defaultFormEncoding); @@ -106,6 +109,9 @@ this.response = new HttpResponse(res); this.webcontext = context; + setView(extractView(this.request)); + setAction(extractAction(this.request)); + this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request); this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, this.response); this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, this.webcontext); @@ -118,33 +124,6 @@ this.objectModel.put(HTTP_SERVLET_CONTEXT, servletContext); } - /** - * extract the action portion from the request - * (must be static because it's called in the super() constructor. - * should maybe go into a helper or directly into sitemap) - */ - private final static String extractAction(HttpServletRequest req) { - String action = req.getParameter(Constants.ACTION_PARAM); - if (action != null) { - /* TC: still support the deprecated syntax */ - return(action); - } - else { - for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) { - String name = (String)e.nextElement(); - if (name.startsWith(Constants.ACTION_PARAM_PREFIX)) { - if (name.endsWith(".x") || name.endsWith(".y")) { - return(name.substring(Constants.ACTION_PARAM_PREFIX.length(),name.length()-2)); - } - else { - return(name.substring(Constants.ACTION_PARAM_PREFIX.length())); - } - } - } - return(null); - } - } - /** * Redirect the client to new URL */ @@ -283,5 +262,4 @@ public boolean isExternal() { return true; } - }