Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Fetching an access token should be a seperate method
Browse files Browse the repository at this point in the history
  • Loading branch information
lholmquist committed Nov 26, 2013
1 parent fdb51c4 commit 085b9df
Showing 1 changed file with 56 additions and 44 deletions.
Expand Up @@ -60,50 +60,7 @@ public void sendMessage( ChromePackagedAppVariant chromePackagedAppVariant, List
return;
}

HttpURLConnection accessTokenConn = null;
JSONParser jsonParser = new JSONParser();
ChromePackagedAppTokenCache accessTokenObject = null;
String accessToken = null;
long expireTime = 0; // in milliseconds

try {
// Not good practice to always get a new access token. so only get one if it is expired or null
accessTokenObject = accessTokenMap.get(chromePackagedAppVariant.getClientId());

if( accessTokenObject != null ) {
accessToken = accessTokenObject.getAccessToken();
expireTime = accessTokenObject.getExpiresIn();
}

if( accessToken == null || expireTime < new Date().getTime() ) {
accessTokenConn = refreshAccessToken(chromePackagedAppVariant);
String stringResponse = getString(accessTokenConn.getInputStream());
accessToken = ((JSONObject)jsonParser.parse(stringResponse)).get("access_token").toString();
String expiresIn = ((JSONObject)jsonParser.parse(stringResponse)).get("expires_in").toString();

// Convert to millis
long ex = Long.parseLong( expiresIn );
expireTime = new Date().getTime() + (ex * 1000);

if( accessTokenObject == null ) {
accessTokenObject = new ChromePackagedAppTokenCache();
}

accessTokenObject.setAccessToken(accessToken);
accessTokenObject.setExpiresIn(expireTime);

accessTokenMap.put(chromePackagedAppVariant.getClientId(),accessTokenObject);
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Error during Post execution to GCM for Chrome Network For access token refresh", e);
} catch (ParseException e) {
logger.log(Level.SEVERE, "Error during Parse of Response ", e);
} finally {
// tear down
if (accessTokenConn != null ) {
accessTokenConn.disconnect();
}
}
String accessToken = fetchAccessToken(chromePackagedAppVariant);

// iterate over all the given channelIDs
for (String channelID : channelIDs) {
Expand Down Expand Up @@ -186,6 +143,61 @@ protected HttpURLConnection refreshAccessToken( ChromePackagedAppVariant chromeP
return conn;
}

/**
*
* @param chromePackagedAppVariant
* @return a valid access token
*/

protected String fetchAccessToken(ChromePackagedAppVariant chromePackagedAppVariant) {
HttpURLConnection accessTokenConn = null;
JSONParser jsonParser = new JSONParser();
ChromePackagedAppTokenCache accessTokenObject = null;
String accessToken = null;
long expireTime = 0; // in milliseconds

try {
// Not good practice to always get a new access token. so only get one if it is expired or null
accessTokenObject = accessTokenMap.get(chromePackagedAppVariant.getClientId());

if( accessTokenObject != null ) {
accessToken = accessTokenObject.getAccessToken();
expireTime = accessTokenObject.getExpiresIn();
}

if( accessToken == null || expireTime < new Date().getTime() ) {
accessTokenConn = refreshAccessToken(chromePackagedAppVariant);
String stringResponse = getString(accessTokenConn.getInputStream());
accessToken = ((JSONObject)jsonParser.parse(stringResponse)).get("access_token").toString();
String expiresIn = ((JSONObject)jsonParser.parse(stringResponse)).get("expires_in").toString();

// Convert to millis
long ex = Long.parseLong( expiresIn );
expireTime = new Date().getTime() + (ex * 1000);

if( accessTokenObject == null ) {
accessTokenObject = new ChromePackagedAppTokenCache();
}

accessTokenObject.setAccessToken(accessToken);
accessTokenObject.setExpiresIn(expireTime);

accessTokenMap.put(chromePackagedAppVariant.getClientId(),accessTokenObject);
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Error during Post execution to GCM for Chrome Network For access token refresh", e);
} catch (ParseException e) {
logger.log(Level.SEVERE, "Error during Parse of Response ", e);
} finally {
// tear down
if (accessTokenConn != null ) {
accessTokenConn.disconnect();
}
}

return accessToken;
}

/**
* Convenience method to open/establish a HttpURLConnection.
*/
Expand Down

0 comments on commit 085b9df

Please sign in to comment.