Search in sources :

Example 1 with Key

use of com.google.appengine.api.datastore.Key in project iosched by google.

the class ApiKeyInitializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key key = KeyFactory.createKey(ENTITY_KIND, ENTITY_KEY);
    Entity entity;
    try {
        entity = datastore.get(key);
    } catch (EntityNotFoundException e) {
        entity = new Entity(key);
        // NOTE: it's not possible to change entities in the local server, so
        // it will be necessary to hardcode the API key below if you are running
        // it locally.
        entity.setProperty(ACCESS_KEY_FIELD, API_KEY);
        datastore.put(entity);
        mLogger.severe("Created fake key. Please go to App Engine admin " + "console, change its value to your API Key (the entity " + "type is '" + ENTITY_KIND + "' and its field to be changed is '" + ACCESS_KEY_FIELD + "'), then restart the server!");
    }
    String accessKey = (String) entity.getProperty(ACCESS_KEY_FIELD);
    event.getServletContext().setAttribute(ATTRIBUTE_ACCESS_KEY, accessKey);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 2 with Key

use of com.google.appengine.api.datastore.Key in project spring-security by spring-projects.

the class GaeDatastoreUserRegistry method registerUser.

public void registerUser(GaeUser newUser) {
    logger.debug("Attempting to create new user " + newUser);
    Key key = KeyFactory.createKey(USER_TYPE, newUser.getUserId());
    Entity user = new Entity(key);
    user.setProperty(USER_EMAIL, newUser.getEmail());
    user.setProperty(USER_NICKNAME, newUser.getNickname());
    user.setProperty(USER_FORENAME, newUser.getForename());
    user.setProperty(USER_SURNAME, newUser.getSurname());
    user.setUnindexedProperty(USER_ENABLED, newUser.isEnabled());
    Collection<AppRole> roles = newUser.getAuthorities();
    long binaryAuthorities = 0;
    for (AppRole r : roles) {
        binaryAuthorities |= 1 << r.getBit();
    }
    user.setUnindexedProperty(USER_AUTHORITIES, binaryAuthorities);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(user);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) AppRole(samples.gae.security.AppRole) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key)

Example 3 with Key

use of com.google.appengine.api.datastore.Key in project spring-security by spring-projects.

the class GaeDatastoreUserRegistry method findUser.

public GaeUser findUser(String userId) {
    Key key = KeyFactory.createKey(USER_TYPE, userId);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try {
        Entity user = datastore.get(key);
        long binaryAuthorities = (Long) user.getProperty(USER_AUTHORITIES);
        Set<AppRole> roles = EnumSet.noneOf(AppRole.class);
        for (AppRole r : AppRole.values()) {
            if ((binaryAuthorities & (1 << r.getBit())) != 0) {
                roles.add(r);
            }
        }
        GaeUser gaeUser = new GaeUser(user.getKey().getName(), (String) user.getProperty(USER_NICKNAME), (String) user.getProperty(USER_EMAIL), (String) user.getProperty(USER_FORENAME), (String) user.getProperty(USER_SURNAME), roles, (Boolean) user.getProperty(USER_ENABLED));
        return gaeUser;
    } catch (EntityNotFoundException e) {
        logger.debug(userId + " not found in datastore");
        return null;
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) AppRole(samples.gae.security.AppRole) DatastoreService(com.google.appengine.api.datastore.DatastoreService) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 4 with Key

use of com.google.appengine.api.datastore.Key in project spring-security by spring-projects.

the class GaeDatastoreUserRegistry method removeUser.

public void removeUser(String userId) {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key key = KeyFactory.createKey(USER_TYPE, userId);
    datastore.delete(key);
}
Also used : DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key)

Example 5 with Key

use of com.google.appengine.api.datastore.Key in project java-docs-samples by GoogleCloudPlatform.

the class GuestbookTestUtilities method cleanDatastore.

public static void cleanDatastore(DatastoreService ds, String book) {
    Query query = new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", book).getKey()).setKeysOnly();
    PreparedQuery pq = ds.prepare(query);
    List<Entity> entities = pq.asList(FetchOptions.Builder.withDefaults());
    ArrayList<Key> keys = new ArrayList<>(entities.size());
    for (Entity e : entities) {
        keys.add(e.getKey());
    }
    ds.delete(keys);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) ArrayList(java.util.ArrayList) KeyFactory(com.google.appengine.api.datastore.KeyFactory) Key(com.google.appengine.api.datastore.Key)

Aggregations

Key (com.google.appengine.api.datastore.Key)90 Entity (com.google.appengine.api.datastore.Entity)65 ArrayList (java.util.ArrayList)35 ClassInfo (siena.ClassInfo)24 Test (org.junit.Test)23 Field (java.lang.reflect.Field)22 Query (com.google.appengine.api.datastore.Query)16 DatastoreService (com.google.appengine.api.datastore.DatastoreService)14 List (java.util.List)14 SienaException (siena.SienaException)14 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)13 HashMap (java.util.HashMap)13 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)12 QueryResultList (com.google.appengine.api.datastore.QueryResultList)12 Map (java.util.Map)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)6 EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)5 Filter (com.google.appengine.api.datastore.Query.Filter)5