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