Example usage for org.springframework.util StringUtils getFilenameExtension

List of usage examples for org.springframework.util StringUtils getFilenameExtension

Introduction

In this page you can find the example usage for org.springframework.util StringUtils getFilenameExtension.

Prototype

@Nullable
public static String getFilenameExtension(@Nullable String path) 

Source Link

Document

Extract the filename extension from the given Java resource path, e.g.

Usage

From source file:com.thoughtworks.go.spark.mocks.MockServletContext.java

@Override
public String getMimeType(String filePath) {
    String extension = StringUtils.getFilenameExtension(filePath);
    if (this.mimeTypes.containsKey(extension)) {
        return this.mimeTypes.get(extension).toString();
    } else {/*from  ww w  .j a  v  a 2  s.c o m*/
        return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
    }
}

From source file:com.wavemaker.studio.StudioService.java

@ExposeToClient
public FileUploadResponse uploadJar(MultipartFile file) {
    FileUploadResponse ret = new FileUploadResponse();
    try {//from  w  ww  .  ja  v a 2 s.  c  om
        String filename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(filename);
        if (!StringUtils.hasLength(filenameExtension)) {
            throw new IOException("Please upload a jar file");
        }
        if (!"jar".equals(filenameExtension)) {
            throw new IOException("Please upload a jar file, not a " + filenameExtension + " file");
        }
        Folder destinationFolder = this.fileSystem.getStudioWebAppRootFolder().getFolder("WEB-INF/lib");
        File destinationFile = destinationFolder.getFile(filename);
        destinationFile.getContent().write(file.getInputStream());
        ret.setPath(destinationFile.getName());

        // Copy jar to common/lib in WaveMaker Home
        Folder commonLib = fileSystem.getWaveMakerHomeFolder().getFolder(CommonConstants.COMMON_LIB);
        commonLib.createIfMissing();
        File destinationJar = commonLib.getFile(filename);
        destinationJar.getContent().write(file.getInputStream());

        // Copy jar to project's lib
        com.wavemaker.tools.io.ResourceFilter included = FilterOn.antPattern("*.jar");
        commonLib.find().include(included).files()
                .copyTo(this.projectManager.getCurrentProject().getRootFolder().getFolder("lib"));
    } catch (IOException e) {
        ret.setError(e.getMessage());
    }
    return ret;
}

From source file:com.wavemaker.tools.project.AbstractDeploymentManager.java

@Override
public FileUploadResponse importFromZip(MultipartFile file, boolean isTemplate) throws IOException {
    FileUploadResponse response = new FileUploadResponse();
    org.springframework.core.io.Resource tmpDir = this.projectManager.getTmpDir();

    // Write the zip file to outputFile
    String originalName = file.getOriginalFilename();

    int upgradeIndex = originalName.indexOf("-upgrade-");
    if (upgradeIndex > 0) {
        originalName = originalName.substring(0, upgradeIndex) + ".zip";
    }/*from w ww . ja va2 s  .c  om*/

    org.springframework.core.io.Resource outputFile = tmpDir.createRelative(originalName);

    OutputStream fos = this.fileSystem.getOutputStream(outputFile);
    FileCopyUtils.copy(file.getInputStream(), fos);

    org.springframework.core.io.Resource finalProjectFolder;
    try {

        // returns null if fails to unzip; need a handler for this...
        org.springframework.core.io.Resource projectFolder = com.wavemaker.tools.project.ResourceManager
                .unzipFile(this.fileSystem, outputFile);

        // If there is only one folder in what we think is the
        // projectFolder, open that one folder because that must be the real
        // project folder
        // Filter out private folders generated by OS or svn, etc...
        // (__MACOS, .svn, etc...)
        List<org.springframework.core.io.Resource> listings = this.fileSystem.listChildren(projectFolder);
        if (listings.size() == 1) {
            org.springframework.core.io.Resource listing = listings.get(0);
            if (StringUtils.getFilenameExtension(listing.getFilename()) == null
                    && !listing.getFilename().startsWith(".") && !listing.getFilename().startsWith("_")) {
                projectFolder = listing;
            }
        }

        // Verify that this looks like a project folder we unzipped
        com.wavemaker.tools.project.Project project = new com.wavemaker.tools.project.Project(projectFolder,
                this.fileSystem);
        org.springframework.core.io.Resource testExistenceFile = project.getWebAppRoot()
                .createRelative("pages/");
        if (!testExistenceFile.exists()) {
            throw new WMRuntimeException(
                    "That didn't look like a project folder; if it was, files were missing");
        }

        com.wavemaker.tools.io.File indexhtml = project.getWebAppRootFolder().getFile("index.html");
        String indexstring = project.readFile(indexhtml);
        int endIndex = indexstring.lastIndexOf("({domNode: \"wavemakerNode\"");
        int startIndex = indexstring.lastIndexOf(" ", endIndex);
        String newProjectName = indexstring.substring(startIndex + 1, endIndex);

        // Get a File to point to where we're going to place this imported
        // project
        if (isTemplate) {
            this.fileSystem.getTemplatesFolder().createIfMissing();
        }
        finalProjectFolder = (isTemplate ? this.fileSystem.getTemplatesDir()
                : this.projectManager.getBaseProjectDir()).createRelative(newProjectName + "/");
        if (isTemplate && finalProjectFolder.exists()) {
            this.fileSystem.deleteFile(finalProjectFolder);
        }
        System.out.println("FINAL PATH: " + finalProjectFolder.getURI().toString());
        String finalname = finalProjectFolder.getFilename();
        String originalFinalname = finalname;
        // If there is already a project at that location, rename the
        // project
        int i = -1;
        do {
            i++;
            finalProjectFolder = (isTemplate ? this.fileSystem.getTemplatesDir()
                    : this.projectManager.getBaseProjectDir())
                            .createRelative(finalname + (i > 0 ? "" + i : "") + "/");
        } while (finalProjectFolder.exists());
        finalname = finalProjectFolder.getFilename();

        // OK, now finalname has the name of the new project,
        // finalProjectFolder has the full path to the new project
        // Move the project into the project folder
        this.fileSystem.rename(projectFolder, finalProjectFolder);

        // If we renamed the project (i.e. if i got incremented) then we
        // need to make some corrections
        if (i > 0) {

            // Correction 1: Rename the js file
            com.wavemaker.tools.project.Project finalProject = new com.wavemaker.tools.project.Project(
                    finalProjectFolder, this.fileSystem);
            File jsFile = finalProject.getWebAppRootFolder().getFile(originalFinalname + ".js");
            File newJsFile = finalProject.getWebAppRootFolder().getFile(finalname + ".js");
            jsFile.rename(newJsFile.getName());

            // Correction 2: Change the class name in the js file
            com.wavemaker.tools.project.ResourceManager.ReplaceTextInProjectFile(finalProject, newJsFile,
                    originalFinalname, finalname);

            // Corection3: Change the constructor in index.html
            File index_html = finalProject.getWebAppRootFolder().getFile("index.html");
            com.wavemaker.tools.project.ResourceManager.ReplaceTextInProjectFile(finalProject, index_html,
                    "new " + originalFinalname + "\\(\\{domNode", "new " + finalname + "({domNode");

            // Correction 4: Change the pointer to the js script read in by
            // index.html
            com.wavemaker.tools.project.ResourceManager.ReplaceTextInProjectFile(finalProject, index_html,
                    "\\\"" + originalFinalname + "\\.js\\\"", '"' + finalname + ".js\"");

            // Correction 5: Change the title
            com.wavemaker.tools.project.ResourceManager.ReplaceTextInProjectFile(finalProject, index_html,
                    "\\<title\\>" + originalFinalname + "\\<\\/title\\>", "<title>" + finalname + "</title>");

        }
    } finally {
        // If the user uploaded a zipfile that had many high level folders,
        // they could make a real mess of things,
        // so just purge the tmp folder after we're done
        this.fileSystem.deleteFile(tmpDir);
    }
    System.out.println("C");
    response.setPath(finalProjectFolder.getFilename());
    response.setError("");
    response.setWidth("");
    response.setHeight("");
    return response;
}

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

/**
 * Copied from Spring 4.1 AbstractVersionStrategy
 * @param requestPath//  www  .j  a  v  a2s  .  c om
 * @param version
 * @return
 */
protected String addVersion(String requestPath, String version) {
    String baseFilename = StringUtils.stripFilenameExtension(requestPath);
    String extension = StringUtils.getFilenameExtension(requestPath);
    return baseFilename + version + "." + extension;
}

From source file:org.broadleafcommerce.common.web.resource.resolver.BLCJSResourceResolver.java

protected String addVersion(String requestPath, String version) {
    String baseFilename = StringUtils.stripFilenameExtension(requestPath);
    String extension = StringUtils.getFilenameExtension(requestPath);
    return baseFilename + version + "." + extension;
}

From source file:org.springframework.boot.actuate.metrics.reader.MetricRegistryMetricReader.java

private static Number getMetric(Object metric, String metricName) {
    String key = StringUtils.getFilenameExtension(metricName);
    return (Number) new BeanWrapperImpl(metric).getPropertyValue(key);
}

From source file:org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter.java

/**
 * Return the actual port file that should be written for the given application
 * context. The default implementation builds a file from the source file and the
 * application context namespace.//  w w  w .ja va 2s  .  c o  m
 * @param applicationContext the source application context
 * @return the file that should be written
 */
protected File getPortFile(EmbeddedWebApplicationContext applicationContext) {
    String contextName = applicationContext.getNamespace();
    if (StringUtils.isEmpty(contextName)) {
        return this.file;
    }
    String name = this.file.getName();
    String extension = StringUtils.getFilenameExtension(this.file.getName());
    name = name.substring(0, name.length() - extension.length() - 1);
    if (isUpperCase(name)) {
        name = name + "-" + contextName.toUpperCase();
    } else {
        name = name + "-" + contextName.toLowerCase();
    }
    if (StringUtils.hasLength(extension)) {
        name = name + "." + extension;
    }
    return new File(this.file.getParentFile(), name);
}

From source file:org.springframework.boot.env.PropertySourcesLoader.java

private boolean isFile(Resource resource) {
    return resource != null && resource.exists()
            && StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()));
}

From source file:org.springframework.boot.web.context.WebServerPortFileWriter.java

/**
 * Return the actual port file that should be written for the given application
 * context. The default implementation builds a file from the source file and the
 * application context namespace if available.
 * @param applicationContext the source application context
 * @return the file that should be written
 *//* ww  w  .  j  av  a 2 s . co m*/
protected File getPortFile(ApplicationContext applicationContext) {
    String namespace = getServerNamespace(applicationContext);
    if (StringUtils.isEmpty(namespace)) {
        return this.file;
    }
    String name = this.file.getName();
    String extension = StringUtils.getFilenameExtension(this.file.getName());
    name = name.substring(0, name.length() - extension.length() - 1);
    if (isUpperCase(name)) {
        name = name + "-" + namespace.toUpperCase(Locale.ENGLISH);
    } else {
        name = name + "-" + namespace.toLowerCase(Locale.ENGLISH);
    }
    if (StringUtils.hasLength(extension)) {
        name = name + "." + extension;
    }
    return new File(this.file.getParentFile(), name);
}

From source file:org.springframework.cloud.config.server.resource.GenericResourceRepository.java

private Collection<String> getProfilePaths(String profiles, String path) {
    Set<String> paths = new LinkedHashSet<>();
    for (String profile : StringUtils.commaDelimitedListToSet(profiles)) {
        if (!StringUtils.hasText(profile) || "default".equals(profile)) {
            paths.add(path);/*from w  w w .j  a  v  a 2 s .co  m*/
        } else {
            String ext = StringUtils.getFilenameExtension(path);
            String file = path;
            if (ext != null) {
                ext = "." + ext;
                file = StringUtils.stripFilenameExtension(path);
            } else {
                ext = "";
            }
            paths.add(file + "-" + profile + ext);
        }
    }
    paths.add(path);
    return paths;
}