View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
28  /**
29   * An abstract rule for creating rewrite rules.
30   */
31  public abstract class Rule
32  {   
33      /**
34       * Interface used to apply a changed target if {@link RuleContainer#setRewriteRequestURI(boolean)} is true.
35       */
36      public interface ApplyURI
37      {
38          void applyURI(Request request, String oldTarget, String newTarget) throws IOException;   
39      }
40      
41      protected boolean _terminating;
42      protected boolean _handling;
43      
44      /**
45       * This method calls tests the rule against the request/response pair and if the Rule 
46       * applies, then the rule's action is triggered.
47       * @param target The target of the request
48       * @param request
49       * @param response
50       * 
51       * @return The new target if the rule has matched, else null
52       * @throws IOException
53       */
54      public abstract String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException;   
55      
56      /**
57       * Sets terminating to true or false.
58       * If true, this rule will terminate the loop if this rule has been applied.
59       * 
60       * @param terminating
61       */    
62      public void setTerminating(boolean terminating)
63      {
64          _terminating = terminating;
65      }
66      
67      /**
68       * Returns the terminating flag value.
69       * 
70       * @return <code>true</code> if the rule needs to terminate; <code>false</code> otherwise. 
71       */
72      public boolean isTerminating()
73      {
74          return _terminating;
75      }
76      
77      /**
78       * Returns the handling flag value.
79       * 
80       * @return <code>true</code> if the rule handles the request and nested handlers should not be called.
81       */
82      public boolean isHandling()
83      {
84          return _handling;
85      }
86      
87      /**
88       * Set the handling flag value.
89       * 
90       * @param handling true if the rule handles the request and nested handlers should not be called.
91       */
92      public void setHandling(boolean handling)
93      {
94          _handling=handling;
95      }
96      
97      /**
98       * Returns the handling and terminating flag values.
99       */
100     @Override
101     public String toString()
102     {
103         return this.getClass().getName()+(_handling?"[H":"[h")+(_terminating?"T]":"t]");
104     }
105 }