Last active
November 11, 2015 23:55
-
-
Save rcgroot/64401f2608098a0bfd4e to your computer and use it in GitHub Desktop.
OAuth 1.0a with Signport on Volley including support for multiple operations and volleys multithreading
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
/* | |
** ------------------------------------------------------------------------------ | |
** Licensed under the Apache License, Version 2.0 (the "License"); | |
** you may not use this file except in compliance with the License. | |
** You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | |
** ------------------------------------------------------------------------------ | |
*/ | |
package nl.sogeti.android.deviceprofiler.network; | |
import android.support.v4.util.LongSparseArray; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.Request; | |
import com.android.volley.toolbox.HurlStack; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.methods.HttpDelete; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.methods.HttpPut; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Map; | |
import oauth.signpost.OAuthConsumer; | |
import oauth.signpost.exception.OAuthException; | |
/** | |
* @author (c) 2015, René de Groot available under Apache License Version 2.0 | |
*/ | |
public class OAuthStack extends HurlStack | |
{ | |
private final OAuthConsumer consumer; | |
private LongSparseArray<String> methods = new LongSparseArray<>(); | |
public OAuthStack(OAuthConsumer consumer) | |
{ | |
this.consumer = consumer; | |
} | |
@Override | |
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) | |
throws IOException, AuthFailureError | |
{ | |
String method; | |
switch (request.getMethod()) | |
{ | |
case Request.Method.DEPRECATED_GET_OR_POST: | |
case Request.Method.POST: | |
method = HttpPost.METHOD_NAME; | |
break; | |
case Request.Method.PUT: | |
method = HttpPut.METHOD_NAME; | |
break; | |
case Request.Method.DELETE: | |
method = HttpDelete.METHOD_NAME; | |
break; | |
case Request.Method.GET: | |
method = HttpGet.METHOD_NAME; | |
break; | |
default: | |
throw new IllegalStateException("Unknown method type."); | |
} | |
methods.put(Thread.currentThread().getId(), method); | |
return super.performRequest(request, additionalHeaders); | |
} | |
@Override | |
protected HttpURLConnection createConnection(URL url) throws IOException | |
{ | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
try | |
{ | |
final long threadId = Thread.currentThread().getId(); | |
String method = methods.get(threadId); | |
if (method != null) | |
{ | |
connection.setRequestMethod(method); | |
// Could be faster by have a single consumer instance per thread and no synchronized | |
synchronized (consumer) | |
{ | |
consumer.sign(connection); | |
} | |
methods.remove(threadId); | |
} | |
} | |
catch (OAuthException e) | |
{ | |
throw new IOException("Failed to OAuth sign this Volley connection", e); | |
} | |
return connection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment