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

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

Introduction

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

Prototype

public static String separatorsToUnix(String path) 

Source Link

Document

Converts all separators to the Unix separator of forward slash.

Usage

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

@Override
public List<String> addOrUpdateResourcesForPaths(FileWorkArea workArea, List<File> files,
        boolean removeFilesFromWorkArea) {
    List<String> result = new ArrayList<String>();
    for (File srcFile : files) {
        if (!srcFile.getAbsolutePath().startsWith(workArea.getFilePathLocation())) {
            throw new FileServiceException("Attempt to update file " + srcFile.getAbsolutePath()
                    + " that is not in the passed in WorkArea " + workArea.getFilePathLocation());
        }/*from w w w  .  j a  va 2 s  . c o  m*/

        String fileName = srcFile.getAbsolutePath().substring(workArea.getFilePathLocation().length());

        // before building the resource name, convert the file path to a url-like path
        String url = FilenameUtils.separatorsToUnix(fileName);
        String resourceName = buildResourceName(url);
        String destinationFilePath = FilenameUtils
                .normalize(getBaseDirectory(false) + File.separator + resourceName);
        File destFile = new File(destinationFilePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }

        try {
            if (removeFilesFromWorkArea) {
                if (destFile.exists()) {
                    FileUtils.deleteQuietly(destFile);
                }
                FileUtils.moveFile(srcFile, destFile);
            } else {
                FileUtils.copyFile(srcFile, destFile);
            }
            result.add(fileName);
        } catch (IOException ioe) {
            throw new FileServiceException("Error copying resource named " + fileName + " from workArea "
                    + workArea.getFilePathLocation() + " to " + resourceName, ioe);
        }
    }
    return result;
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationContextImplTest.java

@Test
public void testGetSystemPropertyReferencedEntryWhenValueIsAbsoluteNotUnderDDFHome() throws Exception {
    final Path migratablePath = testFolder.newFile("test.cfg").toPath().toRealPath(LinkOption.NOFOLLOW_LINKS);
    final String migratableName = FilenameUtils.separatorsToUnix(migratablePath.toString());

    System.setProperty(PROPERTY_NAME, migratablePath.toAbsolutePath().toString());

    final Optional<ExportMigrationEntry> oentry = context.getSystemPropertyReferencedEntry(PROPERTY_NAME,
            (r, v) -> true);//from  w  w  w  .j  ava  2 s.  c o  m

    Assert.assertThat(oentry, OptionalMatchers.isPresent());
    final ExportMigrationEntry entry = oentry.get();

    Assert.assertThat(entry.getId(), Matchers.equalTo(MIGRATABLE_ID));
    Assert.assertThat(entry.getName(), Matchers.equalTo(migratableName));
    Assert.assertThat(entry.getPath(), Matchers.equalTo(migratablePath));
    // now check that it is a system property referenced entry that references the proper property
    // name
    Assert.assertThat(entry, Matchers.instanceOf(ExportMigrationSystemPropertyReferencedEntryImpl.class));
    final ExportMigrationSystemPropertyReferencedEntryImpl sentry = (ExportMigrationSystemPropertyReferencedEntryImpl) entry;

    Assert.assertThat(sentry.getProperty(), Matchers.equalTo(PROPERTY_NAME));
    // finally make sure no warnings or errors were recorded
    Assert.assertThat(report.hasErrors(), Matchers.equalTo(false));
    Assert.assertThat(report.hasWarnings(), Matchers.equalTo(false));
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationContextImplTest.java

@Test
public void testGetEntryWithAbsolutePathNotUnderDDFHome() throws Exception {
    final Path migratablePath = Paths.get(MIGRATABLE_NAME).toAbsolutePath();
    final String migratableName = FilenameUtils.separatorsToUnix(migratablePath.toString());

    final ExportMigrationEntry entry = context.getEntry(migratablePath);

    Assert.assertThat(entry.getId(), Matchers.equalTo(MIGRATABLE_ID));
    Assert.assertThat(entry.getName(), Matchers.equalTo(migratableName));
    Assert.assertThat(entry.getPath(), Matchers.equalTo(migratablePath));
    // now check that it is a standard export entry
    Assert.assertThat(entry, Matchers.instanceOf(ExportMigrationEntryImpl.class));
    // finally make sure no warnings or errors were recorded
    Assert.assertThat(report.hasErrors(), Matchers.equalTo(false));
    Assert.assertThat(report.hasWarnings(), Matchers.equalTo(false));
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImpl.java

/**
 * Instantiates a new migration entry given a migratable context and path.
 *
 * <p><i>Note:</i> In this version of the constructor, the path is either absolute or assumed to
 * be relative to ${ddf.home}. It will also be automatically relativized to ${ddf.home}.
 *
 * @param context the migration context associated with this entry
 * @param path the path for this entry// w  w  w  . j a  v  a 2s .c  om
 * @throws IllegalArgumentException if <code>context</code> or <code>path</code> is <code>null
 * </code>
 */
protected ExportMigrationEntryImpl(ExportMigrationContextImpl context, Path path) {
    Validate.notNull(context, "invalid null context");
    Validate.notNull(path, "invalid null path");
    Path apath;
    Exception aerror;

    try {
        // make sure it is resolved against ddf.home and not the current working directory
        apath = AccessUtils.doPrivileged(() -> {
            final Path p = context.getPathUtils().resolveAgainstDDFHome(path)
                    .toRealPath(LinkOption.NOFOLLOW_LINKS);

            this.isFile = p.toFile().isFile();
            return p;
        });
        aerror = null;
    } catch (IOException e) {
        apath = path;
        this.isFile = true; // since we can't find an absolute path on disk, we got to assume it's a file
        // remember the error in case the migratable attempts to store the file from disk later
        // instead of providing its own data
        aerror = e;
    }
    this.context = context;
    this.absolutePath = apath;
    this.absolutePathError = aerror;
    this.path = context.getPathUtils().relativizeFromDDFHome(apath);
    this.file = apath.toFile();
    // we keep the entry name in Unix style based on our convention
    this.name = FilenameUtils.separatorsToUnix(this.path.toString());
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImplTest.java

@Test
public void testConstructorWithAbsoluteFilePathNotUnderDDFHome() throws Exception {
    final Path absoluteFilePath = createFile(root, "test.ext");
    final String absoluteFileName = FilenameUtils.separatorsToUnix(absoluteFilePath.toString());

    final ExportMigrationEntryImpl entry = new ExportMigrationEntryImpl(context, absoluteFilePath);

    Assert.assertThat(entry.getContext(), Matchers.sameInstance(context));
    Assert.assertThat(entry.getPath(), Matchers.equalTo(absoluteFilePath));
    Assert.assertThat(entry.getAbsolutePath(), Matchers.equalTo(absoluteFilePath));
    Assert.assertThat(entry.getFile(), Matchers.equalTo(absoluteFilePath.toFile()));
    Assert.assertThat(entry.getName(), Matchers.equalTo(absoluteFileName));
    Assert.assertThat(entry.isFile(), Matchers.equalTo(true));
    Assert.assertThat(entry.isDirectory(), Matchers.equalTo(false));
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImplTest.java

@Test
public void testConstructorWithAbsoluteDirPathNotUnderDDFHome() throws Exception {
    final Path absoluteFilePath = root;
    final String absoluteFileName = FilenameUtils.separatorsToUnix(absoluteFilePath.toString());

    final ExportMigrationEntryImpl entry = new ExportMigrationEntryImpl(context, absoluteFilePath);

    Assert.assertThat(entry.getContext(), Matchers.sameInstance(context));
    Assert.assertThat(entry.getPath(), Matchers.equalTo(absoluteFilePath));
    Assert.assertThat(entry.getAbsolutePath(), Matchers.equalTo(absoluteFilePath));
    Assert.assertThat(entry.getFile(), Matchers.equalTo(absoluteFilePath.toFile()));
    Assert.assertThat(entry.getName(), Matchers.equalTo(absoluteFileName));
    Assert.assertThat(entry.isFile(), Matchers.equalTo(false));
    Assert.assertThat(entry.isDirectory(), Matchers.equalTo(true));
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImplTest.java

@Test
public void testGetPropertyReferencedEntryWhenValueIsAbsoluteNotUnderDDFHome() throws Exception {
    final Path migratablePath = testFolder.newFile("test.cfg").toPath().toRealPath(LinkOption.NOFOLLOW_LINKS);
    final String migratableName = FilenameUtils.separatorsToUnix(migratablePath.toString());

    storeProperty(PROPERTY_NAME, migratablePath.toAbsolutePath().toString());

    final Optional<ExportMigrationEntry> oentry = entry.getPropertyReferencedEntry(PROPERTY_NAME,
            (r, v) -> true);/* w w  w  .j av a2 s .c  om*/

    Assert.assertThat(oentry, OptionalMatchers.isPresent());
    final ExportMigrationEntry entry = oentry.get();

    Assert.assertThat(entry.getId(), Matchers.equalTo(MIGRATABLE_ID));
    Assert.assertThat(entry.getName(), Matchers.equalTo(migratableName));
    Assert.assertThat(entry.getPath(), Matchers.equalTo(migratablePath));
    // now check that it is a java property referenced entry that references the proper property
    // name
    Assert.assertThat(entry, Matchers.instanceOf(ExportMigrationJavaPropertyReferencedEntryImpl.class));
    final ExportMigrationJavaPropertyReferencedEntryImpl jentry = (ExportMigrationJavaPropertyReferencedEntryImpl) entry;

    Assert.assertThat(jentry.getProperty(), Matchers.equalTo(PROPERTY_NAME));
    // finally make sure no warnings or errors were recorded
    Assert.assertThat(report.hasErrors(), Matchers.equalTo(false));
    Assert.assertThat(report.hasWarnings(), Matchers.equalTo(false));
}

From source file:org.codice.ddf.configuration.migration.ImportMigrationEntryImpl.java

/**
 * Instantiates a new migration entry by parsing the provided zip entry's name for a migratable
 * identifier and an entry relative name.
 *
 * @param contextProvider a provider for migration contexts given a migratable id
 * @param ze the zip entry for which we are creating an entry
 *///w w  w .  j  a  v a 2 s.co  m
ImportMigrationEntryImpl(Function<String, ImportMigrationContextImpl> contextProvider, ZipEntry ze) {
    // we still must sanitize because there could be a mix of / and \ and Paths.get() doesn't
    // support that
    final Path fqn = Paths.get(FilenameUtils.separatorsToSystem(ze.getName()));
    final int count = fqn.getNameCount();

    if (count > 1) {
        this.context = contextProvider.apply(fqn.getName(0).toString());
        this.path = fqn.subpath(1, count);
    } else { // system entry
        this.context = contextProvider.apply(null);
        this.path = fqn;
    }
    this.absolutePath = context.getPathUtils().resolveAgainstDDFHome(path);
    this.file = absolutePath.toFile();
    this.name = FilenameUtils.separatorsToUnix(path.toString());
    this.entry = ze;
    this.isFile = true;
}

From source file:org.codice.ddf.configuration.migration.ImportMigrationEntryImpl.java

/**
 * Instantiates a new migration entry with the given name.
 *
 * @param context the migration context associated with this entry
 * @param name the entry's relative name
 * @param isFile <code>true</code> if the entry represents a file; <code>false</code> if it
 *     represents a directory/*from w w  w .  jav  a  2  s  . c  o m*/
 */
protected ImportMigrationEntryImpl(ImportMigrationContextImpl context, String name, boolean isFile) {
    this.context = context;
    this.path = Paths.get(name);
    this.name = FilenameUtils.separatorsToUnix(name);
    this.absolutePath = context.getPathUtils().resolveAgainstDDFHome(path);
    this.file = absolutePath.toFile();
    this.entry = null;
    this.isFile = isFile;
}

From source file:org.codice.ddf.configuration.migration.ImportMigrationEntryImpl.java

/**
 * Instantiates a new migration entry with the given path.
 *
 * @param context the migration context associated with this entry
 * @param path the entry's relative path
 * @param isFile <code>true</code> if the entry represents a file; <code>false</code> if it
 *     represents a directory/*from w  w  w  .  j a v a2 s  .c o m*/
 */
protected ImportMigrationEntryImpl(ImportMigrationContextImpl context, Path path, boolean isFile) {
    this.context = context;
    this.path = path;
    this.name = FilenameUtils.separatorsToUnix(path.toString());
    this.absolutePath = context.getPathUtils().resolveAgainstDDFHome(path);
    this.file = absolutePath.toFile();
    this.entry = null;
    this.isFile = isFile;
}