Coverage details for org.chwf.servlet.view.PageConfig

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.servlet.view;
34  
35 import java.util.Map;
36  
37 import javax.servlet.ServletRequest;
38 import javax.servlet.http.HttpServletRequest;
39  
40 import org.chwf.config.Config;
41 import org.chwf.config.ConfigFactory;
42 import org.chwf.config.ConfigMap;
43 import org.chwf.i18n.UserLocale;
44 import org.chwf.servlet.filter.ResourceMapper;
45  
46 /**
47  * Class that exposes information about the page configuration. It wraps
48  * the appropriate {@link org.chwf.config.Config} with type-safe accessors.
49  */
50 public class PageConfig {
51  
52   /** Directory for page configuration. */
53   public static final String PAGE_CONFIG = "page_config";
54  
55   /** Encryption level forcing encryption: "ALWAYS". */
56   public static final String ENCRYPTION_ALWAYS = "ALWAYS";
57  
58   /** Encryption level leaving encryption unchanged: "AS_REQUESTED". */
59   public static final String ENCRYPTION_AS_REQUESTED = "AS_REQUESTED";
60  
61   /** Encryption level forcing non-encryption: "NEVER". */
62   public static final String ENCRYPTION_NEVER = "NEVER";
63  
64   /** Request attribute caching the page config map: "page_config_map". */
65   public static final String PAGE_CONFIG_MAP = "page_config_map";
66  
67   /** Request attribute caching the page config: "page_config". */
68   private static final String CONFIG_ATTRIBUTE = "page_config";
69  
70   /** Config property name for page template. */
71   private static final String PAGE_TEMPLATE = "page-template";
72  
73   /** Config property name for security roles. */
74   private static final String SECURITY_ROLES = "security.roles";
75  
76   /** Config property name for security roles. */
77   private static final String SECURITY_LOGIN = "security.login";
78  
79   /** Config property name for security encryption. */
80   private static final String SECURITY_ENCRYPTION = "security.encryption";
81  
82   /** Empty string array */
831  private static final String[] EMPTY_STRING_ARRAY = new String[0];
84  
85   /**
86    * Get the view config for this request.
87    *
88    * @param request The request.
89    * @return The config.
90    */
91   public static PageConfig getPageConfig(ServletRequest request) {
9230    PageConfig pageConfig = (PageConfig) request.getAttribute(CONFIG_ATTRIBUTE);
9330    if (pageConfig == null) {
9426      String resource = derivePageResource((HttpServletRequest) request);
9526      pageConfig = new PageConfig(resource);
9626      request.setAttribute(CONFIG_ATTRIBUTE, pageConfig);
9726      request.setAttribute(PAGE_CONFIG_MAP, pageConfig.getConfigMap());
98     }
9930    return pageConfig;
100   }
101  
102   /** The raw config. */
103   private final String resource;
104  
105   /** The raw config. */
106   private final Config config;
107  
108   /** Adapter map for configuration. */
109   private final ConfigMap configMap;
110  
111   /**
112    * Constructor.
113    *
114    * @param resource The resource for the config.
115    */
11626  public PageConfig(String resource) {
11726    this.resource = resource;
11826    this.config = ConfigFactory.getConfig(resource, UserLocale.getLocale());
11926    this.configMap = new ConfigMap(config);
12026  }
121  
122   /**
123    * The resource file for page configuration.
124    *
125    * @return The resource file for page configuration.
126    */
127   public String getResource() {
1283    return this.resource;
129   }
130  
131   /**
132    * The page template.
133    *
134    * @return The page template, or <code>null</code> if there isn't one.
135    */
136   public String getPageTemplate() {
1377    return config.get(PAGE_TEMPLATE, null);
138   }
139  
140   /**
141    * The security roles.
142    *
143    * @return The page template, or <code>null</code> if there isn't one.
144    */
145   public String[] getSecurityRoles() {
14613    return config.getList(SECURITY_ROLES, EMPTY_STRING_ARRAY);
147   }
148  
149   /**
150    * The login page.
151    *
152    * @return The login page, or <code>null</code> if there isn't one.
153    */
154   public String getSecurityLogin() {
1553    return config.get(SECURITY_LOGIN, null);
156   }
157  
158   /**
159    * The security encryption level: "ALWAYS", "NEVER" or "AS_REQUESTED".
160    *
161    * @return The security encryption level.
162    */
163   public String getSecurityEncryption() {
1648    String defaultLevel = ENCRYPTION_AS_REQUESTED;
1658    if (this.getSecurityRoles().length > 0) {
1663      defaultLevel = ENCRYPTION_ALWAYS;
167     }
1688    return config.get(SECURITY_ENCRYPTION, defaultLevel);
169   }
170  
171   /**
172    * The page raw configuration data. You can use it to query generic
173    * configuration properties.
174    *
175    * @return The raw configuration.
176    */
177   public Config getRawConfig() {
1781    return this.config;
179   }
180  
181   /**
182    * Get page config data as a map. Useful for expose data to scripting
183    * environments (e.g. JSTL EL).
184    *
185    * @return The config data in a wrapped in a map.
186    */
187   public Map getConfigMap() {
18827    return configMap;
189   }
190  
191   /**
192    * Derive the page config resource name for the given URI, for a lookup
193    * inside the "page_config" directory in the classpath.
194    *
195    * @param request The request.
196    * @return The page config.
197    */
198   private static String derivePageResource(HttpServletRequest request) {
19926    String uri = ResourceMapper.getContextFreeURI(request);
20026    int dot = uri.lastIndexOf('.');
20126    if (dot > 0) {
20225      uri = uri.substring(0, dot);
203     }
20426    return PAGE_CONFIG + uri.replace('/', '.');
205   }
206 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.