Created
August 11, 2014 07:19
-
-
Save dmitrygusev/388040ea136338bc8cea to your computer and use it in GitHub Desktop.
Using Scribe's custom RequestFactory and ConnectionFactory
This file contains hidden or 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.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import javax.net.ssl.HttpsURLConnection; | |
import org.scribe.builder.api.DefaultApi10a; | |
import org.scribe.model.OAuthConfig; | |
import org.scribe.model.OAuthConstants; | |
import org.scribe.model.OAuthRequest; | |
import org.scribe.model.Token; | |
import org.scribe.model.Verb; | |
import org.scribe.model.Verifier; | |
import org.scribe.oauth.OAuth10aServiceImpl; | |
import org.scribe.services.DefaultConnectionFactory; | |
import org.scribe.services.RequestFactory; | |
public class XeroOAuthService extends OAuth10aServiceImpl implements TokenRefresher | |
{ | |
public static class Context | |
{ | |
public XeroToken oldToken; | |
public boolean forRefresh; | |
} | |
private final XeroOAuthService.Context context; | |
public XeroOAuthService(final DefaultApi10a api, OAuthConfig config, final XeroOAuthService.Context context) | |
{ | |
super(api, config, new RequestFactory() | |
{ | |
@Override | |
public OAuthRequest createRequest(Verb verb, String url) | |
{ | |
OAuthRequest request = new OAuthRequest(verb, url, new DefaultConnectionFactory() | |
{ | |
@Override | |
public HttpURLConnection createConnection(String url) throws MalformedURLException, IOException | |
{ | |
HttpsURLConnection connection = (HttpsURLConnection) super.createConnection(url); | |
XeroPartnerApi partnerApi = (XeroPartnerApi) api; | |
connection.setSSLSocketFactory(partnerApi.getSslContext().getSocketFactory()); | |
return connection; | |
} | |
}); | |
if (context.forRefresh) | |
{ | |
request.addOAuthParameter( | |
OAuthConstants.SESSION_HANDLE, | |
context.oldToken.getOauthSessionHandle()); | |
} | |
return request; | |
} | |
}); | |
this.context = context; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public <T extends Jsonable> T refreshToken(T token) | |
{ | |
XeroToken xeroToken = (XeroToken) token; | |
try | |
{ | |
context.forRefresh = true; | |
context.oldToken = xeroToken; | |
// We renew token by getting new access token and passing oauth_session_handle | |
// http://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html#rfc.section.5.3 | |
Token newToken = getAccessToken( | |
new Token(xeroToken.getOauthToken(), xeroToken.getOauthTokenSecret()), | |
new Verifier("ignored")); | |
return (T) XeroToken.fromRawResponse(newToken.getRawResponse()); | |
} | |
finally | |
{ | |
context.forRefresh = false; | |
context.oldToken = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment