Example usage for java.nio.file Files getOwner

List of usage examples for java.nio.file Files getOwner

Introduction

In this page you can find the example usage for java.nio.file Files getOwner.

Prototype

public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException 

Source Link

Document

Returns the owner of a file.

Usage

From source file:VOBackupFile.java

/**
 * Wrapper for metadata of backuped file.
 *
 * @param file backuped file//w w  w .jav a 2s  . co  m
 */
public VOBackupFile(File file) {
    this.path = file.getAbsolutePath();
    this.directory = file.isDirectory();
    this.symLink = Files.isSymbolicLink(file.toPath());

    this.size = ((file.isDirectory() || symLink) ? 0 : file.length());
    this.modify = new Date(file.lastModified());

    if (symLink) {
        try {
            symlinkTarget = Files.readSymbolicLink(file.toPath()).toString();
        } catch (IOException e) {
            e.printStackTrace(); //TODO Lebeda - oetit do logu
        }
    } else {
        // advanced attributes

        try {

            owner = Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS).getName();

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(DosFileAttributeView.class)) {
                dosAttr = Boolean.TRUE;
                final DosFileAttributes dosFileAttributes = Files.readAttributes(file.toPath(),
                        DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

                dosArchive = dosFileAttributes.isArchive();
                dosHidden = dosFileAttributes.isHidden();
                dosSystem = dosFileAttributes.isSystem();
                dosReadOnly = dosFileAttributes.isReadOnly();
            } else {
                dosAttr = Boolean.FALSE;
            }

            if (Files.getFileStore(file.toPath()).supportsFileAttributeView(PosixFileAttributeView.class)) {
                posixAttr = Boolean.TRUE;
                final PosixFileAttributes posixFileAttributes = Files.readAttributes(file.toPath(),
                        PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
                posixGroup = posixFileAttributes.group().getName();
                posixPermitions = PosixFilePermissions.toString(posixFileAttributes.permissions());
            } else {
                posixAttr = Boolean.FALSE;
            }

        } catch (IOException e) {
            e.printStackTrace(); //Todo implementovat
        }
    }

}

From source file:com.twosigma.beaker.core.rest.FileIORest.java

@GET
@Path("getPosixFileOwnerAndPermissions")
@Produces(MediaType.APPLICATION_JSON)/*from   w  w w  . j  a  v a  2s. c o  m*/
public Response getPosixFileOwnerAndPermissions(@QueryParam("path") String pathString) throws IOException {
    pathString = removePrefix(pathString);
    java.nio.file.Path path = Paths.get(pathString);
    if (Files.exists(path)) {
        Map<String, Object> response = new HashMap<>();
        response.put("permissions", Files.getPosixFilePermissions(path));
        response.put("owner", Files.getOwner(path, LinkOption.NOFOLLOW_LINKS).getName());
        response.put("group", Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS)
                .group().getName());
        return status(OK).entity(response).build();
    } else {
        return status(BAD_REQUEST).build();
    }
}

From source file:algorithm.MetsSubmissionInformationPackage.java

@SuppressWarnings("unchecked")
@Override//from ww  w .  j a  va 2s  .  c  o  m
public File encapsulate(File carrier, List<File> userPayloadList) throws IOException {
    List<File> payloadList = new ArrayList<File>();
    payloadList.addAll(userPayloadList);
    Mets metsDocument = new Mets();
    metsDocument.setID(carrier.getName() + "_SIP");
    metsDocument.setLABEL("PeriCAT information package");
    metsDocument.setTYPE("digital object and metadata files");
    // HEADER:
    Agent periPack = new Agent();
    periPack.setID("peripack agent");
    periPack.setROLE(Role.PRESERVATION);
    periPack.setOTHERTYPE("SOFTWARE");
    Name name = new Name();
    PCData nameString = new PCData();
    nameString.add("peripack");
    name.getContent().add(nameString);
    periPack.getContent().add(name);
    Note note = new Note();
    PCData noteString = new PCData();
    noteString.add(TOOL_DESCRIPTION);
    note.getContent().add(noteString);
    periPack.getContent().add(note);
    MetsHdr header = new MetsHdr();
    header.setID("mets header");
    header.setCREATEDATE(new Date());
    header.setLASTMODDATE(new Date());
    header.setRECORDSTATUS("complete");
    header.getContent().add(periPack);
    if (truePersonButton.isSelected()) {
        Agent person = new Agent();
        person.setID("peripack user");
        person.setROLE(Role.CREATOR);
        person.setTYPE(Type.INDIVIDUAL);
        Name personName = new Name();
        PCData personNameString = new PCData();
        personNameString.add(personField.getText());
        personName.getContent().add(personNameString);
        person.getContent().add(personName);
        header.getContent().add(person);
    }
    metsDocument.getContent().add(header);
    // FILE SECTION:
    FileSec fileSection = new FileSec();
    FileGrp carrierGroup = new FileGrp();
    carrierGroup.setID("carrier files");
    carrierGroup.setUSE("digital object");
    // carrier div for structural map:
    Div carrierDiv = new Div();
    carrierDiv.setID("carrier list");
    carrierDiv.setTYPE("carrier files");
    // back to file section:
    edu.harvard.hul.ois.mets.File metsCarrier = new edu.harvard.hul.ois.mets.File();
    metsCarrier.setID(carrier.getAbsolutePath());
    metsCarrier.setGROUPID("carrier");
    metsCarrier.setCHECKSUM("" + FileUtils.checksumCRC32(carrier));
    metsCarrier.setMIMETYPE(Files.probeContentType(carrier.toPath()));
    metsCarrier.setOWNERID(Files.getOwner(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    metsCarrier.setSIZE(Files.size(carrier.toPath()));
    metsCarrier.setUSE(Files.getPosixFilePermissions(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    FLocat fileLocation = new FLocat();
    fileLocation.setXlinkHref(carrier.getAbsolutePath());
    fileLocation.setLOCTYPE(Loctype.OTHER);
    fileLocation.setOTHERLOCTYPE("system file path");
    fileLocation.setXlinkTitle("original file path of the carrier");
    metsCarrier.getContent().add(fileLocation);
    carrierGroup.getContent().add(metsCarrier);
    // add structural map information:
    Fptr carrierFilePointer = new Fptr();
    carrierFilePointer.setFILEID(carrier.getAbsolutePath());
    carrierDiv.getContent().add(carrierFilePointer);
    fileSection.getContent().add(carrierGroup);
    FileGrp payloadGroup = new FileGrp();
    payloadGroup.setID("payload files");
    payloadGroup.setUSE("metadata");

    // payload div for structural map:
    Div payloadDiv = new Div();
    payloadDiv.setID("payload list");
    payloadDiv.setTYPE("payload files");
    // back to file section:
    for (File payload : payloadList) {
        edu.harvard.hul.ois.mets.File metsPayload = new edu.harvard.hul.ois.mets.File();
        metsPayload.setID(payload.getAbsolutePath());
        metsPayload.setGROUPID("payload");
        metsPayload.setCHECKSUM(DigestUtils.md5Hex(new FileInputStream(payload)));
        metsPayload.setCHECKSUMTYPE(Checksumtype.MD5);
        metsPayload.setMIMETYPE(Files.probeContentType(payload.toPath()));
        metsPayload.setOWNERID(Files.getOwner(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        metsPayload.setSIZE(Files.size(payload.toPath()));
        metsPayload
                .setUSE(Files.getPosixFilePermissions(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        FLocat fileLocation2 = new FLocat();
        fileLocation2.setXlinkHref(payload.getAbsolutePath());
        fileLocation2.setLOCTYPE(Loctype.OTHER);
        fileLocation2.setOTHERLOCTYPE("system file path");
        fileLocation2.setXlinkTitle("original file path of the payload");
        metsPayload.getContent().add(fileLocation2);
        payloadGroup.getContent().add(metsPayload);
        // add structural map information:
        Fptr payloadFilePointer = new Fptr();
        payloadFilePointer.setFILEID(payload.getAbsolutePath());
        payloadDiv.getContent().add(payloadFilePointer);
    }
    fileSection.getContent().add(payloadGroup);
    metsDocument.getContent().add(fileSection);
    // STRUCTURAL MAP:
    StructMap structuralMap = new StructMap();
    structuralMap.setID("structural map");
    Div encapsulatedFiles = new Div();
    encapsulatedFiles.setID("peripack files");
    encapsulatedFiles.setTYPE("encapsulated files");
    structuralMap.getContent().add(encapsulatedFiles);
    encapsulatedFiles.getContent().add(carrierDiv);
    encapsulatedFiles.getContent().add(payloadDiv);
    metsDocument.getContent().add(structuralMap);
    File metsFile = new File(OUTPUT_DIRECTORY + "mets.xml");
    FileOutputStream outputStream = new FileOutputStream(metsFile);
    try {
        metsDocument.write(new MetsWriter(outputStream));
    } catch (MetsException e) {
    }
    outputStream.close();
    payloadList.add(metsFile);
    File outputFile = null;
    if (zipButton.isSelected()) {
        outputFile = new ZipPackaging().encapsulate(carrier, payloadList);

    } else if (tarButton.isSelected()) {
        outputFile = new TarPackaging().encapsulate(carrier, payloadList);
    }
    metsFile.delete();
    return outputFile;
}