1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.util.mvc.utils;
35
36 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
37 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
38 import org.apache.log4j.Logger;
39
40 import java.lang.reflect.Method;
41
42 import java.util.Enumeration;
43
44 import javax.servlet.http.HttpServletRequest;
45
46
47
48
49
50 public final class MVCUtils
51 {
52 public static final String PARAMETER_VIEW = "view";
53 public static final String PARAMETER_ACTION = "action";
54 public static final String PARAMETER_PAGE = "page";
55 private static final String PREFIX_VIEW = "view_";
56 private static final String PREFIX_ACTION = "action_";
57 private static Logger _logger = Logger.getLogger( "lutece.mvc" );
58
59
60
61
62 private MVCUtils( )
63 {
64
65 }
66
67
68
69
70
71
72 public static String getView( HttpServletRequest request )
73 {
74 String strView = request.getParameter( PARAMETER_VIEW );
75
76 if ( strView != null )
77 {
78 return strView;
79 }
80
81 Enumeration<String> parameters = request.getParameterNames( );
82
83 while ( parameters.hasMoreElements( ) )
84 {
85 String strParameter = parameters.nextElement( );
86
87 if ( strParameter.startsWith( PREFIX_VIEW ) )
88 {
89 strView = strParameter.substring( PREFIX_VIEW.length( ) );
90
91 break;
92 }
93 }
94
95 return strView;
96 }
97
98
99
100
101
102
103 public static String getAction( HttpServletRequest request )
104 {
105 String strAction = request.getParameter( PARAMETER_ACTION );
106
107 if ( strAction != null )
108 {
109 return strAction;
110 }
111
112 Enumeration<String> parameters = request.getParameterNames( );
113
114 while ( parameters.hasMoreElements( ) )
115 {
116 String strParameter = parameters.nextElement( );
117
118 if ( strParameter.startsWith( PREFIX_ACTION ) )
119 {
120 strAction = strParameter.substring( PREFIX_ACTION.length( ) );
121
122 break;
123 }
124 }
125
126 return strAction;
127 }
128
129
130
131
132
133
134
135 public static Method findViewAnnotedMethod( HttpServletRequest request, Method[] methods )
136 {
137 String strView = getView( request );
138
139 if ( strView != null )
140 {
141 for ( Method m : methods )
142 {
143 if ( m.isAnnotationPresent( View.class ) && strView.equals( m.getAnnotation( View.class ).value( ) ) )
144 {
145 _logger.debug( "MVC controller - process view : '" + strView + "'" );
146
147 return m;
148 }
149 }
150
151 _logger.warn( "MVC controller - No method found to process view : '" + strView + "'" );
152 }
153
154 return null;
155 }
156
157
158
159
160
161
162
163 public static Method findActionAnnotedMethod( HttpServletRequest request, Method[] methods )
164 {
165 String strAction = getAction( request );
166
167 if ( strAction != null )
168 {
169 for ( Method m : methods )
170 {
171 if ( m.isAnnotationPresent( Action.class ) &&
172 strAction.equals( m.getAnnotation( Action.class ).value( ) ) )
173 {
174 _logger.debug( "MVC controller - process action : '" + strAction + "'" );
175
176 return m;
177 }
178 }
179
180 _logger.warn( "MVC controller - No method found to process action : '" + strAction + "'" );
181 }
182
183 return null;
184 }
185
186
187
188
189
190
191 public static Method findDefaultViewMethod( Method[] methods )
192 {
193 for ( Method m : methods )
194 {
195 if ( m.isAnnotationPresent( View.class ) && m.getAnnotation( View.class ).defaultView( ) )
196 {
197 _logger.debug( "MVC controller - process default view" );
198
199 return m;
200 }
201 }
202
203 _logger.error( "MVC controller - No default view found" );
204
205 return null;
206 }
207
208
209
210
211
212 public static Logger getLogger( )
213 {
214 return _logger;
215 }
216 }