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