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
39 import org.apache.log4j.Logger;
40
41 import java.lang.reflect.Method;
42
43 import java.util.Enumeration;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48
49
50
51
52 public final class MVCUtils
53 {
54 public static final String PARAMETER_VIEW = "view";
55 public static final String PARAMETER_ACTION = "action";
56 public static final String PARAMETER_PAGE = "page";
57 private static final String PREFIX_VIEW = "view_";
58 private static final String PREFIX_ACTION = "action_";
59 private static Logger _logger = Logger.getLogger( "lutece.mvc" );
60
61
62
63
64 private MVCUtils( )
65 {
66 }
67
68
69
70
71
72
73 public static String getView( HttpServletRequest request )
74 {
75 String strView = request.getParameter( PARAMETER_VIEW );
76
77 if ( strView != null )
78 {
79 return strView;
80 }
81
82 Enumeration<String> parameters = request.getParameterNames( );
83
84 while ( parameters.hasMoreElements( ) )
85 {
86 String strParameter = parameters.nextElement( );
87
88 if ( strParameter.startsWith( PREFIX_VIEW ) )
89 {
90 strView = strParameter.substring( PREFIX_VIEW.length( ) );
91
92 break;
93 }
94 }
95
96 return strView;
97 }
98
99
100
101
102
103
104 public static String getAction( HttpServletRequest request )
105 {
106 String strAction = request.getParameter( PARAMETER_ACTION );
107
108 if ( strAction != null )
109 {
110 return strAction;
111 }
112
113 Enumeration<String> parameters = request.getParameterNames( );
114
115 while ( parameters.hasMoreElements( ) )
116 {
117 String strParameter = parameters.nextElement( );
118
119 if ( strParameter.startsWith( PREFIX_ACTION ) )
120 {
121 strAction = strParameter.substring( PREFIX_ACTION.length( ) );
122
123 break;
124 }
125 }
126
127 return strAction;
128 }
129
130
131
132
133
134
135
136 public static Method findViewAnnotedMethod( HttpServletRequest request, Method[] methods )
137 {
138 String strView = getView( request );
139
140 if ( strView != null )
141 {
142 for ( Method m : methods )
143 {
144 if ( m.isAnnotationPresent( View.class ) && strView.equals( m.getAnnotation( View.class ).value( ) ) )
145 {
146 _logger.debug( "MVC controller - process view : '" + strView + "'" );
147
148 return m;
149 }
150 }
151
152 _logger.warn( "MVC controller - No method found to process view : '" + strView + "'" );
153 }
154
155 return null;
156 }
157
158
159
160
161
162
163
164 public static Method findActionAnnotedMethod( HttpServletRequest request, Method[] methods )
165 {
166 String strAction = getAction( request );
167
168 if ( strAction != null )
169 {
170 for ( Method m : methods )
171 {
172 if ( m.isAnnotationPresent( Action.class ) &&
173 strAction.equals( m.getAnnotation( Action.class ).value( ) ) )
174 {
175 _logger.debug( "MVC controller - process action : '" + strAction + "'" );
176
177 return m;
178 }
179 }
180
181 _logger.warn( "MVC controller - No method found to process action : '" + strAction + "'" );
182 }
183
184 return null;
185 }
186
187
188
189
190
191
192 public static Method findDefaultViewMethod( Method[] methods )
193 {
194 for ( Method m : methods )
195 {
196 if ( m.isAnnotationPresent( View.class ) && m.getAnnotation( View.class ).defaultView( ) )
197 {
198 _logger.debug( "MVC controller - process default view" );
199
200 return m;
201 }
202 }
203
204 _logger.error( "MVC controller - No default view found" );
205
206 return null;
207 }
208
209
210
211
212
213
214
215 public static void addDownloadHeaderToResponse( HttpServletResponse response, String strFilename,
216 String strContentType )
217 {
218 response.setHeader( "Content-Disposition", "attachment; filename=\"" + strFilename + "\";" );
219 response.setHeader( "Content-type", strContentType );
220 response.addHeader( "Content-Encoding", "UTF-8" );
221 response.addHeader( "Pragma", "public" );
222 response.addHeader( "Expires", "0" );
223 response.addHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
224 }
225
226
227
228
229
230 public static Logger getLogger( )
231 {
232 return _logger;
233 }
234 }