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