Search in sources :

Example 1 with InternalServerErrorException

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

Example 2 with InternalServerErrorException

use of com.google.api.server.spi.response.InternalServerErrorException in project endpoints-java by cloudendpoints.

the class ProxyingDiscoveryProvider method getDirectory.

@Override
public DirectoryList getDirectory(String root) throws InternalServerErrorException {
    try {
        Map<ApiKey, String> configStrings = configWriter.writeConfig(rewriteConfigsWithRoot(getAllApiConfigs(), root));
        ApiConfigs configs = new ApiConfigs();
        configs.setConfigs(Lists.newArrayList(configStrings.values()));
        return discovery.apis().generateDirectory(configs).execute();
    } catch (IOException | ApiConfigException e) {
        logger.log(Level.SEVERE, "Could not generate or cache directory", e);
        throw new InternalServerErrorException("Internal Server Error", e);
    }
}
Also used : ApiKey(com.google.api.server.spi.config.model.ApiKey) ApiConfigException(com.google.api.server.spi.config.ApiConfigException) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) IOException(java.io.IOException) ApiConfigs(com.google.api.services.discovery.model.ApiConfigs)

Example 3 with InternalServerErrorException

use of com.google.api.server.spi.response.InternalServerErrorException in project endpoints-java by cloudendpoints.

the class ProxyingDiscoveryProviderTest method getRpcDocument_internalServerError.

@Test
public void getRpcDocument_internalServerError() throws Exception {
    when(rpcRequest.execute()).thenThrow(new IOException());
    try {
        provider.getRpcDocument(REWRITTEN_ROOT, NAME, V1);
        fail("expected InternalServerErrorException");
    } catch (InternalServerErrorException e) {
    // expected
    }
}
Also used : InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with InternalServerErrorException

use of com.google.api.server.spi.response.InternalServerErrorException in project endpoints-java by cloudendpoints.

the class ProxyingDiscoveryServiceTest method getRestDocument_internalServerError.

@Test
public void getRestDocument_internalServerError() throws Exception {
    ProxyingDiscoveryService discoveryService = createDiscoveryService(true);
    when(provider.getRestDocument(SERVER_ROOT, API_NAME, API_VERSION)).thenThrow(new InternalServerErrorException(""));
    try {
        discoveryService.getRestDocument(createRequest("discovery/v1/apis/tictactoe/v1/rest"), API_NAME, API_VERSION);
        fail("expected InternalServerErrorException");
    } catch (InternalServerErrorException e) {
    // expected
    }
}
Also used : InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) Test(org.junit.Test)

Example 5 with InternalServerErrorException

use of com.google.api.server.spi.response.InternalServerErrorException in project endpoints-java by cloudendpoints.

the class CachingDiscoveryProviderTest method getDirectory_internalServerError.

@Test
public void getDirectory_internalServerError() throws Exception {
    CachingDiscoveryProvider provider = createNonExpiringProvider();
    when(delegate.getDirectory(ROOT)).thenThrow(new InternalServerErrorException(""));
    try {
        provider.getDirectory(ROOT);
        fail("expected InternalServerErrorException");
    } catch (InternalServerErrorException e) {
    // expected
    }
}
Also used : InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) Test(org.junit.Test)

Aggregations

InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)12 Test (org.junit.Test)9 IOException (java.io.IOException)4 ApiConfigException (com.google.api.server.spi.config.ApiConfigException)1 ApiMethod (com.google.api.server.spi.config.ApiMethod)1 ApiKey (com.google.api.server.spi.config.model.ApiKey)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 ApiConfigs (com.google.api.services.discovery.model.ApiConfigs)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 TaskCompletionSource (com.google.firebase.tasks.TaskCompletionSource)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Level (java.util.logging.Level)1