Example usage for java.nio.file AccessDeniedException getMessage

List of usage examples for java.nio.file AccessDeniedException getMessage

Introduction

In this page you can find the example usage for java.nio.file AccessDeniedException getMessage.

Prototype

@Override
public String getMessage() 

Source Link

Document

Returns the detail message string.

Usage

From source file:com.liferay.sync.engine.document.library.handler.GetSyncDLObjectUpdateHandler.java

protected void copyFile(SyncFile sourceSyncFile, SyncFile targetSyncFile) throws Exception {

    if (_logger.isDebugEnabled()) {
        _logger.debug("Copying file {} to {}", sourceSyncFile.getFilePathName(),
                targetSyncFile.getFilePathName());
    }//from   w  w w. j av a2s .co  m

    Path tempFilePath = FileUtil.getTempFilePath(targetSyncFile);

    Files.copy(Paths.get(sourceSyncFile.getFilePathName()), tempFilePath, StandardCopyOption.REPLACE_EXISTING);

    FileKeyUtil.writeFileKey(tempFilePath, String.valueOf(targetSyncFile.getSyncFileId()), false);

    FileUtil.setModifiedTime(tempFilePath, targetSyncFile.getModifiedTime());

    Watcher watcher = WatcherManager.getWatcher(getSyncAccountId());

    watcher.addDownloadedFilePathName(targetSyncFile.getFilePathName());

    boolean exists = FileUtil.exists(Paths.get(targetSyncFile.getFilePathName()));

    try {
        Files.move(tempFilePath, Paths.get(targetSyncFile.getFilePathName()), StandardCopyOption.ATOMIC_MOVE,
                StandardCopyOption.REPLACE_EXISTING);
    } catch (AccessDeniedException ade) {
        _logger.error(ade.getMessage(), ade);

        targetSyncFile.setState(SyncFile.STATE_ERROR);
        targetSyncFile.setUiEvent(SyncFile.UI_EVENT_ACCESS_DENIED_LOCAL);

        SyncFileService.update(targetSyncFile);

        return;
    }

    targetSyncFile.setState(SyncFile.STATE_SYNCED);

    if (GetterUtil.getBoolean(targetSyncFile.getLocalExtraSettingValue("restoreEvent"))) {

        targetSyncFile.unsetLocalExtraSetting("restoreEvent");

        targetSyncFile.setUiEvent(SyncFile.UI_EVENT_RESTORED_REMOTE);
    } else if (exists) {
        targetSyncFile.setUiEvent(SyncFile.UI_EVENT_DOWNLOADED_UPDATE);
    } else {
        targetSyncFile.setUiEvent(SyncFile.UI_EVENT_DOWNLOADED_NEW);
    }

    SyncFileService.update(targetSyncFile);

    IODeltaUtil.copyChecksums(sourceSyncFile, targetSyncFile);
}

From source file:org.kitodo.filemanagement.locking.LockManagement.java

/**
 * Checks if a user is allowed to open a read channel on the file. If the
 * user is authorized to read the file, the method silently returns and
 * nothing happens. If not, an {@code AccessDeniedException} is thrown with
 * a meaningful justification. However, if the user code previously
 * submitted a request for appropriate permissions and verified that the
 * answer was positive, then this case should never happen.
 *
 * <p>//from  w w  w. j  av  a 2  s . c  o  m
 * In case the user gets read access due to an immutable read lock, this
 * method returns the URI to the temporary copy of the main file. The
 * subsequent file access process must therefore open the read channel to
 * this temporary copy, not to the main file.
 *
 * @param lockingResult
 *            which user requests the write channel
 * @param uri
 *            for which file the channel is requested
 * @param write
 *            if true, also check for permission to write, else read only
 * @return the URI to use. This is the same one that was passed, except for
 *         the immutable read lock, where it is the URI of the immutable
 *         read copy.
 * @throws AccessDeniedException
 *             if the user does not have sufficient authorization
 * @throws ProtocolException
 *             if the file had to be first read in again, but this step was
 *             skipped on the protocol. This error can occur with the
 *             UPGRADE_WRITE_ONCE lock because its protocol form requires
 *             that the file must first be read in again and the input
 *             stream must be closed after the lock has been upgraded.
 */
public URI checkPermission(LockResult lockingResult, URI uri, boolean write)
        throws AccessDeniedException, ProtocolException {
    GrantedAccess permissions = tryCast(lockingResult);
    logger.trace("{} wants to open a {} channel to {}.", permissions.getUser(), write ? "writing" : "reading",
            uri);
    try {
        GrantedPermissions granted = grantedPermissions.get(uri);
        if (Objects.isNull(granted)) {
            throw new AccessDeniedException(permissions.getUser() + " claims to have a privilege to " + uri
                    + " he does not have at all. Whatever he did, it was wrong.");
        }
        granted.checkAuthorization(permissions.getUser(), permissions.getLock(uri), write);
    } catch (AccessDeniedException e) {
        logger.trace("{} is not allowed to open a {} channel to {}. The reason is: {}", permissions.getUser(),
                write ? "writing" : "reading", uri, e.getMessage());
        throw e;
    }
    AbstractLock lock = permissions.getLock(uri);
    if (lock instanceof ImmutableReadLock) {
        uri = ((ImmutableReadLock) lock).getImmutableReadCopyURI();
    }
    logger.trace("{} is allowed to open a {} channel to {}.", permissions.getUser(),
            write ? "writing" : "reading", uri);
    return uri;
}

From source file:org.wso2.carbon.siddhi.editor.core.internal.EditorMicroservice.java

@POST
@Path("/workspace/export")
@Produces("application/json")
public Response export(String payload) {
    try {/*from  www.j  av a 2 s . co m*/
        String location = "";
        String configName = "";
        String config = "";
        Matcher locationMatcher = Pattern.compile("location=(.*?)&configName").matcher(payload);
        while (locationMatcher.find()) {
            location = locationMatcher.group(1);
        }
        Matcher configNameMatcher = Pattern.compile("configName=(.*?)&").matcher(payload);
        while (configNameMatcher.find()) {
            configName = configNameMatcher.group(1);
        }
        String[] splitConfigContent = payload.split("config=");
        if (splitConfigContent.length > 1) {
            config = splitConfigContent[1];
        }
        byte[] base64Config = Base64.getDecoder().decode(config);
        byte[] base64ConfigName = Base64.getDecoder().decode(configName);
        byte[] base64Location = Base64.getDecoder().decode(location);
        Files.write(Paths.get(new String(base64Location, Charset.defaultCharset())
                + System.getProperty(FILE_SEPARATOR) + new String(base64ConfigName, Charset.defaultCharset())),
                base64Config);
        JsonObject entity = new JsonObject();
        entity.addProperty(STATUS, SUCCESS);
        return Response.status(Response.Status.OK).entity(entity).type(MediaType.APPLICATION_JSON).build();
    } catch (AccessDeniedException e) {
        Map<String, String> errorMap = new HashMap<>(1);
        errorMap.put("Error", "File access denied. You don't have enough permission to access");
        return Response.serverError().entity(errorMap).build();
    } catch (IOException e) {
        return Response.serverError().entity("failed." + e.getMessage()).build();
    } catch (Throwable ignored) {
        return Response.serverError().entity("failed").build();
    }
}

From source file:org.wso2.carbon.siddhi.editor.core.internal.EditorMicroservice.java

@POST
@Path("/workspace/import")
@Produces("application/json")
public Response importFile(String path) {
    try {/*w w w  .  j a  va  2  s.com*/
        JsonObject content = workspace.read(Paths.get(path));
        String location = (Paths.get(Constants.RUNTIME_PATH, Constants.DIRECTORY_DEPLOYMENT,
                Constants.DIRECTORY_WORKSPACE)).toString();
        String configName = path.substring(path.lastIndexOf(System.getProperty(FILE_SEPARATOR)) + 1);
        String config = content.get("content").getAsString();
        StringBuilder pathBuilder = new StringBuilder();
        pathBuilder.append(location).append(System.getProperty(FILE_SEPARATOR)).append(configName);
        Files.write(Paths.get(pathBuilder.toString()), config.getBytes(Charset.defaultCharset()));
        return Response.status(Response.Status.OK).entity(content).type(MediaType.APPLICATION_JSON).build();
    } catch (AccessDeniedException e) {
        Map<String, String> errorMap = new HashMap<>(1);
        errorMap.put("Error", "File access denied. You don't have enough permission to access");
        return Response.serverError().entity(errorMap).build();
    } catch (IOException e) {
        return Response.serverError().entity("failed." + e.getMessage()).build();
    } catch (Throwable ignored) {
        return Response.serverError().entity("failed").build();
    }
}