1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.handler.rewrite;
16  
17  import java.io.IOException;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.mortbay.util.LazyList;
23  
24  /**
25   * Groups rules that apply only to a specific virtual host
26   * or sets of virtual hosts
27   * 
28   *  @author Athena Yao
29   */
30  
31  public class VirtualHostRuleContainer extends RuleContainer
32  {
33      private String[] _virtualHosts;
34  
35      /* ------------------------------------------------------------ */
36      /** Set the virtual hosts that the rules within this container will apply to
37       * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
38       * A null hostname or null/empty array means any hostname is acceptable.
39       */
40      public void setVirtualHosts( String[] virtualHosts )
41      {
42          if ( virtualHosts == null )
43          {
44              _virtualHosts = virtualHosts;
45          } 
46          else 
47          {
48              _virtualHosts = new String[virtualHosts.length];
49              for ( int i = 0; i < virtualHosts.length; i++ )
50                  _virtualHosts[i] = normalizeHostname( virtualHosts[i]);
51          }
52      }
53  
54      /* ------------------------------------------------------------ */
55      /** Get the virtual hosts that the rules within this container will apply to
56       * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
57       * A null hostname or null/empty array means any hostname is acceptable.
58       */
59      public String[] getVirtualHosts()
60      {
61          return _virtualHosts;
62      }
63      
64      /* ------------------------------------------------------------ */
65      /**
66       * @param virtualHost add a virtual host to the existing list of virtual hosts
67       * A null hostname means any hostname is acceptable 
68       */
69      public void addVirtualHost(String virtualHost)
70      {
71          _virtualHosts = (String[])LazyList.addToArray(_virtualHosts,virtualHost,String.class);
72      }
73  
74      /**
75       * Process the contained rules if the request is applicable to the virtual hosts of this rule
76       * @param target target field to pass on to the contained rules
77       * @param request request object to pass on to the contained rules
78       * @param response response object to pass on to the contained rules
79       */
80      @Override
81      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
82      {
83          if(_virtualHosts != null && _virtualHosts.length > 0 )
84          {
85              String requestHost = normalizeHostname( request.getServerName() );
86              for( String ruleHost : _virtualHosts )
87              {
88                  if(ruleHost == null || ruleHost.equalsIgnoreCase(requestHost)
89                          || (ruleHost.startsWith("*.") && ruleHost.regionMatches(true,2,requestHost,requestHost.indexOf(".")+1,ruleHost.length()-2)))
90                      return apply(target, request, response);
91              }
92          }
93          else
94          {
95              return apply(target, request, response);
96          }
97          return null;
98      }
99  
100     /* ------------------------------------------------------------ */
101     private String normalizeHostname( String host )
102     {
103         if ( host == null )
104             return null;
105         
106         if ( host.endsWith( "." ) )
107             return host.substring( 0, host.length() -1);
108       
109             return host;
110     }
111 
112 }