URLConnectionResponse.java revision b852fcf48a8909164d7f323dd02a35d2a8056a61
1/*
2 * Copyright 2008 Netflix, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package net.oauth.client;
18
19import java.io.IOException;
20import java.io.InputStream;
21import java.net.HttpURLConnection;
22import java.net.URLConnection;
23import java.util.ArrayList;
24import java.util.List;
25import java.util.Map;
26import net.oauth.OAuth;
27import net.oauth.http.HttpMessage;
28import net.oauth.http.HttpResponseMessage;
29
30/**
31 * The response part of a URLConnection, encapsulated as an OAuthMessage.
32 *
33 * @author John Kristian
34 * @hide
35 */
36public class URLConnectionResponse extends HttpResponseMessage {
37
38    /**
39     * Construct an OAuthMessage from the HTTP response, including parameters
40     * from OAuth WWW-Authenticate headers and the body. The header parameters
41     * come first, followed by the ones from the response body.
42     */
43    public URLConnectionResponse(HttpMessage request, String requestHeaders,
44            byte[] requestExcerpt, URLConnection connection) throws IOException {
45        super(request.method, request.url);
46        this.requestHeaders = requestHeaders;
47        this.requestExcerpt = requestExcerpt;
48        this.requestEncoding = request.getContentCharset();
49        this.connection = connection;
50        this.headers.addAll(getHeaders());
51    }
52
53    private final String requestHeaders;
54    private final byte[] requestExcerpt;
55    private final String requestEncoding;
56    private final URLConnection connection;
57
58    @Override
59    public int getStatusCode() throws IOException {
60        if (connection instanceof HttpURLConnection) {
61            return ((HttpURLConnection) connection).getResponseCode();
62        }
63        return STATUS_OK;
64    }
65
66    @Override
67    public InputStream openBody() {
68        try {
69            return connection.getInputStream();
70        } catch (IOException ohWell) {
71        }
72        return null;
73    }
74
75    private List<Map.Entry<String, String>> getHeaders() {
76        List<Map.Entry<String, String>> headers = new ArrayList<Map.Entry<String, String>>();
77        boolean foundContentType = false;
78        String value;
79        for (int i = 0; (value = connection.getHeaderField(i)) != null; ++i) {
80            String name = connection.getHeaderFieldKey(i);
81            if (name != null) {
82                headers.add(new OAuth.Parameter(name, value));
83                if (CONTENT_TYPE.equalsIgnoreCase(name)) {
84                    foundContentType = true;
85                }
86            }
87        }
88        if (!foundContentType) {
89            headers.add(new OAuth.Parameter(CONTENT_TYPE, connection
90                    .getContentType()));
91        }
92        return headers;
93    }
94    /** Return a complete description of the HTTP exchange. */
95    @Override
96    public void dump(Map<String, Object> into) throws IOException {
97        super.dump(into);
98        {
99            StringBuilder request = new StringBuilder(requestHeaders);
100            request.append(EOL);
101            if (requestExcerpt != null) {
102                request.append(new String(requestExcerpt, requestEncoding));
103            }
104            into.put(REQUEST, request.toString());
105        }
106        {
107            HttpURLConnection http = (connection instanceof HttpURLConnection) ? (HttpURLConnection) connection
108                    : null;
109            StringBuilder response = new StringBuilder();
110            String value;
111            for (int i = 0; (value = connection.getHeaderField(i)) != null; ++i) {
112                String name = connection.getHeaderFieldKey(i);
113                if (i == 0 && name != null && http != null) {
114                    String firstLine = "HTTP " + getStatusCode();
115                    String message = http.getResponseMessage();
116                    if (message != null) {
117                        firstLine += (" " + message);
118                    }
119                    response.append(firstLine).append(EOL);
120                }
121                if (name != null) {
122                    response.append(name).append(": ");
123                    name = name.toLowerCase();
124                }
125                response.append(value).append(EOL);
126            }
127            response.append(EOL);
128            if (body != null) {
129                response.append(new String(((ExcerptInputStream) body)
130                        .getExcerpt(), getContentCharset()));
131            }
132            into.put(HttpMessage.RESPONSE, response.toString());
133        }
134    }
135
136}
137