/* * Copyright 2008-2017 by Emeric Vernat * * This file is part of Java Melody. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.bull.javamelody; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /** * Servlet filter to customize resources in html reports, such as css file.<br/> * For example, add the following, before the monitoring filter, in the web.xml file of your webapp, * in order to use your own css or icons: * <pre> * <filter> * <filter-name>customResourceFilter</filter-name> * <filter-class>net.bull.javamelody.CustomResourceFilter</filter-class> * <init-param> * <param-name>monitoring.css</param-name> * <param-value>/customMonitoring.css</param-value> * </init-param> * <init-param> * <param-name>bullets/green.png</param-name> * <param-value>/static/bullets/red.png</param-value> * </init-param> * </filter> * <filter-mapping> * <filter-name>customResourceFilter</filter-name> * <url-pattern>/monitoring</url-pattern> * </filter-mapping> * </pre> * Then add files "customMonitoring.css" and "static/bullets/red.png" at the root of the web content in your webapp.<br/> * You can replace every web resource in <a href='https://github.com/javamelody/javamelody/tree/master/javamelody-core/src/main/resources/net/bull/javamelody/resource'> * this directory and sub-directories</a> * * @author Emeric Vernat */ public class CustomResourceFilter implements Filter { private final Map<String, String> customResources = new HashMap<String, String>(); /** {@inheritDoc} */ @Override public void init(FilterConfig filterConfig) throws ServletException { final List<String> parameterNames = Collections.list(filterConfig.getInitParameterNames()); for (final String parameterName : parameterNames) { customResources.put(parameterName, filterConfig.getInitParameter(parameterName)); } } /** {@inheritDoc} */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final String resource = request.getParameter("resource"); if (resource != null && customResources.get(resource) != null) { final String customResource = customResources.get(resource); final HttpServletResponse httpResponse = (HttpServletResponse) response; MonitoringController.addHeadersForResource(httpResponse, customResource); if (customResources.get("useForward") == null) { request.getRequestDispatcher(customResource).include(request, response); } else { request.getRequestDispatcher(customResource).forward(request, response); } } else { chain.doFilter(request, response); } } /** {@inheritDoc} */ @Override public void destroy() { // nothing } }