View Javadoc

1   package org.mortbay.jetty.handler.rewrite;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import org.mortbay.util.LazyList;
9   
10  /**
11   * Groups rules that apply only to a specific virtual host
12   * or sets of virtual hosts
13   * 
14   *  @author Athena Yao
15   */
16  
17  public class VirtualHostRuleContainer extends RuleContainer
18  {
19      private String[] _virtualHosts;
20  
21      /* ------------------------------------------------------------ */
22      /** Set the virtual hosts that the rules within this container will apply to
23       * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
24       * A null hostname or null/empty array means any hostname is acceptable.
25       */
26      public void setVirtualHosts( String[] virtualHosts )
27      {
28          if ( virtualHosts == null )
29          {
30              _virtualHosts = virtualHosts;
31          } 
32          else 
33          {
34              _virtualHosts = new String[virtualHosts.length];
35              for ( int i = 0; i < virtualHosts.length; i++ )
36                  _virtualHosts[i] = normalizeHostname( virtualHosts[i]);
37          }
38      }
39  
40      /* ------------------------------------------------------------ */
41      /** Get the virtual hosts that the rules within this container will apply to
42       * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
43       * A null hostname or null/empty array means any hostname is acceptable.
44       */
45      public String[] getVirtualHosts()
46      {
47          return _virtualHosts;
48      }
49      
50      /* ------------------------------------------------------------ */
51      /**
52       * @param virtualHost add a virtual host to the existing list of virtual hosts
53       * A null hostname means any hostname is acceptable 
54       */
55      public void addVirtualHost(String virtualHost)
56      {
57          _virtualHosts = (String[])LazyList.addToArray(_virtualHosts,virtualHost,String.class);
58      }
59  
60      /**
61       * Process the contained rules if the request is applicable to the virtual hosts of this rule
62       * @param target target field to pass on to the contained rules
63       * @param request request object to pass on to the contained rules
64       * @param response response object to pass on to the contained rules
65       */
66      @Override
67      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
68      {
69          if(_virtualHosts != null && _virtualHosts.length > 0 )
70          {
71              String requestHost = normalizeHostname( request.getServerName() );
72              for( String ruleHost : _virtualHosts )
73              {
74                  if(ruleHost == null || ruleHost.equalsIgnoreCase(requestHost)
75                          || (ruleHost.startsWith("*.") && ruleHost.regionMatches(true,2,requestHost,requestHost.indexOf(".")+1,ruleHost.length()-2)))
76                      return apply(target, request, response);
77              }
78          }
79          else
80          {
81              return apply(target, request, response);
82          }
83          return null;
84      }
85  
86      /* ------------------------------------------------------------ */
87      private String normalizeHostname( String host )
88      {
89          if ( host == null )
90              return null;
91          
92          if ( host.endsWith( "." ) )
93              return host.substring( 0, host.length() -1);
94        
95              return host;
96      }
97  
98  }