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());
}
}
}
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");
}
}
Aggregations