public final void testGetResourceForPath() {
   replay(servletContext);
   Resource resource = resourceLoader.getResource("/mypath", null);
   assertTrue(resource instanceof ServletContextResource);
   ServletContextResource r = (ServletContextResource) resource;
   assertSame(servletContext, r.getServletContext());
   verify(servletContext);
 }
Пример #2
0
 public InputStream loadInputStream() throws IOException {
   InputStream in = null;
   ServletContextResource resource = new ServletContextResource(servletContext, path);
   {
     if (resource.exists()) {
       in = resource.getInputStream();
     }
   }
   return in;
 }
Пример #3
0
  /**
   * Creates an {@link InputStream} to the requested path. This will look in both the defined
   * classpath and within the META-INF of the application.
   *
   * @param path The path to generate an {@link InputStream} to.
   * @return The {@link InputStream} for the requested resource and <code>null</code> if the
   *     resource could not be found.
   * @throws IOException Thrown when an error occurs attempting to generate an {@link InputStream}
   */
  public InputStream getResourceInputStream(String path) throws IOException {
    InputStream in = null;

    // TODO: Check the sentinel isn't returned...
    ResourceInfo resourceInfo = getCachedResourceInfo(path);
    if (resourceInfo == getResourceInfoSentinel()) {
      // No action required - the ResourceInfoSentinel indicates that we've previously searched
      // for this path and the resource couldn't be found.
    } else if (resourceInfo != null) {
      in = resourceInfo.getInputStream();
    } else {
      // Generate a list of paths that should be checked for the current mode...
      List<String> pathsToCheck = generatePathsForClientMode(path);
      Iterator<String> paths;
      if (this.webFrameworkConfigElement.isRemoteResourceResolvingEnabled()) {
        paths = pathsToCheck.iterator();
        while (in == null && paths.hasNext()) {
          String currPath = paths.next();
          in = this.remoteResourcesHandler.getRemoteResource(currPath);
          if (in != null) {
            addResourceInfoToCache(path, new RemoteResource(currPath));
            in = this.getCachedResourceInfo(path).getInputStream();
          }
        }
      }

      paths = pathsToCheck.iterator();
      while (in == null && paths.hasNext()) {
        String currPath = paths.next();
        Resource r = applicationContext.getResource("classpath*:" + currPath);
        if (r != null && r.exists()) {
          addResourceInfoToCache(path, new ApplicationContextResource(currPath));
          in = this.getCachedResourceInfo(path).getInputStream();
        }
      }

      paths = pathsToCheck.iterator();
      while (in == null && paths.hasNext()) {
        String currPath = paths.next();
        if (currPath.startsWith("/")) {
          currPath = currPath.substring(1);
        }
        URL resourceUrl = ClassUtils.getDefaultClassLoader().getResource("META-INF/" + currPath);
        if (resourceUrl != null) {
          addResourceInfoToCache(path, new ClassLoaderResource(currPath));
          in = this.getCachedResourceInfo(path).getInputStream();
        }
      }

      paths = pathsToCheck.iterator();
      while (this.servletContext != null && in == null && paths.hasNext()) {
        final String p = paths.next();
        // servlet resource paths cannot start with a relative operator - they should always begin
        // at the root - note that ServletContextResource() will prepend "/" automatically to all
        // paths
        // and "/../something" is invalid for most modern containers
        if (!StringUtils.cleanPath(p).startsWith("..")) {
          ServletContextResource resource = new ServletContextResource(this.servletContext, p);
          {
            if (resource.exists()) {
              addResourceInfoToCache(path, new ServletContextRes(p));
              in = this.getCachedResourceInfo(path).getInputStream();
            }
          }
        }
      }
    }

    // If the input stream is still null, add a sentinel...
    if (in == null) {
      addResourceInfoToCache(path, getResourceInfoSentinel());
    }
    return in;
  }