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