vgritsenko 2003/10/30 08:42:58
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.17 +54 -1 cocoon-2.1/src/java/org/apache/cocoon/environment/AbstractEnvironment.java Index: AbstractEnvironment.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/AbstractEnvironment.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- AbstractEnvironment.java 18 Sep 2003 14:40:25 -0000 1.16 +++ AbstractEnvironment.java 30 Oct 2003 16:42:58 -0000 1.17 @@ -63,6 +63,7 @@ import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.cocoon.Constants; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.components.CocoonComponentManager; import org.apache.cocoon.components.source.SourceUtil; @@ -154,6 +155,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.13 +20 -42 cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java Index: HttpEnvironment.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- HttpEnvironment.java 27 Sep 2003 12:59:51 -0000 1.12 +++ HttpEnvironment.java 30 Oct 2003 16:42:58 -0000 1.13 @@ -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 implements Redirector, PermanentRedirector { @@ -92,16 +95,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), root, extractAction(req)); + super(uri, null, root, null); this.request = new HttpRequest(req, this); this.request.setCharacterEncoding(defaultFormEncoding); @@ -109,6 +112,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); @@ -121,33 +127,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); - } - } - public void redirect(boolean sessionmode, String newURL) throws IOException { doRedirect(sessionmode, newURL, false); } @@ -323,5 +302,4 @@ public boolean isExternal() { return true; } - }