commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlmonte...@apache.org
Subject svn commit: r1529219 - in /commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web: ./ handler/ handler/api/ handler/internal/ template/
Date Fri, 04 Oct 2013 17:02:29 GMT
Author: jlmonteiro
Date: Fri Oct  4 17:02:28 2013
New Revision: 1529219

URL: http://svn.apache.org/r1529219
Log:
Makes it possible to override resources - ie. theme

Modified:
    commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringController.java
    commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/FilteringEndpoints.java
    commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/api/TemplateHelper.java
    commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/internal/EndpointInfo.java
    commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/template/Templates.java

Modified: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringController.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringController.java?rev=1529219&r1=1529218&r2=1529219&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringController.java
(original)
+++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/MonitoringController.java
Fri Oct  4 17:02:28 2013
@@ -28,6 +28,7 @@ import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
@@ -59,8 +60,8 @@ public class MonitoringController implem
     public void init(final FilterConfig config) throws ServletException {
         classloader = Thread.currentThread().getContextClassLoader();
         initMapping(config);
-        initHandlers();
         Templates.init(config.getServletContext().getContextPath(), mapping);
+        initHandlers();
     }
 
     private void initHandlers() {
@@ -101,7 +102,9 @@ public class MonitoringController implem
         final HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
         final HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);
 
-        String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()
+ mapping.length());
+        final String baseUri = httpRequest.getContextPath() + mapping;
+        request.setAttribute("baseUri", baseUri);
+        String path = httpRequest.getRequestURI().substring(baseUri.length() + 1);
         if (!path.startsWith("/")) {
             path = "/" + path;
         }
@@ -113,7 +116,9 @@ public class MonitoringController implem
             matcher = entry.getKey().matcher(path);
             if (matcher.matches()) {
                 invoker = entry.getValue();
-                break;
+                if (!entry.getKey().pattern().endsWith(".*")) {
+                    break;
+                }
             }
         }
 
@@ -140,10 +145,13 @@ public class MonitoringController implem
                 }
 
                 if (is != null) {
-                    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                    int i;
-                    while ((i = is.read()) != -1) {
-                        baos.write(i);
+                    ByteArrayOutputStream baos = ByteArrayOutputStream.class.cast(request.getAttribute("resourceCache"));
+                    if (baos == null) {
+                        baos = new ByteArrayOutputStream();
+                        int i;
+                        while ((i = is.read()) != -1) {
+                            baos.write(i);
+                        }
                     }
 
                     bytes = baos.toByteArray();
@@ -151,7 +159,11 @@ public class MonitoringController implem
                 }
             }
             if (bytes != null) {
-                httpResponse.getOutputStream().write(bytes);
+                if (bytes.length == 0) {
+                    httpResponse.setStatus(404);
+                } else {
+                    httpResponse.getOutputStream().write(bytes);
+                }
                 return;
             }
         }

Modified: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/FilteringEndpoints.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/FilteringEndpoints.java?rev=1529219&r1=1529218&r2=1529219&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/FilteringEndpoints.java
(original)
+++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/FilteringEndpoints.java
Fri Oct  4 17:02:28 2013
@@ -18,11 +18,34 @@ package org.apache.commons.monitoring.re
 
 import org.apache.commons.monitoring.reporting.web.handler.api.Regex;
 import org.apache.commons.monitoring.reporting.web.handler.api.TemplateHelper;
+import org.apache.commons.monitoring.reporting.web.template.Templates;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.RuntimeSingleton;
+import org.apache.velocity.runtime.resource.loader.ResourceLoader;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
 
 public class FilteringEndpoints {
     private static final String BOOTSTRAP_CSS = "/resources/css/bootstrap.min.css";
     private static final String MONITORING_CSS = "/resources/css/monitoring.css";
 
+    private ResourceLoader rl;
+
+    public FilteringEndpoints() {
+        try {
+            rl = ResourceLoader.class.cast(FilteringEndpoints.class.getClassLoader().loadClass((String)
RuntimeSingleton.getProperty(Templates.RESOURCE_LOADER_KEY)).newInstance());
+        } catch (final Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
     @Regex(MONITORING_CSS)
     public void filterCss(final TemplateHelper helper) {
         helper.renderPlain(MONITORING_CSS);
@@ -32,4 +55,32 @@ public class FilteringEndpoints {
     public void filterBootstrapCss(final TemplateHelper helper) {
         helper.renderPlain(BOOTSTRAP_CSS);
     }
+
+    @Regex("/resources/.*")
+    public void filterOtherResources(final HttpServletRequest req, final HttpServletResponse
resp) {
+        final InputStream is;
+        try {
+            is = rl.getResourceStream(req.getRequestURI().substring(((String) req.getAttribute("baseUri")).length()));
+        } catch (final ResourceNotFoundException rnfe) {
+            return;
+        }
+
+        try {
+            byte[] buffer = new byte[1024];
+            final ByteArrayOutputStream os = new ByteArrayOutputStream();
+            int length;
+            while ((length = is.read(buffer)) != -1) {
+                os.write(buffer, 0, length);
+            }
+            req.setAttribute("resourceCache", os);
+        } catch (final IOException ioe) {
+            throw new IllegalStateException(ioe);
+        } finally {
+            try {
+                is.close();
+            } catch (final IOException e) {
+                // no-op
+            }
+        }
+    }
 }

Modified: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/api/TemplateHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/api/TemplateHelper.java?rev=1529219&r1=1529218&r2=1529219&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/api/TemplateHelper.java
(original)
+++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/api/TemplateHelper.java
Fri Oct  4 17:02:28 2013
@@ -18,6 +18,8 @@ package org.apache.commons.monitoring.re
 
 import org.apache.commons.monitoring.reporting.web.template.MapBuilder;
 import org.apache.commons.monitoring.reporting.web.template.Templates;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.runtime.RuntimeSingleton;
 
 import java.io.PrintWriter;
 import java.util.Collections;

Modified: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/internal/EndpointInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/internal/EndpointInfo.java?rev=1529219&r1=1529218&r2=1529219&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/internal/EndpointInfo.java
(original)
+++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/handler/internal/EndpointInfo.java
Fri Oct  4 17:02:28 2013
@@ -23,6 +23,7 @@ import org.apache.commons.monitoring.rep
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.Method;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Pattern;

Modified: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/template/Templates.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/template/Templates.java?rev=1529219&r1=1529218&r2=1529219&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/template/Templates.java
(original)
+++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/web/template/Templates.java
Fri Oct  4 17:02:28 2013
@@ -30,6 +30,8 @@ import java.util.Map;
 import java.util.Properties;
 
 public final class Templates {
+    public static final String RESOURCE_LOADER_KEY = "monitoring." + RuntimeConstants.RESOURCE_LOADER
+ ".class";
+
     private static String mapping;
 
     public static void init(final String context, final String filterMapping) {
@@ -42,7 +44,7 @@ public final class Templates {
         velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE,
Boolean.TRUE.toString());
         velocityConfiguration.setProperty(RuntimeConstants.RESOURCE_LOADER, "monitoring");
         velocityConfiguration.setProperty(RuntimeConstants.VM_LIBRARY, "/templates/macro.vm");
-        velocityConfiguration.setProperty("monitoring." + RuntimeConstants.RESOURCE_LOADER
+ ".class", Configuration.getProperty(Configuration.COMMONS_MONITORING_PREFIX + "reporting.resource-loader",
ClasspathResourceLoader.class.getName()));
+        velocityConfiguration.setProperty(RESOURCE_LOADER_KEY, Configuration.getProperty(Configuration.COMMONS_MONITORING_PREFIX
+ "reporting.resource-loader", ClasspathResourceLoader.class.getName()));
         Velocity.init(velocityConfiguration);
 
         if (filterMapping.isEmpty()) {



Mime
View raw message