1 | |
package org.apache.maven.wagon.tck.http.fixture; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import javax.servlet.ServletException; |
23 | |
import javax.servlet.http.HttpServlet; |
24 | |
import javax.servlet.http.HttpServletRequest; |
25 | |
import javax.servlet.http.HttpServletResponse; |
26 | |
import java.io.IOException; |
27 | |
|
28 | |
public class RedirectionServlet |
29 | |
extends HttpServlet |
30 | |
{ |
31 | |
|
32 | |
private static final long serialVersionUID = 1L; |
33 | |
|
34 | |
private final String targetPath; |
35 | |
|
36 | |
private final int code; |
37 | |
|
38 | |
private final int maxRedirects; |
39 | |
|
40 | 0 | private int redirectCount = 0; |
41 | |
|
42 | |
private final String myPath; |
43 | |
|
44 | |
public RedirectionServlet( final int code, final String targetPath ) |
45 | 0 | { |
46 | 0 | this.code = code; |
47 | 0 | this.targetPath = targetPath; |
48 | 0 | this.maxRedirects = 1; |
49 | 0 | this.myPath = null; |
50 | 0 | } |
51 | |
|
52 | |
public RedirectionServlet( final int code, final String myPath, final String targetPath, final int maxRedirects ) |
53 | 0 | { |
54 | 0 | this.code = code; |
55 | 0 | this.myPath = myPath; |
56 | 0 | this.targetPath = targetPath; |
57 | 0 | this.maxRedirects = maxRedirects; |
58 | 0 | } |
59 | |
|
60 | |
public int getRedirectCount() |
61 | |
{ |
62 | 0 | return redirectCount; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
protected void service( final HttpServletRequest req, final HttpServletResponse resp ) |
67 | |
throws ServletException, IOException |
68 | |
{ |
69 | 0 | redirectCount++; |
70 | |
|
71 | 0 | if ( myPath == null ) |
72 | |
{ |
73 | 0 | resp.setStatus( code ); |
74 | 0 | resp.setHeader( "Location", targetPath ); |
75 | |
} |
76 | 0 | else if ( maxRedirects < 0 ) |
77 | |
{ |
78 | 0 | resp.setStatus( code ); |
79 | 0 | resp.setHeader( "Location", myPath ); |
80 | |
} |
81 | 0 | else if ( redirectCount <= maxRedirects ) |
82 | |
{ |
83 | 0 | resp.setStatus( code ); |
84 | 0 | resp.setHeader( "Location", myPath + "/" + redirectCount ); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 0 | resp.setStatus( code ); |
89 | 0 | resp.setHeader( "Location", targetPath ); |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
} |