added persistence for Igtimi access tokens

This commit is contained in:
Axel Uhl committed 2013-11-29 15:05:05 +01:00
1 parent ed35db38b4
commit ff8254c68b
19 files changed
+230 -27

No files matched your search

@@ -6,10 +6,13 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.xml.sax.SAXException;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.igtimiadapter.datatypes.Type;
@@ -84,4 +87,16 @@ public interface IgtimiConnectionFactory {
* IDs of the transmitting units expected to be visible to the account's {@link Account#getUser() user's}
*/
JSONObject getWebSocketConfigurationMessage(Account account, Iterable<String> deviceIds);
Iterable<Account> getAllAccounts();
/**
* Tries to authorize our client on behalf of a user identified by e-mail and password.
*
* @return the authorization code which can then be used to obtain a permanent access token to be used by our client
* to access data owned by the user identified by e-mail and password.
*/
String authorizeAndReturnAuthorizedCode(String userEmail, String userPassword) throws ClientProtocolException,
IOException, IllegalStateException, ParserConfigurationException, SAXException, ClassNotFoundException,
InstantiationException, IllegalAccessException, ClassCastException;
}
@@ -1,10 +1,17 @@
package com.sap.sailing.domain.igtimiadapter.impl;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.parser.ParseException;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.sap.sailing.domain.igtimiadapter.Client;
import com.sap.sailing.domain.igtimiadapter.IgtimiConnectionFactory;
import com.sap.sailing.domain.igtimiadapter.persistence.DomainObjectFactory;
import com.sap.sailing.domain.igtimiadapter.persistence.MongoObjectFactory;
import com.sap.sailing.domain.igtimiadapter.persistence.PersistenceFactory;
/**
* Maintains data about a default {@link Client} that represents this application when interacting with the Igtimi
@@ -23,9 +30,11 @@ public class Activator implements BundleActivator {
private static final String CLIENT_SECRET = "537dbd14a84fcb470c91d85e8c4f8f7a356ac5ffc8727594d1bfe900ee5942ef";
private IgtimiConnectionFactory connectionFactory;
public Activator() {
public Activator() throws ClientProtocolException, IllegalStateException, IOException, ParseException {
Client client = new ClientImpl(CLIENT_ID, CLIENT_SECRET, "http://sapsailing.com");
connectionFactory = new IgtimiConnectionFactoryImpl(client);
DomainObjectFactory domainObjectFactory = PersistenceFactory.INSTANCE.getDefaultDomainObjectFactory();
MongoObjectFactory mongoObjectFactory = PersistenceFactory.INSTANCE.getDefaultMongoObjectFactory();
connectionFactory = new IgtimiConnectionFactoryImpl(client, domainObjectFactory, mongoObjectFactory);
}
@Override
@@ -34,7 +43,7 @@ public class Activator implements BundleActivator {
context.registerService(IgtimiConnectionFactory.class, connectionFactory, /* properties */ null);
}
public static Activator getInstance() {
public static Activator getInstance() throws ClientProtocolException, IllegalStateException, IOException, ParseException {
if (INSTANCE == null) {
INSTANCE = new Activator(); // probably non-OSGi case, as in test execution
}
@@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
@@ -47,6 +48,8 @@ import com.sap.sailing.domain.igtimiadapter.IgtimiConnection;
import com.sap.sailing.domain.igtimiadapter.IgtimiConnectionFactory;
import com.sap.sailing.domain.igtimiadapter.Permission;
import com.sap.sailing.domain.igtimiadapter.datatypes.Type;
import com.sap.sailing.domain.igtimiadapter.persistence.DomainObjectFactory;
import com.sap.sailing.domain.igtimiadapter.persistence.MongoObjectFactory;
public class IgtimiConnectionFactoryImpl implements IgtimiConnectionFactory {
private static final Logger logger = Logger.getLogger(IgtimiConnectionFactoryImpl.class.getName());
@@ -54,11 +57,21 @@ public class IgtimiConnectionFactoryImpl implements IgtimiConnectionFactory {
private final Map<Account, String> accessTokensByAccount;
private Map<String, Account> accountsByEmail;
private final Client client;
private final MongoObjectFactory mongoObjectFactory;
public IgtimiConnectionFactoryImpl(Client client) {
public IgtimiConnectionFactoryImpl(Client client, DomainObjectFactory domainObjectFactory, MongoObjectFactory mongoObjectFactory) {
this.accessTokensByAccount = new HashMap<>();
this.accountsByEmail = new HashMap<>();
this.client = client;
this.mongoObjectFactory = mongoObjectFactory;
for (String accessToken : domainObjectFactory.getAccessTokens()) {
try {
registerAccountForWhichClientIsAuthorized(accessToken);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error registering Igtimi access token "+accessToken+"; probably the access token was revoked or expired.", e);
mongoObjectFactory.removeAccessToken(accessToken);
}
}
}
@Override
@@ -66,6 +79,7 @@ public class IgtimiConnectionFactoryImpl implements IgtimiConnectionFactory {
Account account = getAccount(accessToken);
accountsByEmail.put(account.getUser().getEmail(), account);
accessTokensByAccount.put(account, accessToken);
mongoObjectFactory.storeAccessToken(accessToken);
return account;
}
@@ -109,6 +123,11 @@ public class IgtimiConnectionFactoryImpl implements IgtimiConnectionFactory {
public Account getAccountByEmail(String eMail) {
return accountsByEmail.get(eMail);
}
@Override
public Iterable<Account> getAllAccounts() {
return new ArrayList<Account>(accountsByEmail.values());
}
@Override
public IgtimiConnection connect(Account account) {
@@ -229,12 +248,7 @@ public class IgtimiConnectionFactoryImpl implements IgtimiConnectionFactory {
return result;
}
/**
* Tries to authorize our client on behalf of a user identified by e-mail and password.
*
* @return the authorization code which can then be used to obtain a permanent access token to be used by our client
* to access data owned by the user identified by e-mail and password.
*/
@Override
public String authorizeAndReturnAuthorizedCode(String userEmail, String userPassword)
throws ClientProtocolException, IOException, IllegalStateException, ParserConfigurationException,
SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException {
@@ -29,7 +29,7 @@ public class AuthorizationCallback {
static final String V1_AUTHORIZATIONCALLBACK = V1 + AUTHORIZATIONCALLBACK;
private final IgtimiConnectionFactory connectionFactory;
public AuthorizationCallback() {
public AuthorizationCallback() throws ClientProtocolException, IllegalStateException, IOException, ParseException {
connectionFactory = Activator.getInstance().getConnectionFactory();
}