View Javadoc

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