aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/filter/DiscFilterResponse.java
blob: 4e8b779c516a05bf0c1db7e35b79dbc82bc29e05 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import com.yahoo.jdisc.http.servlet.ServletOrJdiscHttpResponse;

import com.yahoo.jdisc.HeaderFields;
import com.yahoo.jdisc.http.Cookie;


import com.yahoo.jdisc.http.HttpResponse;

/**
 * This class was made abstract from 5.27. Test cases that need
 * a concrete instance should create a {@link JdiscFilterResponse}.
 *
 * @author tejalk
 */
public abstract class DiscFilterResponse {

	private final ServletOrJdiscHttpResponse parent;
	private final HeaderFields untreatedHeaders;
	private final List<Cookie> untreatedCookies;

	public DiscFilterResponse(ServletOrJdiscHttpResponse parent) {
		this.parent = parent;

        this.untreatedHeaders = new HeaderFields();
        parent.copyHeaders(untreatedHeaders);

		this.untreatedCookies = getCookies();
	}

    /* Attributes on the response are only used for unit testing.
     * There is no such thing as 'attributes' in the underlying response. */

	public Enumeration<String> getAttributeNames() {
		return Collections.enumeration(parent.context().keySet());
	}

	public Object getAttribute(String name) {
		return parent.context().get(name);
	}

	public void setAttribute(String name, Object value) {
		parent.context().put(name, value);
	}

	public void removeAttribute(String name) {
		parent.context().remove(name);
	}

	/**
	 * Returns the untreatedHeaders from the parent request
	 */
	public HeaderFields getUntreatedHeaders() {
		return untreatedHeaders;
	}

	/**
	 * Returns the untreatedCookies from the parent request
     */
    public List<Cookie> getUntreatedCookies() {
		return untreatedCookies;
	}

    /**
     * Sets a header with the given name and value.
     * <p>
     * If the header had already been set, the new value overwrites the previous one.
     */
    public abstract void setHeader(String name, String value);

    public abstract void removeHeaders(String name);

	/**
     * Sets a header with the given name and value.
     * <p>
     * If the header had already been set, the new value overwrites the previous one.
     */
	public abstract void setHeaders(String name, String value);

	/**
     * Sets a header with the given name and value.
     * <p>
     * If the header had already been set, the new value overwrites the previous one.
     */
	public abstract void setHeaders(String name, List<String> values);

    /**
     * Adds a header with the given name and value
     * @see com.yahoo.jdisc.HeaderFields#add
     */
    public abstract void addHeader(String name, String value);

	public abstract String getHeader(String name);

	public List<Cookie> getCookies() {
		return parent.decodeSetCookieHeader();
	}

	public abstract void setCookies(List<Cookie> cookies);

	public int getStatus() {
	    return parent.getStatus();
	}

	public abstract void setStatus(int status);

	/**
	 * Return the parent HttpResponse
     */
	public HttpResponse getParentResponse() {
        if (parent instanceof HttpResponse)
            return (HttpResponse)parent;
        throw new UnsupportedOperationException(
                "getParentResponse is not supported for " + parent.getClass().getName());
	}

    public void addCookie(JDiscCookieWrapper cookie) {
        if(cookie != null) {
            List<Cookie> cookies = new ArrayList<>();
            //Get current set of cookies first
            List<Cookie> c = getCookies();
            if((c != null) && (! c.isEmpty())) {
                cookies.addAll(c);
            }
            cookies.add(cookie.getCookie());
            setCookies(cookies);
        }
    }

    /**
     * This method does not actually send the response as it
     * does not have access to responseHandler but
     * just sets the status. The methodName is misleading
	 * for historical reasons.
     */
    public void sendError(int errorCode) throws IOException {
        setStatus(errorCode);
    }

    public void setCookie(String name, String value) {
        Cookie cookie = new Cookie(name, value);
        setCookies(Arrays.asList(cookie));
    }

 }