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
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.eclipse.jetty.util.ArrayUtil;
27
28 /**
29 * Groups rules that apply only to a specific virtual host
30 * or sets of virtual hosts
31 *
32 *
33 */
34
35 public class VirtualHostRuleContainer extends RuleContainer
36 {
37 private String[] _virtualHosts;
38
39 /* ------------------------------------------------------------ */
40 /** Set the virtual hosts that the rules within this container will apply to
41 * @param virtualHosts Array of virtual hosts that the rules within this container are applied to.
42 * A null hostname or null/empty array means any hostname is acceptable.
43 */
44 public void setVirtualHosts( String[] virtualHosts )
45 {
46 if ( virtualHosts == null )
47 {
48 _virtualHosts = virtualHosts;
49 }
50 else
51 {
52 _virtualHosts = new String[virtualHosts.length];
53 for ( int i = 0; i < virtualHosts.length; i++ )
54 _virtualHosts[i] = normalizeHostname( virtualHosts[i]);
55 }
56 }
57
58 /* ------------------------------------------------------------ */
59 /** Get the virtual hosts that the rules within this container will apply to
60 * @return Array of virtual hosts that the rules within this container are applied to.
61 * A null hostname or null/empty array means any hostname is acceptable.
62 */
63 public String[] getVirtualHosts()
64 {
65 return _virtualHosts;
66 }
67
68 /* ------------------------------------------------------------ */
69 /**
70 * @param virtualHost add a virtual host to the existing list of virtual hosts
71 * A null hostname means any hostname is acceptable
72 */
73 public void addVirtualHost(String virtualHost)
74 {
75 _virtualHosts = ArrayUtil.addToArray(_virtualHosts,virtualHost,String.class);
76 }
77
78 /**
79 * Process the contained rules if the request is applicable to the virtual hosts of this rule
80 * @param target target field to pass on to the contained rules
81 * @param request request object to pass on to the contained rules
82 * @param response response object to pass on to the contained rules
83 */
84 @Override
85 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
86 {
87 if(_virtualHosts != null && _virtualHosts.length > 0 )
88 {
89 String requestHost = normalizeHostname( request.getServerName() );
90 for( String ruleHost : _virtualHosts )
91 {
92 if(ruleHost == null || ruleHost.equalsIgnoreCase(requestHost)
93 || (ruleHost.startsWith("*.") && ruleHost.regionMatches(true,2,requestHost,requestHost.indexOf(".")+1,ruleHost.length()-2)))
94 return apply(target, request, response);
95 }
96 }
97 else
98 {
99 return apply(target, request, response);
100 }
101 return null;
102 }
103
104 /* ------------------------------------------------------------ */
105 private String normalizeHostname( String host )
106 {
107 if ( host == null )
108 return null;
109
110 if ( host.endsWith( "." ) )
111 return host.substring( 0, host.length() -1);
112
113 return host;
114 }
115
116 }