View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.http.PathMap;
28  import org.eclipse.jetty.util.URIUtil;
29  
30  /**
31   * Rule implementing the legacy API of RewriteHandler
32   * 
33   *
34   */
35  public class LegacyRule extends Rule
36  {
37      private PathMap _rewrite = new PathMap(true);
38      
39      public LegacyRule()
40      {
41          _handling = false;
42          _terminating = false;
43      }
44  
45      /* ------------------------------------------------------------ */
46      @Override
47      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
48      {
49          Map.Entry<?,?> rewrite =_rewrite.getMatch(target);
50          
51          if (rewrite!=null && rewrite.getValue()!=null)
52          {
53              target=URIUtil.addPaths(rewrite.getValue().toString(),
54                      PathMap.pathInfo(rewrite.getKey().toString(),target));
55  
56              return target;
57          }
58          
59          return null;
60      }
61      
62      /* ------------------------------------------------------------ */
63      /**
64       * Returns the map of rewriting rules.
65       * @return A {@link PathMap} of the rewriting rules.
66       */
67      public PathMap getRewrite()
68      {
69          return _rewrite;
70      }
71  
72      /* ------------------------------------------------------------ */
73      /**
74       * Sets the map of rewriting rules.
75       * @param rewrite A {@link PathMap} of the rewriting rules. Only 
76       * prefix paths should be included.
77       */
78      public void setRewrite(PathMap rewrite)
79      {
80          _rewrite=rewrite;
81      }
82      
83      
84      /* ------------------------------------------------------------ */
85      /** Add a path rewriting rule
86       * @param pattern The path pattern to match. The pattern must start with / and may use
87       * a trailing /* as a wildcard.
88       * @param prefix The path prefix which will replace the matching part of the path.
89       */
90      public void addRewriteRule(String pattern, String prefix)
91      {
92          if (pattern==null || pattern.length()==0 || !pattern.startsWith("/"))
93              throw new IllegalArgumentException();
94          if (_rewrite==null)
95              _rewrite=new PathMap(true);
96          _rewrite.put(pattern,prefix);
97      }
98  
99  
100 }