1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 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 oldURI, String newURI) 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 *
48 * @param target The target of the request
49 * @param request the request
50 * @param response the response
51 * @return The new target if the rule has matched, else null
52 * @throws IOException if unable to match the rule
53 */
54 public abstract String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException;
55
56 /**
57 * Sets terminating to true or false.
58 * @param terminating If true, this rule will terminate the loop if this rule has been applied.
59 */
60 public void setTerminating(boolean terminating)
61 {
62 _terminating = terminating;
63 }
64
65 /**
66 * Returns the terminating flag value.
67 *
68 * @return <code>true</code> if the rule needs to terminate; <code>false</code> otherwise.
69 */
70 public boolean isTerminating()
71 {
72 return _terminating;
73 }
74
75 /**
76 * Returns the handling flag value.
77 *
78 * @return <code>true</code> if the rule handles the request and nested handlers should not be called.
79 */
80 public boolean isHandling()
81 {
82 return _handling;
83 }
84
85 /**
86 * Set the handling flag value.
87 *
88 * @param handling true if the rule handles the request and nested handlers should not be called.
89 */
90 public void setHandling(boolean handling)
91 {
92 _handling=handling;
93 }
94
95 /**
96 * Returns the handling and terminating flag values.
97 */
98 @Override
99 public String toString()
100 {
101 return this.getClass().getName()+(_handling?"[H":"[h")+(_terminating?"T]":"t]");
102 }
103 }