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