View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Utils for MVC components
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       * Private constructor
63       */
64      private MVCUtils( )
65      {
66      }
67  
68      /**
69       * Get the view parameter
70       * 
71       * @param request
72       *            The HTTP request
73       * @return The view parameter or null if not found
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      * Get the action parameter or any parameter with the prefix 'action_'
103      * 
104      * @param request
105      *            The HTTP request
106      * @return The action parameter or null if not found
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      * Find the method that provide a given view
136      * 
137      * @param request
138      *            The HTTP request
139      * @param methods
140      *            The methods list
141      * @return The method
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      * Find the method that provide a given action
167      * 
168      * @param request
169      *            The HTTP request
170      * @param methods
171      *            The methods list
172      * @return The method
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      * Find the method that provide the default view
198      * 
199      * @param methods
200      *            The methods list
201      * @return The method
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      * Add download headers to the response
222      * 
223      * @param response
224      *            The response
225      * @param strFilename
226      *            The name of the file to download
227      * @param strContentType
228      *            The content type of the downloaded file
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( "Pragma", "public" );
236         response.addHeader( "Expires", "0" );
237         response.addHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
238     }
239 
240     /**
241      * Return the MVC logger
242      * 
243      * @return THe logger
244      */
245     public static Logger getLogger( )
246     {
247         return _logger;
248     }
249 }