From 2b69bcc8af4abb1a3daba3726fb58de794788799 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 9 Feb 2018 11:26:53 -0500 Subject: [PATCH] FlowResourceHandler implements createViewResource Also FlowResourceResolver is deprecated. Issue: SWF-1719 --- .../faces/webflow/FlowResourceHandler.java | 43 +++++++++++++++---- .../faces/webflow/FlowResourceResolver.java | 20 +++++---- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceHandler.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceHandler.java index ae7fcc97..eee86a99 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceHandler.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2004-2012 the original author or authors. + * Copyright 2004-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,24 +17,22 @@ package org.springframework.faces.webflow; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import javax.faces.FacesException; import javax.faces.application.ResourceHandler; import javax.faces.application.ResourceHandlerWrapper; +import javax.faces.application.ViewResource; import javax.faces.context.FacesContext; -import javax.faces.view.facelets.ResourceResolver; import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; -import org.springframework.util.ClassUtils; import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.execution.RequestContextHolder; /** * Resolves Facelets resources using Spring Resource paths such as "classpath:foo.xhtml". + *

This handler is auto-configured in the faces-config.xml bundled with the + * "spring-faces" module. + * @since 2.5 */ public class FlowResourceHandler extends ResourceHandlerWrapper { @@ -49,6 +47,35 @@ public class FlowResourceHandler extends ResourceHandlerWrapper { return this.wrapped; } - // FIXME will need to copy logic from FlowResourceResolver + @Override + public ViewResource createViewResource(FacesContext facesContext, String resourceName) { + if (!JsfUtils.isFlowRequest()) { + return this.wrapped.createViewResource(facesContext, resourceName); + } + + try { + RequestContext context = RequestContextHolder.getRequestContext(); + ApplicationContext flowContext = context.getActiveFlow().getApplicationContext(); + if (flowContext == null) { + throw new IllegalStateException( + "A Flow ApplicationContext is required to resolve Flow View Resources"); + } + ApplicationContext appContext = flowContext.getParent(); + Resource viewResource = appContext.getResource(resourceName); + URL url = viewResource.getURL(); + if (viewResource.exists()) { + return new ViewResource() { + @Override + public URL getURL() { + return url; + } + }; + } else { + return this.wrapped.createViewResource(facesContext, resourceName); + } + } catch (IOException ex) { + throw new FacesException(ex); + } + } } diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceResolver.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceResolver.java index b9aa2d28..58202d95 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceResolver.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowResourceResolver.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.List; import javax.faces.FacesException; -import javax.faces.view.facelets.ResourceResolver; import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; @@ -31,20 +30,22 @@ import org.springframework.webflow.execution.RequestContext; import org.springframework.webflow.execution.RequestContextHolder; /** - * Resolves Facelets templates using Spring Resource paths such as "classpath:foo.xhtml". Configure it via a context - * parameter in web.xml: - * + * Resolves Facelets templates using Spring Resource paths such as "classpath:foo.xhtml". + * Configure it via a context parameter in web.xml: *

  * <context-param/> 
  * 	<param-name>facelets.RESOURCE_RESOLVER</param-name>
  * 	<param-value>org.springframework.faces.webflow.FlowResourceResolver</param-value> 
  * </context-param>
  * 
+ * @deprecated as of 2.5 in favor of {@link FlowResourceHandler}. */ -public class FlowResourceResolver extends ResourceResolver { +@Deprecated +@SuppressWarnings("deprecation") +public class FlowResourceResolver extends javax.faces.view.facelets.ResourceResolver { /** - * All known {@link ResourceResolver} implementations in the priority order + * All known {@code ResourceResolver} implementations in the priority order */ private static final List RESOLVERS_CLASSES; static { @@ -54,18 +55,19 @@ public class FlowResourceResolver extends ResourceResolver { RESOLVERS_CLASSES = Collections.unmodifiableList(resolvers); } - private final ResourceResolver delegateResolver; + private final javax.faces.view.facelets.ResourceResolver delegateResolver; public FlowResourceResolver() { this.delegateResolver = createDelegateResolver(); } - private ResourceResolver createDelegateResolver() { + private javax.faces.view.facelets.ResourceResolver createDelegateResolver() { try { ClassLoader classLoader = getClass().getClassLoader(); for (String resolverClass : RESOLVERS_CLASSES) { if (ClassUtils.isPresent(resolverClass, classLoader)) { - return (ResourceResolver) ClassUtils.forName(resolverClass, classLoader).newInstance(); + return (javax.faces.view.facelets.ResourceResolver) + ClassUtils.forName(resolverClass, classLoader).newInstance(); } } } catch (Exception e) {