Search in sources :

Example 1 with ServiceErrors

use of com.linkedin.restli.server.annotations.ServiceErrors in project rest.li by linkedin.

the class AlbumEntryResource method update.

/**
 * Add the specified photo to the specified album.
 * If a matching pair of IDs already exists, this changes the add date.
 */
@Override
@SuccessResponse(statuses = { HttpStatus.S_204_NO_CONTENT })
@ServiceErrors(INVALID_PERMISSIONS)
@ParamError(code = INVALID_ID, parameterNames = { "albumEntryId" })
public UpdateResponse update(CompoundKey key, AlbumEntry entity) {
    long photoId = (Long) key.getPart("photoId");
    long albumId = (Long) key.getPart("albumId");
    // make sure photo and album exist
    if (!_photoDb.getData().containsKey(photoId))
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent photo ID: " + photoId);
    if (!_albumDb.getData().containsKey(albumId))
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent album ID: " + albumId);
    // disallow changing entity ID
    if (entity.hasAlbumId() || entity.hasPhotoId())
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo/album ID are not acceptable in request");
    // make sure the ID in the entity is consistent with the key in the database
    entity.setPhotoId(photoId);
    entity.setAlbumId(albumId);
    _db.getData().put(key, entity);
    return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) SuccessResponse(com.linkedin.restli.server.annotations.SuccessResponse) ServiceErrors(com.linkedin.restli.server.annotations.ServiceErrors) ParamError(com.linkedin.restli.server.annotations.ParamError)

Example 2 with ServiceErrors

use of com.linkedin.restli.server.annotations.ServiceErrors in project rest.li by linkedin.

the class RestLiAnnotationReader method addServiceErrors.

/**
 * Reads annotations on a given resource class in order to build service errors, which are then added to
 * a given resource model.
 *
 * @param resourceModel resource model to add service errors to
 * @param resourceClass class annotated with service errors
 */
private static void addServiceErrors(final ResourceModel resourceModel, final Class<?> resourceClass) {
    final ServiceErrorDef serviceErrorDefAnnotation = resourceClass.getAnnotation(ServiceErrorDef.class);
    final ServiceErrors serviceErrorsAnnotation = resourceClass.getAnnotation(ServiceErrors.class);
    final List<ServiceError> serviceErrors = buildServiceErrors(serviceErrorDefAnnotation, serviceErrorsAnnotation, null, resourceClass, null);
    if (serviceErrors == null) {
        return;
    }
    resourceModel.setServiceErrors(serviceErrors);
}
Also used : ServiceErrorDef(com.linkedin.restli.server.annotations.ServiceErrorDef) ParametersServiceError(com.linkedin.restli.server.errors.ParametersServiceError) ServiceError(com.linkedin.restli.server.errors.ServiceError) ServiceErrors(com.linkedin.restli.server.annotations.ServiceErrors)

Example 3 with ServiceErrors

use of com.linkedin.restli.server.annotations.ServiceErrors in project rest.li by linkedin.

the class RestLiAnnotationReader method addServiceErrors.

/**
 * Reads annotations on a given method in order to build service errors, which are then added to
 * a given resource method descriptor.
 *
 * @param resourceMethodDescriptor resource method descriptor to add service errors to
 * @param method method annotated with service errors
 */
private static void addServiceErrors(final ResourceMethodDescriptor resourceMethodDescriptor, final Method method) {
    final Class<?> resourceClass = method.getDeclaringClass();
    final ServiceErrorDef serviceErrorDefAnnotation = resourceClass.getAnnotation(ServiceErrorDef.class);
    final ServiceErrors serviceErrorsAnnotation = method.getAnnotation(ServiceErrors.class);
    final ParamError[] paramErrorAnnotations = method.getAnnotationsByType(ParamError.class);
    final List<ServiceError> serviceErrors = buildServiceErrors(serviceErrorDefAnnotation, serviceErrorsAnnotation, paramErrorAnnotations, resourceClass, method);
    if (serviceErrors == null) {
        return;
    }
    // Form a set of parameter names which exist on this method
    final Set<String> acceptableParameterNames = resourceMethodDescriptor.getParameters().stream().map(Parameter::getName).collect(Collectors.toSet());
    // Validate that all parameter names are valid
    for (ServiceError serviceError : serviceErrors) {
        if (serviceError instanceof ParametersServiceError) {
            final String[] parameterNames = ((ParametersServiceError) serviceError).parameterNames();
            if (parameterNames != null) {
                for (String parameterName : parameterNames) {
                    if (!acceptableParameterNames.contains(parameterName)) {
                        throw new ResourceConfigException(String.format("Nonexistent parameter '%s' specified for method-level service error '%s' in %s (valid parameters: %s)", parameterName, serviceError.code(), buildExceptionLocationString(resourceClass, method), acceptableParameterNames.toString()));
                    }
                }
            }
        }
    }
    resourceMethodDescriptor.setServiceErrors(serviceErrors);
}
Also used : ParametersServiceError(com.linkedin.restli.server.errors.ParametersServiceError) ServiceErrorDef(com.linkedin.restli.server.annotations.ServiceErrorDef) ParametersServiceError(com.linkedin.restli.server.errors.ParametersServiceError) ServiceError(com.linkedin.restli.server.errors.ServiceError) ServiceErrors(com.linkedin.restli.server.annotations.ServiceErrors) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) ParamError(com.linkedin.restli.server.annotations.ParamError)

Aggregations

ServiceErrors (com.linkedin.restli.server.annotations.ServiceErrors)3 ParamError (com.linkedin.restli.server.annotations.ParamError)2 ServiceErrorDef (com.linkedin.restli.server.annotations.ServiceErrorDef)2 ParametersServiceError (com.linkedin.restli.server.errors.ParametersServiceError)2 ServiceError (com.linkedin.restli.server.errors.ServiceError)2 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 UpdateResponse (com.linkedin.restli.server.UpdateResponse)1 SuccessResponse (com.linkedin.restli.server.annotations.SuccessResponse)1