View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring.reporting.web;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.util.List;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.monitoring.reporting.Renderer;
30  
31  /**
32   * Extends the reporting servlet to return a nice HTML based web UI with
33   * JavaScript enhancements.
34   *
35   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
36   */
37  public class WebUIServlet
38      extends MonitoringServlet
39  {
40      private static final long serialVersionUID = 1L;
41  
42      /**
43       * {@inheritDoc}
44       *
45       * @see org.apache.commons.monitoring.reporting.web.MonitoringServlet#service(javax.servlet.http.HttpServletRequest,
46       * javax.servlet.http.HttpServletResponse)
47       */
48      @Override
49      protected void service( HttpServletRequest request, HttpServletResponse response )
50          throws ServletException, IOException
51      {
52          String pathInfo = request.getPathInfo();
53          if ( pathInfo.startsWith( "/resources" ) )
54          {
55              String path = pathInfo.substring( "/resources".length() );
56              InputStream resource = getClass().getResourceAsStream( path );
57              if ( resource != null )
58              {
59                  response.setContentType( getMimeTypes( pathInfo ) );
60                  response.addHeader( "Cache-Control", "max-age=1000" );
61                  copy( resource, response.getOutputStream() );
62              }
63              return;
64          }
65          super.service( request, response );
66      }
67  
68      /**
69       * @param pathInfo
70       * @return
71       */
72      protected String getMimeTypes( String pathInfo )
73      {
74          if ( pathInfo.endsWith( ".css" ) )
75          {
76              return "text/css";
77          }
78          if ( pathInfo.endsWith( ".js" ) )
79          {
80              return "text/javascript";
81          }
82          if ( pathInfo.endsWith( ".gif" ) )
83          {
84              return "image/gif";
85          }
86          return null;
87      }
88  
89      private static final int DEFAULT_BUFFER_SIZE = 4 * 1024;
90  
91      private void copy( InputStream input, OutputStream output )
92          throws IOException
93      {
94          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
95          int n = 0;
96          while ( -1 != ( n = input.read( buffer ) ) )
97          {
98              output.write( buffer, 0, n );
99          }
100         try
101         {
102             input.close();
103         }
104         catch ( IOException e )
105         {
106             // ignore...
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      *
113      * @see org.apache.commons.monitoring.reporting.web.MonitoringServlet#getRenderer(javax.servlet.http.HttpServletRequest,
114      * javax.servlet.http.HttpServletResponse)
115      */
116     @Override
117     protected Renderer getRenderer( HttpServletRequest request, HttpServletResponse response )
118         throws IOException
119     {
120         List<String> accept = getAcceptedMimeTypes( request );
121         if ( accept.contains( "text/html" ) )
122         {
123             response.setContentType( "text/html;charset=iso-8859-1" );
124             return new NiceHtmlRenderer( request.getContextPath() + request.getServletPath() );
125         }
126         return super.getRenderer( request, response );
127     }
128 }