summaryrefslogtreecommitdiff
path: root/core/src/main/java/net/oauth/http/HttpMessage.java
blob: 8f3ee5f1ff2530138b6eb072f3f732927e279190 (plain)
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
155
156
157
158
159
160
161
/*
 * Copyright 2008 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.oauth.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.oauth.client.ExcerptInputStream;

/**
 * An HTTP request or response.
 * 
 * @author John Kristian
 * @hide
 */
public class HttpMessage
{

    public HttpMessage()
    {
        this(null, null);
    }

    public HttpMessage(String method, URL url)
    {
        this(method, url, null);
    }

    public HttpMessage(String method, URL url, InputStream body)
    {
        this.method = method;
        this.url = url;
        this.body = body;
    }

    public String method;
    public URL url;
    public final List<Map.Entry<String, String>> headers = new ArrayList<Map.Entry<String, String>>();
    protected InputStream body = null;

    /**
     * Get the value of the last header of the given name. The name is
     * case-insensitive.
     */
    public final String getHeader(String name)
    {
        String value = null;
        for (Map.Entry<String, String> header : headers) {
            if (equalsIgnoreCase(name, header.getKey())) {
                value = header.getValue();
            }
        }
        return value;
    }

    /**
     * Remove all headers of the given name. The name is case insensitive.
     * 
     * @return the value of the last header with that name, or null to indicate
     *         there was no such header
     */
    public String removeHeaders(String name)
    {
        String value = null;
        for (Iterator<Map.Entry<String, String>> i = headers.iterator(); i.hasNext();) {
            Map.Entry<String, String> header = i.next();
            if (equalsIgnoreCase(name, header.getKey())) {
                value = header.getValue();
                i.remove();
            }
        }
        return value;
    }

    public final String getContentCharset()
    {
        return getCharset(getHeader(CONTENT_TYPE));
    }

    public final InputStream getBody() throws IOException
    {
        if (body == null) {
            InputStream raw = openBody();
            if (raw != null) {
                body = new ExcerptInputStream(raw);
            }
        }
        return body;
    }

    protected InputStream openBody() throws IOException
    {
        return null;
    }

    /** Put a description of this message and its origins into the given Map. */
    public void dump(Map<String, Object> into) throws IOException
    {
    }

    private static boolean equalsIgnoreCase(String x, String y)
    {
        if (x == null)
            return y == null;
        else
            return x.equalsIgnoreCase(y);
    }

    private static final String getCharset(String mimeType)
    {
        if (mimeType != null) {
            Matcher m = CHARSET.matcher(mimeType);
            if (m.find()) {
                String charset = m.group(1);
                if (charset.length() >= 2 && charset.charAt(0) == '"'
                        && charset.charAt(charset.length() - 1) == '"') {
                    charset = charset.substring(1, charset.length() - 1);
                    charset = charset.replace("\\\"", "\"");
                }
                return charset;
            }
        }
        return DEFAULT_CHARSET;
    }

    /** The name of a dump entry whose value is the HTTP request. */
    public static final String REQUEST = "HTTP request";

    /** The name of a dump entry whose value is the HTTP response. */
    public static final String RESPONSE = "HTTP response";

    public static final String ACCEPT_ENCODING = "Accept-Encoding";
    public static final String CONTENT_ENCODING = "Content-Encoding";
    public static final String CONTENT_LENGTH = "Content-Length";
    public static final String CONTENT_TYPE = "Content-Type";
    public static final String DEFAULT_CHARSET = "ISO-8859-1";

    private static final Pattern CHARSET = Pattern
            .compile("; *charset *= *([^;\"]*|\"([^\"]|\\\\\")*\")(;|$)");

}