View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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 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   * 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 = Logger.getLogger( "lutece.mvc" );
60  
61      /**
62       * Private constructor
63       */
64      private MVCUtils(  )
65      {
66      }
67  
68      /**
69       * Get the view parameter
70       * @param request The HTTP request
71       * @return The view parameter or null if not found
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      * Get the action parameter or any parameter with the prefix 'action_'
101      * @param request The HTTP request
102      * @return The action parameter or null if not found
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      * Find the method that provide a given view
132      * @param request The HTTP request
133      * @param methods The methods list
134      * @return The method
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      * Find the method that provide a given action
160      * @param request The HTTP request
161      * @param methods The methods list
162      * @return The method
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      * Find the method that provide the default view
189      * @param methods The methods list
190      * @return The method
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      * Add download headers to the response
211      * @param response The response
212      * @param strFilename The name of the file to download
213      * @param strContentType The content type of the downloaded file
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      * Return the MVC logger
228      * @return THe logger
229      */
230     public static Logger getLogger(  )
231     {
232         return _logger;
233     }
234 }