Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0239344a40d654cb25eebbbe88560eebb44179e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.util.ArrayUtil;

/**
 * Groups rules that apply only to a specific virtual host
 * or sets of virtual hosts
 * 
 *  
 */

public class VirtualHostRuleContainer extends RuleContainer
{
    private String[] _virtualHosts;

    /* ------------------------------------------------------------ */
    /** Set the virtual hosts that the rules within this container will apply to
     * @param virtualHosts Array of virtual hosts that the rules within this container are applied to. 
     * A null hostname or null/empty array means any hostname is acceptable.
     */
    public void setVirtualHosts( String[] virtualHosts )
    {
        if ( virtualHosts == null )
        {
            _virtualHosts = virtualHosts;
        } 
        else 
        {
            _virtualHosts = new String[virtualHosts.length];
            for ( int i = 0; i < virtualHosts.length; i++ )
                _virtualHosts[i] = normalizeHostname( virtualHosts[i]);
        }
    }

    /* ------------------------------------------------------------ */
    /** Get the virtual hosts that the rules within this container will apply to
     * @return Array of virtual hosts that the rules within this container are applied to. 
     * A null hostname or null/empty array means any hostname is acceptable.
     */
    public String[] getVirtualHosts()
    {
        return _virtualHosts;
    }
    
    /* ------------------------------------------------------------ */
    /**
     * @param virtualHost add a virtual host to the existing list of virtual hosts
     * A null hostname means any hostname is acceptable 
     */
    public void addVirtualHost(String virtualHost)
    {
        _virtualHosts = ArrayUtil.addToArray(_virtualHosts,virtualHost,String.class);
    }

    /**
     * Process the contained rules if the request is applicable to the virtual hosts of this rule
     * @param target target field to pass on to the contained rules
     * @param request request object to pass on to the contained rules
     * @param response response object to pass on to the contained rules
     */
    @Override
    public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        if(_virtualHosts != null && _virtualHosts.length > 0 )
        {
            String requestHost = normalizeHostname( request.getServerName() );
            for( String ruleHost : _virtualHosts )
            {
                if(ruleHost == null || ruleHost.equalsIgnoreCase(requestHost)
                        || (ruleHost.startsWith("*.") && ruleHost.regionMatches(true,2,requestHost,requestHost.indexOf(".")+1,ruleHost.length()-2)))
                    return apply(target, request, response);
            }
        }
        else
        {
            return apply(target, request, response);
        }
        return null;
    }

    /* ------------------------------------------------------------ */
    private String normalizeHostname( String host )
    {
        if ( host == null )
            return null;
        
        if ( host.endsWith( "." ) )
            return host.substring( 0, host.length() -1);
      
            return host;
    }

}

Back to the top