Example usage for org.apache.commons.io FilenameUtils concat

List of usage examples for org.apache.commons.io FilenameUtils concat

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils concat.

Prototype

public static String concat(String basePath, String fullFilenameToAdd) 

Source Link

Document

Concatenates a filename to a base path using normal command line style rules.

Usage

From source file:org.bimserver.charting.Charts.Chart.java

public void saveToSVGInUserDirectory(ArrayList<LinkedHashMap<String, Object>> rawData, String filename) {
    String home = System.getProperty("user.home");
    String directoryPath = ((System.getProperty("os.name").startsWith("Windows")))
            ? FilenameUtils.concat(home, "Desktop")
            : home;//from   w ww .  ja  va  2 s.  c  o m
    saveToSVG(rawData, directoryPath, filename);
}

From source file:org.bimserver.charting.Charts.Chart.java

public void saveToSVG(ArrayList<LinkedHashMap<String, Object>> rawData, String directoryPath, String filename) {
    StringBuilder builder = this.writeSVG(rawData);
    String data = builder.toString();
    PrintWriter writer;/*  www . j  a  v a2  s .c  om*/
    String file = FilenameUtils.concat(directoryPath, filename);
    try {
        writer = new PrintWriter(file, "UTF-8");
        writer.println(data);
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:org.broadleafcommerce.cms.file.service.StaticAssetStorageServiceImpl.java

protected void createLocalFileFromInputStream(InputStream is, File baseLocalFile) throws IOException {
    FileOutputStream tos = null;//from   w w  w. ja  va 2  s  . co  m
    FileWorkArea workArea = null;
    try {
        if (!baseLocalFile.getParentFile().exists()) {
            boolean directoriesCreated = false;
            if (!baseLocalFile.getParentFile().exists()) {
                directoriesCreated = baseLocalFile.getParentFile().mkdirs();
                if (!directoriesCreated) {
                    // There is a chance that another VM created the directories.   If not, we may not have 
                    // proper permissions and this is an error we need to report.
                    if (!baseLocalFile.getParentFile().exists()) {
                        throw new RuntimeException("Unable to create middle directories for file: "
                                + baseLocalFile.getAbsolutePath());
                    }
                }
            }
        }

        workArea = broadleafFileService.initializeWorkArea();
        File tmpFile = new File(FilenameUtils.concat(workArea.getFilePathLocation(), baseLocalFile.getName()));

        tos = new FileOutputStream(tmpFile);

        IOUtils.copy(is, tos);

        // close the input/output streams before trying to move files around
        is.close();
        tos.close();

        // Adding protection against this file already existing / being written by another thread.
        // Adding locks would be useless here since another VM could be executing the code. 
        if (!baseLocalFile.exists()) {
            try {
                FileUtils.moveFile(tmpFile, baseLocalFile);
            } catch (FileExistsException e) {
                // No problem
                if (LOG.isDebugEnabled()) {
                    LOG.debug("File exists error moving file " + tmpFile.getAbsolutePath(), e);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(tos);

        if (workArea != null) {
            broadleafFileService.closeWorkArea(workArea);
        }
    }
}

From source file:org.broadleafcommerce.common.file.service.BroadleafFileServiceImpl.java

/**
 * Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it
 * exists or java.tmp.io if that property has not been set.   
 * //from  w  w w  .j  a v  a 2s. c  o  m
 */
protected String getBaseDirectory(boolean skipSite) {
    String path = "";
    if (StringUtils.isBlank(tempFileSystemBaseDirectory)) {
        path = DEFAULT_STORAGE_DIRECTORY;
    } else {
        path = tempFileSystemBaseDirectory;
    }

    if (!skipSite) {
        // Create site specific directory if Multi-site (all site files will be located in the same directory)
        BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
        if (brc != null && brc.getSite() != null) {
            String siteDirectory = "site-" + brc.getSite().getId();
            String siteHash = DigestUtils.md5Hex(siteDirectory);
            path = FilenameUtils.concat(path, siteHash.substring(0, 2));
            path = FilenameUtils.concat(path, siteDirectory);
        }
    }

    return path;
}

From source file:org.broadleafcommerce.common.file.service.BroadleafFileServiceImpl.java

/**
 * Returns a directory that is unique for this work area. 
 *   //from w  ww  . j  a v  a 2 s. c o m
 */
protected String getTempDirectory(String baseDirectory) {
    assert baseDirectory != null;

    Random random = new Random();

    // This code is used to ensure that we don't have thousands of sub-directories in a single parent directory.
    for (int i = 0; i < maxGeneratedDirectoryDepth; i++) {
        if (i == 4) {
            LOG.warn(
                    "Property asset.server.max.generated.file.system.directories set to high, currently set to "
                            + maxGeneratedDirectoryDepth);
            break;
        }
        // check next int value
        int num = random.nextInt(256);
        baseDirectory = FilenameUtils.concat(baseDirectory, Integer.toHexString(num));
    }
    return baseDirectory;
}

From source file:org.broadleafcommerce.common.file.service.FileSystemFileServiceProvider.java

/**
 * Stores the file on the file-system by performing an MD5 hash of the 
 * the passed in fileName/*  ww  w.ja  va  2s.c o  m*/
 * 
 * To ensure that files can be stored and accessed in an efficient manner, the 
 * system creates directories based on the characters in the hash.   
 * 
 * For example, if the URL is /product/myproductimage.jpg, then the MD5 would be
 * 35ec52a8dbd8cf3e2c650495001fe55f resulting in the following file on the filesystem
 * {assetFileSystemPath}/35/ec/myproductimage.jpg.  
 * 
 * The hash for the filename will include a beginning slash before performing the MD5.   This
 * is done largely for backward compatibility with similar functionality in BLC 3.0.0.
 * 
 * This algorithm has the following benefits:
 * - Efficient file-system storage with
 * - Balanced tree of files that supports 10 million files
 * 
 * If support for more files is needed, implementors should consider one of the following approaches:
 * 1.  Overriding the maxGeneratedFileSystemDirectories property from its default of 2 to 3
 * 2.  Overriding this method to introduce an alternate approach
 * 
 * @param url The URL used to represent an asset for which a name on the fileSystem is desired.
 * 
 * @return
 */
protected String buildResourceName(String url) {
    // Create directories based on hash
    String fileHash = null;
    // Intentionally not using File.separator here since URLs should always end with /
    if (!url.startsWith("/")) {
        fileHash = DigestUtils.md5Hex("/" + url);
    } else {
        fileHash = DigestUtils.md5Hex(url);
    }

    String resourceName = "";
    for (int i = 0; i < maxGeneratedDirectoryDepth; i++) {
        if (i == 4) {
            LOG.warn("Property maxGeneratedDirectoryDepth set to high, ignoring values past 4 - value set to"
                    + maxGeneratedDirectoryDepth);
            break;
        }
        resourceName = FilenameUtils.concat(resourceName, fileHash.substring(i * 2, (i + 1) * 2));
    }

    // use the filename from the URL which is everything after the last slash
    return FilenameUtils.concat(resourceName, FilenameUtils.getName(url));
}

From source file:org.broadleafcommerce.common.file.service.FileSystemFileServiceProvider.java

/**
 * Creates a unique directory on the file system for each site.
 * Each site may be in one of 255 base directories.   This model efficiently supports up to 65,000 sites
 * served from a single file system based on most OS systems ability to quickly access files as long
 * as there are not more than 255 directories.
 * /*from  w ww  .  jav  a2  s.c  o  m*/
 * @param The starting directory for local files which must end with a '/';
 */
protected String getSiteDirectory(String baseDirectory) {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc != null) {
        Site site = brc.getSite();
        if (site != null) {
            String siteDirectory = "site-" + site.getId();
            String siteHash = DigestUtils.md5Hex(siteDirectory);
            String sitePath = FilenameUtils.concat(siteHash.substring(0, 2), siteDirectory);
            return FilenameUtils.concat(baseDirectory, sitePath);
        }
    }

    return baseDirectory;
}

From source file:org.broadleafcommerce.common.file.service.FileSystemFileServiceProviderTest.java

/**
 * For example, if the URL is /product/myproductimage.jpg, then the MD5 would be
 * 35ec52a8dbd8cf3e2c650495001fe55f resulting in the following file on the filesystem
 * {assetFileSystemPath}/64/a7/myproductimage.jpg.
 * //from w ww .j ava 2s  .c  o m
 * If there is a "siteId" in the BroadleafRequestContext then the site is also distributed
 * using a similar algorithm but the system attempts to keep images for sites in their own
 * directory resulting in an extra two folders required to reach any given product.   So, for
 * site with id 125, the system will MD5 "site125" in order to build the URL string.   "site125" has an md5
 * string of "7d905e85b8cb72a0477632be2c342bd6".    
 * 
 * So, in this case with the above product URL in site125, the full URL on the filesystem
 * will be:
 * 
 * {assetFileSystemPath}/7d/site125/64/a7/myproductimage.jpg.
 * @throws Exception
 */
public void testBuildFileName() throws Exception {
    FileSystemFileServiceProvider provider = new FileSystemFileServiceProvider();
    String tmpdir = FileUtils.getTempDirectoryPath();
    if (!tmpdir.endsWith(File.separator)) {
        tmpdir = tmpdir + File.separator;
    }
    provider.fileSystemBaseDirectory = FilenameUtils.concat(tmpdir, "test");
    provider.maxGeneratedDirectoryDepth = 2;
    File file = provider.getResource("/product/myproductimage.jpg");

    String resultPath = tmpdir
            + StringUtils.join(new String[] { "test", "35", "ec", "myproductimage.jpg" }, File.separator);
    assertEquals(file.getAbsolutePath(), FilenameUtils.normalize(resultPath));

    BroadleafRequestContext brc = new BroadleafRequestContext();
    BroadleafRequestContext.setBroadleafRequestContext(brc);

    Site site = new SiteImpl();
    site.setId(125L);
    brc.setSite(site);

    // try with site specific directory
    file = provider.getResource("/product/myproductimage.jpg");
    resultPath = tmpdir + StringUtils
            .join(new String[] { "test", "c8", "site-125", "35", "ec", "myproductimage.jpg" }, File.separator);
    assertEquals(file.getAbsolutePath(), resultPath);

    // try with 3 max generated directories
    provider.maxGeneratedDirectoryDepth = 3;
    file = provider.getResource("/product/myproductimage.jpg");
    resultPath = tmpdir + StringUtils.join(
            new String[] { "test", "c8", "site-125", "35", "ec", "52", "myproductimage.jpg" }, File.separator);
    assertEquals(file.getAbsolutePath(), resultPath);

    // Remove the request context from thread local so it doesn't get in the way of subsequent tests
    BroadleafRequestContext.setBroadleafRequestContext(null);
}

From source file:org.broadleafcommerce.common.resource.service.ResourceBundlingServiceImpl.java

protected void saveBundle(Resource resource) {
    FileWorkArea tempWorkArea = fileService.initializeWorkArea();
    String fileToSave = FilenameUtils.separatorsToSystem(getResourcePath(resource.getDescription()));
    String tempFilename = FilenameUtils.concat(tempWorkArea.getFilePathLocation(), fileToSave);
    File tempFile = new File(tempFilename);
    if (!tempFile.getParentFile().exists()) {
        if (!tempFile.getParentFile().mkdirs()) {
            if (!tempFile.getParentFile().exists()) {
                throw new RuntimeException("Unable to create parent directories for file: " + tempFilename);
            }//from   ww w . jav a  2 s  . co m
        }
    }

    BufferedOutputStream out = null;
    InputStream ris = null;
    try {
        ris = resource.getInputStream();
        out = new BufferedOutputStream(new FileOutputStream(tempFile));
        StreamUtils.copy(ris, out);

        ris.close();
        out.close();

        fileService.addOrUpdateResourceForPath(tempWorkArea, tempFile, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ris);
        IOUtils.closeQuietly(out);
        fileService.closeWorkArea(tempWorkArea);
    }
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

/**
 * hook for overriding name used for resource in S3
 * @param name//w ww .j  a  v  a 2  s .  c  o  m
 * @return
 */
protected String buildResourceName(String name) {
    // Strip the starting slash to prevent empty directories in S3 as well as required references by // in the
    // public S3 URL
    if (name.startsWith("/")) {
        name = name.substring(1);
    }

    String baseDirectory = s3ConfigurationService.lookupS3Configuration().getBucketSubDirectory();
    if (StringUtils.isNotEmpty(baseDirectory)) {
        if (baseDirectory.startsWith("/")) {
            baseDirectory = baseDirectory.substring(1);
        }
    } else {
        // ensure subDirectory is non-null
        baseDirectory = "";
    }

    String siteSpecificResourceName = getSiteSpecificResourceName(name);
    return FilenameUtils.concat(baseDirectory, siteSpecificResourceName);
}