예제 #1
0
  private boolean compressionPathIsValid(ClientResourceInfo resource) throws IOException {

    ExternalContext extContext = getFacesContext().getExternalContext();
    File tempDir = (File) extContext.getApplicationMap().get("javax.servlet.context.tempdir");
    File expected = new File(tempDir, "/jsf-compressed" + File.separatorChar + resource.getPath());
    return expected.getCanonicalPath().equals(resource.getCompressedPath());
  }
예제 #2
0
  public void testELEvalDisabledIfNoExpressionEvaluated() throws Exception {

    ResourceManager manager = new ResourceManager(null);
    ClientResourceInfo resource =
        (ClientResourceInfo)
            manager.findResource(null, "simple.css", "text/css", getFacesContext());
    assertNotNull(resource);
    assertTrue(resource.supportsEL());
    ResourceImpl resImpl = new ResourceImpl(resource, "text/css", 0, 0);
    InputStream in = resImpl.getInputStream();
    for (int i = in.read(); i != -1; i = in.read()) {}
    try {
      in.close();
    } catch (Exception ioe) {
      fail(ioe.toString());
    }
    assertTrue(!resource.supportsEL());

    resource =
        (ClientResourceInfo)
            manager.findResource(null, "simple-with-el.css", "text/css", getFacesContext());

    assertNotNull(resource);
    assertTrue(resource.supportsEL());
    resImpl = new ResourceImpl(resource, "text/css", 0, 0);
    in = resImpl.getInputStream();
    for (int i = in.read(); i != -1; i = in.read()) {}
    try {
      in.close();
    } catch (Exception ioe) {
      fail(ioe.toString());
    }
    assertTrue(resource.supportsEL());
  }
예제 #3
0
 public void testWebappVersionedResource() throws Exception {
   ClientResourceInfo resource =
       (ClientResourceInfo) manager.findResource(null, "duke.gif", "image/gif", getFacesContext());
   assertTrue(resource != null);
   assertTrue(resource.getLibraryInfo() == null);
   assertTrue(resource.getHelper() instanceof WebappResourceHelper);
   assertTrue(!resource.isCompressable());
   assertTrue(resource.getCompressedPath() == null);
   assertTrue("1_1".equals(resource.getVersion().toString()));
   assertTrue("duke.gif".equals(resource.getName()));
   assertTrue("/resources/duke.gif/1_1.gif".equals(resource.getPath()));
 }
예제 #4
0
 public void testJarNonVersionedResources() throws Exception {
   ClientResourceInfo resource =
       (ClientResourceInfo)
           manager.findResource(null, "duke-jar-nv.gif", "image/gif", getFacesContext());
   assertTrue(resource != null);
   assertTrue(resource.getLibraryInfo() == null);
   assertTrue(resource.getHelper() instanceof ClasspathResourceHelper);
   assertTrue(resource.getVersion() == null);
   assertTrue(!resource.isCompressable());
   assertTrue(resource.getCompressedPath() == null);
   assertTrue("duke-jar-nv.gif".equals(resource.getName()));
   assertTrue("META-INF/resources/duke-jar-nv.gif".equals(resource.getPath()));
 }
예제 #5
0
  public void testClientResourceInfoCompression() throws Exception {
    WebConfiguration config = WebConfiguration.getInstance();
    config.overrideContextInitParameter(
        WebConfiguration.WebContextInitParameter.CompressableMimeTypes,
        "image/gif,text/css,text/plain");
    // create a new ResourceManager so that the mime type configuration is picked up
    ResourceManager manager = new ResourceManager(null);
    ClientResourceInfo resource =
        (ClientResourceInfo)
            manager.findResource("nvLibrary", "duke-nv.gif", "image/gif", getFacesContext());
    assertTrue(resource != null);
    assertTrue(resource.isCompressable());
    assertTrue(compressionPathIsValid(resource));

    // ensure compression disabled for a content type that is null
    resource =
        (ClientResourceInfo)
            manager.findResource("nvLibrary", "duke-nv.gif", "text/javascript", getFacesContext());
    assertTrue(resource != null);
    assertTrue(!resource.isCompressable());
    assertTrue(resource.getCompressedPath() == null);

    // if a resource is compressable, but the compressed result is larger
    // than the original resource, the returned ClientResourceInfo shouldn't
    // be marked as compressable and getCompressedPath() will be null
    resource =
        (ClientResourceInfo)
            manager.findResource(null, "simple.txt", "text/plain", getFacesContext());
    assertTrue(resource != null);
    assertTrue(!resource.isCompressable());
    assertTrue(resource.getCompressedPath() == null);

    // if a resource is compressable, but the compressed result is larger
    // than the original resource, the returned ClientResourceInfo should be
    // marked compressable.  However, since css files may have EL expressions
    // embedded within, the the resource will be marked as supporting such.
    resource =
        (ClientResourceInfo)
            manager.findResource(null, "simple.css", "text/plain", getFacesContext());
    assertTrue(resource != null);
    assertTrue(resource.isCompressable());
    assertTrue(resource.supportsEL());
    assertTrue(resource.getCompressedPath() == null);
  }
  /**
   * @see ResourceHelper#findResource(LibraryInfo, String, String, boolean,
   *     javax.faces.context.FacesContext)
   */
  public ResourceInfo findResource(
      LibraryInfo library,
      String resourceName,
      String localePrefix,
      boolean compressable,
      FacesContext ctx) {

    resourceName = trimLeadingSlash(resourceName);
    ContractInfo[] outContract = new ContractInfo[1];
    outContract[0] = null;

    ClassLoader loader = Util.getCurrentLoader(this);
    String basePath;
    if (library != null) {
      basePath = library.getPath(localePrefix) + '/' + resourceName;
    } else {
      if (localePrefix == null) {
        basePath = getBaseResourcePath() + '/' + resourceName;
      } else {
        basePath = getBaseResourcePath() + '/' + localePrefix + '/' + resourceName;
      }
    }

    URL basePathURL = loader.getResource(basePath);
    if (basePathURL == null) {
      // try using this class' loader (necessary when running in OSGi)
      basePathURL = this.getClass().getClassLoader().getResource(basePath);
      if (basePathURL == null) {
        // Try it without the localePrefix
        if (library != null) {
          basePath = library.getPath(null) + '/' + resourceName;
        } else {
          basePath = getBaseResourcePath() + '/' + resourceName;
        }
        basePathURL = loader.getResource(basePath);
        if (basePathURL == null) {
          // try using this class' loader (necessary when running in OSGi)
          basePathURL = this.getClass().getClassLoader().getResource(basePath);
          if (basePathURL == null) {
            return null;
          }
        }

        localePrefix = null;
      }
    }

    ClientResourceInfo value;

    if (library != null) {
      value =
          new ClientResourceInfo(
              library,
              outContract[0],
              resourceName,
              null,
              compressable,
              resourceSupportsEL(resourceName, library.getName(), ctx),
              ctx.isProjectStage(ProjectStage.Development),
              cacheTimestamp);
    } else {
      value =
          new ClientResourceInfo(
              outContract[0],
              resourceName,
              null,
              localePrefix,
              this,
              compressable,
              resourceSupportsEL(resourceName, null, ctx),
              ctx.isProjectStage(ProjectStage.Development),
              cacheTimestamp);
    }

    if (value.isCompressable()) {
      value = handleCompression(value);
    }
    return value;
  }
예제 #7
0
  public void testNoExtensionVersionedResource() throws Exception {
    ClientResourceInfo resource =
        (ClientResourceInfo)
            manager.findResource("vLibrary", "duke2.gif", "image/gif", getFacesContext());
    assertTrue(resource != null);

    // validate the library
    assertTrue(resource.getLibraryInfo() != null);
    assertTrue("vLibrary".equals(resource.getLibraryInfo().getName()));
    assertTrue("2_0".equals(resource.getLibraryInfo().getVersion().toString()));
    assertTrue(resource.getLibraryInfo().getHelper() instanceof WebappResourceHelper);
    assertTrue("/resources/vLibrary/2_0".equals(resource.getLibraryInfo().getPath()));

    // validate the resource
    assertTrue(resource.getHelper() instanceof WebappResourceHelper);
    assertTrue(!resource.isCompressable());
    assertTrue(resource.getCompressedPath() == null);
    assertTrue("1_1".equals(resource.getVersion().toString()));
    assertTrue(resource.getVersion().getExtension() == null);
    assertTrue("duke2.gif".equals(resource.getName()));
    assertTrue("/resources/vLibrary/2_0/duke2.gif/1_1".equals(resource.getPath()));
  }