2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.shale.clay.faces;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletRequest;
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.apache.commons.chain.Command;
28 import org.apache.commons.chain.Context;
29 import org.apache.shale.clay.config.Globals;
30
31 /***
32 * <p>This is a Shale "preprocess" command that should be registered
33 * with the shale chains catalog. It is only need for implementing
34 * full {@link org.apache.shale.clay.component.Clay} html or XML views with myfaces.
35 * The myfaces implementation pays strict attention to the javax.faces.DEFAULT_SUFFIX setting,
36 * only allowing viewId's with this suffix. This command will place an indicator in request scope
37 * before the request is processed by the faces servlet. The {@link ClayViewHandler}
38 * will use the indicator to determine that the view should be handled by the
39 * {@link ClayViewHandler}.
40 * </p>
41 */
42 public class ClayViewHandlerCommand implements Command {
43
44 /***
45 * <p>Holds the suffixes used to identify a Clay full HTML or XML view template.</p>
46 */
47 private String[] suffixes = null;
48
49 /***
50 * <p>Looks at the request uri to determine if the target page
51 * is a clay template. If the request's pathInfo matches the
52 * clay template suffixes, a flag is added to the request attributes.
53 * This is need for the MyFaces JSF implementation.</p>
54 *
55 * @param context commons chains
56 * @return <code>true</code> if the chains is final
57 * @exception Exception throws back up the calling stack
58 */
59 public boolean execute(Context context) throws Exception {
60
61 ServletRequest request = (ServletRequest) context.get("request");
62 int index = indexOfClayTemplateSuffix(context);
63 if (index != -1) {
64 request.setAttribute(Globals.CLAY_FULL_VIEW_SUFFIX, suffixes[index]);
65 }
66
67 return false;
68 }
69
70
71 /***
72 * <p>Checks the <code>request.uri</code> to determine if it's suffix matches one of
73 * the <code>suffixes</code>. If a match is found, the index into the <code>suffixes</code>
74 * array is returned. A value of -1 is returned if a match is not found.</p>
75 *
76 * @param context commons chains
77 * @return index into <code>suffixes</code>
78 */
79 protected int indexOfClayTemplateSuffix(Context context) {
80 HttpServletRequest request = (HttpServletRequest) context.get("request");
81 ServletContext servletContext = (ServletContext) context.get("context");
82 if (suffixes == null) {
83 suffixes = new String[2];
84
85 suffixes[0] = servletContext.getInitParameter(
86 Globals.CLAY_HTML_TEMPLATE_SUFFIX);
87 if (suffixes[0] == null) {
88 suffixes[0] = Globals.CLAY_DEFAULT_HTML_TEMPLATE_SUFFIX;
89 }
90
91 suffixes[1] = servletContext.getInitParameter(
92 Globals.CLAY_XML_TEMPLATE_SUFFIX);
93 if (suffixes[1] == null) {
94 suffixes[1] = Globals.CLAY_DEFAULT_XML_TEMPLATE_SUFFIX;
95 }
96
97 }
98 String uri = request.getRequestURI();
99
100 if (uri != null) {
101
102 for (int i = 0; i < suffixes.length; i++) {
103 if (uri.endsWith(suffixes[i])) {
104 return i;
105 }
106 }
107 }
108
109 return -1;
110 }
111
112 }