Search in sources :

Example 1 with SuccessResponse

use of com.linkedin.restli.server.annotations.SuccessResponse 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 SuccessResponse

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

the class AlbumEntryResource method search.

/**
 * Find all entries matching the given album and photo IDs. <code>null</code> is treated
 * as a wildcard.
 *
 * @param albumId provides the id to match for albums to match, if not provided, it is treated as a wildcard
 * @param photoId provides the id to match for photos to match, if not provided, it is treated as a wildcard
 * @return a list of {@link AlbumEntry} matching the  given parameters
 */
@Finder("search")
@SuccessResponse(statuses = { HttpStatus.S_200_OK })
@ParamError(code = INVALID_ID, parameterNames = { "albumId", "photoId" })
@ParamError(code = UNSEARCHABLE_ALBUM_ID, parameterNames = { "albumId" })
public List<AlbumEntry> search(@Optional @QueryParam("albumId") Long albumId, @Optional @QueryParam("photoId") Long photoId) {
    List<AlbumEntry> result = new ArrayList<>();
    for (Map.Entry<CompoundKey, AlbumEntry> entry : _db.getData().entrySet()) {
        CompoundKey key = entry.getKey();
        // (treat all values as a match)
        if (albumId != null && !key.getPart("albumId").equals(albumId))
            continue;
        if (photoId != null && !key.getPart("photoId").equals(photoId))
            continue;
        result.add(entry.getValue());
    }
    return result;
}
Also used : AlbumEntry(com.linkedin.restli.example.AlbumEntry) CompoundKey(com.linkedin.restli.common.CompoundKey) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) SuccessResponse(com.linkedin.restli.server.annotations.SuccessResponse) Finder(com.linkedin.restli.server.annotations.Finder) ParamError(com.linkedin.restli.server.annotations.ParamError)

Example 3 with SuccessResponse

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

the class RestLiAnnotationReader method addSuccessStatuses.

/**
 * Reads annotations on a given resource method in order to build success statuses, which are then added to
 * a given resource method descriptor.
 *
 * @param resourceMethodDescriptor resource method descriptor to add success statuses to
 * @param method method possibly annotated with a success annotation
 */
private static void addSuccessStatuses(final ResourceMethodDescriptor resourceMethodDescriptor, final Method method) {
    final Class<?> resourceClass = method.getDeclaringClass();
    final SuccessResponse successResponseAnnotation = method.getAnnotation(SuccessResponse.class);
    if (successResponseAnnotation == null) {
        return;
    }
    // Build success status list from the annotation
    final List<HttpStatus> successStatuses = Arrays.stream(successResponseAnnotation.statuses()).collect(Collectors.toList());
    if (successStatuses.isEmpty()) {
        throw new ResourceConfigException(String.format("@%s annotation on %s specifies no success statuses", SuccessResponse.class.getSimpleName(), buildExceptionLocationString(resourceClass, method)));
    }
    // Validate the success statuses
    for (HttpStatus successStatus : successStatuses) {
        if (successStatus.getCode() < 200 || successStatus.getCode() >= 400) {
            throw new ResourceConfigException(String.format("Invalid success status '%s' specified in %s", successStatus, buildExceptionLocationString(resourceClass, method)));
        }
    }
    resourceMethodDescriptor.setSuccessStatuses(successStatuses);
}
Also used : SuccessResponse(com.linkedin.restli.server.annotations.SuccessResponse) HttpStatus(com.linkedin.restli.common.HttpStatus) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

SuccessResponse (com.linkedin.restli.server.annotations.SuccessResponse)3 ParamError (com.linkedin.restli.server.annotations.ParamError)2 CompoundKey (com.linkedin.restli.common.CompoundKey)1 HttpStatus (com.linkedin.restli.common.HttpStatus)1 AlbumEntry (com.linkedin.restli.example.AlbumEntry)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 UpdateResponse (com.linkedin.restli.server.UpdateResponse)1 Finder (com.linkedin.restli.server.annotations.Finder)1 ServiceErrors (com.linkedin.restli.server.annotations.ServiceErrors)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1