KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > executor > jsf > FlowNavigationHandlerArgumentExtractor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.executor.jsf;
17
18 import org.springframework.util.StringUtils;
19 import org.springframework.webflow.context.ExternalContext;
20 import org.springframework.webflow.executor.support.FlowExecutorArgumentExtractionException;
21 import org.springframework.webflow.executor.support.FlowExecutorArgumentExtractor;
22 import org.springframework.webflow.executor.support.RequestParameterFlowExecutorArgumentHandler;
23
24 /**
25  * An {@link FlowExecutorArgumentExtractor} that is aware of JSF
26  * outcomes that communicate requests to launch flow executions and
27  * signal event in existing flow executions.
28  *
29  * @author Keith Donald
30  */

31 public class FlowNavigationHandlerArgumentExtractor extends RequestParameterFlowExecutorArgumentHandler {
32     
33     /*
34      * Implementation note: subclasses an FlowExecutorArgumentHandler but is really
35      * just a FlowExecutorArgumentExtractor.
36      */

37
38     /**
39      * The default prefix of a outcome string that indicates a new flow should be launched.
40      */

41     private static final String JavaDoc FLOW_ID_PREFIX = "flowId:";
42
43     private String JavaDoc flowIdPrefix = FLOW_ID_PREFIX;
44
45     /**
46      * Returns the configured prefix for outcome strings that indicate a new flow should be launched.
47      */

48     public String JavaDoc getFlowIdPrefix() {
49         return flowIdPrefix;
50     }
51
52     /**
53      * Sets the prefix of a outcome string that indicates a new flow should be launched.
54      */

55     public void setFlowIdPrefix(String JavaDoc flowIdPrefix) {
56         this.flowIdPrefix = flowIdPrefix;
57     }
58
59     public boolean isEventIdPresent(ExternalContext context) {
60         return StringUtils.hasText(getOutcome(context)) || super.isEventIdPresent(context);
61     }
62
63     // overidden to return the eventId from the action outcome string.
64
public String JavaDoc extractEventId(ExternalContext context) throws FlowExecutorArgumentExtractionException {
65         String JavaDoc outcome = getOutcome(context);
66         if (StringUtils.hasText(outcome)) {
67             return outcome;
68         }
69         else {
70             return super.extractEventId(context);
71         }
72     }
73
74     public boolean isFlowIdPresent(ExternalContext context) throws FlowExecutorArgumentExtractionException {
75         String JavaDoc outcome = getOutcome(context);
76         if (outcome != null && outcome.startsWith(getFlowIdPrefix())) {
77             return true;
78         }
79         else {
80             return super.isFlowIdPresent(context);
81         }
82     }
83
84     // overidden to return the flowId from a JSF outcome in format <code>flowId:${flowId}</code>
85
public String JavaDoc extractFlowId(ExternalContext context) throws FlowExecutorArgumentExtractionException {
86         String JavaDoc outcome = getOutcome(context);
87         if (StringUtils.hasText(outcome)) {
88             int index = outcome.indexOf(getFlowIdPrefix());
89             if (index == -1) {
90                 throw new FlowExecutorArgumentExtractionException(
91                         "Unable to extract flow id; make sure the JSF outcome is prefixed with '" + getFlowIdPrefix()
92                                 + "' to launch a new flow execution");
93             }
94             String JavaDoc flowId = outcome.substring(getFlowIdPrefix().length());
95             if (!StringUtils.hasText(flowId)) {
96                 throw new FlowExecutorArgumentExtractionException(
97                         "Unable to extract flow id; make sure the flow id is provided in the outcome string");
98             }
99             return flowId;
100         }
101         else {
102             return super.extractFlowId(context);
103         }
104     }
105
106     private String JavaDoc getOutcome(ExternalContext context) {
107         return ((JsfExternalContext)context).getOutcome();
108     }
109 }
Popular Tags