Search in sources :

Example 1 with FileApiResource

use of com.bluenimble.platform.server.impls.fs.FileApiResource in project serverless by bluenimble.

the class DefaultApiResourcesManager method put.

@Override
public ApiResource put(String[] path, InputStream payload, boolean overwrite) throws ApiResourcesManagerException {
    if (path == null || path.length == 0) {
        throw new ApiResourcesManagerException("invalid or null resource path");
    }
    if (path.length == 1 && Reserved.contains(path[0])) {
        throw new ApiResourcesManagerException("a protected resource called " + path[0] + " already exists");
    }
    String sPath = checkAndGetPath(path);
    if (Lang.isNullOrEmpty(sPath)) {
        throw new ApiResourcesManagerException("invalid resource path " + Lang.join(path, Lang.SLASH));
    }
    File parent = null;
    String[] aParent = Lang.moveRight(path, 1);
    if (aParent == null) {
        parent = resources;
    } else {
        parent = new File(resources, Lang.join(aParent));
    }
    File file = new File(parent, path[path.length - 1]);
    if (file.exists()) {
        if (file.isFile()) {
            if (!overwrite) {
                throw new ApiResourcesManagerException("resource " + sPath + " already exists");
            }
        } else {
            throw new ApiResourcesManagerException("resource " + sPath + " already exists");
        }
    }
    if (parent.exists()) {
        if (parent.isFile()) {
            throw new ApiResourcesManagerException("parent resource " + Lang.join(aParent, Lang.SLASH) + " is a file. It should be a valid folder");
        }
    } else {
        parent.mkdirs();
    }
    FileApiResource resource = new FileApiResource(owner, resources, file);
    if (payload == null) {
        file.mkdir();
    } else {
        try {
            resource.pipe(payload, 0, -1);
        } catch (IOException e) {
            throw new ApiResourcesManagerException(e.getMessage(), e);
        }
    }
    return new FileApiResource(owner, resources, file);
}
Also used : FileApiResource(com.bluenimble.platform.server.impls.fs.FileApiResource) IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) File(java.io.File)

Aggregations

ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)1 FileApiResource (com.bluenimble.platform.server.impls.fs.FileApiResource)1 File (java.io.File)1 IOException (java.io.IOException)1