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