Skip to content

Commit 7450a60

Browse files
committed
make Response do not know abouth HttpUrlConnection (or any other HTTP stuff)
1 parent 840dbeb commit 7450a60

10 files changed

Lines changed: 87 additions & 102 deletions

File tree

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import java.net.URL;
66
import java.util.Map;
77
import com.github.scribejava.core.exceptions.OAuthConnectionException;
8+
import com.github.scribejava.core.exceptions.OAuthException;
89
import com.github.scribejava.core.oauth.OAuthService;
910
import java.io.File;
11+
import java.net.UnknownHostException;
12+
import java.util.HashMap;
13+
import java.util.List;
1014

1115
public class OAuthRequest extends AbstractRequest {
1216

@@ -57,7 +61,29 @@ Response doSend() throws IOException {
5761
addBody(getByteArrayPayload());
5862
}
5963
}
60-
return new Response(connection);
64+
65+
try {
66+
connection.connect();
67+
final int responseCode = connection.getResponseCode();
68+
return new Response(responseCode, connection.getResponseMessage(), parseHeaders(connection),
69+
responseCode >= 200 && responseCode < 400 ? connection.getInputStream()
70+
: connection.getErrorStream());
71+
} catch (UnknownHostException e) {
72+
throw new OAuthException("The IP address of a host could not be determined.", e);
73+
}
74+
}
75+
76+
private static Map<String, String> parseHeaders(HttpURLConnection conn) {
77+
final Map<String, String> headers = new HashMap<>();
78+
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
79+
final String key = entry.getKey();
80+
if ("Content-Encoding".equalsIgnoreCase(key)) {
81+
headers.put("Content-Encoding", entry.getValue().get(0));
82+
} else {
83+
headers.put(key, entry.getValue().get(0));
84+
}
85+
}
86+
return headers;
6187
}
6288

6389
private void createConnection() throws IOException {

scribejava-core/src/main/java/com/github/scribejava/core/model/Response.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,54 @@
1313

1414
public class Response {
1515

16-
private int code;
17-
private String message;
16+
private final int code;
17+
private final String message;
18+
private final Map<String, String> headers;
1819
private String body;
1920
private InputStream stream;
20-
private Map<String, String> headers;
2121

22+
/**
23+
*
24+
* @param code code
25+
* @param message message
26+
* @param headers headers
27+
* @param body body
28+
* @param stream stream
29+
* @deprecated use either {@link #Response(int, java.lang.String, java.util.Map, java.io.InputStream) }
30+
* or {@link #Response(int, java.lang.String, java.util.Map, java.lang.String) }
31+
*/
32+
@Deprecated
2233
public Response(int code, String message, Map<String, String> headers, String body, InputStream stream) {
2334
this.code = code;
35+
this.message = message;
2436
this.headers = headers;
2537
this.body = body;
38+
this.stream = stream;
39+
}
40+
41+
private Response(int code, String message, Map<String, String> headers) {
42+
this.code = code;
2643
this.message = message;
44+
this.headers = headers;
45+
}
46+
47+
public Response(int code, String message, Map<String, String> headers, InputStream stream) {
48+
this(code, message, headers);
2749
this.stream = stream;
2850
}
2951

52+
public Response(int code, String message, Map<String, String> headers, String body) {
53+
this(code, message, headers);
54+
this.body = body;
55+
}
56+
57+
/**
58+
*
59+
* @param connection connection
60+
* @throws IOException
61+
* @deprecated use {@link #Response(int, java.lang.String, java.util.Map, java.lang.String, java.io.InputStream) }
62+
*/
63+
@Deprecated
3064
Response(HttpURLConnection connection) throws IOException {
3165
try {
3266
connection.connect();
@@ -40,14 +74,24 @@ public Response(int code, String message, Map<String, String> headers, String bo
4074
}
4175

4276
private String parseBodyContents() throws IOException {
77+
if (stream == null) {
78+
return null;
79+
}
4380
if ("gzip".equals(getHeader("Content-Encoding"))) {
44-
body = StreamUtils.getGzipStreamContents(getStream());
81+
body = StreamUtils.getGzipStreamContents(stream);
4582
} else {
46-
body = StreamUtils.getStreamContents(getStream());
83+
body = StreamUtils.getStreamContents(stream);
4784
}
4885
return body;
4986
}
5087

88+
/**
89+
*
90+
* @param conn conn
91+
* @return
92+
* @deprecated use {@link OAuthRequest#parseHeaders(java.net.HttpURLConnection) }
93+
*/
94+
@Deprecated
5195
private Map<String, String> parseHeaders(HttpURLConnection conn) {
5296
final Map<String, String> headers = new HashMap<>();
5397
for (Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
@@ -62,7 +106,7 @@ private Map<String, String> parseHeaders(HttpURLConnection conn) {
62106
}
63107

64108
public final boolean isSuccessful() {
65-
return getCode() >= 200 && getCode() < 400;
109+
return code >= 200 && code < 400;
66110
}
67111

68112
public String getBody() throws IOException {

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth1AccessTokenExtractorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public void shouldThrowExceptionIfResponseIsEmptyString() throws IOException {
7777
}
7878

7979
private static Response ok(String body) {
80-
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(),
81-
body, /* stream */ null);
80+
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
8281
}
8382
}

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenExtractorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public void shouldThrowExceptionIfResponseIsEmptyString() throws IOException {
7676
}
7777

7878
private static Response ok(String body) {
79-
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(),
80-
body, /* stream */ null);
79+
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
8180
}
8281
}

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenJsonExtractorTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
4848
}
4949

5050
private static Response ok(String body) {
51-
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(),
52-
body, /* stream */ null);
51+
return new Response(200, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
5352
}
5453

5554
private static Response error(String body) {
56-
return new Response(400, /* message */ null, /* headers */ Collections.<String, String>emptyMap(),
57-
body, /* stream */ null);
55+
return new Response(400, /* message */ null, /* headers */ Collections.<String, String>emptyMap(), body);
5856
}
5957
}

scribejava-core/src/test/java/com/github/scribejava/core/model/ConnectionStub.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
import java.net.HttpURLConnection;
99
import java.net.MalformedURLException;
1010
import java.net.URL;
11-
import java.util.Arrays;
11+
import java.util.Collections;
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
1515

1616
public class ConnectionStub extends HttpURLConnection {
1717

1818
private final Map<String, String> headers = new HashMap<>();
19-
private final Map<String, List<String>> responseHeaders = new HashMap<>();
20-
private int inputStreamCalled;
2119

2220
public ConnectionStub() throws MalformedURLException {
2321
super(new URL("http://example.com"));
@@ -44,14 +42,9 @@ public int getResponseCode() throws IOException {
4442

4543
@Override
4644
public InputStream getInputStream() throws IOException {
47-
inputStreamCalled++;
4845
return new ByteArrayInputStream("contents".getBytes());
4946
}
5047

51-
public int getTimesCalledInpuStream() {
52-
return inputStreamCalled;
53-
}
54-
5548
@Override
5649
public OutputStream getOutputStream() throws IOException {
5750
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -61,11 +54,7 @@ public OutputStream getOutputStream() throws IOException {
6154

6255
@Override
6356
public Map<String, List<String>> getHeaderFields() {
64-
return responseHeaders;
65-
}
66-
67-
public void addResponseHeader(String key, String value) {
68-
responseHeaders.put(key, Arrays.asList(value));
57+
return Collections.emptyMap();
6958
}
7059

7160
@Override

scribejava-core/src/test/java/com/github/scribejava/core/model/ResponseTest.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

scribejava-httpclient-ahc/src/main/java/com/github/scribejava/httpclient/ahc/OAuthAsyncCompletionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public T onCompleted(org.asynchttpclient.Response ahcResponse) throws IOExceptio
2828
headersMap.put(header.getKey(), header.getValue());
2929
}
3030
final Response response = new Response(ahcResponse.getStatusCode(), ahcResponse.getStatusText(), headersMap,
31-
ahcResponse.getResponseBody(), ahcResponse.getResponseBodyAsStream());
31+
ahcResponse.getResponseBodyAsStream());
3232

3333
@SuppressWarnings("unchecked")
3434
final T t = converter == null ? (T) response : converter.convert(response);

scribejava-httpclient-ning/src/main/java/com/github/scribejava/httpclient/ning/OAuthAsyncCompletionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public T onCompleted(com.ning.http.client.Response ningResponse) throws IOExcept
3333
headersMap.put(header.getKey(), value.toString());
3434
}
3535
final Response response = new Response(ningResponse.getStatusCode(), ningResponse.getStatusText(), headersMap,
36-
ningResponse.getResponseBody(), ningResponse.getResponseBodyAsStream());
36+
ningResponse.getResponseBodyAsStream());
3737

3838
@SuppressWarnings("unchecked")
3939
final T t = converter == null ? (T) response : converter.convert(response);

scribejava-httpclient-okhttp/src/main/java/com/github/scribejava/httpclient/okhttp/OAuthAsyncCompletionHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ public void onResponse(Call call, okhttp3.Response okHttpResponse) throws IOExce
4545
headersMap.put(name, headers.get(name));
4646
}
4747

48-
final Response response = new Response(okHttpResponse.code(),
49-
okHttpResponse.message(),
50-
headersMap,
51-
null, // cannot return both body String and InputStream
52-
okHttpResponse.body().byteStream());
48+
final Response response = new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap,
49+
okHttpResponse.body().byteStream());
5350

5451
@SuppressWarnings("unchecked")
5552
final T t = converter == null ? (T) response : converter.convert(response);

0 commit comments

Comments
 (0)