webwork/src/main/webwork/dispatcher ServletDispatcher.java,1.35,1.36

[email protected] Mon, 20 May 2002 14:10:05 -0700
Newsgroups gmane.comp.java.webwork.cvs
Message-ID <[email protected]>
Update of /cvsroot/webwork/webwork/src/main/webwork/dispatcher
In directory usw-pr-cvs1:/tmp/cvs-serv632

Modified Files:
	ServletDispatcher.java 
Log Message:
added multipart logic

Index: ServletDispatcher.java
===================================================================
RCS file: /cvsroot/webwork/webwork/src/main/webwork/dispatcher/ServletDispatcher.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -d -r1.35 -r1.36
--- ServletDispatcher.java	5 May 2002 15:07:44 -0000	1.35
+++ ServletDispatcher.java	20 May 2002 21:10:03 -0000	1.36
@@ -9,12 +9,14 @@
 import org.apache.log4j.Category;
 import org.apache.log4j.NDC;
 import webwork.action.Action;
-import webwork.action.ResultException;
 import webwork.action.ActionContext;
+import webwork.action.ResultException;
 import webwork.action.factory.ActionFactory;
 import webwork.config.Configuration;
-import webwork.util.ValueStack;
+import webwork.multipart.MultiPartRequest;
+import webwork.multipart.MultiPartRequestWrapper;
 import webwork.util.LoggerUtil;
+import webwork.util.ValueStack;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletConfig;
@@ -24,8 +26,8 @@
 import javax.servlet.http.HttpServletResponse;
 import java.beans.Beans;
 import java.beans.Introspector;
+import java.io.File;
 import java.io.IOException;
-import java.security.*;
 
 /**
  * Main dispatcher servlet. It works in three phases: first propagate all
@@ -40,7 +42,7 @@
  * @version $Revision$
  */
 public class ServletDispatcher
-  extends HttpServlet
+      extends HttpServlet
 {
    // Attributes ----------------------------------------------------
 
@@ -54,6 +56,8 @@
 
    ViewMapping mapping;
    ClassLoader classLoader;
+   private static String saveDir;
+   private static Integer maxSize;
 
 
    // HttpServlet overrides -----------------------------------------
@@ -64,7 +68,7 @@
     * @exception   ServletException
     */
    public void init(ServletConfig config)
-     throws ServletException
+         throws ServletException
    {
       super.init(config);
 
@@ -81,16 +85,13 @@
       ValueStack.clearMethods();
 
       // Choose classloader
-      try
-      {
+      try {
          classLoader = Thread.currentThread().getContextClassLoader();
-      } catch(Throwable e)
-      {
+      } catch (Throwable e) {
          // Ignore
       }
       // If we couldn't use the context classloader, let's try the loader for this servlet instead
-      if (classLoader == null)
-      {
+      if (classLoader == null) {
          log.error("Context classloader not properly set");
          classLoader = getClass().getClassLoader();
       }
@@ -100,24 +101,54 @@
 
       // Initialize view mapping
       String mappingName;
-      try
-      {
+      try {
          mappingName = Configuration.getString("webwork.viewmapping");
-      } catch (IllegalArgumentException e)
-      {
+      } catch (IllegalArgumentException e) {
          // Default
          mappingName = DefaultViewMapping.class.getName();
       }
 
-      try
-      {
-         log.info("Loading view mapping "+mappingName);
-         mapping = (ViewMapping)Beans.instantiate(classLoader, mappingName);
-      } catch (Exception e)
-      {
-         throw new ServletException("Could not instantiate view mapping implementation",e);
+      try {
+         log.info("Loading view mapping " + mappingName);
+         mapping = (ViewMapping) Beans.instantiate(classLoader, mappingName);
+      } catch (Exception e) {
+         throw new ServletException("Could not instantiate view mapping implementation", e);
       }
 
+      //load multipart configuration
+      //saveDir
+      try {
+         saveDir = Configuration.getString("webwork.multipart.saveDir");
+      } catch (IllegalArgumentException e) {
+         //use default
+         File tempdir = (File) config.getServletContext().getAttribute("javax.servlet.context.tempdir");
+         log.warn("Unable to find 'webwork.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir");
+         if (tempdir != null) {
+            saveDir = tempdir.toString();
+         }
+      }
+      log.debug("saveDir=" + saveDir);
+
+      //maxSize
+      try {
+         String maxSizeStr = Configuration.getString("webwork.multipart.maxSize");
+         if (maxSizeStr != null) {
+            try {
+               maxSize = new Integer(maxSizeStr);
+            } catch (NumberFormatException e) {
+               maxSize = new Integer(Integer.MAX_VALUE);
+               log.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
+            }
+         } else {
+            maxSize = new Integer(Integer.MAX_VALUE);
+            log.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
+         }
+      } catch (IllegalArgumentException e1) {
+         maxSize = new Integer(Integer.MAX_VALUE);
+         log.warn("Unable to format 'webwork.multipart.maxSize' property setting. Defaulting to Integer.MAX_VALUE");
+      }
+      log.debug("maxSize=" + maxSize);
+
       log.info("Action dispatcher initialized");
    }
 
@@ -129,13 +160,16 @@
     * @exception   ServletException
     */
    public void service(HttpServletRequest aRequest, HttpServletResponse aResponse)
-     throws ServletException
+         throws ServletException
    {
+      //wrap request if needed
+      aRequest = wrapRequest(aRequest);
+
       // Get action
-      String pathInfo = (String)aRequest.getAttribute("javax.servlet.include.path_info");
+      String pathInfo = (String) aRequest.getAttribute("javax.servlet.include.path_info");
       if (pathInfo == null)
          pathInfo = aRequest.getPathInfo();
-      String servletPath = (String)aRequest.getAttribute("javax.servlet.include.servlet_path");
+      String servletPath = (String) aRequest.getAttribute("javax.servlet.include.servlet_path");
       if (servletPath == null)
          servletPath = aRequest.getServletPath();
 
@@ -162,20 +196,16 @@
          context.setServletContext(getServletContext());
          ActionContext.setContext(context);
 
-         try
-         {
+         try {
             action = ActionFactory.getAction(actionName);
-         } catch(Exception e)
-         {
+         } catch (Exception e) {
             // Log the error
-            log.error("Could not instantiate action:"+actionName, e);
+            log.error("Could not instantiate action:" + actionName, e);
 
-            try
-            {
+            try {
                aResponse.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
                return;
-            } catch(IOException ex)
-            {
+            } catch (IOException ex) {
                throw new ServletException(ex);
             }
          }
@@ -184,31 +214,25 @@
          String result = null;
          log.debug("Action:" + actionName);
          NDC.push(actionName);
-         try
-         {
+         try {
             result = action.execute();
-         } catch(SecurityException e)
-         {
+         } catch (SecurityException e) {
             // Security exception thrown
-            throw new ServletException("You're not allowed to access the action "+actionName, e);
-         } catch(ResultException e)
-         {
+            throw new ServletException("You're not allowed to access the action " + actionName, e);
+         } catch (ResultException e) {
             // Show chosen view
-            result = (String)e.getResult();
-         } catch(Exception e)
-         {
+            result = (String) e.getResult();
+         } catch (Exception e) {
             log.debug("Action execution exception", e);
             throw new ServletException("System error while executing " + action.getClass(), e);
-         } finally
-         {
+         } finally {
             NDC.pop();
          }
 
          log.debug("Result:" + result);
 
          // No result
-         if (result == null)
-         {
+         if (result == null) {
             log.debug("Action has no result");
             throw new ServletException("System error while executing " + action.getClass());
          }
@@ -216,26 +240,21 @@
          // Get view corresponding to result
          // Check request override first
          String view;
-         if ((view = aRequest.getParameter(result)) == null)
-         {
-            try
-            {
-               view = (String)mapping.getView(actionName, result);
-            } catch (IllegalArgumentException e)
-            {
+         if ((view = aRequest.getParameter(result)) == null) {
+            try {
+               view = (String) mapping.getView(actionName, result);
+            } catch (IllegalArgumentException e) {
                // No view found
-               log.debug("No view found for action "+actionName);
+               log.debug("No view found for action " + actionName);
             }
          }
 
          // Only show if a view is assigned to this actions result
-         if (view != null)
-         {
+         if (view != null) {
             // Show view
             if (!view.startsWith("/")) view = actionPath + view; // Add path prefix
 
-            try
-            {
+            try {
                log.debug("View:" + view);
 
                // Push view name onto NDC
@@ -243,47 +262,39 @@
 
 
                RequestDispatcher dispatcher = null;
-               try
-               {
+               try {
                   dispatcher = aRequest.getRequestDispatcher(view);
-               } catch(Throwable e)
-               {
+               } catch (Throwable e) {
                   // Ignore
                }
 
                if (dispatcher == null)
                   throw new ServletException("No presentation file with name '" + view + "' found!");
 
-               try
-               {
+               try {
                   // Store result for use in view
                   ValueStack.getStack(aRequest).pushValue(action);
 
                   // If we're included, then include the view
                   // Otherwise do forward
                   // This allow the page to, for example, set content type
-                  if (aRequest.getAttribute("javax.servlet.include.servlet_path") == null)
-                  {
+                  if (aRequest.getAttribute("javax.servlet.include.servlet_path") == null) {
                      aRequest.setAttribute("webwork.view_uri", view);
-                     aRequest.setAttribute("webwork.request_uri",aRequest.getRequestURI());
-      //               aRequest.setAttribute("webwork.contextPath",aRequest.getContextPath());
+                     aRequest.setAttribute("webwork.request_uri", aRequest.getRequestURI());
+                     //               aRequest.setAttribute("webwork.contextPath",aRequest.getContextPath());
                      dispatcher.forward(aRequest, aResponse);
-                  } else
-                  {
-      //               aRequest.setAttribute("webwork.request_uri",aRequest.getAttribute("javax.servlet.include.request_uri"));
-      //               aRequest.setAttribute("webwork.contextPath",aRequest.getAttribute("javax.servlet.include.context_path"));
+                  } else {
+                     //               aRequest.setAttribute("webwork.request_uri",aRequest.getAttribute("javax.servlet.include.request_uri"));
+                     //               aRequest.setAttribute("webwork.contextPath",aRequest.getAttribute("javax.servlet.include.context_path"));
                      dispatcher.include(aRequest, aResponse);
                   }
-               } catch(IOException e)
-               {
+               } catch (IOException e) {
                   throw new ServletException(e);
-               } finally
-               {
+               } finally {
                   // Reset result and store in request attribute STACK_HEAD
-                  aRequest.setAttribute(STACK_HEAD,ValueStack.getStack(aRequest).popValue());
+                  aRequest.setAttribute(STACK_HEAD, ValueStack.getStack(aRequest).popValue());
                }
-            } finally
-            {
+            } finally {
                // Remove view name from NDC
                NDC.pop();
             }
@@ -292,5 +303,26 @@
          // Reset context. Important if this action was included
          ActionContext.setContext(oldContext);
       }
+   }
+
+   /**
+    * Wrap servlet request with the appropriate request. It will check to
+    * see if request is a multipart request and wrap in appropriately.
+    *
+    * @param request
+    * @return wrapped request or original request
+    */
+   private HttpServletRequest wrapRequest(HttpServletRequest request)
+   {
+      if (MultiPartRequest.isMultiPart((HttpServletRequest) request)) {
+         try {
+            request = new MultiPartRequestWrapper((HttpServletRequest) request,
+                  saveDir, maxSize);
+         } catch (IOException e) {
+            request.setAttribute("webwork.action.ResultException",
+                  new ResultException(Action.ERROR, e.getLocalizedMessage()));
+         }
+      }
+      return request;
    }
 }



_______________________________________________________________

Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm