Example usage for org.springframework.util Assert hasLength

List of usage examples for org.springframework.util Assert hasLength

Introduction

In this page you can find the example usage for org.springframework.util Assert hasLength.

Prototype

public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) 

Source Link

Document

Assert that the given String is not empty; that is, it must not be null and not the empty String.

Usage

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public FileInfo getFileInfo(String path) {
    Assert.hasLength(path, "Enter the file or directory.");

    boolean isDir = path.endsWith("/");

    try {/*from  ww  w  .j  a v  a  2  s .  co  m*/
        String bucket = S3Utils.getBucket(path);
        String objectKey = null;
        if (isDir) {
            objectKey = S3Utils.getObjectKey(path);
            if (path.equals("/" + S3Utils.getBucket(path) + "/")) { // Bucket
                List<Bucket> buckets = awsClient.listBuckets();
                for (Bucket b : buckets) {
                    if (b.getName().equals(bucket)) {
                        return new S3BucketInfo(b);
                    }
                }
            } else {
                S3Object object = awsClient.getObject(bucket, objectKey);
                return new S3DirectoryInfo(path, object);
            }
        }

        objectKey = "/" + S3Utils.getBucket(path) + "/";
        objectKey = StringUtils.remove(path, objectKey);
        S3Object object = awsClient.getObject(bucket, objectKey);
        return new S3ObjectInfo(object, objectKey);
    } catch (Exception ex) {
        //            throw new FileSystemException("  '" + path + "'?  ? ?    .", ex);
        throw new FileSystemException("Cannot get file information.", ex);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public InputStream getContent(String path) {
    Assert.hasLength(path, "Enter the path.");

    try {/*from www  . j  a  va  2 s.c o m*/
        String bucket = S3Utils.getBucket(path);
        String relativePath = StringUtils.remove(path, "/" + bucket + "/");

        return awsClient.getObject(bucket, relativePath).getObjectContent();
    } catch (Exception ex) {
        throw new FileSystemException(
                "The specified path can not get the file input stream file system, please check.", ex);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public boolean delete(String path) {
    Assert.hasLength(path, "Please enter the file path.");

    if (S3Utils.isDirectory(path)) {
        ObjectListing objectListing = awsClient.listObjects(new ListObjectsRequest()
                .withBucketName(S3Utils.getBucket(path)).withPrefix(S3Utils.getObjectKey(path)));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            awsClient.deleteObject(objectSummary.getBucketName(), objectSummary.getKey());
        }//from www  . ja  v a2 s.c o m
    } else {
        String bucket = S3Utils.getBucket(path);
        String relativePath = StringUtils.remove(path, "/" + bucket + "/");
        awsClient.deleteObject(bucket, relativePath);
    }
    // auditService.delete(FileSystemType.S3, username, path);
    return true;
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public boolean move(String fromPath, String toPath) {
    //        Assert.hasLength(fromPath, "?? ? ? ? ?  'from'?  .");
    Assert.hasLength(fromPath, "Please enter the source path.");
    //        Assert.hasLength(toPath, "?? ?  'to'  .");
    Assert.hasLength(toPath, "Please enter the destination path.");

    if (!exists(fromPath))
        throw new FileSystemException("Not exist file");

    if (!toPath.endsWith("/")) {
        //            throw new FileSystemException("?? ?  '" + toPath + "'? ?? '" + fromPath + "' ??  . ?? ? ? ? ?? .");
        throw new FileSystemException("Files can not be moved.");
    }//from www  . ja va  2 s. c o m
    // ??    ? ? ?? ??  .
    if (exists(toPath))
        throw new FileSystemException("Already exist a file or a directory.");
    //            throw new FileSystemException("'" + toPath + "' ? ? ?     . ?? ??  ? ?  ? ?   .");

    try {
        boolean copyResult = copy(fromPath, toPath);
        boolean deleteResult = delete(fromPath);
        /*
                    if (copyResult && deleteResult && auditService != null)
        auditService.move(FileSystemType.S3, username, fromPath, toPath);
        */
        return copyResult;
    } catch (AmazonServiceException ase) {
        throw new FileSystemException("Cannot move the file.", ase);
    } catch (AmazonClientException ace) {
        throw new FileSystemException("Cannot move the file", ace);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public List<String> move(List<String> filesPath, String to) {
    Assert.notEmpty(filesPath, "Please enter the source path.");
    Assert.hasLength(to, "Please enter the destination path.");

    List<String> notMoved = new ArrayList<String>();
    for (String file : filesPath) {
        boolean result = move(file, to);
        if (!result) {
            notMoved.add(file);/*ww  w  .j a  va2s.c om*/
        }
        if (result && auditService != null)
            ;
        // auditService.move(FileSystemType.S3, username, file, to);
    }
    return null;
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public boolean copy(String from, String to) {
    //        Assert.hasLength(from, " ?? ?  'from'?  .");
    Assert.hasLength(from, "Please enter the source path.");
    //        Assert.hasLength(to, " ?  'to'  .");
    Assert.hasLength(to, "Please enter the destination path.");

    String fromBucket = S3Utils.getBucket(from);
    String toBucket = S3Utils.getBucket(to);
    String fromKey = StringUtils.remove(from, "/" + fromBucket + "/");
    String toKey = S3Utils.getObjectKey(to);
    String fileName = getFileName(fromKey);

    try {/* w ww  . j av  a  2 s  . c  o  m*/
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(fromBucket, fromKey, toBucket,
                toKey + fileName);
        awsClient.copyObject(copyObjectRequest);
        return true;
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error " + "response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());

        //            throw new FileSystemException("??    . ? ? ? .", ase);
        throw new FileSystemException("Cannot copy the file.", ase);
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to " + " communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
        //            throw new FileSystemException("??    . ? ? ? .", ace);
        throw new FileSystemException("Cannot copy the file.", ace);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public boolean mkdir(String path) {
    //        Assert.hasLength(path, " ?  'path'  .");
    Assert.hasLength(path, "Please enter the path");

    String bucket = S3Utils.getBucket(path);
    String relativePath = S3Utils.getObjectKey(path);

    try {/* w  w  w  .jav a  2  s  . c om*/
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(0);
        InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, relativePath, emptyContent, metadata);
        awsClient.putObject(putObjectRequest);
        /*
                    auditService.mkdir(FileSystemType.S3, username, path);
        */
        return true;
    } catch (AmazonServiceException ase) {
        //            throw new FileSystemException(" ?   ?  . ? ? ? .", ase);
        throw new FileSystemException("Cannot create the directory.", ase);
    } catch (AmazonClientException ace) {
        //            throw new FileSystemException(" ?   ?  . ? ? ? .", ace);
        throw new FileSystemException("Cannot create the directory.", ace);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

@Override
public List<String> copy(List<String> files, String to) {
    //        Assert.notEmpty(files, " ? ? 'files'?  .");
    Assert.notEmpty(files, "Please enter the list of files.");
    //        Assert.hasLength(to, "??  ?  'to'?  .");
    Assert.hasLength(to, "Please enter the destination path.");

    List<String> notCopied = new ArrayList<String>();
    for (String file : files) {
        try {//from   w ww.  j a  va  2  s.com
            boolean result = copy(file, to);
            if (!result) {
                notCopied.add(file);
            }
            /*
                            if (result && auditService != null)
            auditService.copy(FileSystemType.S3, username, file, to);
            */
        } catch (Exception ex) {
            //  ?  ? ?       ? ?  ?? .
            notCopied.add(file);
        }
    }
    return notCopied;
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

public boolean save(InputStream is, long size, String path) {
    //        Assert.notNull(is, " ??   'is'?  .");
    Assert.notNull(is, "Please enter the input stream.");
    //        Assert.hasLength(path, "??  ? ??  'path'?  .");
    Assert.hasLength(path, "Please enter the path.");

    try {/*from   ww  w .  j a  va 2 s  . c o m*/
        String bucket = S3Utils.getBucket(path);
        String key = StringUtils.remove(path, "/" + bucket + "/");
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(Headers.CONTENT_LENGTH, size);
        awsClient.putObject(new PutObjectRequest(bucket, key, is, metadata));
        return true;
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error " + "response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());

        //            throw new FileSystemException("??    . ? ? ? .", ase);
        throw new FileSystemException("Connot copy the file.", ase);
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to " + " communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());

        //            throw new FileSystemException("??   . ? ? ?.", ace);
        throw new FileSystemException("Connot copy the file.", ace);
    }
}

From source file:org.openflamingo.maven.PutMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    Assert.hasLength(namenode, "'namenode' is null");
    Assert.hasLength(artifactPath, "'artifactPath' is null");
    Assert.hasLength(hdfsPath, "'hdfsPath' is null");

    getLog().info("Namenode is " + namenode);
    getLog().info(MessageFormatter.format("Copying {} to {}", hdfsPath, artifactPath).getMessage());

    if (!new File(artifactPath).exists()) {
        String message = MessageFormatter.format("'{}' doesn't exists", artifactPath).getMessage();
        getLog().error(message);/*  w  w  w  . j  a v  a 2s .c o  m*/
        throw new MojoExecutionException(message);
    }

    FileSystem fs = HdfsUtils.getFileSystem(namenode);
    if (deleteIfExists && HdfsUtils.isExist(fs, artifactPath)) {
        HdfsUtils.delete(fs, artifactPath);
        getLog().info(MessageFormatter.format("Deleted {}", artifactPath).getMessage());
    }

    if (!HdfsUtils.isExist(fs, hdfsPath)) {
        HdfsUtils.mkdir(fs, hdfsPath);
        getLog().info(MessageFormatter.format("Created {}", hdfsPath).getMessage());
    }

    FileInputStream fis = null;
    FSDataOutputStream fdos = null;
    try {
        fis = new FileInputStream(artifactPath);
        String filename = FileUtils.getFilename(artifactPath);
        fdos = fs.create(new Path(hdfsPath, filename));
        IOUtils.copy(fis, fdos);

        getLog().info(MessageFormatter.format("Copied {} to {}", hdfsPath, artifactPath).getMessage());
    } catch (Exception ex) {
        String message = MessageFormatter.format("Cannot copied {} to {}", hdfsPath, artifactPath).getMessage();
        throw new MojoExecutionException(message, ex);
    } finally {
        IOUtils.closeQuietly(fdos);
        IOUtils.closeQuietly(fis);
    }
}