View Javadoc

1   package org.apache.maven.wagon.tck.http.fixture;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }