View Javadoc

1   // ========================================================================
2   // $Id$
3   // Copyright (c) 2004-2009 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   // The Eclipse Public License is available at 
9   // http://www.eclipse.org/legal/epl-v10.html
10  // The Apache License v2.0 is available at
11  // http://www.opensource.org/licenses/apache2.0.php
12  // You may elect to redistribute this code under either of these licenses. 
13  // ========================================================================
14  package org.eclipse.jetty.rewrite.handler;
15  
16  import java.io.IOException;
17  import java.util.Map;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.eclipse.jetty.http.PathMap;
23  import org.eclipse.jetty.util.URIUtil;
24  
25  /**
26   * Rule implementing the legacy API of RewriteHandler
27   * 
28   *
29   */
30  public class LegacyRule extends Rule
31  {
32      private PathMap _rewrite = new PathMap(true);
33      
34      public LegacyRule()
35      {
36          _handling = false;
37          _terminating = false;
38      }
39  
40      /* ------------------------------------------------------------ */
41      @Override
42      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
43      {
44          Map.Entry<?,?> rewrite =_rewrite.getMatch(target);
45          
46          if (rewrite!=null && rewrite.getValue()!=null)
47          {
48              target=URIUtil.addPaths(rewrite.getValue().toString(),
49                      PathMap.pathInfo(rewrite.getKey().toString(),target));
50  
51              return target;
52          }
53          
54          return null;
55      }
56      
57      /* ------------------------------------------------------------ */
58      /**
59       * Returns the map of rewriting rules.
60       * @return A {@link PathMap} of the rewriting rules.
61       */
62      public PathMap getRewrite()
63      {
64          return _rewrite;
65      }
66  
67      /* ------------------------------------------------------------ */
68      /**
69       * Sets the map of rewriting rules.
70       * @param rewrite A {@link PathMap} of the rewriting rules. Only 
71       * prefix paths should be included.
72       */
73      public void setRewrite(PathMap rewrite)
74      {
75          _rewrite=rewrite;
76      }
77      
78      
79      /* ------------------------------------------------------------ */
80      /** Add a path rewriting rule
81       * @param pattern The path pattern to match. The pattern must start with / and may use
82       * a trailing /* as a wildcard.
83       * @param prefix The path prefix which will replace the matching part of the path.
84       */
85      public void addRewriteRule(String pattern, String prefix)
86      {
87          if (pattern==null || pattern.length()==0 || !pattern.startsWith("/"))
88              throw new IllegalArgumentException();
89          if (_rewrite==null)
90              _rewrite=new PathMap(true);
91          _rewrite.put(pattern,prefix);
92      }
93  
94  
95  }