Search in sources :

Example 1 with BasicFileAttributeView

use of org.entando.entando.aps.system.services.storage.BasicFileAttributeView in project entando-core by entando.

the class LocalStorageManagerInterface method deleteResource.

public void deleteResource(Properties properties) throws ApiException, Throwable {
    String pathValue = properties.getProperty(PARAM_PATH);
    String protectedValue = properties.getProperty(PARAM_IS_PROTECTED);
    boolean isProtected = StringUtils.equalsIgnoreCase(protectedValue, "true");
    try {
        if (StringUtils.isNotBlank(pathValue))
            pathValue = URLDecoder.decode(pathValue, "UTF-8");
        String path = StringUtils.removeEnd(pathValue, "/");
        String parentFolder = FilenameUtils.getFullPathNoEndSeparator(path);
        BasicFileAttributeView parentBasicFileAttributeView = this.getStorageManager().getAttributes(parentFolder, isProtected);
        if (null == parentBasicFileAttributeView || !parentBasicFileAttributeView.isDirectory()) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid parent directory", Response.Status.CONFLICT);
        }
        if (!StorageManagerUtil.isValidDirName(parentFolder)) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid parent directory", Response.Status.CONFLICT);
        }
        BasicFileAttributeView basicFileAttributeView = this.getStorageManager().getAttributes(pathValue, isProtected);
        if (null == basicFileAttributeView) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "The file does not exists", Response.Status.CONFLICT);
        }
        String filename = StringUtils.substringAfter(pathValue, parentFolder);
        if (basicFileAttributeView.isDirectory()) {
            if (!StorageManagerUtil.isValidDirName(filename)) {
                throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid dir name", Response.Status.CONFLICT);
            }
            String recursiveDelete = properties.getProperty(PARAM_DELETE_RECURSIVE);
            boolean isRecursiveDelete = StringUtils.equalsIgnoreCase(recursiveDelete, "true");
            if (!isRecursiveDelete) {
                String[] dirContents = this.getStorageManager().list(pathValue, isProtected);
                if (null != dirContents && dirContents.length > 0) {
                    throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "The directory is not empty", Response.Status.CONFLICT);
                }
            }
            this.getStorageManager().deleteDirectory(pathValue, isProtected);
        } else {
            // it's a file
            if (!StorageManagerUtil.isValidFilename(filename)) {
                throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid filename", Response.Status.CONFLICT);
            }
            this.getStorageManager().deleteFile(pathValue, isProtected);
        }
    } catch (ApiException ae) {
        throw ae;
    } catch (Throwable t) {
        _logger.error("Error adding new storage resource", t);
        throw t;
    }
}
Also used : BasicFileAttributeView(org.entando.entando.aps.system.services.storage.BasicFileAttributeView) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 2 with BasicFileAttributeView

use of org.entando.entando.aps.system.services.storage.BasicFileAttributeView in project entando-core by entando.

the class LocalStorageManagerInterface method addResource.

public void addResource(JAXBStorageResource storageResource, Properties properties) throws ApiException, Throwable {
    try {
        boolean isProtected = storageResource.isProtectedResource();
        // validate parent directory;
        String path = StringUtils.removeEnd(storageResource.getName(), "/");
        String parentFolder = FilenameUtils.getFullPathNoEndSeparator(path);
        if (StringUtils.isNotBlank(parentFolder))
            parentFolder = URLDecoder.decode(parentFolder, "UTF-8");
        if (!StorageManagerUtil.isValidDirName(parentFolder)) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid parent directory", Response.Status.CONFLICT);
        }
        BasicFileAttributeView parentBasicFileAttributeView = this.getStorageManager().getAttributes(parentFolder, isProtected);
        if (null == parentBasicFileAttributeView || !parentBasicFileAttributeView.isDirectory()) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid parent directory", Response.Status.CONFLICT);
        }
        // validate exists
        BasicFileAttributeView basicFileAttributeView2 = this.getStorageManager().getAttributes(path, isProtected);
        if (null != basicFileAttributeView2) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "File already present", Response.Status.CONFLICT);
        }
        String filename = StringUtils.substringAfter(storageResource.getName(), parentFolder);
        if (storageResource.isDirectory()) {
            if (!StorageManagerUtil.isValidDirName(filename)) {
                throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid dir name", Response.Status.CONFLICT);
            }
            this.getStorageManager().createDirectory(storageResource.getName(), isProtected);
        } else {
            // validate file content
            if (!storageResource.isDirectory() && (null == storageResource.getBase64() || storageResource.getBase64().length == 0)) {
                throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "A file cannot be empty", Response.Status.CONFLICT);
            }
            if (!StorageManagerUtil.isValidFilename(filename)) {
                throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid filename name", Response.Status.CONFLICT);
            }
            this.getStorageManager().saveFile(storageResource.getName(), isProtected, new ByteArrayInputStream(storageResource.getBase64()));
        }
    } catch (ApiException ae) {
        throw ae;
    } catch (Throwable t) {
        _logger.error("Error adding new storage resource", t);
        throw t;
    }
}
Also used : BasicFileAttributeView(org.entando.entando.aps.system.services.storage.BasicFileAttributeView) ByteArrayInputStream(java.io.ByteArrayInputStream) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 3 with BasicFileAttributeView

use of org.entando.entando.aps.system.services.storage.BasicFileAttributeView in project entando-core by entando.

the class LocalStorageManagerInterface method getApiResourceUrl.

@Override
public String getApiResourceUrl(Object object, String applicationBaseUrl, String langCode, MediaType mediaType) {
    if (!(object instanceof BasicFileAttributeView) || null == applicationBaseUrl || null == langCode) {
        return null;
    }
    BasicFileAttributeView basicFileAttributeView = (BasicFileAttributeView) object;
    StringBuilder stringBuilder = new StringBuilder(applicationBaseUrl);
    stringBuilder.append("api/rs/").append(langCode).append("/core/");
    if (basicFileAttributeView.isDirectory()) {
        stringBuilder.append("storage");
    } else {
        stringBuilder.append("storageResource");
    }
    if (null == mediaType || mediaType.equals(MediaType.APPLICATION_XML_TYPE)) {
        stringBuilder.append(".xml");
    } else {
        stringBuilder.append(".json");
    }
    return stringBuilder.toString();
}
Also used : BasicFileAttributeView(org.entando.entando.aps.system.services.storage.BasicFileAttributeView)

Example 4 with BasicFileAttributeView

use of org.entando.entando.aps.system.services.storage.BasicFileAttributeView in project entando-core by entando.

the class LocalStorageManagerInterface method getFile.

public JAXBBasicFileAttributeView getFile(Properties properties) throws ApiException, Throwable {
    JAXBBasicFileAttributeView apiResult;
    String pathValue = properties.getProperty(PARAM_PATH);
    String protectedValue = properties.getProperty(PARAM_IS_PROTECTED);
    boolean isProtected = StringUtils.equalsIgnoreCase(protectedValue, "true");
    try {
        if (StringUtils.isNotBlank(pathValue))
            pathValue = URLDecoder.decode(pathValue, "UTF-8");
        BasicFileAttributeView result = this.getStorageManager().getAttributes(pathValue, isProtected);
        if (null == result) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "The path '" + pathValue + "' does not exists", Response.Status.CONFLICT);
        }
        InputStream fis = null;
        if (!result.isDirectory()) {
            fis = this.getStorageManager().getStream(pathValue, isProtected);
        }
        apiResult = new JAXBBasicFileAttributeView(result, fis);
    } catch (ApiException ae) {
        throw ae;
    } catch (Throwable t) {
        _logger.error("Error extracting storage resource in path: {} and protected flag: {} ", pathValue, isProtected, t);
        throw t;
    }
    return apiResult;
}
Also used : BasicFileAttributeView(org.entando.entando.aps.system.services.storage.BasicFileAttributeView) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 5 with BasicFileAttributeView

use of org.entando.entando.aps.system.services.storage.BasicFileAttributeView in project entando-core by entando.

the class TestFileBrowserAction method testBrowseFileSystem_3.

public void testBrowseFileSystem_3() throws Throwable {
    String result = this.executeList("admin", "conf/", false);
    assertEquals(Action.SUCCESS, result);
    FileBrowserAction action = (FileBrowserAction) super.getAction();
    BasicFileAttributeView[] fileAttributes = action.getFilesAttributes();
    assertEquals(3, fileAttributes.length);
    int dirCounter = 0;
    int fileCounter = 0;
    for (int i = 0; i < fileAttributes.length; i++) {
        BasicFileAttributeView bfav = fileAttributes[i];
        if (bfav.isDirectory()) {
            dirCounter++;
        } else {
            fileCounter++;
        }
    }
    assertEquals(0, dirCounter);
    assertEquals(3, fileCounter);
}
Also used : BasicFileAttributeView(org.entando.entando.aps.system.services.storage.BasicFileAttributeView)

Aggregations

BasicFileAttributeView (org.entando.entando.aps.system.services.storage.BasicFileAttributeView)10 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 LinkedListItem (org.entando.entando.aps.system.services.api.model.LinkedListItem)1