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.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: UrlPatternDelegate.java 32667 2010-03-13 00:37:06Z gjoseph $
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      /**
62       * The configured Url
63       * @return the configured Url
64       */
65      public String getUrl() {
66          return url;
67      }
68  
69      /**
70       * Sets the Url pattern (using {@link SimpleUrlPattern} internally)
71       * @param pattern url pattern
72       */
73      public void setUrl(String pattern) {
74          this.url = pattern;
75          this.urlPattern = new SimpleUrlPattern(pattern);
76      }
77  
78      /**
79       * The configured host
80       * @return the configured host
81       */
82      public String getHost() {
83          return host;
84      }
85  
86      /**
87       * Sets the host pattern (using {@link SimpleUrlPattern} internally)
88       * @param host host pattern
89       */
90      public void setHost(String host) {
91          this.host = host;
92          this.hostPattern = new SimpleUrlPattern(host);
93      }
94  
95      /**
96       * Compares the reques with the url and host patterns
97       * @param request HttpServletRequest
98       * @return <code>true</code> if the pattern matches the configured host (if set) and url (if set)
99       */
100     public boolean match(HttpServletRequest request) {
101 
102         boolean match = true;
103 
104         if (hostPattern != null) {
105             match = hostPattern.match(request.getServerName());
106         }
107 
108         if (urlPattern != null) {
109             match = urlPattern.match(MgnlContext.getAggregationState().getCurrentURI());
110         }
111 
112         return match;
113     }
114 
115     /**
116      * Sets the delegate.
117      * @param delegate the delegate to set
118      */
119     public void setDelegate(Object delegate) {
120         this.delegate = delegate;
121     }
122 
123     /**
124      * Returns the delegate.
125      * @return the delegate
126      */
127     public Object getDelegate() {
128         return delegate;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     public String toString() {
135         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("url", this.url).append(
136             "host",
137             this.host).toString();
138     }
139 
140 }