Search in sources :

Example 1 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService 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 DatastoreService

use of com.google.appengine.api.datastore.DatastoreService 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 DatastoreService

use of com.google.appengine.api.datastore.DatastoreService 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 DatastoreService

use of com.google.appengine.api.datastore.DatastoreService 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 DatastoreService

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

the class RemoteApiExample method main.

public static void main(String[] args) throws IOException {
    String serverString = args[0];
    RemoteApiOptions options;
    if (serverString.equals("localhost")) {
        options = new RemoteApiOptions().server(serverString, 8080).useDevelopmentServerCredential();
    } else {
        options = new RemoteApiOptions().server(serverString, 443).useApplicationDefaultCredential();
    }
    RemoteApiInstaller installer = new RemoteApiInstaller();
    installer.install(options);
    try {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!")));
    } finally {
        installer.uninstall();
    }
}
Also used : RemoteApiInstaller(com.google.appengine.tools.remoteapi.RemoteApiInstaller) Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) RemoteApiOptions(com.google.appengine.tools.remoteapi.RemoteApiOptions)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)28 Entity (com.google.appengine.api.datastore.Entity)23 Test (org.junit.Test)18 Key (com.google.appengine.api.datastore.Key)13 Query (com.google.appengine.api.datastore.Query)13 Transaction (com.google.appengine.api.datastore.Transaction)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)5 Date (java.util.Date)4 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)3 Filter (com.google.appengine.api.datastore.Query.Filter)3 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)2 ReadPolicy (com.google.appengine.api.datastore.ReadPolicy)2 PrintWriter (java.io.PrintWriter)2 AppRole (samples.gae.security.AppRole)2 Capability (com.google.appengine.api.capabilities.Capability)1 CapabilityStatus (com.google.appengine.api.capabilities.CapabilityStatus)1 EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)1 FetchOptions (com.google.appengine.api.datastore.FetchOptions)1