From 034dbf12fe2c4f3aa2269cef5605de0e08510006 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 22 May 2014 16:43:15 -0400 Subject: [PATCH] Fix JSF < 2.2 compatibility issues Issue: SWF-1634 --- .../faces/webflow/FlowFacesContext.java | 9 ++++--- .../faces/webflow/Jsf2FlowFacesContext.java | 25 +++++++++++++------ .../faces/webflow/JsfRuntimeInformation.java | 20 ++++++++++++++- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowFacesContext.java b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowFacesContext.java index d945da6c..82de53f2 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/FlowFacesContext.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/FlowFacesContext.java @@ -90,7 +90,7 @@ public class FlowFacesContext extends FacesContext { this.context = context; this.delegate = delegate; this.messageDelegate = new FlowFacesContextMessageDelegate(context); - this.externalContext = new FlowExternalContext(delegate.getExternalContext()); + this.externalContext = new FlowExternalContext(delegate.getExternalContext(), context); setCurrentInstance(this); } @@ -218,12 +218,15 @@ public class FlowFacesContext extends FacesContext { return messageDelegate; } - protected class FlowExternalContext extends ExternalContextWrapper { + protected static class FlowExternalContext extends ExternalContextWrapper { private static final String CUSTOM_RESPONSE = "customResponse"; - public FlowExternalContext(ExternalContext delegate) { + private RequestContext context; + + public FlowExternalContext(ExternalContext delegate, RequestContext context) { super(delegate); + this.context = context; } public Object getResponse() { diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/Jsf2FlowFacesContext.java b/spring-faces/src/main/java/org/springframework/faces/webflow/Jsf2FlowFacesContext.java index 7c0d89bb..02f5a622 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/Jsf2FlowFacesContext.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/Jsf2FlowFacesContext.java @@ -44,15 +44,16 @@ import org.springframework.webflow.execution.RequestContext; */ public class Jsf2FlowFacesContext extends FlowFacesContext { - private ExternalContext externalContext; + private final ExternalContext externalContext; + + private final PartialViewContext partialViewContext; - private PartialViewContext partialViewContext; public Jsf2FlowFacesContext(RequestContext context, FacesContext delegate) { super(context, delegate); - - this.externalContext = new Jsf2FlowExternalContext(getDelegate().getExternalContext()); - + this.externalContext = (JsfRuntimeInformation.isAtLeastJsf22() ? + new Jsf22FlowExternalContext(getDelegate().getExternalContext(), context) : + new Jsf2FlowExternalContext(getDelegate().getExternalContext(), context)); PartialViewContextFactory factory = (PartialViewContextFactory) FactoryFinder .getFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY); PartialViewContext partialViewContextDelegate = factory.getPartialViewContext(this); @@ -138,12 +139,12 @@ public class Jsf2FlowFacesContext extends FlowFacesContext { return getDelegate().isReleased(); } - protected class Jsf2FlowExternalContext extends FlowExternalContext { + private static class Jsf2FlowExternalContext extends FlowExternalContext { Log logger = LogFactory.getLog(FlowExternalContext.class); - public Jsf2FlowExternalContext(ExternalContext delegate) { - super(delegate); + public Jsf2FlowExternalContext(ExternalContext delegate, RequestContext context) { + super(delegate, context); } public void responseSendError(int statusCode, String message) throws IOException { @@ -267,6 +268,14 @@ public class Jsf2FlowFacesContext extends FlowFacesContext { delegate.setSessionMaxInactiveInterval(interval); } + } + + private static class Jsf22FlowExternalContext extends FlowExternalContext { + + public Jsf22FlowExternalContext(ExternalContext delegate, RequestContext context) { + super(delegate, context); + } + // --------------- JSF 2.2 Pass-through delegate methods ------------------// public String getApplicationContextPath() { diff --git a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfRuntimeInformation.java b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfRuntimeInformation.java index 84b5042e..74bbcae3 100644 --- a/spring-faces/src/main/java/org/springframework/faces/webflow/JsfRuntimeInformation.java +++ b/spring-faces/src/main/java/org/springframework/faces/webflow/JsfRuntimeInformation.java @@ -37,13 +37,23 @@ public class JsfRuntimeInformation { /** JSF Version 2.0 */ public static final int JSF_20 = 2; + /** JSF Version 2.1 */ + public static final int JSF_21 = 3; + + /** JSF Version 2.2 */ + public static final int JSF_22 = 4; + private static final int jsfVersion; private static final boolean myFacesPresent = ClassUtils.isPresent("org.apache.myfaces.webapp.MyFacesServlet", JsfUtils.class.getClassLoader()); static { - if (ReflectionUtils.findMethod(FacesContext.class, "isPostback") != null) { + if (ReflectionUtils.findMethod(FacesContext.class, "getResourceLibraryContracts") != null) { + jsfVersion = JSF_22; + } else if (ReflectionUtils.findMethod(FacesContext.class, "isReleased") != null) { + jsfVersion = JSF_21; + } else if (ReflectionUtils.findMethod(FacesContext.class, "isPostback") != null) { jsfVersion = JSF_20; } else if (ReflectionUtils.findMethod(FacesContext.class, "getELContext") != null) { jsfVersion = JSF_12; @@ -52,6 +62,14 @@ public class JsfRuntimeInformation { } } + public static boolean isAtLeastJsf22() { + return jsfVersion >= JSF_22; + } + + public static boolean isAtLeastJsf21() { + return jsfVersion >= JSF_21; + } + public static boolean isAtLeastJsf20() { return jsfVersion >= JSF_20; }