Search in sources :

Example 1 with ForbiddenException

use of com.google.api.server.spi.response.ForbiddenException 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)

Example 2 with ForbiddenException

use of com.google.api.server.spi.response.ForbiddenException in project iosched by google.

the class RegistrationEndpoint method registrationStatus.

@ApiMethod(path = "status", httpMethod = ApiMethod.HttpMethod.GET)
public RegistrationResult registrationStatus(ServletContext context, @Named("firebaseUserToken") String firebaseUserToken) throws IOException, ForbiddenException, ExecutionException, InterruptedException, InternalServerErrorException {
    String databaseUrl = context.getInitParameter("databaseUrl");
    LOG.info("databaseUrl: " + databaseUrl);
    String serviceAccountKey = context.getInitParameter("accountKey");
    LOG.info("accountKey: " + serviceAccountKey);
    InputStream serviceAccount = context.getResourceAsStream(serviceAccountKey);
    LOG.info("serviceAccount: " + serviceAccount);
    firebaseWrapper.initFirebase(databaseUrl, serviceAccount);
    firebaseWrapper.authenticateFirebaseUser(firebaseUserToken);
    if (!firebaseWrapper.isUserAuthenticated()) {
        throw new ForbiddenException("Not authenticated");
    }
    boolean isRegistered = isUserRegistered(context);
    final TaskCompletionSource<Boolean> isRegisteredTCS = new TaskCompletionSource<>();
    final Task<Boolean> isRegisteredTCSTask = isRegisteredTCS.getTask();
    // Update the user registration state in the Real-time Database.
    DatabaseReference dbRef = firebaseWrapper.getDatabaseReference();
    int rtdbRetries = 0;
    while (rtdbRetries < RTDB_RETRY_LIMIT) {
        dbRef.child("users").child(firebaseWrapper.getUserId()).setValue(isRegistered).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    isRegisteredTCS.setResult(true);
                } else {
                    isRegisteredTCS.setResult(false);
                }
            }
        });
        // If writing to RTDB was successful break out.
        if (Tasks.await(isRegisteredTCSTask)) {
            break;
        }
        LOG.info("Writing to RTDB has failed.");
        rtdbRetries++;
    }
    // indeed registered.
    if (rtdbRetries >= RTDB_RETRY_LIMIT) {
        throw new InternalServerErrorException("Unable to write registration status to RTDB.");
    } else {
        // Return the user registration state.
        return new RegistrationResult(isRegistered);
    }
}
Also used : ForbiddenException(com.google.api.server.spi.response.ForbiddenException) DatabaseReference(com.google.firebase.database.DatabaseReference) InputStream(java.io.InputStream) TaskCompletionSource(com.google.firebase.tasks.TaskCompletionSource) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

ApiMethod (com.google.api.server.spi.config.ApiMethod)2 ForbiddenException (com.google.api.server.spi.response.ForbiddenException)2 BadRequestException (com.google.api.server.spi.response.BadRequestException)1 InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)1 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 TaskCompletionSource (com.google.firebase.tasks.TaskCompletionSource)1 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)1 NotFoundException (com.googlecode.objectify.NotFoundException)1 InputStream (java.io.InputStream)1