/** Added for issue 3331 */ public void testResourceELEvalAfterPrematureClosureOfStream() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); handler.createResource("simple-with-el.css").getInputStream().close(); testResourceELEval(); }
@SuppressWarnings({"deprecation"}) public void testUserAgentNeedsUpdate1() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); // no If-Modified-Since request header, so this should always // return true Resource resource = handler.createResource("duke-nv.gif"); assertTrue(resource.userAgentNeedsUpdate(getFacesContext())); // set the creation date of the ResourceHandler back in time so that // if the header was present it would return true - the lack of the header // should result in true being returned in this case Date date = new Date(); date.setYear(1980); long origTime = (Long) TestingUtil.invokePrivateMethod( "getCreationTime", null, null, ResourceHandlerImpl.class, handler); TestingUtil.invokePrivateMethod( "setCreationTime", new Class[] {Long.TYPE}, new Object[] {date.getTime()}, ResourceHandlerImpl.class, handler); assertTrue(resource.userAgentNeedsUpdate(getFacesContext())); TestingUtil.invokePrivateMethod( "setCreationTime", new Class[] {Long.TYPE}, new Object[] {origTime}, ResourceHandlerImpl.class, handler); }
public void testJarResourceGetInputStream() throws Exception { // validate the behavior of getInputStream() for a webapp-based resource ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); // step 1 - non-versioned byte[] controlBytes = getBytes( Util.getCurrentLoader(this.getClass()) .getResource("META-INF/resources/duke-jar-nv.gif")); Resource resource = handler.createResource("duke-jar-nv.gif"); assertTrue(resource != null); InputStream in = resource.getInputStream(); assertTrue(in != null); byte[] underTest = getBytes(in); assertTrue(Arrays.equals(controlBytes, underTest)); // step 2 - versioned /* controlBytes = getBytes(Util.getCurrentLoader(this.getClass()).getResource("META-INF/resources/duke-jar.gif/1_1.gif")); resource = handler.createResource("duke-jar.gif"); assertTrue(resource != null); in = resource.getInputStream(); assertTrue(in != null); underTest = getBytes(in); assertTrue(Arrays.equals(controlBytes, underTest)); */ }
public void testEqualsOnResourceAndRelatedClasses() throws Exception { // validate the behavior of getInputStream() for a webapp-based resource ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); Object x = handler.createResource("duke-nv.gif", "nvLibrary", "image/gif"), y = handler.createResource("duke-nv.gif", "nvLibrary", "image/gif"), z = handler.createResource("duke-nv.gif", "nvLibrary", "image/gif"); this.verifyEqualsContractPositive(x, y, z); y = handler.createResource("simple.css"); assertFalse(x.equals(y)); VersionInfo viA = new VersionInfo("1.0", null), viB = new VersionInfo("1.0", null), viC = new VersionInfo("1.0", null); this.verifyEqualsContractPositive(viA, viB, viC); ResourceHelper helper = new ClasspathResourceHelper(); FacesContext context = this.getFacesContext(); LibraryInfo liA = helper.findLibrary("vLibrary-jar", null, null, context), liB = helper.findLibrary("vLibrary-jar", null, null, context), liC = helper.findLibrary("vLibrary-jar", null, null, context); this.verifyEqualsContractPositive(liA, liB, liC); liB = helper.findLibrary("vLibrary", null, null, context); assertFalse(liA.equals(liB)); }
public void testWebppResourceGetInputStream() throws Exception { // validate the behavior of getInputStream() for a webapp-based resource ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); // step 1 - non-versioned byte[] controlBytes = getBytes(getFacesContext().getExternalContext().getResource("/resources/duke-nv.gif")); Resource resource = handler.createResource("duke-nv.gif"); assertTrue(resource != null); InputStream in = resource.getInputStream(); assertTrue(in != null); byte[] underTest = getBytes(in); assertTrue(Arrays.equals(controlBytes, underTest)); // step 2 - versioned controlBytes = getBytes(getFacesContext().getExternalContext().getResource("/resources/duke.gif/1_1.gif")); resource = handler.createResource("duke.gif"); assertTrue(resource != null); in = resource.getInputStream(); assertTrue(in != null); underTest = getBytes(in); assertTrue(Arrays.equals(controlBytes, underTest)); }
/** Added for issue 1274. */ public void testResourceELEval() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertNotNull(handler); Resource resource = handler.createResource("simple-with-el.css"); assertNotNull(resource); byte[] bytes = getBytes(resource.getInputStream()); ByteArrayInputStream bai = new ByteArrayInputStream(bytes); BufferedReader reader = new BufferedReader(new InputStreamReader(bai)); List<String> lines = new ArrayList<String>(); for (String l = reader.readLine(); l != null; l = reader.readLine()) { String t = l.trim(); if (t.length() > 0) { lines.add(t); } } assertEquals(4, lines.size()); final String[] expectedLines = {"# /test", "# /test", "h2 { color: red }", "# /test}"}; for (int i = 0, len = expectedLines.length; i < len; i++) { assertEquals(expectedLines[i], expectedLines[i], lines.get(i)); } }
public void testFaceletResources() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); Resource resource = handler.createResource("test.xhtml"); assertNotNull(resource); }
public void testUserAgentNeedsUpdate2() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); // If-Modified-Since request header, so this should always // return true Resource resource = handler.createResource("duke-nv.gif"); assertTrue(!resource.userAgentNeedsUpdate(getFacesContext())); }
@Override public Resource createResource(String resourceName, String libraryName) { Resource resource = wrapped.createResource(resourceName, libraryName); if (activated && resource != null && libraryName != null && LIBRARY.equals(libraryName)) { if (ProjectStageProducer.getInstance().getProjectStage() == ProjectStage.Development) { resource = wrapped.createResource(resourceName, LIBRARY_UNCOMPRESSED); } resource = new DeltaSpikeResource(resource, version); } return resource; }
public static String getResourcePath(FacesContext context, String library, String resourceName) { String path = null; if (resourceName != null) { ResourceHandler resourceHandler = context.getApplication().getResourceHandler(); Resource resource = (library != null) ? resourceHandler.createResource(resourceName, library) : resourceHandler.createResource(resourceName); if (resource != null) { path = resource.getRequestPath(); } } return path; }
public void testGetContentType() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); // non-versioned resource containing one path element Resource resource = handler.createResource("duke-jar.gif"); assertTrue(resource != null); assertTrue("image/gif".equals(resource.getContentType())); // versioned resource containing one path element resource = handler.createResource("duke.gif"); assertTrue(resource != null); assertTrue("image/gif".equals(resource.getContentType())); }
/** * @see * javax.faces.view.ViewDeclarationLanguage#getScriptComponentResource(javax.faces.context.FacesContext, * javax.faces.application.Resource) */ public Resource getScriptComponentResource(FacesContext context, Resource componentResource) { if (!groovyAvailable) { return null; } Resource result = null; String resourceName = componentResource.getResourceName(); if (resourceName.endsWith(".xhtml")) { resourceName = resourceName.substring(0, resourceName.length() - 6) + ".groovy"; ResourceHandler resourceHandler = context.getApplication().getResourceHandler(); result = resourceHandler.createResource(resourceName, componentResource.getLibraryName()); } return result; }
public void testToURIExtensionMapping() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); Resource resource = handler.createResource("duke-nv.gif"); assertTrue(resource != null); String expectedURI = "/test/javax.faces.resource/duke-nv.gif.faces"; assertTrue(expectedURI.equals(resource.getRequestPath())); resource = handler.createResource("duke-nv.gif", "nvLibrary"); assertTrue(resource != null); expectedURI = "/test/javax.faces.resource/duke-nv.gif.faces?ln=nvLibrary"; assertTrue(expectedURI.equals(resource.getRequestPath())); resource = handler.createResource("duke.gif"); assertTrue(resource != null); expectedURI = "/test/javax.faces.resource/duke.gif.faces?v=1_1"; assertTrue(expectedURI.equals(resource.getRequestPath())); resource = handler.createResource("duke.gif", "nvLibrary"); assertTrue(resource != null); expectedURI = "/test/javax.faces.resource/duke.gif.faces?ln=nvLibrary&v=1_1"; assertTrue(expectedURI.equals(resource.getRequestPath())); resource = handler.createResource("duke.gif", "vLibrary"); assertTrue(resource != null); expectedURI = "/test/javax.faces.resource/duke.gif.faces?ln=vLibrary&v=2_01_1"; assertTrue(expectedURI.equals(resource.getRequestPath())); resource = handler.createResource("duke-nv.gif", "vLibrary"); assertTrue(resource != null); expectedURI = "/test/javax.faces.resource/duke-nv.gif.faces?ln=vLibrary&v=2_0"; assertTrue(expectedURI.equals(resource.getRequestPath())); }
/** * Renders the Javascript necessary to add and remove request parameters to the current form. * * @param writer the <code>ResponseWriter</code> * @param context the <code>FacesContext</code> for the current request * @throws java.io.IOException if an error occurs writing to the response */ public static void renderFormInitScript(ResponseWriter writer, FacesContext context) throws IOException { WebConfiguration webConfig = WebConfiguration.getInstance(context.getExternalContext()); if (webConfig.isOptionEnabled(BooleanWebContextInitParameter.ExternalizeJavaScript)) { // PENDING // We need to look into how to make this work in a portlet environment. // For the time being, this feature will need to be disabled when running // in a portlet. /* String mapping = Util.getFacesMapping(context); String uri; if ((mapping != null) && (Util.isPrefixMapped(mapping))) { uri = mapping + '/' + RIConstants.SUN_JSF_JS_URI; } else { uri = '/' + RIConstants.SUN_JSF_JS_URI + mapping; } writer.write('\n'); writer.startElement("script", null); writer.writeAttribute("type", "text/javascript", null); writer.writeAttribute("src", context.getExternalContext() .getRequestContextPath() + uri, null); writer.endElement("script"); writer.write("\n"); */ ResourceHandler handler = context.getApplication().getResourceHandler(); Resource resource = handler.createResource("scripts/sunjsf.js", "jsfri"); writer.write('\n'); writer.startElement("script", null); writer.writeAttribute("type", "text/javascript", null); writer.writeAttribute("src", ((resource != null) ? resource.getRequestPath() : ""), null); writer.endElement("script"); } else { writer.write('\n'); writer.startElement("script", null); writer.writeAttribute("type", "text/javascript", null); writer.writeAttribute("language", "Javascript", null); writeSunJS(context, writer); writer.endElement("script"); writer.write("\n"); } }
public void testResourceImplSerialization() throws Exception { ResourceHandler handler = getFacesContext().getApplication().getResourceHandler(); assertTrue(handler != null); Resource resource = handler.createResource("duke-nv.gif"); byte[] serializedBytes = serialize(resource); resource = (Resource) deserialize(serializedBytes); assertNotNull(resource); assertNull(resource.getLibraryName()); assertEquals("duke-nv.gif", "duke-nv.gif", resource.getResourceName()); assertEquals("image/gif", "image/gif", resource.getContentType()); resource = handler.createResource("duke-nv.gif", "nvLibrary"); serializedBytes = serialize(resource); resource = (Resource) deserialize(serializedBytes); assertNotNull(resource); assertEquals("nvLibrary", "nvLibrary", resource.getLibraryName()); assertEquals("duke-nv.gif", "duke-nv.gif", resource.getResourceName()); assertEquals("image/gif", "image/gif", resource.getContentType()); }
/** * Attempt to obtain the resource from the server by parsing the valueExpression of the image * attribute. Returns null if the valueExpression is not of the form * #{resource['path/to/resource']} or #{resource['library:name']}. Otherwise returns the value * obtained by ResourceHandler.createResource(). */ private Resource getImageResource(FacesContext facesContext, ImageCropper imageCropper) { Resource resource = null; ValueExpression imageValueExpression = imageCropper.getValueExpression(ImageCropper.PropertyKeys.image.toString()); if (imageValueExpression != null) { String imageValueExpressionString = imageValueExpression.getExpressionString(); if (imageValueExpressionString.matches("^[#][{]resource\\['[^']+'\\][}]$")) { imageValueExpressionString = imageValueExpressionString.replaceFirst("^[#][{]resource\\['", ""); imageValueExpressionString = imageValueExpressionString.replaceFirst("'\\][}]$", ""); String resourceLibrary = null; String resourceName; String[] resourceInfo = imageValueExpressionString.split(":"); if (resourceInfo.length == 2) { resourceLibrary = resourceInfo[0]; resourceName = resourceInfo[1]; } else { resourceName = resourceInfo[0]; } if (resourceName != null) { Application application = facesContext.getApplication(); ResourceHandler resourceHandler = application.getResourceHandler(); if (resourceLibrary != null) { resource = resourceHandler.createResource(resourceName, resourceLibrary); } else { resource = resourceHandler.createResource(resourceName); } } } } return resource; }
/** * <span class="changed_modified_2_2">Process</span> an incoming request, and create the * corresponding response according to the following specification. <div * class="changed_modified_2_0"> * * <p>If the <code>request</code> and <code>response</code> arguments to this method are not * instances of <code>HttpServletRequest</code> and <code>HttpServletResponse</code>, * respectively, the results of invoking this method are undefined. * * <p>This method must respond to requests that start with the following strings by invoking the * <code>sendError</code> method on the response argument (cast to <code>HttpServletResponse * </code>), passing the code <code>HttpServletResponse.SC_NOT_FOUND</code> as the argument. * * <ul> * <pre><code> * /WEB-INF/ * /WEB-INF * /META-INF/ * /META-INF * </code></pre> * </ul> * * <p>If none of the cases described above in the specification for this method apply to the * servicing of this request, the following action must be taken to service the request. * * <p>Acquire a {@link FacesContext} instance for this request. * * <p>Acquire the <code>ResourceHandler</code> for this request by calling {@link * javax.faces.application.Application#getResourceHandler}. Call {@link * javax.faces.application.ResourceHandler#isResourceRequest}. If this returns <code>true</code> * call {@link javax.faces.application.ResourceHandler#handleResourceRequest}. If this returns * <code>false</code>, <span class="changed_added_2_2">call {@link * javax.faces.lifecycle.Lifecycle#attachWindow} followed by </span> {@link * javax.faces.lifecycle.Lifecycle#execute} followed by {@link * javax.faces.lifecycle.Lifecycle#render}. If a {@link javax.faces.FacesException} is thrown in * either case, extract the cause from the <code>FacesException</code>. If the cause is <code>null * </code> extract the message from the <code>FacesException</code>, put it inside of a new <code> * ServletException</code> instance, and pass the <code>FacesException</code> instance as the root * cause, then rethrow the <code>ServletException</code> instance. If the cause is an instance of * <code>ServletException</code>, rethrow the cause. If the cause is an instance of <code> * IOException</code>, rethrow the cause. Otherwise, create a new <code>ServletException</code> * instance, passing the message from the cause, as the first argument, and the cause itself as * the second argument. * * <p class="changed_modified_2_0_rev_a">The implementation must make it so {@link * javax.faces.context.FacesContext#release} is called within a finally block as late as possible * in the processing for the JSF related portion of this request. </div> * * @param req The servlet request we are processing * @param resp The servlet response we are creating * @throws IOException if an input/output error occurs during processing * @throws ServletException if a servlet error occurs during processing */ public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; requestStart(request.getRequestURI()); // V3 Probe hook if (!isHttpMethodValid(request)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } if (Thread.currentThread().isInterrupted()) { if (LOGGER.isLoggable(Level.FINER)) { LOGGER.log( Level.FINE, "Thread {0} given to FacesServlet.service() in interrupted state", Thread.currentThread().getName()); } } // If prefix mapped, then ensure requests for /WEB-INF are // not processed. String pathInfo = request.getPathInfo(); if (pathInfo != null) { pathInfo = pathInfo.toUpperCase(); if (pathInfo.startsWith("/WEB-INF/") || pathInfo.equals("/WEB-INF") || pathInfo.startsWith("/META-INF/") || pathInfo.equals("/META-INF")) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } if (!initFacesContextReleased) { FacesContext initFacesContext = FacesContext.getCurrentInstance(); if (null != initFacesContext) { initFacesContext.release(); } initFacesContextReleased = true; } // Acquire the FacesContext instance for this request FacesContext context = facesContextFactory.getFacesContext( servletConfig.getServletContext(), request, response, lifecycle); // Execute the request processing lifecycle for this request try { ResourceHandler handler = context.getApplication().getResourceHandler(); if (handler.isResourceRequest(context)) { handler.handleResourceRequest(context); } else { lifecycle.attachWindow(context); lifecycle.execute(context); lifecycle.render(context); } } catch (FacesException e) { Throwable t = e.getCause(); if (t == null) { throw new ServletException(e.getMessage(), e); } else { if (t instanceof ServletException) { throw ((ServletException) t); } else if (t instanceof IOException) { throw ((IOException) t); } else { throw new ServletException(t.getMessage(), t); } } } finally { // Release the FacesContext instance for this request context.release(); } requestEnd(); // V3 Probe hook }