Search in sources :

Example 11 with UnauthorizedException

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);
}
Also used : MessageSender(com.google.samples.apps.iosched.server.gcm.device.MessageSender) Device(com.google.samples.apps.iosched.server.gcm.db.models.Device) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 12 with UnauthorizedException

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);
}
Also used : MessageSender(com.google.samples.apps.iosched.server.gcm.device.MessageSender) Device(com.google.samples.apps.iosched.server.gcm.db.models.Device) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 13 with UnauthorizedException

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);
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) Test(org.junit.Test)

Example 14 with UnauthorizedException

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);
    }
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) BadRequestException(com.google.api.server.spi.response.BadRequestException) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) Level(java.util.logging.Level) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 15 with UnauthorizedException

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
    }
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) User(com.google.api.server.spi.auth.common.User) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Aggregations

UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)15 ApiMethod (com.google.api.server.spi.config.ApiMethod)7 BadRequestException (com.google.api.server.spi.response.BadRequestException)4 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)4 EndpointMethod (com.google.api.server.spi.EndpointMethod)3 User (com.google.api.server.spi.auth.common.User)3 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)3 Test (org.junit.Test)3 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)2 MessageSender (com.google.samples.apps.iosched.server.gcm.device.MessageSender)2 Method (java.lang.reflect.Method)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ForbiddenException (com.google.api.server.spi.response.ForbiddenException)1 InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)1 NotFoundException (com.google.api.server.spi.response.NotFoundException)1 Queue (com.google.appengine.api.taskqueue.Queue)1 TaskOptions (com.google.appengine.api.taskqueue.TaskOptions)1 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)1 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1