use of org.dcache.restful.providers.JsonFileAttributes in project dcache by dCache.
the class FileResources method getFileAttributes.
@GET
@ApiOperation(value = "Find metadata and optionally directory contents.", notes = "The method offers the possibility to list the content of a " + "directory in addition to providing metadata of a " + "specified file or directory.")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error") })
@Path("{path : .*}")
@Produces(MediaType.APPLICATION_JSON)
public JsonFileAttributes getFileAttributes(@ApiParam("Path of file or directory.") @PathParam("path") String requestPath, @ApiParam("Whether to include directory listing.") @DefaultValue("false") @QueryParam("children") boolean isList, @ApiParam("Whether to include file locality information.") @DefaultValue("false") @QueryParam("locality") boolean isLocality, @ApiParam(value = "Whether to include replica locations.") @QueryParam("locations") boolean isLocations, @ApiParam(value = "Whether to include quality of service.") @DefaultValue("false") @QueryParam("qos") boolean isQos, @ApiParam("Whether to include extended attributes.") @QueryParam("xattr") boolean isXattr, @ApiParam("Whether to include labels.") @QueryParam("labels") boolean isLabels, @ApiParam("Whether or not to list checksum values.") @QueryParam("checksum") boolean isChecksum, @ApiParam("Limit number of replies in directory listing.") @QueryParam("limit") String limit, @ApiParam("Number of entries to skip in directory listing.") @QueryParam("offset") String offset) throws CacheException {
JsonFileAttributes fileAttributes = new JsonFileAttributes();
Set<FileAttribute> attributes = NamespaceUtils.getRequestedAttributes(isLocality, isLocations, isQos, isChecksum, false);
PnfsHandler handler = HandlerBuilders.roleAwarePnfsHandler(pnfsmanager);
FsPath path = pathMapper.asDcachePath(request, requestPath, ForbiddenException::new);
try {
FileAttributes namespaceAttributes = handler.getFileAttributes(path, attributes);
NamespaceUtils.chimeraToJsonAttributes(path.name(), fileAttributes, namespaceAttributes, isLocality, isLocations, isLabels, false, isXattr, isChecksum, request, poolMonitor);
if (isQos) {
NamespaceUtils.addQoSAttributes(fileAttributes, namespaceAttributes, request, poolMonitor, pinmanager);
}
// fill children list id it's a directory and listing is requested
if (namespaceAttributes.getFileType() == FileType.DIR && isList) {
Range<Integer> range;
try {
int lower = (offset == null) ? 0 : Integer.parseInt(offset);
int ceiling = (limit == null) ? Integer.MAX_VALUE : Integer.parseInt(limit);
if (ceiling < 0 || lower < 0) {
throw new BadRequestException("limit and offset can not be less than zero.");
}
range = (Integer.MAX_VALUE - lower < ceiling) ? Range.atLeast(lower) : Range.closedOpen(lower, lower + ceiling);
} catch (NumberFormatException e) {
throw new BadRequestException("limit and offset must be an integer value.");
}
List<JsonFileAttributes> children = new ArrayList<>();
DirectoryStream stream = listDirectoryHandler.list(HttpServletRequests.roleAwareSubject(request), HttpServletRequests.roleAwareRestriction(request), path, null, range, attributes);
for (DirectoryEntry entry : stream) {
String fName = entry.getName();
JsonFileAttributes childrenAttributes = new JsonFileAttributes();
NamespaceUtils.chimeraToJsonAttributes(fName, childrenAttributes, entry.getFileAttributes(), isLocality, isLocations, isLabels, false, isXattr, isChecksum, request, poolMonitor);
childrenAttributes.setFileName(fName);
if (isQos) {
NamespaceUtils.addQoSAttributes(childrenAttributes, entry.getFileAttributes(), request, poolMonitor, pinmanager);
}
children.add(childrenAttributes);
}
fileAttributes.setChildren(children);
}
} catch (FileNotFoundCacheException e) {
throw new NotFoundException(e);
} catch (PermissionDeniedCacheException e) {
if (RequestUser.isAnonymous()) {
throw new NotAuthorizedException(e);
} else {
throw new ForbiddenException(e);
}
} catch (CacheException | InterruptedException | NoRouteToCellException ex) {
LOG.warn(Exceptions.meaningfulMessage(ex));
throw new InternalServerErrorException(ex);
}
return fileAttributes;
}
use of org.dcache.restful.providers.JsonFileAttributes in project dcache by dCache.
the class IdResources method getAttributes.
@GET
@ApiOperation(value = "Discover information about a file from the PNFS-ID.", notes = "Retrieve all file attributes plus the file's path from the " + "given PNFS-ID.")
@ApiResponses({ @ApiResponse(code = 400, message = "Bad pnsfid"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error") })
@Path("{pnfsid}")
@Produces(MediaType.APPLICATION_JSON)
public JsonFileAttributes getAttributes(@ApiParam("The PNFS-ID of a file or directory.") @PathParam("pnfsid") String value) {
Set<FileAttribute> attributeSet = NamespaceUtils.getRequestedAttributes(true, true, true, true, true);
JsonFileAttributes result = new JsonFileAttributes();
PnfsHandler handler = HandlerBuilders.roleAwarePnfsHandler(pnfsmanager);
try {
PnfsId id = new PnfsId(value);
FileAttributes attributes = handler.getFileAttributes(id, attributeSet);
/*
* Caveat: Because there is a possibility that a given file could have
* a number of hard-linked paths, and that the current path finder
* code selects only the most recently created path/link, there
* is a possibility of getting a path which may not correspond
* to the expected one.
*/
FsPath path = FsPath.create(attributes.getStorageInfo().getKey("path"));
/*
* Since FileResources maps according to the effective root,
* we should return the path in the same form here.
*/
result.setPath(pathMapper.asRequestPath(request, path));
String name = path.name();
result.setFileName(name);
NamespaceUtils.chimeraToJsonAttributes(name, result, attributes, true, true, true, true, false, true, request, poolMonitor);
NamespaceUtils.addQoSAttributes(result, attributes, request, poolMonitor, pinmanager);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Bad pnsfid " + value, e);
} catch (FileNotFoundCacheException e) {
throw new NotFoundException(e);
} catch (PermissionDeniedCacheException e) {
if (RequestUser.isAnonymous()) {
throw new NotAuthorizedException(e);
} else {
throw new ForbiddenException(e);
}
} catch (CacheException | InterruptedException | NoRouteToCellException e) {
LOGGER.warn(Exceptions.meaningfulMessage(e));
throw new InternalServerErrorException(e);
}
return result;
}
use of org.dcache.restful.providers.JsonFileAttributes in project dcache by dCache.
the class LabelsResources method getFileAttributes.
@GET
@ApiOperation(value = "Find metadata and optionally virtual directory contents.", notes = "The method offers the possibility to list the content of a virtual" + "directory for labels.")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error") })
@Path("{path : .*}")
@Produces(MediaType.APPLICATION_JSON)
public JsonFileAttributes getFileAttributes(@ApiParam("Path of file or directory.") @PathParam("path") String requestPath, @ApiParam("Whether to include directory listing.") @DefaultValue("false") @QueryParam("children") boolean isList, @ApiParam("List virtual dir.") @DefaultValue("false") @QueryParam("locality") boolean isLocality, @ApiParam(value = "Whether to include replica locations.") @QueryParam("locations") boolean isLocations, @ApiParam(value = "Whether to include quality of service.") @DefaultValue("false") @QueryParam("qos") boolean isQos, @ApiParam("Whether to include extended attributes.") @QueryParam("xattr") boolean isXattr, @ApiParam("Whether to include labels.") @QueryParam("labels") boolean isLabels, @ApiParam("Limit number of replies in directory listing.") @QueryParam("limit") String limit, @ApiParam("Number of entries to skip in directory listing.") @QueryParam("offset") String offset) throws CacheException {
JsonFileAttributes fileAttributes = new JsonFileAttributes();
Set<FileAttribute> attributes = NamespaceUtils.getRequestedAttributes(isLocality, isLocations, false, false, false);
FsPath path = pathMapper.asDcachePath(request, requestPath, ForbiddenException::new);
Range<Integer> range;
try {
int lower = (offset == null) ? 0 : Integer.parseInt(offset);
int ceiling = (limit == null) ? Integer.MAX_VALUE : Integer.parseInt(limit);
if (ceiling < 0 || lower < 0) {
throw new BadRequestException("limit and offset can not be less than zero.");
}
range = (Integer.MAX_VALUE - lower < ceiling) ? Range.atLeast(lower) : Range.closedOpen(lower, lower + ceiling);
} catch (NumberFormatException e) {
throw new BadRequestException("limit and offset must be an integer value.");
}
try {
List<JsonFileAttributes> children = new ArrayList<>();
DirectoryStream stream = listDirectoryHandler.listVirtualDirectory(HttpServletRequests.roleAwareSubject(request), HttpServletRequests.roleAwareRestriction(request), path, range, attributes);
for (DirectoryEntry entry : stream) {
String fPath = entry.getName();
String fName = entry.getName();
JsonFileAttributes childrenAttributes = new JsonFileAttributes();
NamespaceUtils.chimeraToJsonAttributes(fPath, childrenAttributes, entry.getFileAttributes(), isLocality, isLocations, isLabels, false, isXattr, false, request, poolMonitor);
childrenAttributes.setSourcePath(fPath);
childrenAttributes.setFileName(fName);
if (isQos) {
NamespaceUtils.addQoSAttributes(childrenAttributes, entry.getFileAttributes(), request, poolMonitor, pinmanager);
}
children.add(childrenAttributes);
}
fileAttributes.setChildren(children);
} catch (FileNotFoundCacheException e) {
throw new NotFoundException(e);
} catch (PermissionDeniedCacheException e) {
if (RequestUser.isAnonymous()) {
throw new NotAuthorizedException(e);
} else {
throw new ForbiddenException(e);
}
} catch (CacheException | InterruptedException | NoRouteToCellException ex) {
LOGGER.warn(Exceptions.meaningfulMessage(ex));
throw new InternalServerErrorException(ex);
}
return fileAttributes;
}
Aggregations