Search in sources :

Example 1 with FileOwnerAttributeView

use of java.nio.file.attribute.FileOwnerAttributeView in project nifi by apache.

the class ListFile method createAttributes.

@Override
protected Map<String, String> createAttributes(final FileInfo fileInfo, final ProcessContext context) {
    final Map<String, String> attributes = new HashMap<>();
    final String fullPath = fileInfo.getFullPathFileName();
    final File file = new File(fullPath);
    final Path filePath = file.toPath();
    final Path directoryPath = new File(getPath(context)).toPath();
    final Path relativePath = directoryPath.toAbsolutePath().relativize(filePath.getParent());
    String relativePathString = relativePath.toString();
    relativePathString = relativePathString.isEmpty() ? "." + File.separator : relativePathString + File.separator;
    final Path absPath = filePath.toAbsolutePath();
    final String absPathString = absPath.getParent().toString() + File.separator;
    attributes.put(CoreAttributes.PATH.key(), relativePathString);
    attributes.put(CoreAttributes.FILENAME.key(), fileInfo.getFileName());
    attributes.put(CoreAttributes.ABSOLUTE_PATH.key(), absPathString);
    try {
        FileStore store = Files.getFileStore(filePath);
        if (store.supportsFileAttributeView("basic")) {
            try {
                final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
                BasicFileAttributeView view = Files.getFileAttributeView(filePath, BasicFileAttributeView.class);
                BasicFileAttributes attrs = view.readAttributes();
                attributes.put(FILE_SIZE_ATTRIBUTE, Long.toString(attrs.size()));
                attributes.put(FILE_LAST_MODIFY_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastModifiedTime().toMillis())));
                attributes.put(FILE_CREATION_TIME_ATTRIBUTE, formatter.format(new Date(attrs.creationTime().toMillis())));
                attributes.put(FILE_LAST_ACCESS_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastAccessTime().toMillis())));
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
        if (store.supportsFileAttributeView("owner")) {
            try {
                FileOwnerAttributeView view = Files.getFileAttributeView(filePath, FileOwnerAttributeView.class);
                attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName());
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
        if (store.supportsFileAttributeView("posix")) {
            try {
                PosixFileAttributeView view = Files.getFileAttributeView(filePath, PosixFileAttributeView.class);
                attributes.put(FILE_PERMISSIONS_ATTRIBUTE, PosixFilePermissions.toString(view.readAttributes().permissions()));
                attributes.put(FILE_GROUP_ATTRIBUTE, view.readAttributes().group().getName());
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
    } catch (IOException ioe) {
        // well then this FlowFile gets none of these attributes
        getLogger().warn("Error collecting attributes for file {}, message is {}", new Object[] { absPathString, ioe.getMessage() });
    }
    return attributes;
}
Also used : Path(java.nio.file.Path) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileStore(java.nio.file.FileStore) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with FileOwnerAttributeView

use of java.nio.file.attribute.FileOwnerAttributeView in project nifi by apache.

the class GetFile method getAttributesFromFile.

protected Map<String, String> getAttributesFromFile(final Path file) {
    Map<String, String> attributes = new HashMap<>();
    try {
        FileStore store = Files.getFileStore(file);
        if (store.supportsFileAttributeView("basic")) {
            try {
                final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
                BasicFileAttributeView view = Files.getFileAttributeView(file, BasicFileAttributeView.class);
                BasicFileAttributes attrs = view.readAttributes();
                attributes.put(FILE_LAST_MODIFY_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastModifiedTime().toMillis())));
                attributes.put(FILE_CREATION_TIME_ATTRIBUTE, formatter.format(new Date(attrs.creationTime().toMillis())));
                attributes.put(FILE_LAST_ACCESS_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastAccessTime().toMillis())));
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
        if (store.supportsFileAttributeView("owner")) {
            try {
                FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
                attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName());
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
        if (store.supportsFileAttributeView("posix")) {
            try {
                PosixFileAttributeView view = Files.getFileAttributeView(file, PosixFileAttributeView.class);
                attributes.put(FILE_PERMISSIONS_ATTRIBUTE, PosixFilePermissions.toString(view.readAttributes().permissions()));
                attributes.put(FILE_GROUP_ATTRIBUTE, view.readAttributes().group().getName());
            } catch (Exception ignore) {
            }
        // allow other attributes if these fail
        }
    } catch (IOException ioe) {
    // well then this FlowFile gets none of these attributes
    }
    return attributes;
}
Also used : FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileStore(java.nio.file.FileStore) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 3 with FileOwnerAttributeView

use of java.nio.file.attribute.FileOwnerAttributeView in project cryptofs by cryptomator.

the class CryptoFileSystemImpl method copyAttributes.

private void copyAttributes(Path src, Path dst) throws IOException {
    Set<Class<? extends FileAttributeView>> supportedAttributeViewTypes = fileStore.supportedFileAttributeViewTypes();
    if (supportedAttributeViewTypes.contains(BasicFileAttributeView.class)) {
        BasicFileAttributes srcAttrs = Files.readAttributes(src, BasicFileAttributes.class);
        BasicFileAttributeView dstAttrView = Files.getFileAttributeView(dst, BasicFileAttributeView.class);
        dstAttrView.setTimes(srcAttrs.lastModifiedTime(), srcAttrs.lastAccessTime(), srcAttrs.creationTime());
    }
    if (supportedAttributeViewTypes.contains(FileOwnerAttributeView.class)) {
        FileOwnerAttributeView srcAttrView = Files.getFileAttributeView(src, FileOwnerAttributeView.class);
        FileOwnerAttributeView dstAttrView = Files.getFileAttributeView(dst, FileOwnerAttributeView.class);
        dstAttrView.setOwner(srcAttrView.getOwner());
    }
    if (supportedAttributeViewTypes.contains(PosixFileAttributeView.class)) {
        PosixFileAttributes srcAttrs = Files.readAttributes(src, PosixFileAttributes.class);
        PosixFileAttributeView dstAttrView = Files.getFileAttributeView(dst, PosixFileAttributeView.class);
        dstAttrView.setGroup(srcAttrs.group());
        dstAttrView.setPermissions(srcAttrs.permissions());
    }
    if (supportedAttributeViewTypes.contains(DosFileAttributeView.class)) {
        DosFileAttributes srcAttrs = Files.readAttributes(src, DosFileAttributes.class);
        DosFileAttributeView dstAttrView = Files.getFileAttributeView(dst, DosFileAttributeView.class);
        dstAttrView.setArchive(srcAttrs.isArchive());
        dstAttrView.setHidden(srcAttrs.isHidden());
        dstAttrView.setReadOnly(srcAttrs.isReadOnly());
        dstAttrView.setSystem(srcAttrs.isSystem());
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileAttributeView(java.nio.file.attribute.FileAttributeView) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 4 with FileOwnerAttributeView

use of java.nio.file.attribute.FileOwnerAttributeView in project fess-crawler by codelibs.

the class FileSystemClient method getResponseData.

protected ResponseData getResponseData(final String uri, final boolean includeContent) {
    final ResponseData responseData = new ResponseData();
    try {
        responseData.setMethod(Constants.GET_METHOD);
        final String filePath = preprocessUri(uri);
        responseData.setUrl(filePath);
        File file = null;
        try {
            file = new File(new URI(filePath));
        } catch (final URISyntaxException e) {
            logger.warn("Could not parse url: " + filePath, e);
        }
        if (file == null) {
            responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
            responseData.setCharSet(charset);
            responseData.setContentLength(0);
        } else if (file.isFile()) {
            // check file size
            responseData.setContentLength(file.length());
            checkMaxContentLength(responseData);
            try {
                final FileOwnerAttributeView ownerAttrView = Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class);
                if (ownerAttrView != null) {
                    UserPrincipal owner = ownerAttrView.getOwner();
                    if (owner != null) {
                        responseData.addMetaData(FS_FILE_USER, owner.getName());
                    }
                }
            } catch (Exception e) {
                logger.warn("Failed to parse FileOwnerAttributeView.", e);
            }
            try {
                final AclFileAttributeView aclView = Files.getFileAttributeView(file.toPath(), AclFileAttributeView.class);
                if (aclView != null) {
                    responseData.addMetaData(FILE_ATTRIBUTE_VIEW, aclView);
                    responseData.addMetaData(FS_FILE_GROUPS, aclView.getAcl().stream().map(acl -> acl.principal().getName()).toArray(n -> new String[n]));
                }
            } catch (Exception e) {
                logger.warn("Failed to parse AclFileAttributeView.", e);
            }
            try {
                final PosixFileAttributeView posixView = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
                if (posixView != null) {
                    responseData.addMetaData(FILE_ATTRIBUTE_VIEW, posixView);
                    responseData.addMetaData(FS_FILE_GROUPS, new String[] { posixView.readAttributes().group().getName() });
                }
            } catch (Exception e) {
                logger.warn("Failed to parse PosixFileAttributeView.", e);
            }
            responseData.setHttpStatusCode(Constants.OK_STATUS_CODE);
            responseData.setCharSet(geCharSet(file));
            responseData.setLastModified(new Date(file.lastModified()));
            if (file.canRead()) {
                final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper");
                try (final InputStream is = new BufferedInputStream(new FileInputStream(file))) {
                    responseData.setMimeType(mimeTypeHelper.getContentType(is, file.getName()));
                } catch (final Exception e) {
                    responseData.setMimeType(mimeTypeHelper.getContentType(null, file.getName()));
                }
                if (contentLengthHelper != null) {
                    final long maxLength = contentLengthHelper.getMaxLength(responseData.getMimeType());
                    if (responseData.getContentLength() > maxLength) {
                        throw new MaxLengthExceededException("The content length (" + responseData.getContentLength() + " byte) is over " + maxLength + " byte. The url is " + filePath);
                    }
                }
                if (includeContent) {
                    if (file.length() < maxCachedContentSize) {
                        try (InputStream contentStream = new BufferedInputStream(new FileInputStream(file))) {
                            responseData.setResponseBody(InputStreamUtil.getBytes(contentStream));
                        } catch (final Exception e) {
                            logger.warn("I/O Exception.", e);
                            responseData.setHttpStatusCode(Constants.SERVER_ERROR_STATUS_CODE);
                        }
                    } else {
                        responseData.setResponseBody(file, false);
                    }
                }
            } else {
                // Forbidden
                responseData.setHttpStatusCode(Constants.FORBIDDEN_STATUS_CODE);
                responseData.setMimeType(APPLICATION_OCTET_STREAM);
            }
        } else if (file.isDirectory()) {
            final Set<RequestData> requestDataSet = new HashSet<>();
            if (includeContent) {
                final File[] files = file.listFiles();
                if (files != null) {
                    for (final File f : files) {
                        final String chileUri = f.toURI().toASCIIString();
                        requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
                    }
                }
            }
            throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData");
        } else {
            responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
            responseData.setCharSet(charset);
            responseData.setContentLength(0);
        }
    } catch (final CrawlerSystemException e) {
        CloseableUtil.closeQuietly(responseData);
        throw e;
    } catch (final Exception e) {
        CloseableUtil.closeQuietly(responseData);
        throw new CrawlingAccessException("Could not access " + uri, e);
    }
    return responseData;
}
Also used : FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) BufferedInputStream(java.io.BufferedInputStream) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) AbstractCrawlerClient(org.codelibs.fess.crawler.client.AbstractCrawlerClient) HashSet(java.util.HashSet) UserPrincipal(java.nio.file.attribute.UserPrincipal) URI(java.net.URI) ContentLengthHelper(org.codelibs.fess.crawler.helper.ContentLengthHelper) MimeTypeHelper(org.codelibs.fess.crawler.helper.MimeTypeHelper) InputStreamUtil(org.codelibs.core.io.InputStreamUtil) AclFileAttributeView(java.nio.file.attribute.AclFileAttributeView) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) Set(java.util.Set) FileInputStream(java.io.FileInputStream) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) CrawlerContainer(org.codelibs.fess.crawler.container.CrawlerContainer) File(java.io.File) CloseableUtil(org.codelibs.core.io.CloseableUtil) Constants(org.codelibs.fess.crawler.Constants) URLEncoder(java.net.URLEncoder) RequestData(org.codelibs.fess.crawler.entity.RequestData) AccessTimeoutTarget(org.codelibs.fess.crawler.client.AccessTimeoutTarget) TimeoutManager(org.codelibs.core.timer.TimeoutManager) TimeoutTask(org.codelibs.core.timer.TimeoutTask) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestDataBuilder(org.codelibs.fess.crawler.builder.RequestDataBuilder) InputStream(java.io.InputStream) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) AclFileAttributeView(java.nio.file.attribute.AclFileAttributeView) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) MimeTypeHelper(org.codelibs.fess.crawler.helper.MimeTypeHelper) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UserPrincipal(java.nio.file.attribute.UserPrincipal) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) URISyntaxException(java.net.URISyntaxException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BufferedInputStream(java.io.BufferedInputStream) RequestData(org.codelibs.fess.crawler.entity.RequestData) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with FileOwnerAttributeView

use of java.nio.file.attribute.FileOwnerAttributeView in project jimfs by google.

the class OwnerAttributeProviderTest method testView.

@Test
public void testView() throws IOException {
    FileOwnerAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
    assertThat(view).isNotNull();
    assertThat(view.name()).isEqualTo("owner");
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));
    view.setOwner(createUserPrincipal("root"));
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
    assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
}
Also used : FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) Test(org.junit.Test)

Aggregations

FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)11 File (java.io.File)4 Path (java.nio.file.Path)4 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)4 UserPrincipal (java.nio.file.attribute.UserPrincipal)4 HashMap (java.util.HashMap)4 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 Date (java.util.Date)3 IOException (java.io.IOException)2 FileStore (java.nio.file.FileStore)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1