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.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.commons.monitoring.Monitor;
28  import org.apache.commons.monitoring.Role;
29  import org.apache.commons.monitoring.Unit;
30  import org.apache.commons.monitoring.Monitor.Key;
31  import org.apache.commons.monitoring.reporting.OptionsSupport;
32  
33  public class HttpSerlvetRequestOptions
34      extends OptionsSupport
35  {
36      protected final HttpServletRequest request;
37  
38      protected List<String> roles;
39  
40      protected List<String> categories;
41  
42      protected List<String> subsystems;
43  
44      /**
45       * @param request
46       */
47      public HttpSerlvetRequestOptions( HttpServletRequest request )
48      {
49          this.request = request;
50          this.roles = new ArrayList<String>();
51          // Roles set as "role=x&role=y"
52          String[] values = request.getParameterValues( "role" );
53          if ( values != null )
54          {
55              roles.addAll( Arrays.asList( values ) );
56          }
57          // Roles set as "roles=x,y"
58          String value = request.getParameter( "roles" );
59          if ( value != null )
60          {
61              roles.addAll( Arrays.asList( value.split( "," ) ) );
62          }
63  
64          values = request.getParameterValues( "category" );
65          categories = values != null ? Arrays.asList( values ) : Collections.<String> emptyList();
66          values = request.getParameterValues( "subsystem" );
67          subsystems = values != null ? Arrays.asList( values ) : Collections.<String> emptyList();
68      }
69  
70      @Override
71      @SuppressWarnings("unchecked")
72      public boolean renderRole( Role role )
73      {
74          return roles.isEmpty() ? true : roles.contains( role.getName() );
75      }
76  
77      @Override
78      public boolean render( Monitor monitor )
79      {
80          Key key = monitor.getKey();
81          return ( categories.isEmpty() || categories.contains( key.getCategory() ) )
82              && ( subsystems.isEmpty() || subsystems.contains( key.getSubsystem() ) );
83      }
84  
85      @Override
86      @SuppressWarnings("unchecked")
87      public boolean render( Role role, String attribute )
88      {
89          // attribute hidden using "myrole.x=false"
90          String param = request.getParameter( role.getName() + "." + attribute );
91          if (param != null)
92          {
93              return Boolean.parseBoolean( param );
94          }
95          // attribute selected using "myrole.columns=x,y,z"
96          String columns = request.getParameter( role.getName() + ".columns" );
97          if ( columns == null )
98          {
99              return true;
100         }
101         return columns.indexOf( attribute ) >= 0;
102     }
103 
104     @Override
105     @SuppressWarnings("unchecked")
106     public Unit unitFor( Role role )
107     {
108         String unitName = request.getParameter( role.getName() + ".unit" );
109         if ( unitName != null )
110         {
111             if ( role.getUnit() != null )
112             {
113                 Unit unit = role.getUnit().getDerived( unitName );
114                 if ( unit != null )
115                 {
116                     return unit;
117                 }
118             }
119         }
120         return role.getUnit();
121     }
122 }