Search in sources :

Example 1 with NotFoundException

use of com.googlecode.objectify.NotFoundException in project iosched by google.

the class SyncReservationsQueueWorker method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = getServletContext();
    String serviceAccountKey = context.getInitParameter("accountKey");
    LOG.info("accountKey: " + serviceAccountKey);
    InputStream serviceAccount = context.getResourceAsStream(serviceAccountKey);
    LOG.info("serviceAccount: " + serviceAccount);
    String accessToken = firebaseWrapper.getAccessToken(serviceAccount);
    if (accessToken == null) {
        LOG.severe("Unable to get access token");
        return;
    }
    // Get all existing reservations from RTDB.
    JsonArray firebaseReservations = getFirebaseReservations(accessToken);
    // For each user update reservations in datastore with reservations from RTDB.
    for (int i = 0; firebaseReservations != null && i < firebaseReservations.size(); i++) {
        JsonObject jReservation = firebaseReservations.get(i).getAsJsonObject();
        String userId = jReservation.get("userId").getAsString();
        try {
            // Deserialize reservations from RTDB.
            Map<String, ReservedSession> reservedSessions = new HashMap<>();
            if (jReservation.has(RESERVATIONS_KEY)) {
                JsonArray jUserReservations = jReservation.get(RESERVATIONS_KEY).getAsJsonArray();
                for (int j = 0; j < jUserReservations.size(); j++) {
                    JsonObject jUserReservation = jUserReservations.get(j).getAsJsonObject();
                    String sessionId = jUserReservation.get("sessionId").getAsString();
                    Status status = mapToDatastoreStatus(jUserReservation.get("status").getAsString());
                    ReservedSession reservedSession = new ReservedSession(sessionId, status, System.currentTimeMillis());
                    reservedSessions.put(sessionId, reservedSession);
                }
            }
            // Update reservations in datastore with those from RTDB.
            UserData userData = ofy().load().type(UserData.class).id(userId).safe();
            for (String sessionId : userData.reservedSessions.keySet()) {
                if (reservedSessions.containsKey(sessionId)) {
                    userData.reservedSessions.put(sessionId, reservedSessions.get(sessionId));
                    reservedSessions.remove(sessionId);
                } else {
                    // Mark DELETED if reservation does not exist in RTDB.
                    userData.reservedSessions.get(sessionId).status = Status.DELETED;
                    userData.reservedSessions.get(sessionId).timestampUTC = System.currentTimeMillis();
                }
            }
            // Add reservations from RTDB that do not exist in datastore.
            for (ReservedSession reservedSession : reservedSessions.values()) {
                userData.reservedSessions.put(reservedSession.sessionID, reservedSession);
            }
            ofy().save().entity(userData).now();
        } catch (NotFoundException e) {
            LOG.severe(e.getMessage());
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Status(com.google.samples.apps.iosched.server.userdata.db.ReservedSession.Status) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) HashMap(java.util.HashMap) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) JsonObject(com.google.gson.JsonObject) NotFoundException(com.googlecode.objectify.NotFoundException)

Example 2 with NotFoundException

use of com.googlecode.objectify.NotFoundException in project iosched by google.

the class FcmRegistrationEndpoint method unregister.

/**
 * Remove a registration of a user's device. When a user signs out of a client they should
 * unregister. This will prevent messages from being sent to the wrong user if multiple users
 * are using the same device.
 *
 * @param deviceId FCM token representing the device.
 * @return Result containing a message about the un-registration.
 * @throws BadRequestException Thrown when there is no device ID in the request.
 */
@ApiMethod(path = "unregister", httpMethod = HttpMethod.POST)
public void unregister(User user, @Named(PARAMETER_DEVICE_ID) String deviceId) throws BadRequestException, UnauthorizedException, com.google.api.server.spi.response.NotFoundException, ForbiddenException {
    // Check to see if deviceId.
    if (Strings.isNullOrEmpty(deviceId)) {
        // Drop request.
        throw new BadRequestException("Invalid request: Request must contain " + PARAMETER_DEVICE_ID);
    }
    // Check that user making requests is non null.
    if (user == null) {
        throw new UnauthorizedException("Invalid credentials");
    }
    try {
        Device device = ofy().load().type(Device.class).id(deviceId).safe();
        // Check that the user trying to unregister the token is the same one that registered it.
        if (!device.getUserId().equals(user.getId())) {
            throw new ForbiddenException("Not authorized to unregister token");
        }
        DeviceStore.unregister(deviceId);
    } catch (NotFoundException e) {
        throw new com.google.api.server.spi.response.NotFoundException("Device ID: " + deviceId + " not found");
    }
}
Also used : ForbiddenException(com.google.api.server.spi.response.ForbiddenException) Device(com.google.samples.apps.iosched.server.gcm.db.models.Device) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) BadRequestException(com.google.api.server.spi.response.BadRequestException) NotFoundException(com.googlecode.objectify.NotFoundException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

NotFoundException (com.googlecode.objectify.NotFoundException)2 ApiMethod (com.google.api.server.spi.config.ApiMethod)1 BadRequestException (com.google.api.server.spi.response.BadRequestException)1 ForbiddenException (com.google.api.server.spi.response.ForbiddenException)1 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)1 ReservedSession (com.google.samples.apps.iosched.server.userdata.db.ReservedSession)1 Status (com.google.samples.apps.iosched.server.userdata.db.ReservedSession.Status)1 UserData (com.google.samples.apps.iosched.server.userdata.db.UserData)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1