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  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   * Utils for MVC components
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       * Private constructor
61       */
62      private MVCUtils(  )
63      {
64          
65      }
66      
67      /**
68       * Get the view parameter
69       * @param request The HTTP request
70       * @return The view parameter or null if not found
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       * Get the action parameter or any parameter with the prefix 'action_'
100      * @param request The HTTP request
101      * @return The action parameter or null if not found
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      * Find the method that provide a given view
131      * @param request The HTTP request
132      * @param methods The methods list
133      * @return The method
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      * Find the method that provide a given action
159      * @param request The HTTP request
160      * @param methods The methods list
161      * @return The method
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      * Find the method that provide the default view
188      * @param methods The methods list
189      * @return The method
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      * Return the MVC logger
210      * @return THe logger
211      */
212     public static Logger getLogger(  )
213     {
214         return _logger;
215     }
216 }