|
| 1 | +package org.alfresco.integrations.salesforce.chatter; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Date; |
| 5 | + |
| 6 | +import org.alfresco.service.cmr.oauth2.OAuth2CredentialsStoreService; |
| 7 | +import org.alfresco.service.cmr.remoteticket.NoSuchSystemException; |
| 8 | +import org.apache.commons.logging.Log; |
| 9 | +import org.apache.commons.logging.LogFactory; |
| 10 | +import org.json.JSONException; |
| 11 | +import org.json.JSONObject; |
| 12 | +import org.json.JSONTokener; |
| 13 | +import org.springframework.extensions.webscripts.AbstractWebScript; |
| 14 | +import org.springframework.extensions.webscripts.WebScriptException; |
| 15 | +import org.springframework.extensions.webscripts.WebScriptRequest; |
| 16 | +import org.springframework.extensions.webscripts.WebScriptResponse; |
| 17 | + |
| 18 | +/** |
| 19 | + * Save an OAuth 2.0 ticket into the credentials store. |
| 20 | + * |
| 21 | + * @author Will Abson |
| 22 | + */ |
| 23 | +public class SaveOAuthToken extends AbstractWebScript |
| 24 | +{ |
| 25 | + |
| 26 | + private static Log logger = LogFactory.getLog(SaveOAuthToken.class); |
| 27 | + |
| 28 | + // Services |
| 29 | + private OAuth2CredentialsStoreService oauth2CredentialsStoreService; |
| 30 | + |
| 31 | + public void setOauth2CredentialsStoreService(OAuth2CredentialsStoreService oauth2CredentialsStoreService) |
| 32 | + { |
| 33 | + this.oauth2CredentialsStoreService = oauth2CredentialsStoreService; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void execute(WebScriptRequest req, WebScriptResponse arg1) |
| 38 | + throws IOException |
| 39 | + { |
| 40 | + String jsonStr = req.getContent().getContent(); |
| 41 | + if (logger.isDebugEnabled()) |
| 42 | + { |
| 43 | + logger.debug("Got JSON data: " + jsonStr); |
| 44 | + } |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + JSONObject reqJson = new JSONObject(new JSONTokener(jsonStr)); |
| 49 | + |
| 50 | + String remoteSystem = reqJson.getString("name"), |
| 51 | + accessToken = reqJson.getString("accessToken"), |
| 52 | + refreshToken = reqJson.getString("refreshToken"); |
| 53 | + |
| 54 | + if (logger.isDebugEnabled()) |
| 55 | + { |
| 56 | + logger.debug("Name: " + remoteSystem); |
| 57 | + logger.debug("Access token: " + accessToken); |
| 58 | + logger.debug("Refresh token: " + refreshToken); |
| 59 | + } |
| 60 | + |
| 61 | + Date expiresIn = null; // TODO need to pick an arbitrary date? |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + oauth2CredentialsStoreService.storePersonalOAuth2Credentials(remoteSystem, accessToken, refreshToken, expiresIn, new Date()); |
| 66 | + } |
| 67 | + catch (NoSuchSystemException nsse) |
| 68 | + { |
| 69 | + throw nsse; |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + catch (JSONException e) |
| 74 | + { |
| 75 | + throw new WebScriptException("A problem occurred parsing the request JSON", e); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments