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 private int redirectCount = 0;
41
42 private final String myPath;
43
44 public RedirectionServlet( final int code, final String targetPath )
45 {
46 this.code = code;
47 this.targetPath = targetPath;
48 this.maxRedirects = 1;
49 this.myPath = null;
50 }
51
52 public RedirectionServlet( final int code, final String myPath, final String targetPath, final int maxRedirects )
53 {
54 this.code = code;
55 this.myPath = myPath;
56 this.targetPath = targetPath;
57 this.maxRedirects = maxRedirects;
58 }
59
60 public int getRedirectCount()
61 {
62 return redirectCount;
63 }
64
65 @Override
66 protected void service( final HttpServletRequest req, final HttpServletResponse resp )
67 throws ServletException, IOException
68 {
69 redirectCount++;
70
71 if ( myPath == null )
72 {
73 resp.setStatus( code );
74 resp.setHeader( "Location", targetPath );
75 }
76 else if ( maxRedirects < 0 )
77 {
78 resp.setStatus( code );
79 resp.setHeader( "Location", myPath );
80 }
81 else if ( redirectCount <= maxRedirects )
82 {
83 resp.setStatus( code );
84 resp.setHeader( "Location", myPath + "/" + redirectCount );
85 }
86 else
87 {
88 resp.setStatus( code );
89 resp.setHeader( "Location", targetPath );
90 }
91 }
92
93 }