use of com.google.api.server.spi.response.UnauthorizedException in project iosched by google.
the class FcmSendEndpoint method sendSelfSync.
/**
* Clients can initiate a sync on all of a user's devices. This will usually be called
* when a client pushes a user data update to the server and wants other clients to
* sync that change.
*
* @param context Servlet context (injected by Endpoints)
* @param user User requesting the sync (injected by Endpoints)
*/
@ApiMethod(name = "sendSelfSync", path = "self")
public void sendSelfSync(ServletContext context, User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException(INVALID_CREDENTIALS_MSG);
}
MessageSender sender = new MessageSender(context);
String userId = user.getId();
List<Device> devices = DeviceStore.findDevicesByUserId(userId);
sender.multicastSend(devices, ACTION_SYNC_USER, null);
}
use of com.google.api.server.spi.response.UnauthorizedException in project iosched by google.
the class FcmSendEndpoint method sendUserDataSync.
/**
* Ping all users' devices to sync user data.
*
* @param context Servlet context (injected by Endpoints)
* @param user User making the request (injected by Endpoints)
*/
@ApiMethod(name = "sendUserDataSync", path = "users", clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID })
public void sendUserDataSync(ServletContext context, User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException(INVALID_CREDENTIALS_MSG);
}
MessageSender sender = new MessageSender(context);
List<Device> devices = DeviceStore.getAllDevices();
sender.multicastSend(devices, ACTION_SYNC_USER, null);
}
use of com.google.api.server.spi.response.UnauthorizedException in project endpoints-java by cloudendpoints.
the class ServiceExceptionTest method testWithLogLevel.
@Test
public void testWithLogLevel() {
UnauthorizedException ex = new UnauthorizedException("");
assertThat(ex.getLogLevel()).isEqualTo(Level.INFO);
assertThat(ServiceException.withLogLevel(ex, Level.WARNING).getLogLevel()).isEqualTo(Level.WARNING);
}
use of com.google.api.server.spi.response.UnauthorizedException in project endpoints-java by cloudendpoints.
the class SystemService method invokeServiceMethod.
/**
* Invokes a {@code method} on a {@code service} given a {@code paramReader} to read parameters
* and a {@code resultWriter} to write result.
*/
public void invokeServiceMethod(Object service, Method method, ParamReader paramReader, ResultWriter resultWriter) throws IOException {
try {
Object[] params = paramReader.read();
logger.atFine().log("params=%s (String)", Arrays.toString(params));
Object response = method.invoke(service, params);
resultWriter.write(response);
} catch (IllegalArgumentException | IllegalAccessException e) {
logger.atSevere().withCause(e).log("exception occurred while calling backend method");
resultWriter.writeError(new BadRequestException(e));
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
Level level = Level.INFO;
if (cause instanceof ServiceException) {
resultWriter.writeError((ServiceException) cause);
} else if (cause instanceof IllegalArgumentException) {
resultWriter.writeError(isIllegalArgumentBackendError ? new InternalServerErrorException(cause) : new BadRequestException(cause));
} else if (isOAuthRequestException(cause.getClass())) {
resultWriter.writeError(new UnauthorizedException(cause));
} else if (cause.getCause() != null && cause.getCause() instanceof ServiceException) {
ServiceException serviceException = (ServiceException) cause.getCause();
level = serviceException.getLogLevel();
resultWriter.writeError(serviceException);
} else {
level = Level.SEVERE;
resultWriter.writeError(new InternalServerErrorException(cause));
}
logger.at(level).withCause(cause).log("exception occurred while calling backend method");
} catch (ServiceException e) {
logger.at(e.getLogLevel()).withCause(e).log("exception occurred while calling backend method");
resultWriter.writeError(e);
}
}
use of com.google.api.server.spi.response.UnauthorizedException in project endpoints-java by cloudendpoints.
the class ServletRequestParamReaderTest method testUserInjectionThrowsExceptionIfRequired.
@Test
public void testUserInjectionThrowsExceptionIfRequired() throws Exception {
@SuppressWarnings("unused")
class TestUser {
@SuppressWarnings("unused")
public void getUser(User user) {
}
}
ApiMethodConfig methodConfig = Mockito.mock(ApiMethodConfig.class);
when(methodConfig.getAuthLevel()).thenReturn(AuthLevel.REQUIRED);
methodConfig.setAuthLevel(AuthLevel.REQUIRED);
try {
Method method = TestUser.class.getDeclaredMethod("getUser", User.class);
readParameters("{}", EndpointMethod.create(method.getDeclaringClass(), method), methodConfig, null, null);
fail("expected unauthorized method exception");
} catch (UnauthorizedException ex) {
// expected
}
}
Aggregations