| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.odesk; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.http.Request; |
| 34 | |
import com.jcabi.http.Response; |
| 35 | |
import com.jcabi.http.Wire; |
| 36 | |
import com.jcabi.immutable.Array; |
| 37 | |
import com.jcabi.log.Logger; |
| 38 | |
import java.io.IOException; |
| 39 | |
import java.io.InputStream; |
| 40 | |
import java.net.URLDecoder; |
| 41 | |
import java.util.AbstractMap; |
| 42 | |
import java.util.Collection; |
| 43 | |
import java.util.Map; |
| 44 | |
import java.util.logging.Level; |
| 45 | |
import javax.ws.rs.core.HttpHeaders; |
| 46 | |
import lombok.EqualsAndHashCode; |
| 47 | |
import lombok.ToString; |
| 48 | |
import org.scribe.builder.ServiceBuilder; |
| 49 | |
import org.scribe.builder.api.DefaultApi10a; |
| 50 | |
import org.scribe.model.OAuthRequest; |
| 51 | |
import org.scribe.model.Token; |
| 52 | |
import org.scribe.model.Verb; |
| 53 | |
import org.scribe.oauth.OAuthService; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
@Immutable |
| 63 | 0 | @ToString |
| 64 | 0 | @EqualsAndHashCode(of = { "origin", "key", "secret", "token", "tsecret" }) |
| 65 | |
public final class OAuthWire implements Wire { |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private final transient Wire origin; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private final transient String key; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
private final transient String secret; |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private final transient String token; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
private final transient String tsecret; |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public OAuthWire(final Wire wire, final String akey, final String scrt, |
| 102 | 0 | final String tkn, final String tscrt) { |
| 103 | 0 | this.origin = wire; |
| 104 | 0 | this.key = akey; |
| 105 | 0 | this.secret = scrt; |
| 106 | 0 | this.token = tkn; |
| 107 | 0 | this.tsecret = tscrt; |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
@Override |
| 112 | |
public Response send(final Request req, final String home, |
| 113 | |
final String method, |
| 114 | |
final Collection<Map.Entry<String, String>> headers, |
| 115 | |
final InputStream content, final int connect, final int read) |
| 116 | |
throws IOException { |
| 117 | 0 | final OAuthService service = new ServiceBuilder() |
| 118 | |
.provider(OAuthWire.OdeskApi.class) |
| 119 | |
.apiKey(this.key) |
| 120 | |
.apiSecret(this.secret) |
| 121 | |
.debugStream(Logger.stream(Level.FINE, this)) |
| 122 | |
.build(); |
| 123 | 0 | final String[] parts = home.split("\\?", 2); |
| 124 | 0 | final OAuthRequest oauth = new OAuthRequest( |
| 125 | |
Verb.valueOf(method), parts[0] |
| 126 | |
); |
| 127 | 0 | if (parts.length == 2) { |
| 128 | 0 | for (final String pair : parts[1].split("&")) { |
| 129 | 0 | final String[] eqn = pair.split("=", 2); |
| 130 | |
final String value; |
| 131 | 0 | if (eqn.length == 2) { |
| 132 | 0 | value = URLDecoder.decode(eqn[1], "UTF-8"); |
| 133 | |
} else { |
| 134 | 0 | value = ""; |
| 135 | |
} |
| 136 | 0 | oauth.addQuerystringParameter(eqn[0], value); |
| 137 | |
} |
| 138 | |
} |
| 139 | 0 | service.signRequest(new Token(this.token, this.tsecret), oauth); |
| 140 | 0 | return this.origin.send( |
| 141 | |
req, oauth.getCompleteUrl(), method, |
| 142 | |
new Array<Map.Entry<String, String>>(headers).with( |
| 143 | |
new AbstractMap.SimpleEntry<String, String>( |
| 144 | |
HttpHeaders.AUTHORIZATION, |
| 145 | |
oauth.getHeaders().get(HttpHeaders.AUTHORIZATION) |
| 146 | |
) |
| 147 | |
), |
| 148 | |
content, |
| 149 | |
connect, read |
| 150 | |
); |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | 0 | public static final class OdeskApi extends DefaultApi10a { |
| 158 | |
@Override |
| 159 | |
public String getAccessTokenEndpoint() { |
| 160 | 0 | return "https://www.odesk.com/api/auth/v1/oauth/token/access"; |
| 161 | |
} |
| 162 | |
@Override |
| 163 | |
public String getAuthorizationUrl(final Token tkn) { |
| 164 | 0 | return String.format( |
| 165 | |
"https://www.odesk.com/services/api/auth?oauth_token=%s", |
| 166 | |
tkn.getToken() |
| 167 | |
); |
| 168 | |
} |
| 169 | |
@Override |
| 170 | |
public String getRequestTokenEndpoint() { |
| 171 | 0 | return "https://www.odesk.com/api/auth/v1/oauth/token/request"; |
| 172 | |
} |
| 173 | |
} |
| 174 | |
|
| 175 | |
} |