Created
April 23, 2015 07:52
-
-
Save kaiinui/c7d0dea6ec8687e757ab to your computer and use it in GitHub Desktop.
AppEngine+Retrofit+OAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
import oauth.signpost.http.HttpRequest; | |
import retrofit.client.Header; | |
import retrofit.client.Request; | |
public class HttpRequestAdapter implements HttpRequest { | |
private static final String DEFAULT_CONTENT_TYPE = "application/json"; | |
private Request request; | |
private String contentType; | |
public HttpRequestAdapter(Request request) { | |
this(request, request.getBody() != null ? request.getBody().mimeType() : DEFAULT_CONTENT_TYPE); | |
} | |
public HttpRequestAdapter(Request request, String contentType) { | |
this.request = request; | |
this.contentType = contentType; | |
} | |
@Override | |
public Map<String, String> getAllHeaders() { | |
HashMap<String, String> headers = new HashMap<String, String>(); | |
for(Header header : request.getHeaders()) { | |
headers.put(header.getName(), header.getValue()); | |
} | |
return headers; | |
} | |
@Override | |
public String getContentType() { | |
return contentType; | |
} | |
@Override | |
public String getHeader(String key) { | |
for(Header header : request.getHeaders()) { | |
if(key.equals(header.getName())) { | |
return header.getValue(); | |
} | |
} | |
return null; | |
} | |
@Override | |
public InputStream getMessagePayload() throws IOException { | |
final String contentType = getContentType(); | |
if (null != contentType && contentType.startsWith("application/x-www-form-urlencoded")) { | |
long contentLength = request.getBody().length(); | |
ByteArrayOutputStream output = new ByteArrayOutputStream(Long.valueOf(contentLength) | |
.intValue()); | |
request.getBody().writeTo(output); | |
return new ByteArrayInputStream(output.toByteArray()); | |
} | |
throw new UnsupportedOperationException("The content type" + (contentType != null ? " " + | |
contentType : "") + " is not supported."); | |
} | |
@Override | |
public String getMethod() { | |
return request.getMethod(); | |
} | |
@Override | |
public String getRequestUrl() { | |
return request.getUrl(); | |
} | |
@Override | |
public void setHeader(String key, String value) { | |
ArrayList<Header> headers = new ArrayList<Header>(); | |
headers.addAll(request.getHeaders()); | |
headers.add(new Header(key, value)); | |
Request copy = new Request(request.getMethod(), request.getUrl(), headers, request.getBody()); | |
request = copy; | |
} | |
@Override | |
public void setRequestUrl(String url) { | |
Request copy = new Request(request.getMethod(), url, request.getHeaders(), request.getBody()); | |
request = copy; | |
} | |
@Override | |
public Object unwrap() { | |
return request; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import oauth.signpost.AbstractOAuthConsumer; | |
import oauth.signpost.http.HttpRequest; | |
import retrofit.client.Request; | |
public class RetrofitHttpOAuthConsumer extends AbstractOAuthConsumer { | |
public RetrofitHttpOAuthConsumer(String consumerKey, String consumerSecret) { | |
super(consumerKey, consumerSecret); | |
} | |
@Override | |
protected HttpRequest wrap(Object request) { | |
if (!(request instanceof retrofit.client.Request)) { | |
throw new IllegalArgumentException("This consumer expects requests of type " + retrofit.client.Request.class.getCanonicalName()); | |
} | |
return new HttpRequestAdapter((Request) request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import oauth.signpost.exception.OAuthCommunicationException; | |
import oauth.signpost.exception.OAuthExpectationFailedException; | |
import oauth.signpost.exception.OAuthMessageSignerException; | |
import retrofit.appengine.UrlFetchClient; | |
import retrofit.client.Request; | |
import retrofit.client.Response; | |
import java.io.IOException; | |
public class SigningUrlFetchClient extends UrlFetchClient { | |
private final RetrofitHttpOAuthConsumer oAuthConsumer; | |
public SigningUrlFetchClient(RetrofitHttpOAuthConsumer consumer) { | |
oAuthConsumer = consumer; | |
} | |
@Override | |
public Response execute(Request request) throws IOException { | |
Request requestToSend = request; | |
try { | |
HttpRequestAdapter signedAdapter = (HttpRequestAdapter) oAuthConsumer.sign(request); | |
requestToSend = (Request) signedAdapter.unwrap(); | |
} catch (OAuthMessageSignerException e) { | |
e.printStackTrace(); | |
} catch (OAuthExpectationFailedException e) { | |
e.printStackTrace(); | |
} catch (OAuthCommunicationException e) { | |
e.printStackTrace(); | |
} | |
return super.execute(requestToSend); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment