View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.Request;
27  import org.eclipse.jetty.util.ArrayUtil;
28  import org.eclipse.jetty.util.URIUtil;
29  import org.eclipse.jetty.util.log.Log;
30  import org.eclipse.jetty.util.log.Logger;
31  
32  /**
33   * Base container to group rules. Can be extended so that the contained rules
34   * will only be applied under certain conditions
35   */
36  public class RuleContainer extends Rule
37  {
38      private static final Logger LOG = Log.getLogger(RuleContainer.class);
39  
40      protected Rule[] _rules;
41      
42      protected String _originalPathAttribute;
43      protected boolean _rewriteRequestURI=true;
44      protected boolean _rewritePathInfo=true;
45       
46      /* ------------------------------------------------------------ */
47      /**
48       * Returns the list of rules.
49       * @return an array of {@link Rule}.
50       */
51      public Rule[] getRules()
52      {
53          return _rules;
54      }
55  
56      /* ------------------------------------------------------------ */
57      /**
58       * Assigns the rules to process.
59       * @param rules an array of {@link Rule}. 
60       */
61      public void setRules(Rule[] rules)
62      {
63          _rules = rules;
64      }
65  
66      /* ------------------------------------------------------------ */
67      /**
68       * Add a Rule
69       * @param rule The rule to add to the end of the rules array
70       */
71      public void addRule(Rule rule)
72      {
73          _rules = ArrayUtil.addToArray(_rules,rule,Rule.class);
74      }
75     
76  
77      /* ------------------------------------------------------------ */
78      /**
79       * @return the rewriteRequestURI If true, this handler will rewrite the value
80       * returned by {@link HttpServletRequest#getRequestURI()}.
81       */
82      public boolean isRewriteRequestURI()
83      {
84          return _rewriteRequestURI;
85      }
86  
87      /* ------------------------------------------------------------ */
88      /**
89       * @param rewriteRequestURI true if this handler will rewrite the value
90       * returned by {@link HttpServletRequest#getRequestURI()}.
91       */
92      public void setRewriteRequestURI(boolean rewriteRequestURI)
93      {
94          _rewriteRequestURI=rewriteRequestURI;
95      }
96  
97      /* ------------------------------------------------------------ */
98      /**
99       * @return true if this handler will rewrite the value
100      * returned by {@link HttpServletRequest#getPathInfo()}.
101      */
102     public boolean isRewritePathInfo()
103     {
104         return _rewritePathInfo;
105     }
106 
107     /* ------------------------------------------------------------ */
108     /**
109      * @param rewritePathInfo true if this handler will rewrite the value
110      * returned by {@link HttpServletRequest#getPathInfo()}.
111      */
112     public void setRewritePathInfo(boolean rewritePathInfo)
113     {
114         _rewritePathInfo=rewritePathInfo;
115     }
116 
117     /* ------------------------------------------------------------ */
118     /**
119      * @return the originalPathAttribte. If non null, this string will be used
120      * as the attribute name to store the original request path.
121      */
122     public String getOriginalPathAttribute()
123     {
124         return _originalPathAttribute;
125     }
126 
127     /* ------------------------------------------------------------ */
128     /**
129      * @param originalPathAttribte If non null, this string will be used
130      * as the attribute name to store the original request path.
131      */
132     public void setOriginalPathAttribute(String originalPathAttribte)
133     {
134         _originalPathAttribute=originalPathAttribte;
135     }
136     
137     /**
138      * Process the contained rules
139      * @param target target field to pass on to the contained rules
140      * @param request request object to pass on to the contained rules
141      * @param response response object to pass on to the contained rules
142      */
143     @Override
144     public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
145     {
146         return apply(target, request, response);
147     }
148 
149     /**
150      * Process the contained rules (called by matchAndApply) 
151      * @param target target field to pass on to the contained rules
152      * @param request request object to pass on to the contained rules
153      * @param response response object to pass on to the contained rules
154      * @return the target
155      * @throws IOException if unable to apply the rule
156      */
157     protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
158     {
159         boolean original_set=_originalPathAttribute==null;
160                 
161         for (Rule rule : _rules)
162         {
163             String applied=rule.matchAndApply(target,request, response);
164             if (applied!=null)
165             {       
166                 LOG.debug("applied {}",rule);
167                 LOG.debug("rewrote {} to {}",target,applied);
168                 if (!original_set)
169                 {
170                     original_set=true;
171                     request.setAttribute(_originalPathAttribute, target);
172                 }     
173 
174                 if (_rewriteRequestURI)
175                 {
176                     String encoded=URIUtil.encodePath(applied);
177                     if (rule instanceof Rule.ApplyURI)
178                         ((Rule.ApplyURI)rule).applyURI((Request)request,((Request)request).getRequestURI(), encoded);
179                     else
180                         ((Request)request).setURIPathQuery(encoded);
181                 }
182 
183                 if (_rewritePathInfo)
184                     ((Request)request).setPathInfo(applied);
185 
186                 target=applied;
187                 
188                 if (rule.isHandling())
189                 {
190                     LOG.debug("handling {}",rule);
191                     Request.getBaseRequest(request).setHandled(true);
192                 }
193 
194                 if (rule.isTerminating())
195                 {
196                     LOG.debug("terminating {}",rule);
197                     break;
198                 }
199             }
200         }
201 
202         return target;
203     }
204 }