2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: ClayViewHandlerCommand.java 464373 2006-10-16 04:21:54Z rahul $
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             //look at the path
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 }