Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c50139bbd69027164b055e9d0859468887a2f014 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
//  ========================================================================
//  Copyright (c) 1995-2015 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;


/**
 * Sets the header in the response whenever the rule finds a match.
 */
public class HeaderPatternRule extends PatternRule
{
    private String _name;
    private String _value;
    private boolean _add=false;

    /* ------------------------------------------------------------ */
    public HeaderPatternRule()
    {
        _handling = false;
        _terminating = false;
    }

    /* ------------------------------------------------------------ */
    /**
     * Sets the header name.
     * 
     * @param name name of the header field
     */
    public void setName(String name)
    {
        _name = name;
    }

    /* ------------------------------------------------------------ */
    /**
     * Sets the header value. The value can be either a <code>String</code> or <code>int</code> value.
     * 
     * @param value of the header field
     */
    public void setValue(String value)
    {
        _value = value;
    }

    /* ------------------------------------------------------------ */
    /**
     * Sets the Add flag. 
     * @param add If true, the header is added to the response, otherwise the header it is set on the response.
     */
    public void setAdd(boolean add)
    {
        _add = add;
    }

    /* ------------------------------------------------------------ */
    /**
     * Invokes this method when a match found. If the header had already been set, 
     * the new value overwrites the previous one. Otherwise, it adds the new 
     * header name and value.
     * 
     *@see org.eclipse.jetty.rewrite.handler.Rule#matchAndApply(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        // process header
        if (_add)
            response.addHeader(_name, _value);
        else
            response.setHeader(_name, _value); 
        return target;
    }
    
    

    /* ------------------------------------------------------------ */
    /**
     * Returns the header name.
     * @return the header name.
     */
    public String getName()
    {
        return _name;
    }

    /* ------------------------------------------------------------ */
    /**
     * Returns the header value.
     * @return the header value.
     */
    public String getValue()
    {
        return _value;
    }

    /* ------------------------------------------------------------ */
    /**
     * Returns the add flag value.
     * @return true if add flag set
     */
    public boolean isAdd()
    {
        return _add;
    }

    /* ------------------------------------------------------------ */
    /**
     * Returns the header contents.
     */
    @Override
    public String toString()
    {
        return super.toString()+"["+_name+","+_value+"]";
    }
}

Back to the top