Skip to content

Instantly share code, notes, and snippets.

@marianogonzalez
Created October 3, 2012 19:11
Show Gist options
  • Save marianogonzalez/3829112 to your computer and use it in GitHub Desktop.
Save marianogonzalez/3829112 to your computer and use it in GitHub Desktop.
public class SharedOAuthTokenTransformer extends AbstractMessageTransformer {
private ObjectStore<Serializable> objectStore;
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
String accessTokenId = message.getInvocationProperty("OAuthAccessTokenId");
if (StringUtils.isBlank(accessTokenId)) {
throw new IllegalStateException("OAuthAccessTokenId is blank or null");
}
GoogleCalendarConnectorOAuthState state = null;
try {
state = (GoogleCalendarConnectorOAuthState) this.objectStore.retrieve(accessTokenId);
} catch (ObjectStoreException e) {
throw new RuntimeException(String.format("exception reading OAuth object store with key %s", accessTokenId), e);
}
if (state == null) {
throw new IllegalStateException("Got null oauth state for key " + accessTokenId);
}
GoogleTasksConnectorOAuthState taskState = new GoogleTasksConnectorOAuthState();
taskState.setAccessToken(state.getAccessToken());
taskState.setAccessTokenUrl(state.getAccessTokenUrl());
taskState.setAuthorizationUrl(state.getAccessTokenUrl());
taskState.setRefreshToken(state.getRefreshToken());
try {
this.objectStore.store(accessTokenId + "Task", taskState);
} catch (ObjectStoreException e) {
throw new RuntimeException(String.format("exception writing task OAuth state with key %s", accessTokenId), e);
}
return message;
}
public void setObjectStore(ObjectStore<Serializable> objectStore) {
this.objectStore = objectStore;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment