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

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

Introduction

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

Prototype

public static String getExtension(String filename) 

Source Link

Document

Gets the extension of a filename.

Usage

From source file:com.blackducksoftware.ohcount4j.detect.ExtnBASResolver.java

private boolean vbSpecificExtensions(List<String> filenames) {
    // The existence of these extensions anywhere in code tree
    // strongly hints that all *.bas files are Visual Basic
    String[] extensions = { "frm", "frx", "vba", "vbp", "vbs" };

    for (String filename : filenames) {
        String extension = FilenameUtils.getExtension(filename);
        for (String vb_ext : extensions) {
            if (extension != null && extension.equals(vb_ext)) {
                return true;
            }/*from www  .j a  v  a 2  s . c o m*/
        }
    }

    return false;
}

From source file:com.siberhus.tdfl.DefaultResourceCreator.java

@Override
public Resource create(Resource example) throws IOException {

    String parent = example.getFile().getParent();
    String targetDirPath = parent + File.separator + subdirectory;
    File targetDir = new File(targetDirPath);
    if (!targetDir.exists()) {
        if (createDirectory) {
            targetDir.mkdir();/*from  ww w.j  av  a 2s  .co m*/
        }
    }
    String filename = example.getFilename();
    if (extension == null)
        extension = FilenameUtils.getExtension(filename);
    filename = FilenameUtils.getBaseName(filename);
    if (prefix != null) {
        filename = prefix + filename;
    }
    if (suffix != null) {
        filename = filename + suffix;
    }
    filename = filename + "." + extension;
    return new FileSystemResource(targetDirPath + File.separator + filename);
}

From source file:ffx.potential.parsers.ARCFileFilter.java

/**
 * {@inheritDoc}//from   www  .j a v a2 s.  c  o  m
 *
 * This method return <code>true</code> if the file is a directory or TINKER
 * Archive (*.ARC).
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName());
    return ext.toUpperCase().startsWith("ARC");
}

From source file:com.artofsolving.jodconverter.web.DocumentConverterServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    ServletFileUpload fileUpload = (ServletFileUpload) applicationContext.getBean("fileUpload");
    DocumentConverter converter = (DocumentConverter) applicationContext.getBean("documentConverter");
    DocumentFormatRegistry registry = (DocumentFormatRegistry) applicationContext
            .getBean("documentFormatRegistry");

    if (!ServletFileUpload.isMultipartContent(request)) {
        throw new IllegalArgumentException("request is not multipart");
    }//from  w  ww.  j a va 2  s  .c  o  m

    // determine output format based on the request uri
    String outputExtension = FilenameUtils.getExtension(request.getRequestURI());
    DocumentFormat outputFormat = registry.getFormatByFileExtension(outputExtension);
    if (outputFormat == null) {
        throw new IllegalArgumentException("invalid outputFormat: " + outputExtension);
    }

    FileItem inputFileUpload = getInputFileUpload(request, fileUpload);
    if (inputFileUpload == null) {
        throw new IllegalArgumentException("inputDocument is null");
    }
    String inputExtension = FilenameUtils.getExtension(inputFileUpload.getName());
    DocumentFormat inputFormat = registry.getFormatByFileExtension(inputExtension);

    response.setContentType(outputFormat.getMimeType());
    String fileName = FilenameUtils.getBaseName(inputFileUpload.getName()) + "."
            + outputFormat.getFileExtension();
    response.setHeader("Content-Disposition", "inline; filename=" + fileName);
    //response.setContentLength(???);

    converter.convert(inputFileUpload.getInputStream(), inputFormat, response.getOutputStream(), outputFormat);
}

From source file:ffx.potential.parsers.PDBMLFileFilter.java

/**
 * {@inheritDoc}/*  w  ww .  jav  a  2 s .  co  m*/
 *
 * This method return <code>true</code> if the file is a directory or
 * Protein Databank XML File (*.XML).
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName());
    return ext.toUpperCase().startsWith("XML");
}

From source file:ffx.potential.parsers.FFXFileFilter.java

/**
 * {@inheritDoc}/*from   ww  w .  ja va  2  s.c o  m*/
 *
 * This method return <code>true</code> if the file is a directory or Force
 * Field X script (*.FFX).
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName());
    return ext.toUpperCase().startsWith("FFX");
}

From source file:com.confighub.api.server.filters.UrlRewriteFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;//w  w w  .j  av  a2 s.  c o m
    }

    String url = ((HttpServletRequest) request).getRequestURL().toString().toLowerCase();
    URL aUrl = new URL(url);
    String path = aUrl.getPath();

    if (path.startsWith("/email-verification") || path.startsWith("/passwordReset")) {
        request.getRequestDispatcher("/index.html").forward(request, response);
        return;
    }

    if (Utils.isBlank(path) || path.equals("/") || path.equals("/index.html") || path.startsWith("/rest")) {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            request.getRequestDispatcher("/404.html").forward(request, response);
        }
        return;
    }

    if (path.startsWith("/r/") || path.startsWith("/account/") || path.contains("edit/file/")) {
        request.getRequestDispatcher("/index.html").forward(request, response);
        return;
    }

    boolean hasExt = Utils.isBlank(FilenameUtils.getExtension(url));

    if (!hasExt) {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            request.getRequestDispatcher("/404.html").forward(request, response);
        }
        return;
    }

    request.getRequestDispatcher("/index.html").forward(request, response);
}

From source file:com.aliyun.odps.mapred.bridge.MetaExplorerImpl.java

/**
 * Upload a resource file and add it to resource list. The resource name is
 * <code>fileprefix+padding+extension</code>
 *
 * @param filePath//from  ww w.ja  v a 2 s .  c  o  m
 * @param type
 * @param padding
 * @param isTempResource
 * @return genuine resource name
 * @throws OdpsException
 */
@Override
public String addFileResourceWithRetry(String filePath, Resource.Type type, String padding,
        boolean isTempResource) throws OdpsException {

    String baseName = FilenameUtils.getBaseName(filePath);
    String extension = FilenameUtils.getExtension(filePath);
    String resourceName = baseName + padding;

    File file = new File(filePath);
    FileResource res = null;
    switch (type) {
    case FILE:
        res = new FileResource();
        break;
    case JAR:
        res = new JarResource();
        break;
    default:
        throw new OdpsException("Unsupported resource type:" + type);
    }

    FileInputStream in = null;
    int trial = 0;
    res.setIsTempResource(isTempResource);
    String rname = null;
    while (trial <= 3) {
        try {
            rname = resourceName + "_" + trial;
            if (extension != null && !extension.isEmpty()) {
                rname += "." + extension;
            }
            res.setName(rname);
            in = new FileInputStream(file);
            odps.resources().create(res, in);
            return rname;
        } catch (OdpsException e) {
            LOG.error("Upload resource " + rname + " failed:" + e.getMessage() + ", retry count=" + trial);
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e1) {
                // Silently swallow it.
            }
        } catch (FileNotFoundException e) {
            throw new OdpsException("Resource file: " + filePath + " not found.", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new OdpsException(e);
                }
            }
        }
        trial++;
    }
    throw new OdpsException("Upload resource failed.");

}

From source file:ffx.potential.parsers.ForceFieldFileFilter.java

/**
 * {@inheritDoc}//from ww w .  ja  v  a2s . c  o m
 *
 * This method return <code>true</code> if the file is a directory or TINKER
 * Parameter file (*.PRM).
 */
@Override
public boolean accept(File file) {
    if (file.isDirectory()) {
        return true;
    }
    String ext = FilenameUtils.getExtension(file.getName());
    return ext.toUpperCase().startsWith("PRM");
}

From source file:gov.nih.nci.cabig.caaers.service.adverseevent.AdditionalInformationDocumentService.java

public AdditionalInformationDocument uploadFile(String fileName, AdditionalInformation additionalInformation,
        InputStream inputStream, AdditionalInformationDocumentType additionalInformationDocumentType) {

    try {/*from   w  w  w  .ja va 2 s  . c o  m*/

        String aeAttachmentsLocation = configuration.get(Configuration.AE_ATTACHMENTS_LOCATION);

        String directory = FilenameUtils.normalize(aeAttachmentsLocation + "/" + additionalInformation.getId());

        String extension = StringUtils.isNotBlank(FilenameUtils.getExtension(fileName))
                ? "." + FilenameUtils.getExtension(fileName)
                : "";

        String filePath = FilenameUtils.normalize(directory + "/" + FilenameUtils.getBaseName(fileName)
                + Calendar.getInstance().getTimeInMillis() + RandomUtils.nextInt(100) + extension);

        if (logger.isDebugEnabled()) {
            logger.debug(String.format("creating file  %s of type %s for additional information %s at %s ",
                    fileName, additionalInformationDocumentType, additionalInformation.getId(), filePath));
        }

        FileUtils.forceMkdir(new File(directory));

        File file = new File(filePath);
        if (file.createNewFile()) {
            long bytesCopied = IOUtils.copyLarge(inputStream, new FileOutputStream(file));

            AdditionalInformationDocument additionalInformationDocument = new AdditionalInformationDocument();
            additionalInformationDocument
                    .setAdditionalInformationDocumentType(additionalInformationDocumentType);
            additionalInformationDocument.setAdditionalInformation(additionalInformation);
            additionalInformationDocument.setFileId(DigestUtils.md5Hex(file.getAbsolutePath()));
            additionalInformationDocument.setOriginalFileName(fileName);
            additionalInformationDocument.setFileName(file.getName());

            additionalInformationDocument.setFilePath(file.getCanonicalPath());
            additionalInformationDocument.setRelativePath(file.getAbsolutePath());
            additionalInformationDocument.setFileSize(bytesCopied);
            additionalInformationDocumentDao.save(additionalInformationDocument);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format(
                        "successfully created file  %s of type %s for additional information %s at %s. File information is - %s ",
                        fileName, additionalInformationDocumentType, additionalInformation.getId(), filePath,
                        additionalInformationDocument));
            }

            return additionalInformationDocument;
        } else {
            String errorMessage = String.format(
                    "error while creating  file  %s of type %s for additional information %s ", fileName,
                    additionalInformationDocumentType, additionalInformation.getId());
            throw new RuntimeException(errorMessage);
        }
    } catch (Exception e) {
        String errorMessage = String.format(
                "error while creating  file  %s of type %s for additional information %s ", fileName,
                additionalInformationDocumentType, additionalInformation.getId());

        logger.error(errorMessage, e);
        return null;
    }

}