View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Issues a (3xx) Redirect response whenever the rule finds a match.
30   * <p>
31   * All redirects are part of the <a href="http://tools.ietf.org/html/rfc7231#section-6.4"><code>3xx Redirection</code> status code set</a>.
32   * <p>
33   * Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3"><code>302 Found</code></a>
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       * Sets the redirect location.
48       * 
49       * @param value the location to redirect.
50       */
51      public void setLocation(String value)
52      {
53          _location = value;
54      }
55      
56      /**
57       * Sets the redirect status code.
58       * 
59       * @param statusCode the 3xx redirect status code
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(); // no output / content
80          response.getOutputStream().close();
81          return target;
82      }
83  
84      /**
85       * Returns the redirect status code and location.
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  }