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.plugins.contextinclude.business;
35  
36  import java.util.Arrays;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Map.Entry;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import javax.validation.constraints.Min;
44  
45  
46  /**
47   *
48   * Context
49   *
50   */
51  public class Context
52  {
53      private int _nIdContext;
54      private String _strHtml;
55      @Min( 0 )
56      private int _nNbParams;
57      private int _nPriority;
58      private boolean _bStrict;
59      private boolean _bActive;
60      private Map<String, List<String>> _mapParameters;
61  
62      /**
63       * Sets the id context.
64       *
65       * @param nIdContext the _nIdContext to set
66       */
67      public void setIdContext( int nIdContext )
68      {
69          this._nIdContext = nIdContext;
70      }
71  
72      /**
73       * Gets the id context.
74       *
75       * @return the _nIdContext
76       */
77      public int getIdContext(  )
78      {
79          return _nIdContext;
80      }
81  
82      /**
83       * Sets the html.
84       *
85       * @param strHtml the _strHtml to set
86       */
87      public void setHtml( String strHtml )
88      {
89          this._strHtml = strHtml;
90      }
91  
92      /**
93       * Gets the html.
94       *
95       * @return the _strHtml
96       */
97      public String getHtml(  )
98      {
99          return _strHtml;
100     }
101 
102     /**
103      * Sets the nb params.
104      *
105      * @param nNbParams the _nNbParams to set
106      */
107     public void setNbParams( int nNbParams )
108     {
109         this._nNbParams = nNbParams;
110     }
111 
112     /**
113      * Gets the nb params.
114      *
115      * @return the _nNbParams
116      */
117     public int getNbParams(  )
118     {
119         return _nNbParams;
120     }
121 
122     /**
123      * Sets the priority.
124      *
125      * @param nPriority the _nPriority to set
126      */
127     public void setPriority( int nPriority )
128     {
129         this._nPriority = nPriority;
130     }
131 
132     /**
133      * Gets the priority.
134      *
135      * @return the _nPriority
136      */
137     public int getPriority(  )
138     {
139         return _nPriority;
140     }
141 
142     /**
143      * Sets the strict.
144      *
145      * @param bStrict the _bStrict to set
146      */
147     public void setStrict( boolean bStrict )
148     {
149         this._bStrict = bStrict;
150     }
151 
152     /**
153      * Checks if is strict.
154      *
155      * @return the _bStrict
156      */
157     public boolean isStrict(  )
158     {
159         return _bStrict;
160     }
161 
162     /**
163      * Sets the active.
164      *
165      * @param bActive the _bActive to set
166      */
167     public void setActive( boolean bActive )
168     {
169         this._bActive = bActive;
170     }
171 
172     /**
173      * Checks if is active.
174      *
175      * @return the _bActive
176      */
177     public boolean isActive(  )
178     {
179         return _bActive;
180     }
181 
182     /**
183      * Sets the map parameters.
184      *
185      * @param mapParameters the mapParameters to set
186      */
187     public void setMapParameters( Map<String, List<String>> mapParameters )
188     {
189         this._mapParameters = mapParameters;
190     }
191 
192     /**
193      * Gets the map parameters.
194      *
195      * @return the mapParameters
196      */
197     public Map<String, List<String>> getMapParameters(  )
198     {
199         return _mapParameters;
200     }
201 
202     /**
203      * Check if this context is invoked.
204      *
205      * @param request the HTTP request
206      * @return true if it context, false otherwise
207      */
208     public boolean isInvoked( HttpServletRequest request )
209     {
210         // Check request for daemons
211         if ( request == null )
212         {
213             return false;
214         }
215 
216         // No parameters
217         if ( _mapParameters == null )
218         {
219             // In strict mode, the request must not have any parameters
220             if ( _bStrict )
221             {
222                 return ( request.getParameterMap(  ) == null ) || request.getParameterMap(  ).isEmpty(  );
223             }
224 
225             // Otherwise, always return true
226             return true;
227         }
228 
229         // In strict mode, check the size of the parameter map
230         if ( _bStrict )
231         {
232             if ( request.getParameterMap(  ) == null )
233             {
234                 return false;
235             }
236 
237             if ( _mapParameters.size(  ) != request.getParameterMap(  ).size(  ) )
238             {
239                 return false;
240             }
241         }
242 
243         // Check each parameter
244         for ( Entry<String, List<String>> params : _mapParameters.entrySet(  ) )
245         {
246             // Configured parameters
247             String strKey = params.getKey(  );
248             List<String> listValues = params.getValue(  );
249 
250             // Request parameters
251             String[] listRequestValues = request.getParameterValues( strKey );
252 
253             if ( listRequestValues != null )
254             {
255                 // In strict mode, the size of the configured and request parameters must be equal
256                 if ( _bStrict )
257                 {
258                     if ( listRequestValues.length != listValues.size(  ) )
259                     {
260                         return false;
261                     }
262                 }
263 
264                 // Check if the configured parameter is contained in the list of request parameters
265                 for ( String strValue : listValues )
266                 {
267                     if ( !Arrays.asList( listRequestValues ).contains( strValue ) )
268                     {
269                         return false;
270                     }
271                 }
272             }
273             else
274             {
275                 return false;
276             }
277         }
278 
279         return true;
280     }
281 }