click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From med...@apache.org
Subject svn commit: r793897 - in /incubator/click/trunk/click/framework/src/org/apache/click: ClickServlet.java service/ClickResourceService.java service/ResourceService.java
Date Tue, 14 Jul 2009 13:30:10 GMT
Author: medgar
Date: Tue Jul 14 13:30:09 2009
New Revision: 793897

URL: http://svn.apache.org/viewvc?rev=793897&view=rev
Log:
CLK-564

Modified:
    incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java
    incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java
    incubator/click/trunk/click/framework/src/org/apache/click/service/ResourceService.java

Modified: incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java?rev=793897&r1=793896&r2=793897&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/ClickServlet.java Tue Jul 14
13:30:09 2009
@@ -19,7 +19,6 @@
 package org.apache.click;
 
 import java.io.IOException;
-import java.io.OutputStream;
 import java.io.Writer;
 import java.lang.reflect.Field;
 import java.util.Date;
@@ -161,6 +160,9 @@
     /** The application log service. */
     protected LogService logger;
 
+    /** The application resource service. */
+    protected ResourceService resourceService;
+
     /** The request parameters OGNL type converter. */
     protected TypeConverter typeConverter;
 
@@ -188,6 +190,8 @@
                             + " mode");
             }
 
+            resourceService = configService.getResourceService();
+
         } catch (Throwable e) {
             // In mock mode this exception can occur if click.xml is not
             // available.
@@ -291,9 +295,9 @@
     protected void handleRequest(HttpServletRequest request,
         HttpServletResponse response, boolean isPost) throws IOException {
 
-        // Handle requests for static click resources
-        if (isResourceRequest(request)) {
-            handleResourceRequest(request, response);
+        // Handle requests for click resources, i.e. CSS, JS and image files
+        if (resourceService.isResourceRequest(request)) {
+            resourceService.renderResource(request, response);
             return;
         }
 
@@ -394,26 +398,6 @@
     }
 
     /**
-     * Return true if the request is for a static click resource
-     *
-     * @param request the servlet request
-     * @return true if the request is for a static click resource
-     */
-    protected boolean isResourceRequest(HttpServletRequest request) {
-        String resourcePath = ClickUtils.getResourcePath(request);
-        if (resourcePath.startsWith("/click/")) {
-
-            // If not a click page and not JSP and not a directory
-            return !resourcePath.endsWith(".htm")
-                && !resourcePath.endsWith(".jsp")
-                && !resourcePath.endsWith("/");
-
-        } else {
-            return false;
-        }
-    }
-
-    /**
      * Provides the application exception handler. The application exception
      * will be delegated to the configured error page. The default error page is
      * {@link ErrorPage} and the page template is "click/error.htm" <p/>
@@ -509,46 +493,6 @@
     }
 
     /**
-     * Handle the given static /click/ resource servlet request and render the
-     * results to the servlet response.
-     *
-     * @param request the servlet request
-     * @param response the servlet response to render the results to
-     * @throws IOException if the response data could not be rendered
-     */
-    protected void handleResourceRequest(HttpServletRequest request,
-            HttpServletResponse response) throws IOException {
-
-        String resourcePath = ClickUtils.getResourcePath(request);
-
-        ResourceService resourceService = getConfigService().getResourceService();
-
-        byte[] resourceData = resourceService.getResourceData(resourcePath);
-
-        if (resourceData != null) {
-            String mimeType = ClickUtils.getMimeType(resourcePath);
-
-            OutputStream outputStream = null;
-            try {
-                if (mimeType != null) {
-                    response.setContentType(mimeType);
-                }
-                response.setContentLength(resourceData.length);
-
-                outputStream = response.getOutputStream();
-                outputStream.write(resourceData);
-                outputStream.flush();
-
-            } finally {
-                ClickUtils.close(outputStream);
-            }
-
-        } else {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND);
-        }
-    }
-
-    /**
      * Process the given page invoking its "on" event callback methods
      * and directing the response.
      * <p/>

Modified: incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java?rev=793897&r1=793896&r2=793897&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java
(original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java
Tue Jul 14 13:30:09 2009
@@ -22,6 +22,8 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
 import java.net.URL;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -32,6 +34,8 @@
 import java.util.jar.JarInputStream;
 
 import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.click.util.ClickUtils;
 import org.apache.commons.io.FileUtils;
@@ -51,6 +55,9 @@
     /** The application log service. */
     protected LogService logService;
 
+    /** The application template service. */
+    protected TemplateService templateService;
+
     /**
      * @see ResourceService#onInit(ServletContext)
      *
@@ -61,6 +68,7 @@
 
         ConfigService configService = ClickUtils.getConfigService(servletContext);
         logService = configService.getLogService();
+        templateService = configService.getTemplateService();
 
         // Load all JAR resources
         loadAllJarResources();
@@ -77,13 +85,84 @@
     }
 
     /**
-     * @see ResourceService#getResourceData(String)
+     * @see ResourceService#isResourceRequest(HttpServletRequest)
+     *
+     * @param request the servlet request
+     * @return true if the request is for a static click resource
+     */
+    public boolean isResourceRequest(HttpServletRequest request) {
+        String resourcePath = ClickUtils.getResourcePath(request);
+
+        // If not a click page and not JSP and not a directory
+        return !resourcePath.endsWith(".htm")
+            && !resourcePath.endsWith(".jsp")
+            && !resourcePath.endsWith("/");
+    }
+
+    /**
+     * @see ResourceService#renderResource(HttpServletRequest, HttpServletResponse)
      *
-     * @param resourcePath the path of the resource to lookup
-     * @return the resource data byte array if found or null otherwise
+     * @param request the servlet resource request
+     * @param response the servlet response
+     * @throws IOException if an IO error occurs rendering the resource
      */
-    public byte[] getResourceData(String resourcePath) {
-        return resourceCache.get(resourcePath);
+    public void renderResource(HttpServletRequest request, HttpServletResponse response)
+        throws IOException {
+
+        String resourcePath = ClickUtils.getResourcePath(request);
+
+        if (!resourceCache.containsKey(resourcePath)) {
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return;
+        }
+
+        String mimeType = ClickUtils.getMimeType(resourcePath);
+        if (mimeType != null) {
+            response.setContentType(mimeType);
+        }
+
+        String lowercasePath = resourcePath.toLowerCase();
+        if (lowercasePath.endsWith(".js") || lowercasePath.endsWith(".css")) {
+
+            String templatePath = "META-INF/web" + resourcePath;
+
+            Map<String, Object> model = new HashMap<String, Object>();
+            model.put("context", request.getContextPath());
+            model.put("request", request);
+
+            PrintWriter writer = response.getWriter();
+
+            try {
+                templateService.renderTemplate(templatePath, model, writer);
+
+                writer.flush();
+
+            } catch (Exception e) {
+                if (e instanceof IOException) {
+                    throw (IOException) e;
+
+                } else {
+                    String msg = (e.getMessage() != null) ? e.getMessage() : e.toString();
+                    throw new RuntimeException(msg, e);
+                }
+            }
+
+        } else {
+
+            byte[] resourceData = resourceCache.get(resourcePath);
+
+            OutputStream outputStream = null;
+            try {
+                response.setContentLength(resourceData.length);
+
+                outputStream = response.getOutputStream();
+                outputStream.write(resourceData);
+                outputStream.flush();
+
+            } finally {
+                ClickUtils.close(outputStream);
+            }
+        }
     }
 
     // Private Methods --------------------------------------------------------
@@ -269,7 +348,7 @@
     private void loadClickDirResources(ServletContext servletContext)
         throws IOException {
 
-        Set resources = servletContext.getResourcePaths("/click/");
+        Set resources = servletContext.getResourcePaths("/");
 
         if (resources != null) {
             // Add all resources withtin web application

Modified: incubator/click/trunk/click/framework/src/org/apache/click/service/ResourceService.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/service/ResourceService.java?rev=793897&r1=793896&r2=793897&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/service/ResourceService.java
(original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/service/ResourceService.java
Tue Jul 14 13:30:09 2009
@@ -21,6 +21,8 @@
 import java.io.IOException;
 
 import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * Provides a static resource service interface.
@@ -60,12 +62,21 @@
     public void onDestroy();
 
     /**
-     * Return the resource data byte array for the given resource path or null
-     * if not resource was found.
+     * Return true if the request is for a static resource
      *
-     * @param resourcePath the path of the resource to lookup
-     * @return the resource data byte array if found or null otherwise
+     * @param request the servlet request
+     * @return true if the request is for a static resource
      */
-    public byte[] getResourceData(String resourcePath);
+    public boolean isResourceRequest(HttpServletRequest request);
+
+    /**
+     * Render the resource request to the given servlet resource response
+     *
+     * @param request the servlet resource request
+     * @param response the servlet response
+     * @throws IOException if an IO error occurs rendering the resource
+     */
+    public void renderResource(HttpServletRequest request, HttpServletResponse response)
+        throws IOException;
 
 }



Mime
View raw message