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  import java.util.regex.Matcher;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.http.HttpStatus;
28  
29  /**
30   * Issues a (3xx) Redirect response whenever the rule finds a match via regular expression.
31   * <p>
32   * The replacement string may use $n" to replace the nth capture group.
33   * <p>
34   * 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>.
35   * <p>
36   * Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3"><code>302 Found</code></a>
37   */
38  public class RedirectRegexRule extends RegexRule
39  {
40      protected String _replacement;
41      private int _statusCode = HttpStatus.FOUND_302;
42      
43      public RedirectRegexRule()
44      {
45          _handling = true;
46          _terminating = true;
47      }
48  
49      /**
50       * Whenever a match is found, it replaces with this value.
51       * 
52       * @param replacement the replacement string.
53       */
54      public void setReplacement(String replacement)
55      {
56          _replacement = replacement;
57      }
58      
59      /**
60       * Sets the redirect status code.
61       * 
62       * @param statusCode the 3xx redirect status code
63       */
64      public void setStatusCode(int statusCode)
65      {
66          if ((300 <= statusCode) || (statusCode >= 399))
67          {
68              _statusCode = statusCode;
69          }
70          else
71          {
72              throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)");
73          }
74      }
75      
76      @Override
77      protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
78              throws IOException
79      {
80          target=_replacement;
81          for (int g=1;g<=matcher.groupCount();g++)
82          {
83              String group = matcher.group(g);
84              target=target.replaceAll("\\$"+g,group);
85          }
86          
87          target = response.encodeRedirectURL(target);
88          response.setHeader("Location",RedirectUtil.toRedirectURL(request,target));
89          response.setStatus(_statusCode);
90          response.getOutputStream().flush(); // no output / content
91          response.getOutputStream().close();
92          return target;
93      }
94      
95      /**
96       * Returns the redirect status code and replacement.
97       */
98      @Override
99      public String toString()
100     {
101         StringBuilder str = new StringBuilder();
102         str.append(super.toString());
103         str.append('[').append(_statusCode);
104         str.append('>').append(_replacement);
105         str.append(']');
106         return str.toString();
107     }
108 
109 }