View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 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.cms.util;
35  
36  import info.magnolia.context.MgnlContext;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.lang.builder.ToStringBuilder;
41  import org.apache.commons.lang.builder.ToStringStyle;
42  
43  
44  /**
45   * An implementation of {@link PatternDelegate} that evaluates as condition a request uri patter or a hostname pattern.
46   * @author fgiust
47   * @version $Id$
48   */
49  public class UrlPatternDelegate implements PatternDelegate {
50  
51      private String url;
52  
53      private SimpleUrlPattern urlPattern;
54  
55      private String host;
56  
57      private SimpleUrlPattern hostPattern;
58  
59      private Object delegate;
60  
61      private String originalUrl;
62  
63      private SimpleUrlPattern originalUrlPattern;
64  
65      /**
66       * Returns the configured Url.
67       */
68      public String getUrl() {
69          return url;
70      }
71  
72      public String getOriginalUrl() {
73          return originalUrl;
74      }
75  
76      /**
77       * Sets the Url pattern (using {@link SimpleUrlPattern} internally).
78       * @param pattern url pattern
79       */
80      public void setUrl(String pattern) {
81          this.url = pattern;
82          this.urlPattern = new SimpleUrlPattern(pattern);
83      }
84  
85      public String getHost() {
86          return host;
87      }
88  
89      /**
90       * Sets the host pattern (using {@link SimpleUrlPattern} internally).
91       * @param host host pattern
92       */
93      public void setHost(String host) {
94          this.host = host;
95          this.hostPattern = new SimpleUrlPattern(host);
96      }
97  
98      public void setOriginalUrl(String pattern) {
99          this.originalUrl = pattern;
100         this.originalUrlPattern = new SimpleUrlPattern(pattern);
101     }
102 
103     /**
104      * Compares the reques with the url and host patterns.
105      * @return <code>true</code> if the pattern matches the configured host (if set) and url (if set)
106      */
107     @Override
108     public boolean match(HttpServletRequest request) {
109 
110         boolean match = true;
111 
112         if (hostPattern != null) {
113             match = hostPattern.match(request.getServerName());
114         }
115 
116         if (urlPattern != null) {
117             match = urlPattern.match(MgnlContext.getAggregationState().getCurrentURI());
118         }
119 
120         if (originalUrlPattern != null) {
121             match = originalUrlPattern.match(MgnlContext.getAggregationState().getOriginalURI());
122         }
123 
124         return match;
125     }
126 
127     /**
128      * Sets the delegate.
129      * @param delegate the delegate to set
130      */
131     public void setDelegate(Object delegate) {
132         this.delegate = delegate;
133     }
134 
135     /**
136      * Returns the delegate.
137      * @return the delegate
138      */
139     @Override
140     public Object getDelegate() {
141         return delegate;
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public String toString() {
149         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("url", this.url).append(
150             "host",
151             this.host).toString();
152     }
153 
154 }