Example usage for javax.servlet ServletContext getResourcePaths

List of usage examples for javax.servlet ServletContext getResourcePaths

Introduction

In this page you can find the example usage for javax.servlet ServletContext getResourcePaths.

Prototype

public Set<String> getResourcePaths(String path);

Source Link

Document

Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument.

Usage

From source file:edu.cornell.mannlib.vitro.webapp.i18n.selection.LocaleSelectorUtilities.java

/**
 * Look in the current theme directory to find a selection image for this
 * Locale.//  w  w w.  ja  va2s.  c om
 * 
 * Images are expected at a resource path like
 * /[themeDir]/i18n/images/select_locale_[locale_code].*
 * 
 * For example, /themes/wilma/i18n/images/select_locale_en.png
 * /themes/wilma/i18n/images/select_locale_en.JPEG
 * /themes/wilma/i18n/images/select_locale_en.gif
 * 
 * To create a proper URL, prepend the context path.
 */
public static String getImageUrl(VitroRequest vreq, Locale locale) throws FileNotFoundException {
    String filename = "select_locale_" + locale + ".";

    String themeDir = vreq.getAppBean().getThemeDir();
    String imageDirPath = "/" + themeDir + "i18n/images/";

    ServletContext ctx = vreq.getSession().getServletContext();
    @SuppressWarnings("unchecked")
    Set<String> resourcePaths = ctx.getResourcePaths(imageDirPath);
    if (resourcePaths != null) {
        for (String resourcePath : resourcePaths) {
            if (resourcePath.contains(filename)) {
                String fullPath = vreq.getContextPath() + resourcePath;
                log.debug("Found image for " + locale + " at '" + fullPath + "'");
                return fullPath;
            }
        }
    }
    throw new FileNotFoundException("Can't find an image for " + locale);
}

From source file:fr.norad.servlet.sample.html.template.ContextUtils.java

public static void servletContextResourceToFile(ServletContext context, String contextPath, String output)
        throws IOException {
    @SuppressWarnings("unchecked")
    Set<String> resources = context.getResourcePaths(contextPath);
    if (resources == null) {
        new File(output + contextPath).mkdirs();
        return;/*from  w  ww  . j a  v a  2s  .  c om*/
    }
    for (String resource : resources) {
        InputStream resourceAsStream = context.getResourceAsStream(resource);
        if (resourceAsStream == null) {
            // this should be a directory
            servletContextResourceToFile(context, resource, output);
        } else { // this is a file
            try {
                File full = new File(output + resource);
                full.getParentFile().mkdirs();
                FileUtils.copyInputStreamToFile(resourceAsStream, full);
            } finally {
                resourceAsStream.close();
            }
        }
    }
}

From source file:com.concursive.connect.scheduler.ScheduledJobs.java

/**
 * Scans the jobs path for the given ServletContext
 *
 * @param scheduler//from   w w w.j  a va  2s .  co  m
 * @param context
 * @throws SchedulerException
 */
public static void addJobs(Scheduler scheduler, ServletContext context) throws SchedulerException {
    // Determine the ApplicationPrefs
    ApplicationPrefs prefs = (ApplicationPrefs) scheduler.getContext().get("ApplicationPrefs");
    // Find job files in the jobs path
    Set<String> jobFiles = context.getResourcePaths("/WEB-INF/jobs/");
    if (jobFiles != null && jobFiles.size() > 0) {
        for (String thisFile : jobFiles) {
            if (thisFile.endsWith(".xml")) {
                try {
                    LOG.debug("Adding jobs from... " + thisFile);
                    QuartzUtils.addJobs(scheduler, context.getResource(thisFile), prefs.getPrefs());
                } catch (Exception e) {
                    LOG.error("addJobs exception", e);
                }
            }
        }
    }
}

From source file:io.lightlink.utils.ClasspathScanUtils.java

private static void findFromServletContext(ServletContext servletContext, String initialPath,
        String currentPath, List<String> res) {
    String totalPath = initialPath + currentPath;
    if (totalPath.endsWith("/")) {
        Set<String> paths = servletContext.getResourcePaths(totalPath);
        if (paths != null)
            for (String p : paths) {
                findFromServletContext(servletContext, initialPath, p.substring(initialPath.length()), res);
            }/*from  w  ww .j  av a  2s .  c o m*/
    } else
        res.add(currentPath);
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.jena.ExtendedLinkedDataUtils.java

public static Model createModelFromQueries(ServletContext sc, String rootDir, OntModel sourceModel,
        String subject) {/*from  w  ww.  ja va 2s  . c om*/
    log.debug("Exploring queries in directory '" + rootDir + "'");

    Model model = ModelFactory.createDefaultModel();

    @SuppressWarnings("unchecked")
    Set<String> pathSet = sc.getResourcePaths(rootDir);

    if (pathSet == null) {
        log.warn(rootDir + " not found.");
        return model;
    }

    for (String path : pathSet) {
        File file = new File(sc.getRealPath(path));
        if (file.isDirectory()) {
            model.add(createModelFromQueries(sc, path, sourceModel, subject));
        } else if (file.isFile()) {
            if (!path.endsWith(".sparql")) {
                log.warn("Ignoring file " + path + " because the file extension is not sparql.");
                continue;
            }
            model.add(createModelFromQuery(file, sourceModel, subject));
            log.debug("model size is " + model.size() + " after query in '" + path + "'");
        } else {
            log.warn("path is neither a directory nor a file " + path);
        }
    } // end - for

    return model;
}

From source file:io.lavagna.web.support.ResourceController.java

private static void extractFilesWithExtensionRec(ServletContext context, String initialPath, String extension,
        Set<String> res) {
    for (String s : context.getResourcePaths(initialPath)) {
        if (s.endsWith("/")) {
            extractFilesWithExtensionRec(context, s, extension, res);
        } else if (s.endsWith(extension)) {
            res.add(s);/*from  w  ww. j a v  a 2s.  com*/
        }
    }
}

From source file:io.lavagna.web.support.ResourceController.java

private static void concatenateResourcesWithExtension(ServletContext context, String initialPath,
        String extension, OutputStream os, BeforeAfter ba) throws IOException {
    for (String s : new TreeSet<>(context.getResourcePaths(initialPath))) {
        if (s.endsWith(extension)) {
            output(s, context, os, ba);// w  w  w. j a  va  2 s  . co  m
        } else if (s.endsWith("/")) {
            concatenateResourcesWithExtension(context, s, extension, os, ba);
        }
    }
}

From source file:com.tbodt.jswerve.servlet.JSwerveServlet.java

private void spiderWar(ServletContext ctx, String start, Set<Class<?>> classes) {
    for (String path : ctx.getResourcePaths(start))
        if (ctx.getResourcePaths(path) != null && !ctx.getResourcePaths(path).isEmpty())
            spiderWar(ctx, path, classes);
        else if (path.endsWith(".class"))
            try {
                classes.add(ctx.getClassLoader().loadClass(path
                        .substring("/WEB-INF/classes/".length(), path.indexOf(".class")).replace('/', '.')));
            } catch (ClassNotFoundException ex) {
                throw new WTFException("class" + ex.getMessage() + " not found! but I saw it!");
            }/*from ww w  .  jav a 2s . c  om*/
}

From source file:nl.tue.gale.ae.config.GaleContextLoader.java

private void findLocations(ServletContext sc, List<String> locations) {
    @SuppressWarnings("unchecked")
    Set<String> paths = (Set<String>) sc.getResourcePaths("/WEB-INF/lib/");
    for (String path : paths)
        if (path.endsWith(".jar"))
            findInJar(sc, path, locations);
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.SmokeTestController.java

private void readSmokeTestFilesFromPath(ServletContext context) {

    log.debug("Reading smoketest files from " + FILE_PATH);
    Set<String> paths = context.getResourcePaths(FILE_PATH);
    if (paths != null) {
        for (String p : paths) {
            readSmokeTestClassesFromFile(p, context);
        }/* w w w  .jav  a2  s . c o m*/
    }
}