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

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

Introduction

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

Prototype

public static String normalize(String filename) 

Source Link

Document

Normalizes a path, removing double and single dot path steps.

Usage

From source file:org.opencastproject.composer.gstreamer.AbstractGSEncoderEngine.java

@Override
public List<File> extract(File mediaSource, EncodingProfile profile, Map<String, String> properties,
        long... times) throws EncoderException {

    Map<String, String> params = new HashMap<String, String>();
    if (properties != null) {
        params.putAll(properties);//  w w w .  j av  a  2 s . c  o  m
    }

    try {
        if (mediaSource == null) {
            throw new IllegalArgumentException("Media source must be specified.");
        }
        if (times.length == 0) {
            throw new IllegalArgumentException("At least one time has to be specified");
        }

        // build string definition
        String imageDimensions = profile.getExtension("gstreamer.image.dimensions");
        if (imageDimensions == null) {
            throw new EncoderException("Missing dimension definition in encoding profile");
        }
        StringBuilder definition = new StringBuilder();
        definition.append(Long.toString(times[0]) + ":" + imageDimensions);
        for (int i = 1; i < times.length; i++) {
            definition.append("," + Long.toString(times[i]) + ":" + imageDimensions);
        }
        params.put("gstreamer.image.extraction", definition.toString());

        // Set encoding parameters
        String mediaInput = FilenameUtils.normalize(mediaSource.getAbsolutePath());
        params.put("in.video.path", mediaInput);
        params.put("in.video.mimetype",
                MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(mediaInput));

        String outDir = mediaSource.getAbsoluteFile().getParent();
        String outFileName = FilenameUtils.getBaseName(mediaSource.getName());
        String outSuffix = profile.getSuffix();

        // add time template
        if (!outSuffix.contains("#{time}")) {
            outFileName += "_#{time}";
        }

        File encodedFileTemplate = new File(outDir, outFileName + outSuffix);
        params.put("out.file.path", encodedFileTemplate.getAbsolutePath());

        // extract images
        List<File> outputImages = extractMultipleImages(profile, params);
        if (outputImages.size() == 0) {
            logger.warn("No images were extracted from video track {} using encoding profile '{}'",
                    mediaSource.getName(), profile.getIdentifier());
        }

        logger.info("Images successfully extracted from video track {} using profile '{}'",
                new String[] { mediaSource.getName(), profile.getIdentifier() });
        fireEncoded(this, profile, mediaSource);
        return outputImages;
    } catch (EncoderException e) {
        logger.warn("Error while extracting images from video track {} using '{}': {}",
                new String[] { (mediaSource == null ? "N/A" : mediaSource.getName()), profile.getIdentifier(),
                        e.getMessage() });
        fireEncodingFailed(this, profile, e, mediaSource);
        throw e;
    } catch (Exception e) {
        logger.warn("Error while extracting images from video track {} using '{}': {}",
                new String[] { (mediaSource == null ? "N/A" : mediaSource.getName()), profile.getIdentifier(),
                        e.getMessage() });
        fireEncodingFailed(this, profile, e, mediaSource);
        throw new EncoderException(this, e.getMessage(), e);
    }
}

From source file:org.opencastproject.composer.impl.AbstractCmdlineEncoderEngine.java

/**
 * Executes the command line encoder with the given set of files and properties and using the provided encoding
 * profile.// w w w  .  ja va  2 s .  c o m
 * 
 * @param audioSource
 *          the audio file (used when muxing)
 * @param videoSource
 *          the video file
 * @param profile
 *          the profile identifier
 * @param properties
 *          the encoding properties to be interpreted by the actual encoder implementation
 * @return the processed file
 * @throws EncoderException
 *           if processing fails
 */
protected Option<File> process(File audioSource, File videoSource, EncodingProfile profile,
        Map<String, String> properties) throws EncoderException {
    // Fist, update the parameters
    if (properties != null)
        params.putAll(properties);
    // build command
    BufferedReader in = null;
    Process encoderProcess = null;
    if (videoSource == null && audioSource == null) {
        throw new IllegalArgumentException("At least one track must be specified.");
    }
    try {
        // Set encoding parameters
        String audioInput = null;
        if (audioSource != null) {
            audioInput = FilenameUtils.normalize(audioSource.getAbsolutePath());
            params.put("in.audio.path", audioInput);
            params.put("in.audio.name", FilenameUtils.getBaseName(audioInput));
            params.put("in.audio.suffix", FilenameUtils.getExtension(audioInput));
            params.put("in.audio.filename", FilenameUtils.getName(audioInput));
            params.put("in.audio.mimetype",
                    MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(audioInput));
        }
        if (videoSource != null) {
            String videoInput = FilenameUtils.normalize(videoSource.getAbsolutePath());
            params.put("in.video.path", videoInput);
            params.put("in.video.name", FilenameUtils.getBaseName(videoInput));
            params.put("in.video.suffix", FilenameUtils.getExtension(videoInput));
            params.put("in.video.filename", FilenameUtils.getName(videoInput));
            params.put("in.video.mimetype",
                    MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(videoInput));
        }
        File parentFile;
        if (videoSource == null) {
            parentFile = audioSource;
        } else {
            parentFile = videoSource;
        }
        String outDir = parentFile.getAbsoluteFile().getParent();
        String outFileName = FilenameUtils.getBaseName(parentFile.getName());
        String outSuffix = processParameters(profile.getSuffix());

        if (params.containsKey("time")) {
            outFileName += "_" + properties.get("time");
        }

        // generate random name if multiple jobs are producing file with identical name (MH-7673)
        outFileName += "_" + UUID.randomUUID().toString();

        params.put("out.dir", outDir);
        params.put("out.name", outFileName);
        params.put("out.suffix", outSuffix);

        // create encoder process.
        // no special working dir is set which means the working dir of the
        // current java process is used.
        // TODO: Parallelisation (threading)
        List<String> command = buildCommand(profile);
        StringBuilder sb = new StringBuilder();
        for (String cmd : command) {
            sb.append(cmd);
            sb.append(" ");
        }
        logger.info("Executing encoding command: {}", sb);
        ProcessBuilder pbuilder = new ProcessBuilder(command);
        pbuilder.redirectErrorStream(REDIRECT_ERROR_STREAM);
        encoderProcess = pbuilder.start();

        // tell encoder listeners about output
        in = new BufferedReader(new InputStreamReader(encoderProcess.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            handleEncoderOutput(profile, line, audioSource, videoSource);
        }

        // wait until the task is finished
        encoderProcess.waitFor();
        int exitCode = encoderProcess.exitValue();
        if (exitCode != 0) {
            throw new EncoderException(this, "Encoder exited abnormally with status " + exitCode);
        }

        if (audioSource != null) {
            logger.info("Audio track {} and video track {} successfully encoded using profile '{}'",
                    new String[] { (audioSource == null ? "N/A" : audioSource.getName()),
                            (videoSource == null ? "N/A" : videoSource.getName()), profile.getIdentifier() });
        } else {
            logger.info("Video track {} successfully encoded using profile '{}'",
                    new String[] { videoSource.getName(), profile.getIdentifier() });
        }
        fireEncoded(this, profile, audioSource, videoSource);
        if (profile.getOutputType() != EncodingProfile.MediaType.Nothing)
            return some(new File(parentFile.getParent(), outFileName + outSuffix));
        else
            return none();
    } catch (EncoderException e) {
        if (audioSource != null) {
            logger.warn("Error while encoding audio track {} and video track {} using '{}': {}",
                    new String[] { (audioSource == null ? "N/A" : audioSource.getName()),
                            (videoSource == null ? "N/A" : videoSource.getName()), profile.getIdentifier(),
                            e.getMessage() });
        } else {
            logger.warn("Error while encoding video track {} using '{}': {}",
                    new String[] { (videoSource == null ? "N/A" : videoSource.getName()),
                            profile.getIdentifier(), e.getMessage() });
        }
        fireEncodingFailed(this, profile, e, audioSource, videoSource);
        throw e;
    } catch (Exception e) {
        logger.warn("Error while encoding audio {} and video {} to {}:{}, {}",
                new Object[] { (audioSource == null ? "N/A" : audioSource.getName()),
                        (videoSource == null ? "N/A" : videoSource.getName()), profile.getName(),
                        e.getMessage() });
        fireEncodingFailed(this, profile, e, audioSource, videoSource);
        throw new EncoderException(this, e.getMessage(), e);
    } finally {
        IoSupport.closeQuietly(in);
        IoSupport.closeQuietly(encoderProcess);
    }
}

From source file:org.opencastproject.composer.impl.VideoConcaternationTest.java

/**
 * Test method for//w w  w. j  av a2 s .c  o m
 * {@link org.opencastproject.composer.impl.ComposerServiceImpl#convertImage(org.opencastproject.mediapackage.Attachment, String)}
 */
@Test
@Ignore
public void testConcat() throws Exception {
    if (!ffmpegInstalled)
        return;
    String videoName1 = "video.mp4";
    String videoName2 = "av.mov";
    URL sourceVideo1Url = getClass().getResource("/" + videoName1);
    URL sourceVideo2Url = getClass().getResource("/" + videoName2);
    File sourceVideo1 = new File(workingDirectory, videoName1);
    File sourceVideo2 = new File(workingDirectory, videoName2);
    File sourceTextFile = new File(workingDirectory, "filesToConcat.txt");
    FileUtils.copyURLToFile(sourceVideo1Url, sourceVideo1);
    FileUtils.copyURLToFile(sourceVideo2Url, sourceVideo2);

    StringBuilder sb = new StringBuilder();
    sb.append("file '").append(sourceVideo1.getAbsolutePath()).append("'").append(IOUtils.LINE_SEPARATOR);
    sb.append("file '").append(sourceVideo2.getAbsolutePath()).append("'").append(IOUtils.LINE_SEPARATOR);
    FileUtils.writeStringToFile(sourceTextFile, sb.toString());

    EncodingProfile imageConversionProfile = profiles.get("concat");
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("videoListFile", FilenameUtils.normalize(sourceTextFile.getAbsolutePath()));
    File concatenedVideo = engine.encode(sourceVideo1, imageConversionProfile, properties).get();

    // These are weak assertions, but anything else would require either integration with another 3rd party tool
    // or manual parsing of ffmpeg output. Instead, we keep this test generic (but weak).
    assertTrue(concatenedVideo.exists());
    assertTrue(concatenedVideo.length() > 0);

}

From source file:org.opensextant.xtext.collectors.web.HyperLink.java

/**
 * URL wrangling, mainly to take a found URL and adapt it so it looks like a file path safe for a file system.
 *
 * @param link// www .j av  a2s .  c  om
 *            found link
 * @param referringLink
 *            - Normalized, absolute URL string
 * @param site
 *            top level site
 * @throws MalformedURLException
 *             on err
 * @throws NoSuchAlgorithmException
 *             on err
 * @throws UnsupportedEncodingException
 *             on err, when URL contains poorly encoded characters
 */
public HyperLink(String link, URL referringLink, URL site)
        throws MalformedURLException, NoSuchAlgorithmException, UnsupportedEncodingException {
    urlValue = link;
    urlNominal = link;
    siteURL = site;
    siteValue = site.toString();
    referrerURL = referringLink;
    String url_lc = urlNominal.toLowerCase();
    String site_lc = siteValue.toLowerCase();
    // If referrer, e.g. page containing this link is a folder or file, detect that.
    //   "/a/b/c"  is a folder but ensure referrer is tracked as "/a/b/c/" with trailing slash here.
    //  Otherwise, url is a page.
    String base_lc = referrerURL.toString().toLowerCase();
    boolean isReferrerFolder = false;
    String urlPath = null;

    isAbsolute = (url_lc.startsWith("http:") || url_lc.startsWith("https:"));

    if (!isAbsolute) {
        absoluteURL = new URL(referrerURL, urlValue);
        urlValue = absoluteURL.toString();
    } else {
        absoluteURL = new URL(urlValue);
    }

    // Use this to represent the object identity.
    linkId = TextUtils.text_id(getAbsoluteURL());
    query = absoluteURL.getQuery();

    urlPath = absoluteURL.getPath().toLowerCase();
    pathExtension = FilenameUtils.getExtension(urlPath);
    String referrerExt = FilenameUtils.getExtension(base_lc);

    isFolder = isFolder(url_lc, pathExtension);
    isReferrerFolder = isFolder(referrerURL.getPath(), referrerExt);

    String abs_lc = absoluteURL.toString().toLowerCase();

    String path = absoluteURL.getPath();
    if (isBlank(path)) {
        normalizedPath = "./";
        isFolder = true;
    } else {
        normalizedPath = path;
        if (normalizedPath.endsWith("/")) {
            normalizedPath = normalizedPath.substring(0, normalizedPath.length() - 1);
        }
    }

    // Optional
    boolean derivedPath = deriveFilepathFromQuery();

    if (!derivedPath) {
        String p = FilenameUtils.normalize(normalizedPath);
        if (p == null) {
            throw new MalformedURLException("Unable to parse/normalize path for: " + normalizedPath);
        }
        normalizedPath = p;
    }

    if (isFolder) {
        directory = new File(normalizedPath).getPath();
    } else {
        directory = new File(normalizedPath).getParent();
    }

    if (directory == null) {
        directory = path;
    }

    if (!isFolder) {
        archiveFileExtension = FilenameUtils.getExtension(normalizedPath);
    }

    // If base/referring page is a directory see if it is in same folder
    // as current link
    //
    String dirB = base_lc;
    if (isReferrerFolder && !dirB.endsWith("/")) {
        dirB = dirB + "/";
    } else if (!isReferrerFolder) {
        int b = base_lc.lastIndexOf('/');
        dirB = base_lc.substring(0, b);
    }

    int s = site_lc.lastIndexOf('/');
    String siteDir = site_lc.substring(0, s);

    isCurrentSite = abs_lc.startsWith(siteDir);
    if (isCurrentSite) {
        if (isFolder) {
            isCurrentPage = abs_lc.startsWith(dirB);
        } else {
            int a = abs_lc.lastIndexOf('/');
            String dirA = abs_lc.substring(0, a) + "/";
            isCurrentPage = dirA.startsWith(dirB);
        }
    }
    String linkHost = absoluteURL.getHost();
    String siteHost = siteURL.getHost();
    isCurrentHost = linkHost.equalsIgnoreCase(siteHost);
}

From source file:org.oxymores.chronix.core.ChronixContext.java

/**
 * Creates a minimal Context with no applications loaded.
 * //from  w  ww . ja v  a  2  s .  c  om
 * @param appConfDirectory
 * @param transacUnitName
 * @param historyUnitName
 * @param brokerInterface
 * @param simulation
 * @return
 */
public static ChronixContext initContext(String appConfDirectory, String transacUnitName,
        String historyUnitName, String brokerInterface, boolean simulation) {
    ChronixContext ctx = new ChronixContext();
    ctx.loaded = org.joda.time.DateTime.now();
    ctx.configurationDirectoryPath = FilenameUtils.normalize(appConfDirectory);
    ctx.configurationDirectory = new File(ctx.configurationDirectoryPath);
    ctx.applicationsById = new HashMap<UUID, Application>();
    ctx.applicationsByName = new HashMap<String, Application>();
    ctx.historyUnitName = historyUnitName;
    ctx.transacUnitName = transacUnitName;
    ctx.localUrl = brokerInterface;
    ctx.dns = ctx.localUrl.split(":")[0];
    ctx.port = Integer.parseInt(ctx.localUrl.split(":")[1]);
    ctx.setSimulation(simulation);

    return ctx;
}

From source file:org.oxymores.chronix.engine.Runner.java

public void startListening(Broker broker) throws ChronixInitializationException {
    try {/*from w  w  w  .j av a 2  s  . c  om*/
        this.init(broker, true, false);

        // Log repository
        this.logDbPath = FilenameUtils.normalize(FilenameUtils.concat(ctx.getContextRoot(), "GLOBALJOBLOG"));
        if (!(new File(this.logDbPath)).exists() && !(new File(this.logDbPath)).mkdir()) {
            throw new ChronixInitializationException("Could not create directory " + this.logDbPath);
        }

        // Internal queue
        resolving = new ArrayList<PipelineJob>();

        // Log
        this.qName = String.format(Constants.Q_RUNNERMGR, brokerName);
        log.debug(String.format("(%s) Registering a jobrunner listener on queue %s", ctx.getContextRoot(),
                qName));

        // Outgoing producer for running commands
        this.producerRunDescription = this.jmsSession.createProducer(null);
        this.producerHistory = this.jmsSession.createProducer(null);
        this.producerEvents = this.jmsSession.createProducer(null);

        // Register on Log Shipping queue
        this.subscribeTo(String.format(Constants.Q_LOGFILE, brokerName));

        // Register on Request queue
        this.subscribeTo(qName);

        // Register on End of job queue
        destEndJob = this.subscribeTo(String.format(Constants.Q_ENDOFJOB, brokerName));
    } catch (JMSException e) {
        throw new ChronixInitializationException("Could not create a Runner", e);
    }
}

From source file:org.oxymores.chronix.engine.RunnerAgent.java

void startListening(Broker broker) throws JMSException, IOException {
    this.init(broker, false, false);
    log.info(String.format("(%s) Starting a runner agent", brokerName));

    // Log repository
    this.logDbPath = FilenameUtils.normalize(FilenameUtils.concat(ctx.getContextRoot(), "LOCALJOBLOG"));
    if (!(new File(this.logDbPath)).exists()) {
        (new File(this.logDbPath)).mkdir();
    }//from   w w  w. j  a  va 2s.c om

    // Producer to send run results
    this.jmsProducer = this.jmsSession.createProducer(null);

    // Queue listener
    qName = String.format(Constants.Q_RUNNER, brokerName);
    this.subscribeTo(qName);
}

From source file:org.panbox.desktop.common.vfs.backend.generic.GenericVirtualVolumeImpl.java

@Override
public VirtualFile getFile(String fileName) throws IOException {
    File f = fileName.startsWith(realFile.getAbsolutePath()) ? new File(fileName)
            : new File(realFile, fileName);

    // TODO: filename normalization at higher level
    // only return one VirtualFile-instance for a given normalized filename
    String normalizedName = FilenameUtils.normalize(f.getAbsolutePath());
    return new GenericVirtualFileImpl(normalizedName, this);
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootMultiuserVolume.java

/**
 * Gets the share key as SecretKey instance for the specified file. This
 * will iterate over all shares and check which share contains this file. If
 * the share notifies, that it contains the file, the share key of this
 * share will be used as files' share key.
 * //  w  w w .  j a  v  a2s.  c  o m
 * @param fileName
 *            Relative path of the file.
 * @return SecretKey instance of the share key of the share that contains
 *         the specified file.
 */
public synchronized SecretKey getObfuscationKeyForFile(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);

    for (VirtualMultiuserRootFile f : rootFilesPerUser.values()) {
        for (VFSShare share : f.getShares()) {
            if (share.contains(normalizedFileName)) {
                return share.getObfuscationKey();
            }
        }
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootMultiuserVolume.java

public synchronized Obfuscator getObfuscator(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);

    for (VirtualMultiuserRootFile f : rootFilesPerUser.values()) {
        for (VFSShare share : f.getShares()) {
            if (share.contains(normalizedFileName)) {
                return share.getObfuscator();
            }/*from  w  w  w .ja va 2 s. c om*/
        }
    }
    return null;
}