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