View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.rewrite.handler;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.util.LazyList;
22  
23  /**
24   * Groups rules that apply only to a specific virtual host
25   * or sets of virtual hosts
26   * 
27   *  
28   */
29  
30  public class VirtualHostRuleContainer extends RuleContainer
31  {
32      private String[] _virtualHosts;
33  
34      /* ------------------------------------------------------------ */
35      /** Set the virtual hosts that the rules within this container will apply to
36       * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
37       * A null hostname or null/empty array means any hostname is acceptable.
38       */
39      public void setVirtualHosts( String[] virtualHosts )
40      {
41          if ( virtualHosts == null )
42          {
43              _virtualHosts = virtualHosts;
44          } 
45          else 
46          {
47              _virtualHosts = new String[virtualHosts.length];
48              for ( int i = 0; i < virtualHosts.length; i++ )
49                  _virtualHosts[i] = normalizeHostname( virtualHosts[i]);
50          }
51      }
52  
53      /* ------------------------------------------------------------ */
54      /** Get the virtual hosts that the rules within this container will apply to
55       * @return Array of virtual hosts that the rules within this container are applied to. 
56       * A null hostname or null/empty array means any hostname is acceptable.
57       */
58      public String[] getVirtualHosts()
59      {
60          return _virtualHosts;
61      }
62      
63      /* ------------------------------------------------------------ */
64      /**
65       * @param virtualHost add a virtual host to the existing list of virtual hosts
66       * A null hostname means any hostname is acceptable 
67       */
68      public void addVirtualHost(String virtualHost)
69      {
70          _virtualHosts = (String[])LazyList.addToArray(_virtualHosts,virtualHost,String.class);
71      }
72  
73      /**
74       * Process the contained rules if the request is applicable to the virtual hosts of this rule
75       * @param target target field to pass on to the contained rules
76       * @param request request object to pass on to the contained rules
77       * @param response response object to pass on to the contained rules
78       */
79      @Override
80      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
81      {
82          if(_virtualHosts != null && _virtualHosts.length > 0 )
83          {
84              String requestHost = normalizeHostname( request.getServerName() );
85              for( String ruleHost : _virtualHosts )
86              {
87                  if(ruleHost == null || ruleHost.equalsIgnoreCase(requestHost)
88                          || (ruleHost.startsWith("*.") && ruleHost.regionMatches(true,2,requestHost,requestHost.indexOf(".")+1,ruleHost.length()-2)))
89                      return apply(target, request, response);
90              }
91          }
92          else
93          {
94              return apply(target, request, response);
95          }
96          return null;
97      }
98  
99      /* ------------------------------------------------------------ */
100     private String normalizeHostname( String host )
101     {
102         if ( host == null )
103             return null;
104         
105         if ( host.endsWith( "." ) )
106             return host.substring( 0, host.length() -1);
107       
108             return host;
109     }
110 
111 }