View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.voting.voters;
35  
36  import info.magnolia.context.MgnlContext;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  
43  /**
44   * Voters which uses the urls to match against the pattern. The returned vote is
45   * the length of the pattern. This allows overstearing less precise votes (like
46   * allow /something/* but deny /somthing/otherthing/*). You can use the inverse
47   * property which will then return the negative value on a match. This is not
48   * the same thing as using the not property which will then vote if the pattern
49   * does not match at all.
50   *
51   * @author philipp
52   * @version $Id: BasePatternVoter.java 32667 2010-03-13 00:37:06Z gjoseph $
53   *
54   */
55  public abstract class BasePatternVoter extends AbstractBoolVoter {
56  
57      // use default value to prevent NPE when initialized before pattern is set.
58      private String pattern = StringUtils.EMPTY;
59      private boolean inverse;
60      private boolean autoTrueValue = true;
61  
62      public void init() {
63          if(autoTrueValue){
64              if(!isInverse()){
65                  setTrueValue(pattern.length());
66              }
67              else{
68                  setTrueValue(-pattern.length());
69              }
70          }
71      }
72  
73      public void setTrueValue(int positiveVoteValue) {
74          autoTrueValue = false;
75          super.setTrueValue(positiveVoteValue);
76      }
77  
78      public String getPattern() {
79          return this.pattern;
80      }
81  
82      public void setPattern(String pattern) {
83          this.pattern = pattern;
84      }
85  
86      public boolean isInverse() {
87          return this.inverse;
88      }
89  
90      public void setInverse(boolean inverse) {
91          this.inverse = inverse;
92      }
93  
94      protected String resolveURIFromValue(Object value) {
95          String uri = null;
96          if(value instanceof String){
97              uri = (String) value;
98          }
99          else{
100             if(MgnlContext.hasInstance()){
101                 uri = MgnlContext.getAggregationState().getCurrentURI();
102             }
103             else{
104                 if (value instanceof HttpServletRequest) {
105                     HttpServletRequest request = (HttpServletRequest) value;
106                     uri = StringUtils.substringAfter(request.getRequestURI(), request.getContextPath());
107                 }
108             }
109         }
110         return uri;
111     }
112 
113     public String toString() {
114         return super.toString() + " pattern: " + this.getPattern();
115     }
116 
117 }