View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.rewrite.handler;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.http.PathMap;
22  import org.eclipse.jetty.server.HttpConnection;
23  import org.eclipse.jetty.server.Request;
24  import org.eclipse.jetty.util.LazyList;
25  import org.eclipse.jetty.util.log.Log;
26  
27  /**
28   * Base container to group rules. Can be extended so that the contained rules
29   * will only be applied under certain conditions
30   * 
31   * 
32   */
33  
34  public class RuleContainer extends Rule
35  {
36      protected Rule[] _rules;
37      
38      protected String _originalPathAttribute;
39      protected boolean _rewriteRequestURI=true;
40      protected boolean _rewritePathInfo=true;
41      
42      protected LegacyRule _legacy;
43  
44      /* ------------------------------------------------------------ */
45      @Deprecated
46      public LegacyRule getLegacyRule()
47      {
48          if (_legacy==null)
49          {
50              _legacy= new LegacyRule();
51              addRule(_legacy);
52          }
53          return _legacy;
54      }
55      
56  
57      /* ------------------------------------------------------------ */
58      /**
59       * To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
60       * originalPathAttribute
61       * 
62       * @param legacyRule old style rewrite rule
63       */
64      @Deprecated
65      public void setLegacyRule(LegacyRule legacyRule)
66      {
67          _legacy = legacyRule;
68      }
69  
70      /* ------------------------------------------------------------ */
71      /**
72       * Returns the list of rules.
73       * @return an array of {@link Rule}.
74       */
75      public Rule[] getRules()
76      {
77          return _rules;
78      }
79  
80      /* ------------------------------------------------------------ */
81      /**
82       * Assigns the rules to process.
83       * @param rules an array of {@link Rule}. 
84       */
85      public void setRules(Rule[] rules)
86      {
87          if (_legacy==null)
88              _rules = rules;
89          else
90          {
91              _rules=null;
92              addRule(_legacy);
93              if (rules!=null)
94                  for (Rule rule:rules)
95                      addRule(rule);
96          }
97      }
98  
99      /* ------------------------------------------------------------ */
100     /**
101      * Add a Rule
102      * @param rule The rule to add to the end of the rules array
103      */
104     public void addRule(Rule rule)
105     {
106         _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
107     }
108    
109 
110     /* ------------------------------------------------------------ */
111     /**
112      * @return the rewriteRequestURI If true, this handler will rewrite the value
113      * returned by {@link HttpServletRequest#getRequestURI()}.
114      */
115     public boolean isRewriteRequestURI()
116     {
117         return _rewriteRequestURI;
118     }
119 
120     /* ------------------------------------------------------------ */
121     /**
122      * @param rewriteRequestURI true if this handler will rewrite the value
123      * returned by {@link HttpServletRequest#getRequestURI()}.
124      */
125     public void setRewriteRequestURI(boolean rewriteRequestURI)
126     {
127         _rewriteRequestURI=rewriteRequestURI;
128     }
129 
130     /* ------------------------------------------------------------ */
131     /**
132      * @return true if this handler will rewrite the value
133      * returned by {@link HttpServletRequest#getPathInfo()}.
134      */
135     public boolean isRewritePathInfo()
136     {
137         return _rewritePathInfo;
138     }
139 
140     /* ------------------------------------------------------------ */
141     /**
142      * @param rewritePathInfo true if this handler will rewrite the value
143      * returned by {@link HttpServletRequest#getPathInfo()}.
144      */
145     public void setRewritePathInfo(boolean rewritePathInfo)
146     {
147         _rewritePathInfo=rewritePathInfo;
148     }
149 
150     /* ------------------------------------------------------------ */
151     /**
152      * @return the originalPathAttribte. If non null, this string will be used
153      * as the attribute name to store the original request path.
154      */
155     public String getOriginalPathAttribute()
156     {
157         return _originalPathAttribute;
158     }
159 
160     /* ------------------------------------------------------------ */
161     /**
162      * @param originalPathAttribte If non null, this string will be used
163      * as the attribute name to store the original request path.
164      */
165     public void setOriginalPathAttribute(String originalPathAttribte)
166     {
167         _originalPathAttribute=originalPathAttribte;
168     }
169     
170     /**
171      * Process the contained rules
172      * @param target target field to pass on to the contained rules
173      * @param request request object to pass on to the contained rules
174      * @param response response object to pass on to the contained rules
175      */
176     @Override
177     public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
178     {
179         return apply(target, request, response);
180     }
181 
182     /**
183      * Process the contained rules (called by matchAndApply) 
184      * @param target target field to pass on to the contained rules
185      * @param request request object to pass on to the contained rules
186      * @param response response object to pass on to the contained rules
187      */
188     protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
189     {
190         boolean original_set=_originalPathAttribute==null;
191                 
192         for (Rule rule : _rules)
193         {
194             String applied=rule.matchAndApply(target,request, response);
195             if (applied!=null)
196             {       
197                 Log.debug("applied {}",rule);
198                 if (!target.equals(applied))
199                 { 
200                     Log.debug("rewrote {} to {}",target,applied);
201                     if (!original_set)
202                     {
203                         original_set=true;
204                         request.setAttribute(_originalPathAttribute, target);
205                     }     
206 
207                     if (_rewriteRequestURI)
208                     {
209                         if (rule instanceof Rule.ApplyURI && !target.equals(request.getRequestURI()))
210                             ((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
211                         else
212                             ((Request)request).setRequestURI(applied);
213                     }
214 
215                     if (_rewritePathInfo)
216                         ((Request)request).setPathInfo(applied);
217 
218                     target=applied;
219                 }
220                 
221                 if (rule.isHandling())
222                 {
223                     Log.debug("handling {}",rule);
224                     (request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest()).setHandled(true);
225                 }
226 
227                 if (rule.isTerminating())
228                 {
229                     Log.debug("terminating {}",rule);
230                     break;
231                 }
232             }
233         }
234 
235         return target;
236     }
237 }