Search in sources :

Example 1 with JsonFileAttributes

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;
}
Also used : AttributeExistsCacheException(diskCacheV111.util.AttributeExistsCacheException) CacheException(diskCacheV111.util.CacheException) NoAttributeCacheException(diskCacheV111.util.NoAttributeCacheException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) ArrayList(java.util.ArrayList) DirectoryStream(org.dcache.util.list.DirectoryStream) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) JsonFileAttributes(org.dcache.restful.providers.JsonFileAttributes) JsonFileAttributes(org.dcache.restful.providers.JsonFileAttributes) FileAttributes(org.dcache.vehicles.FileAttributes) FsPath(diskCacheV111.util.FsPath) ForbiddenException(javax.ws.rs.ForbiddenException) PnfsHandler(diskCacheV111.util.PnfsHandler) DirectoryEntry(org.dcache.util.list.DirectoryEntry) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) BadRequestException(javax.ws.rs.BadRequestException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) FileAttribute(org.dcache.namespace.FileAttribute) Path(javax.ws.rs.Path) FsPath(diskCacheV111.util.FsPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with JsonFileAttributes

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;
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) CacheException(diskCacheV111.util.CacheException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) PnfsId(diskCacheV111.util.PnfsId) NotFoundException(javax.ws.rs.NotFoundException) PnfsHandler(diskCacheV111.util.PnfsHandler) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) JsonFileAttributes(org.dcache.restful.providers.JsonFileAttributes) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) BadRequestException(javax.ws.rs.BadRequestException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JsonFileAttributes(org.dcache.restful.providers.JsonFileAttributes) FileAttributes(org.dcache.vehicles.FileAttributes) FileAttribute(org.dcache.namespace.FileAttribute) FsPath(diskCacheV111.util.FsPath) FsPath(diskCacheV111.util.FsPath) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with JsonFileAttributes

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;
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) CacheException(diskCacheV111.util.CacheException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) ArrayList(java.util.ArrayList) DirectoryStream(org.dcache.util.list.DirectoryStream) NotFoundException(javax.ws.rs.NotFoundException) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) DirectoryEntry(org.dcache.util.list.DirectoryEntry) PermissionDeniedCacheException(diskCacheV111.util.PermissionDeniedCacheException) JsonFileAttributes(org.dcache.restful.providers.JsonFileAttributes) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) BadRequestException(javax.ws.rs.BadRequestException) FileNotFoundCacheException(diskCacheV111.util.FileNotFoundCacheException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) FileAttribute(org.dcache.namespace.FileAttribute) FsPath(diskCacheV111.util.FsPath) FsPath(diskCacheV111.util.FsPath) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

CacheException (diskCacheV111.util.CacheException)3 FileNotFoundCacheException (diskCacheV111.util.FileNotFoundCacheException)3 FsPath (diskCacheV111.util.FsPath)3 PermissionDeniedCacheException (diskCacheV111.util.PermissionDeniedCacheException)3 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 BadRequestException (javax.ws.rs.BadRequestException)3 ForbiddenException (javax.ws.rs.ForbiddenException)3 GET (javax.ws.rs.GET)3 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)3 NotFoundException (javax.ws.rs.NotFoundException)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 FileAttribute (org.dcache.namespace.FileAttribute)3 JsonFileAttributes (org.dcache.restful.providers.JsonFileAttributes)3 PnfsHandler (diskCacheV111.util.PnfsHandler)2 ArrayList (java.util.ArrayList)2 DirectoryEntry (org.dcache.util.list.DirectoryEntry)2