1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.oauth;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import oauth.signpost.AbstractOAuthConsumer;
|
---|
10 | import oauth.signpost.AbstractOAuthProvider;
|
---|
11 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Adapters to make {@link oauth.signpost} work with {@link HttpClient}.
|
---|
15 | */
|
---|
16 | public final class SignpostAdapters {
|
---|
17 |
|
---|
18 | private SignpostAdapters() {
|
---|
19 | }
|
---|
20 |
|
---|
21 | public static class OAuthProvider extends AbstractOAuthProvider {
|
---|
22 |
|
---|
23 | public OAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, String authorizationWebsiteUrl) {
|
---|
24 | super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | protected HttpRequest createRequest(String endpointUrl) throws Exception {
|
---|
29 | return new HttpRequest(HttpClient.create(new URL(endpointUrl), "GET"));
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | protected HttpResponse sendRequest(oauth.signpost.http.HttpRequest request) throws Exception {
|
---|
34 | return new HttpResponse(((HttpRequest) request).request.connect());
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | protected void closeConnection(oauth.signpost.http.HttpRequest request, oauth.signpost.http.HttpResponse response) throws Exception {
|
---|
39 | if (response != null) {
|
---|
40 | ((HttpResponse) response).response.disconnect();
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static class OAuthConsumer extends AbstractOAuthConsumer {
|
---|
46 |
|
---|
47 | public OAuthConsumer(String consumerKey, String consumerSecret) {
|
---|
48 | super(consumerKey, consumerSecret);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected HttpRequest wrap(Object request) {
|
---|
53 | return new HttpRequest(((HttpClient) request));
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static class HttpRequest implements oauth.signpost.http.HttpRequest {
|
---|
58 | private final HttpClient request;
|
---|
59 |
|
---|
60 | HttpRequest(HttpClient request) {
|
---|
61 | this.request = request;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void setHeader(String name, String value) {
|
---|
66 | request.setHeader(name, value);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String getMethod() {
|
---|
71 | return request.getRequestMethod();
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public String getRequestUrl() {
|
---|
76 | return request.getURL().toExternalForm();
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String getContentType() {
|
---|
81 | return request.getRequestHeader("Content-Type");
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public String getHeader(String name) {
|
---|
86 | return request.getRequestHeader(name);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public InputStream getMessagePayload() throws IOException {
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public void setRequestUrl(String url) {
|
---|
96 | throw new IllegalStateException();
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public Map<String, String> getAllHeaders() {
|
---|
101 | throw new IllegalStateException();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public Object unwrap() {
|
---|
106 | throw new IllegalStateException();
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private static class HttpResponse implements oauth.signpost.http.HttpResponse {
|
---|
111 | private final HttpClient.Response response;
|
---|
112 |
|
---|
113 | HttpResponse(HttpClient.Response response) {
|
---|
114 | this.response = response;
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public int getStatusCode() throws IOException {
|
---|
119 | return response.getResponseCode();
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public String getReasonPhrase() throws Exception {
|
---|
124 | return response.getResponseMessage();
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public InputStream getContent() throws IOException {
|
---|
129 | return response.getContent();
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public Object unwrap() {
|
---|
134 | throw new IllegalStateException();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | }
|
---|