Commit 87fea8d2 authored by Matija Obreza's avatar Matija Obreza

Handle HTTP redirects as errors, throw HttpRedirectException

parent 1b702534
package org.genesys2.client.oauth;
public class HttpRedirectException extends GenesysApiException {
private static final long serialVersionUID = 1L;
private String redirectUrl;
public HttpRedirectException(String redirectUrl) {
this.redirectUrl = redirectUrl;
}
public String getRedirectUrl() {
return redirectUrl;
}
}
......@@ -20,7 +20,7 @@ package org.genesys2.client.oauth;
* API exception indicating that the operation failed due to concurrency issues
* and should be retried by the client.
*/
public class PleaseRetryException extends RuntimeException {
public class PleaseRetryException extends GenesysApiException {
/**
* Instantiates a new please retry exception.
......
......@@ -19,6 +19,10 @@ package org.genesys2.client.oauth.api;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpHeaders;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.genesys2.client.oauth.GenesysApiException;
import org.scribe.builder.api.DefaultApi20;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.extractors.JsonTokenExtractor;
......@@ -33,25 +37,26 @@ import org.scribe.utils.OAuthEncoder;
* Genesys API v1.
*/
public class GenesysApi extends DefaultApi20 {
private static final Logger _log = LogManager.getLogger(GenesysApi.class);
/** OAuth2 authorization endpoint that is appended to {@link #baseUrl} */
public static final String AUTHORIZE_URL = "/oauth/authorize?client_id=%s&client_secret=%s&response_type=code&redirect_uri=%s";
/** OAuth2 access token endpoint that is appended to {@link #baseUrl} */
private static final String TOKEN_ENDPOINT = "/oauth/token";
/** The base URL of the Genesys server (e.g. https://www.genesys-pgr.org) */
private String baseUrl;
/** The complete authorization url */
private String authorizeUrl;
/** The scoped authorize url. */
private String scopedAuthorizeUrl;
/** The access token endpoint. */
private String accessTokenEndpoint;
/** The refresh token endpoint. */
private String refreshTokenEndpoint;
......@@ -68,7 +73,9 @@ public class GenesysApi extends DefaultApi20 {
this.accessTokenEndpoint = this.baseUrl + TOKEN_ENDPOINT + "?grant_type=authorization_code";
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.scribe.builder.api.DefaultApi20#getAccessTokenEndpoint()
*/
@Override
......@@ -85,17 +92,25 @@ public class GenesysApi extends DefaultApi20 {
return this.refreshTokenEndpoint;
}
/* (non-Javadoc)
* @see org.scribe.builder.api.DefaultApi20#getAuthorizationUrl(org.scribe.model.OAuthConfig)
/*
* (non-Javadoc)
*
* @see
* org.scribe.builder.api.DefaultApi20#getAuthorizationUrl(org.scribe.model
* .OAuthConfig)
*/
@Override
public String getAuthorizationUrl(OAuthConfig config) {
return config.hasScope() ? String.format(this.scopedAuthorizeUrl, config.getApiKey(), config.getApiSecret(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()))
: String.format(this.authorizeUrl, config.getApiKey(), config.getApiSecret(), OAuthEncoder.encode(config.getCallback()));
return config.hasScope() ? String.format(this.scopedAuthorizeUrl, config.getApiKey(), config.getApiSecret(),
OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope())) : String
.format(this.authorizeUrl, config.getApiKey(), config.getApiSecret(),
OAuthEncoder.encode(config.getCallback()));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.scribe.builder.api.DefaultApi20#getAccessTokenExtractor()
*/
@Override
......@@ -124,22 +139,30 @@ public class GenesysApi extends DefaultApi20 {
* http://stackoverflow.com/questions/20044222/spring-security-oauth-2-
* implicit-grant-no-support-for-refresh-token
*
* /oauth/token?client_id=MyClient&grant_type=refresh_token&client_secret=
* /oauth/token?client_id=MyClient&grant_type=refresh_token&
* client_secret=
* mysecret&refresh_token=19698a4a-960a-4d24-a8cc-44d4b71df47b
*
* @param apiKey the api key
* @param apiSecret the api secret
* @param refreshToken the refresh token
* @return the access token
* @throws GenesysApiException
*/
public Token getAccessToken(String apiKey, String apiSecret, Token refreshToken) {
public Token getAccessToken(String apiKey, String apiSecret, Token refreshToken) throws GenesysApiException {
OAuthRequest request = new OAuthRequest(getAccessTokenVerb(), this.refreshTokenEndpoint);
request.addQuerystringParameter(OAuthConstants.CLIENT_ID, apiKey);
request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, apiSecret);
request.addQuerystringParameter("grant_type", "refresh_token");
request.addQuerystringParameter("refresh_token", refreshToken.getToken());
Response response = request.send();
return getAccessTokenExtractor().extract(response.getBody());
_log.debug("HTTP status code " + response.getCode());
_log.debug("Redirect: " + response.getHeader(HttpHeaders.LOCATION));
if (response.getCode() >= 200 && response.getCode() < 300) {
return getAccessTokenExtractor().extract(response.getBody());
} else {
throw new GenesysApiException("Server responded with unexpected HTTP response code " + response.getCode());
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment