1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.http.HttpStatus;
27
28
29
30
31
32
33
34
35 public class RedirectPatternRule extends PatternRule
36 {
37 private String _location;
38 private int _statusCode = HttpStatus.FOUND_302;
39
40 public RedirectPatternRule()
41 {
42 _handling = true;
43 _terminating = true;
44 }
45
46
47
48
49
50
51 public void setLocation(String value)
52 {
53 _location = value;
54 }
55
56
57
58
59
60
61 public void setStatusCode(int statusCode)
62 {
63 if ((300 <= statusCode) || (statusCode >= 399))
64 {
65 _statusCode = statusCode;
66 }
67 else
68 {
69 throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)");
70 }
71 }
72
73 @Override
74 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
75 {
76 String location = response.encodeRedirectURL(_location);
77 response.setHeader("Location",RedirectUtil.toRedirectURL(request,location));
78 response.setStatus(_statusCode);
79 response.getOutputStream().flush();
80 response.getOutputStream().close();
81 return target;
82 }
83
84
85
86
87 @Override
88 public String toString()
89 {
90 StringBuilder str = new StringBuilder();
91 str.append(super.toString());
92 str.append('[').append(_statusCode);
93 str.append('>').append(_location);
94 str.append(']');
95 return str.toString();
96 }
97 }