summaryrefslogtreecommitdiff
path: root/core/src/main/java/net/oauth/client/httpclient4/HttpMethodResponse.java
blob: 1f7b7477bc201d8fe15584dfb4dda939e2fbbeb5 (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
/*
 * Copyright 2008 Sean Sullivan
 *
 * 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.client.httpclient4;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.oauth.OAuth;
import net.oauth.client.ExcerptInputStream;
import net.oauth.http.HttpMessage;
import net.oauth.http.HttpResponseMessage;
import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;

/**
 * An HttpResponse, encapsulated as an OAuthMessage.
 * 
 * This class relies on <a href="http://hc.apache.org">Apache HttpClient</a>
 * version 4.
 * 
 * @author Sean Sullivan
 * @hide
 */
public class HttpMethodResponse extends HttpResponseMessage
{

    /**
     * Construct an OAuthMessage from the HTTP response, including parameters
     * from OAuth WWW-Authenticate headers and the body. The header parameters
     * come first, followed by the ones from the response body.
     */
    public HttpMethodResponse(HttpRequestBase request, HttpResponse response, byte[] requestBody,
            String requestEncoding) throws IOException
    {
        super(request.getMethod(), new URL(request.getURI().toString()));
        this.httpRequest = request;
        this.httpResponse = response;
        this.requestBody = requestBody;
        this.requestEncoding = requestEncoding;
        this.headers.addAll(getHeaders());
    }

    private final HttpRequestBase httpRequest;
    private final HttpResponse httpResponse;
    private final byte[] requestBody;
    private final String requestEncoding;

    @Override
    public int getStatusCode()
    {
        return httpResponse.getStatusLine().getStatusCode();
    }

    @Override
    public InputStream openBody() throws IOException
    {
        return httpResponse.getEntity().getContent();
    }

    private List<Map.Entry<String, String>> getHeaders()
    {
        List<Map.Entry<String, String>> headers = new ArrayList<Map.Entry<String, String>>();
        Header[] allHeaders = httpResponse.getAllHeaders();
        if (allHeaders != null) {
            for (Header header : allHeaders) {
                headers.add(new OAuth.Parameter(header.getName(), header.getValue()));
            }
        }
        return headers;
    }

    /** Return a complete description of the HTTP exchange. */
    @Override
    public void dump(Map<String, Object> into) throws IOException
    {
        super.dump(into);
        {
            StringBuilder request = new StringBuilder(httpRequest.getMethod());
            request.append(" ").append(httpRequest.getURI().getPath());
            String query = httpRequest.getURI().getQuery();
            if (query != null && query.length() > 0) {
                request.append("?").append(query);
            }
            request.append(EOL);
            for (Header header : httpRequest.getAllHeaders()) {
                request.append(header.getName()).append(": ").append(header.getValue()).append(EOL);
            }
            if (httpRequest instanceof HttpEntityEnclosingRequest) {
                HttpEntityEnclosingRequest r = (HttpEntityEnclosingRequest) httpRequest;
                long contentLength = r.getEntity().getContentLength();
                if (contentLength >= 0) {
                    request.append("Content-Length: ").append(contentLength).append(EOL);
                }
            }
            request.append(EOL);
            if (requestBody != null) {
                request.append(new String(requestBody, requestEncoding));
            }
            into.put(REQUEST, request.toString());
        }
        {
            StringBuilder response = new StringBuilder();
            String value = httpResponse.getStatusLine().toString();
            response.append(value).append(EOL);
            for (Header header : httpResponse.getAllHeaders()) {
                String name = header.getName();
                value = header.getValue();
                response.append(name).append(": ").append(value).append(EOL);
            }
            response.append(EOL);
            if (body != null) {
                response.append(new String(((ExcerptInputStream) body).getExcerpt(),
                        getContentCharset()));
            }
            into.put(HttpMessage.RESPONSE, response.toString());
        }
    }
}