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