1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47 public HttpSerlvetRequestOptions( HttpServletRequest request )
48 {
49 this.request = request;
50 this.roles = new ArrayList<String>();
51
52 String[] values = request.getParameterValues( "role" );
53 if ( values != null )
54 {
55 roles.addAll( Arrays.asList( values ) );
56 }
57
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
90 String param = request.getParameter( role.getName() + "." + attribute );
91 if (param != null)
92 {
93 return Boolean.parseBoolean( param );
94 }
95
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 }