SWF-1375 Add <faces:resources /> element, JsfFlowHandlerAdapter, and provide useful error message when FacesContext is null.

This commit is contained in:
Rossen Stoyanchev
2010-09-14 12:26:24 +00:00
parent 9e2137d3c6
commit 5d158abce7
11 changed files with 260 additions and 25 deletions

View File

@@ -25,5 +25,6 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class FacesNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("flow-builder-services", new FacesFlowBuilderServicesBeanDefinitionParser());
registerBeanDefinitionParser("resources", new ResourcesBeanDefinitionParser());
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2004-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.faces.config;
import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.w3c.dom.Element;
/**
* Parser for the resources tag.
*
* @author Rossen Stoyanchev
* @since 2.2.0
*/
public class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
static final String RESOURCE_HANDLER_BEAN_NAME = "jsfResourceRequestHandler";
static final String RESOURCE_HANDLER_CLASS_NAME = "org.springframework.faces.webflow.JsfResourceRequestHandler";
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
registerHandlerAdapterIfNecessary(parserContext, source);
registerResourceHandler(parserContext, source);
registerHandlerMappings(element, parserContext, source);
return null;
}
private void registerHandlerMappings(Element element, ParserContext parserContext, Object source) {
Map<String, String> urlMap = new ManagedMap<String, String>();
urlMap.put("/javax.faces.resource/**", RESOURCE_HANDLER_BEAN_NAME);
RootBeanDefinition beanDefinition = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
beanDefinition.setSource(source);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
beanDefinition.getPropertyValues().add("urlMap", urlMap);
String order = element.getAttribute("order");
beanDefinition.getPropertyValues().add("order", StringUtils.hasText(order) ? order : 0);
parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
}
private void registerResourceHandler(ParserContext parserContext, Object source) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(RESOURCE_HANDLER_CLASS_NAME);
beanDefinition.setSource(source);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
parserContext.getRegistry().registerBeanDefinition(RESOURCE_HANDLER_BEAN_NAME, beanDefinition);
}
private void registerHandlerAdapterIfNecessary(ParserContext parserContext, Object source) {
if (parserContext.getRegistry().containsBeanDefinition(
"org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter")) {
return;
}
RootBeanDefinition beanDefinition = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
beanDefinition.setSource(source);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
}
}

View File

@@ -73,4 +73,25 @@ When set to true, changes to a flow definition will be auto-detected and will re
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="resources">
<xsd:annotation>
<xsd:documentation
source="java:org.springframework.faces.webflow.JsfResourceRequestHandler"><![CDATA[
Configures a handler that uses the JSF ResourceHandler API introduced in JSF 2 to serve web application and
classpath resources such as images, CSS and JavaScript files from well-known locations.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="order" type="xsd:int">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Specifies the order of the HandlerMapping for the resource handler. The default order is 0.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2004-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.faces.webflow;
import org.springframework.js.ajax.AjaxHandler;
import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter;
/**
* An extension of {@link FlowHandlerAdapter} that replaces the default {@link AjaxHandler} instance with a
* {@link JsfAjaxHandler} assuming JSF 2 is the runtime environment.
*
* @author Rossen Stoyanchev
* @since 2.2.0
*/
public class JsfFlowHandlerAdapter extends FlowHandlerAdapter {
public void afterPropertiesSet() throws Exception {
boolean initializeAjaxHandler = getAjaxHandler() == null;
super.afterPropertiesSet();
if (initializeAjaxHandler) {
if (JsfRuntimeInformation.isAtLeastJsf20()) {
JsfAjaxHandler ajaxHandler = new JsfAjaxHandler();
ajaxHandler.setApplicationContext(getApplicationContext());
setAjaxHandler(ajaxHandler);
}
}
}
}

View File

@@ -34,7 +34,7 @@ import org.springframework.web.context.support.WebApplicationObjectSupport;
* @author Rossen Stoyanchev
* @see ResourceHandler
*/
public class FacesJsfResourceRequestHandler extends WebApplicationObjectSupport implements HttpRequestHandler {
public class JsfResourceRequestHandler extends WebApplicationObjectSupport implements HttpRequestHandler {
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {

View File

@@ -72,6 +72,12 @@ public class JsfViewFactory implements ViewFactory {
*/
public View getView(RequestContext context) {
FacesContext facesContext = FlowFacesContext.getCurrentInstance();
if (facesContext == null) {
throw new IllegalStateException(
"FacesContext has not been initialized within the current Web Flow request."
+ " Check the configuration for your <webflow:flow-executor>."
+ " For JSF you will need FlowFacesContextLifecycleListener configured as one of its flow execution listeners.");
}
if (isAtLeastJsf20()) {
facesContext.setCurrentPhaseId(PhaseId.RESTORE_VIEW);
}

View File

@@ -0,0 +1,39 @@
package org.springframework.faces.config;
import java.util.Map;
import junit.framework.TestCase;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.faces.webflow.JsfResourceRequestHandler;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
public class ResourcesBeanDefinitionParserTests extends TestCase {
private ClassPathXmlApplicationContext context;
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext("org/springframework/faces/config/resources.xml");
}
protected void tearDown() throws Exception {
}
public void testConfigureDefaults() {
Map map = context.getBeansOfType(HttpRequestHandlerAdapter.class);
assertEquals(1, map.values().size());
Object resourceHandler = context.getBean(ResourcesBeanDefinitionParser.RESOURCE_HANDLER_BEAN_NAME);
assertNotNull(resourceHandler);
assertTrue(resourceHandler instanceof JsfResourceRequestHandler);
map = context.getBeansOfType(SimpleUrlHandlerMapping.class);
assertEquals(1, map.values().size());
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) map.values().iterator().next();
assertEquals(ResourcesBeanDefinitionParser.RESOURCE_HANDLER_BEAN_NAME, handlerMapping.getUrlMap().get(
"/javax.faces.resource/**"));
assertEquals(0, handlerMapping.getOrder());
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd">
<faces:resources />
</beans>

View File

@@ -0,0 +1,47 @@
package org.springframework.faces.webflow;
import junit.framework.TestCase;
import org.springframework.js.ajax.AjaxHandler;
import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
import org.springframework.webflow.context.ExternalContext;
import org.springframework.webflow.core.FlowException;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.executor.FlowExecutionResult;
import org.springframework.webflow.executor.FlowExecutor;
public class JsfFlowHandlerAdapterTests extends TestCase {
private JsfFlowHandlerAdapter handlerAdapter;
protected void setUp() throws Exception {
handlerAdapter = new JsfFlowHandlerAdapter();
handlerAdapter.setFlowExecutor(new StubFlowExecutor());
}
public void testAjaxHandlerNotProvided() throws Exception {
handlerAdapter.afterPropertiesSet();
assertNotNull(handlerAdapter.getAjaxHandler());
assertTrue(handlerAdapter.getAjaxHandler() instanceof JsfAjaxHandler);
}
public void testAjaxHandlerProvided() throws Exception {
AjaxHandler myAjaxHandler = new SpringJavascriptAjaxHandler();
handlerAdapter.setAjaxHandler(myAjaxHandler);
handlerAdapter.afterPropertiesSet();
assertTrue(myAjaxHandler == handlerAdapter.getAjaxHandler());
}
private final class StubFlowExecutor implements FlowExecutor {
public FlowExecutionResult resumeExecution(String flowExecutionKey, ExternalContext context)
throws FlowException {
throw new UnsupportedOperationException("Not expected");
}
public FlowExecutionResult launchExecution(String flowId, MutableAttributeMap input, ExternalContext context)
throws FlowException {
throw new UnsupportedOperationException("Not expected");
}
}
}

View File

@@ -4,17 +4,14 @@
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces-2.0.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd">
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextLifecycleListener"/>
<webflow:listener ref="facesContextListener"/>
<webflow:listener ref="jpaFlowExecutionListener" />
<webflow:listener ref="securityFlowExecutionListener" />
</webflow:flow-execution-listeners>
@@ -29,7 +26,7 @@
<faces:flow-builder-services id="facesFlowBuilderServices" development="true" />
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextLifecycleListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<!-- Installs a listener that manages JPA persistence contexts for flows that require them -->
<bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">

View File

@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd">
<!-- Maps JSF 2 resource requests to an instance of org.springframework.faces.webflow.FacesJsfResourceRequestHandler -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="0"/>
<property name="mappings" value="/javax.faces.resource/**=jsfResourceHandler"/>
</bean>
<faces:resources />
<!-- Maps request paths to flows in the flowRegistry; e.g. a path of /hotels/booking looks for a flow with id "hotels/booking" -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
@@ -30,17 +29,8 @@
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
<property name="ajaxHandler">
<bean class="org.springframework.faces.webflow.JsfAjaxHandler"/>
</property>
</bean>
<!-- Dispatches requests mapped to org.springframework.web.HttpRequestHandler implementations -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- Delegates to the JSF ResourceHandler to serve web application resources such as images, .css and .js files. -->
<bean id="jsfResourceHandler" class="org.springframework.faces.webflow.FacesJsfResourceRequestHandler"/>
</beans>