1 //========================================================================
2 //$Id: RedirectPatternRule.java 966 2008-04-17 13:53:44Z gregw $
3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15 package org.mortbay.jetty.handler.rewrite;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import java.io.IOException;
20
21 /**
22 * Redirects the response whenever the rule finds a match.
23 */
24 public class RedirectPatternRule extends PatternRule
25 {
26 private String _location;
27
28 /* ------------------------------------------------------------ */
29 public RedirectPatternRule()
30 {
31 _handling = true;
32 _terminating = true;
33 }
34
35 /* ------------------------------------------------------------ */
36 /**
37 * Sets the redirect location.
38 *
39 * @param value the location to redirect.
40 */
41 public void setLocation(String value)
42 {
43 _location = value;
44 }
45
46 /* ------------------------------------------------------------ */
47 /*
48 * (non-Javadoc)
49 * @see org.mortbay.jetty.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
50 */
51 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
52 {
53 response.sendRedirect(_location);
54 return target;
55 }
56
57 /* ------------------------------------------------------------ */
58 /**
59 * Returns the redirect location.
60 */
61 public String toString()
62 {
63 return super.toString()+"["+_location+"]";
64 }
65 }