| Line | Hits | Source |
|---|---|---|
| 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 */ | |
| 83 | 1 | 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) { | |
| 92 | 30 | PageConfig pageConfig = (PageConfig) request.getAttribute(CONFIG_ATTRIBUTE); |
| 93 | 30 | if (pageConfig == null) { |
| 94 | 26 | String resource = derivePageResource((HttpServletRequest) request); |
| 95 | 26 | pageConfig = new PageConfig(resource); |
| 96 | 26 | request.setAttribute(CONFIG_ATTRIBUTE, pageConfig); |
| 97 | 26 | request.setAttribute(PAGE_CONFIG_MAP, pageConfig.getConfigMap()); |
| 98 | } | |
| 99 | 30 | 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 | */ | |
| 116 | 26 | public PageConfig(String resource) { |
| 117 | 26 | this.resource = resource; |
| 118 | 26 | this.config = ConfigFactory.getConfig(resource, UserLocale.getLocale()); |
| 119 | 26 | this.configMap = new ConfigMap(config); |
| 120 | 26 | } |
| 121 | ||
| 122 | /** | |
| 123 | * The resource file for page configuration. | |
| 124 | * | |
| 125 | * @return The resource file for page configuration. | |
| 126 | */ | |
| 127 | public String getResource() { | |
| 128 | 3 | 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() { | |
| 137 | 7 | 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() { | |
| 146 | 13 | 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() { | |
| 155 | 3 | 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() { | |
| 164 | 8 | String defaultLevel = ENCRYPTION_AS_REQUESTED; |
| 165 | 8 | if (this.getSecurityRoles().length > 0) { |
| 166 | 3 | defaultLevel = ENCRYPTION_ALWAYS; |
| 167 | } | |
| 168 | 8 | 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() { | |
| 178 | 1 | 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() { | |
| 188 | 27 | 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) { | |
| 199 | 26 | String uri = ResourceMapper.getContextFreeURI(request); |
| 200 | 26 | int dot = uri.lastIndexOf('.'); |
| 201 | 26 | if (dot > 0) { |
| 202 | 25 | uri = uri.substring(0, dot); |
| 203 | } | |
| 204 | 26 | return PAGE_CONFIG + uri.replace('/', '.'); |
| 205 | } | |
| 206 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |