Example usage for org.apache.shiro.io ResourceUtils FILE_PREFIX

List of usage examples for org.apache.shiro.io ResourceUtils FILE_PREFIX

Introduction

In this page you can find the example usage for org.apache.shiro.io ResourceUtils FILE_PREFIX.

Prototype

String FILE_PREFIX

To view the source code for org.apache.shiro.io ResourceUtils FILE_PREFIX.

Click Source Link

Document

Resource path prefix that specifies to load from a file location, value is file:

Usage

From source file:uk.co.q3c.v7.base.config.ConfigUtil.java

License:Apache License

/**
 * Shiro uses a context prefix with its resource path {@link ResourceUtils#getInputStreamForPath(String)}. This
 * method allows the inclusion of a system variable within the file path specification, expands the variable, and
 * returns the result in a format suitable for use with Shiro ResourceUtils
 * /*from   w  w  w. j av  a2s  . c o m*/
 * @param resourcePath
 *            a resource path of the form "file:$user.home/directory/filename". If there is no "file:" prefix, or
 *            variable specified, <code>resourcePath</code> is returned unmodified.
 * @return the resourcePath with the system variable expanded. This "file:$user.home/directory/filename" will be
 *         returned, for example as "file:/home/david/directory/filename"
 */
public static String shiroFilePathWithExpandedVariable(String resourcePath) {
    if (resourcePath.startsWith(ResourceUtils.FILE_PREFIX)) {
        int c1 = resourcePath.indexOf('$');
        int c2 = resourcePath.indexOf('/');
        int c3 = resourcePath.lastIndexOf('/');
        int c4 = resourcePath.length();
        String base = (c1 < 0) ? null : resourcePath.substring(c1, c2);
        if (Strings.isNullOrEmpty(base)) {
            return resourcePath;
        }
        String directory = resourcePath.substring(c2, c3);
        String filename = resourcePath.substring(c3, c4);
        return "file:" + fileFromPathWithVariable(base, directory, filename);
    } else {
        return resourcePath;
    }
}

From source file:uk.co.q3c.v7.base.config.ConfigUtilTest.java

License:Apache License

@Test
public void shiroFilePathWithExpandedVariable_fullSpec() {

    // given//w  w w .  ja va2 s . co m
    resourcePath = ResourceUtils.FILE_PREFIX + "$user.home/directory/filename.txt";
    String expected = "file:" + buildPath("directory", "filename.txt");
    // when
    String result = ConfigUtil.shiroFilePathWithExpandedVariable(resourcePath);
    // then
    assertThat(result).isEqualTo(expected);

}

From source file:uk.co.q3c.v7.base.config.ConfigUtilTest.java

License:Apache License

@Test
public void shiroFilePathWithExpandedVariable_nobase_noLeadingSlash() {

    // given/*from  ww  w  .  jav  a 2s  .co  m*/
    resourcePath = ResourceUtils.FILE_PREFIX + "directory/filename.txt";
    // when
    String result = ConfigUtil.shiroFilePathWithExpandedVariable(resourcePath);
    // then
    assertThat(result).isEqualTo(resourcePath);
}

From source file:uk.co.q3c.v7.base.config.ConfigUtilTest.java

License:Apache License

@Test
public void shiroFilePathWithExpandedVariable_nobase_withLeadingSlash() {

    // given//from w  w w .  j  a  va  2s.  c  o  m
    resourcePath = ResourceUtils.FILE_PREFIX + "/directory/filename.txt";
    // when
    String result = ConfigUtil.shiroFilePathWithExpandedVariable(resourcePath);
    // then
    assertThat(result).isEqualTo(resourcePath);
}

From source file:uk.co.q3c.v7.base.config.ConfigUtilTest.java

License:Apache License

@Test
public void shiroFilePathWithExpandedVariable_noDirectory() {

    // given//w  w  w  .ja  v  a2 s. c  om
    resourcePath = ResourceUtils.FILE_PREFIX + "$user.home/filename.txt";
    String expected = "file:" + buildPath("filename.txt");
    // when
    String result = ConfigUtil.shiroFilePathWithExpandedVariable(resourcePath);
    // then
    assertThat(result).isEqualTo(expected);

}