Example usage for org.springframework.web.context.support ServletContextResourcePatternResolver ServletContextResourcePatternResolver

List of usage examples for org.springframework.web.context.support ServletContextResourcePatternResolver ServletContextResourcePatternResolver

Introduction

In this page you can find the example usage for org.springframework.web.context.support ServletContextResourcePatternResolver ServletContextResourcePatternResolver.

Prototype

public ServletContextResourcePatternResolver(ResourceLoader resourceLoader) 

Source Link

Document

Create a new ServletContextResourcePatternResolver.

Usage

From source file:de.pksoftware.springstrap.sapi.util.WebappZipper.java

/**
  * Zip it//w  ww  .  j  a v  a2s. c o  m
  * @param zipFile output ZIP file location
  */
public void addResource(String resourcePath, boolean fromContext) {
    byte[] buffer = new byte[1024];

    try {
        logger.info("Adding resource: {}", resourcePath);

        PathMatchingResourcePatternResolver resolver;

        if (fromContext) {
            resolver = new ServletContextResourcePatternResolver(servletContext);
        } else {
            resolver = new PathMatchingResourcePatternResolver();
        }

        // Ant-style path matching
        Resource[] resources = resolver.getResources(resourcePath);

        for (Resource resource : resources) {
            String fileAbsolute = resource.getURL().toString();
            String fileRelative = null;
            boolean addFile = false;

            if (fromContext) {
                //File comes from webapp folder
                fileRelative = "www" + servletContext.getContextPath()
                        + ((ServletContextResource) resource).getPath();
                if (!fileRelative.endsWith("/")) {
                    addFile = true;
                }

            } else {
                //Files comes from webjar
                int jarSeparatorIndex = fileAbsolute.indexOf("!/");

                if (jarSeparatorIndex != -1) {
                    String relativeFileName = fileAbsolute.substring(jarSeparatorIndex);

                    Pattern pathPattern = Pattern.compile("\\!\\/webjar(\\/[a-z0-9_]+)+\\/www\\/");
                    Matcher pathMatcher = pathPattern.matcher(relativeFileName);
                    if (pathMatcher.find()) {
                        fileRelative = pathMatcher.replaceFirst("www" + servletContext.getContextPath() + "/");
                        addFile = true;
                    }
                }
            }

            if (addFile && null != fileRelative) {
                logger.debug("Adding File: {} => {}", fileAbsolute, fileRelative);

                if (null != files.get(fileRelative)) {
                    continue;
                }

                if (fileRelative.startsWith("temp")) {
                    logger.error(fileAbsolute);
                }

                files.put(fileRelative, resource);

                ZipEntry ze = new ZipEntry(fileRelative);
                zos.putNextEntry(ze);

                InputStream in = null;
                try {
                    in = resource.getInputStream();

                    int len;
                    while ((len = in.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                } catch (FileNotFoundException fio) {
                    logger.error(fio.getMessage());
                } finally {
                    if (null != in) {
                        in.close();
                    }
                }
            }
        }

        zos.closeEntry();
        //remember close it

        logger.info("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.jnap.core.assets.StaticAssetsHandler.java

@PostConstruct
public void init() throws Exception {
    Assert.notNull(servletContext);/*from   ww  w.java 2  s. c o m*/
    this.resourceResolver = new ServletContextResourcePatternResolver(this.servletContext);
    if (this.shouldInit()) {
        this.handle();
        if (monitor) {
            fileMonitor = new FileAlterationMonitor(this.monitorInterval);
            FileAlterationObserver observer = new FileAlterationObserver(path.getFile());
            observer.addListener(new FileAlterationListenerAdaptor() {
                public void onFileCreate(File file) {
                    StaticAssetsHandler.this.handle();
                }

                public void onFileChange(File file) {
                    StaticAssetsHandler.this.handle();
                }

                public void onFileDelete(File file) {
                    StaticAssetsHandler.this.handle();
                }
            });
            fileMonitor.addObserver(observer);
            fileMonitor.start();
            logger.info(getClass().getSimpleName() + " started and watching for file changes...");
        }
        logger.info(getClass().getSimpleName() + " executed!");
    }
}

From source file:com.haulmont.cuba.core.sys.dbupdate.ScriptScanner.java

protected ResourcePatternResolver createAppropriateResourceResolver() {
    if (dbScriptsDirectory.startsWith(WEB_INF_LABEL)) {
        return new ServletContextResourcePatternResolver(ServletContextHolder.getServletContext());
    } else {//from  w w  w. java2  s.  c o m
        return new PathMatchingResourcePatternResolver();
    }
}

From source file:com.productone.spring.ServerOsgiBundleXmlWebApplicationContext.java

/**
 * {@inheritDoc}//from  w  ww  .  j a va  2s  .  c o  m
 */
public void setServletContext(final ServletContext servletContext) {

    this.servletContext = servletContext;
    this.servletContextResourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);

    // If the BundleContext has not yet been set, attempt to retrieve it from the ServletContext or the parent
    // ApplicationContext.
    if (getBundleContext() == null) {
        getBundleContext();
    }
}

From source file:net.solarnetwork.web.gemini.ServerOsgiBundleXmlWebApplicationContext.java

/**
 * {@inheritDoc}/* w  w  w  .ja  va2 s  .c  o  m*/
 */
@Override
public void setServletContext(final ServletContext servletContext) {

    this.servletContext = servletContext;
    this.servletContextResourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);

    // If the BundleContext has not yet been set, attempt to retrieve it from the ServletContext or the parent
    // ApplicationContext.
    if (getBundleContext() == null) {
        getBundleContext();
    }
}

From source file:fi.eis.applications.helpers.ServerOsgiBundleXmlWebApplicationContext.java

/**
 * {@inheritDoc}//from  w w  w.java  2 s.c  o m
 */
public void setServletContext(final ServletContext servletContext) {

    this.servletContext = servletContext;
    this.servletContextResourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);

    // If the BundleContext has not yet been set, attempt to retrieve it
    // from the ServletContext or the parent
    // ApplicationContext.
    if (getBundleContext() == null) {
        getBundleContext();
    }
}

From source file:ch.rasc.edsutil.optimizer.WebResourceProcessor.java

private List<Resource> enumerateResourcesFromWebapp(final String line, final String suffix) throws IOException {
    if (line.endsWith("/")) {
        ServletContextResourcePatternResolver resourceResolver = new ServletContextResourcePatternResolver(
                this.servletContext);
        String location = line + "**/*" + suffix;
        Resource[] resources = resourceResolver.getResources(location);
        return Arrays.asList(resources);
    }/*from w w  w .j  ava 2 s  .  com*/

    if (line.endsWith(suffix)) {
        return Collections.singletonList(new ServletContextResource(this.servletContext, line));
    }

    return Collections.emptyList();
}

From source file:org.apache.cocoon.core.container.spring.avalon.ConfigurationReader.java

private ConfigurationReader(ConfigurationInfo parentInfo, ResourceLoader resourceLoader) throws Exception {
    if (resourceLoader == null) {
        throw new IllegalArgumentException("ResourceLoader not set!");
    }//from  ww  w.  j  a v a 2s.  c om
    this.isRootContext = parentInfo == null;
    this.resolver = new ServletContextResourcePatternResolver(resourceLoader);

    // now add selectors from parent
    if (parentInfo != null) {
        this.configInfo = new ConfigurationInfo(parentInfo);

        final Iterator i = parentInfo.getComponents().values().iterator();
        while (i.hasNext()) {
            final ComponentInfo current = (ComponentInfo) i.next();
            if (current.isSelector()) {
                this.configInfo.addRole(current.getRole(), current.copy());
            }
        }

        /* TODO - we should add the processor to each container
              This would avoid the hacky getting of the current container in the tree processor
        ComponentInfo processorInfo = (ComponentInfo) parentInfo.getComponents().get(Processor.ROLE);
        if (processorInfo != null) {
        this.configInfo.getComponents().put(Processor.ROLE, processorInfo.copy());
        }
        */
    } else {
        this.configInfo = new ConfigurationInfo();
    }
}