1 /* 2 * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH 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 de.smartics.exceptions.i18n.message; 17 18 import java.util.List; 19 import java.util.Map; 20 21 import de.smartics.exceptions.i18n.message.MessageParamParser.MessageParamInfo; 22 23 /** 24 * Defines the valid message types to be displayed. 25 * <p> 26 * Note that most of the time the messages provided to exceptions are intended 27 * for the system operator or software developer as long as the presentation 28 * tier is not concerned. Displaying messages in the presentation tier often 29 * requires more structured text than simple text (e.g. messages shown in a web 30 * browser). Presentation tier messages should abstract from the messages 31 * provided to the exceptions caught from lower levels. 32 * </p> 33 * <p> 34 * Displaying messages intended for the operator or software developer to the 35 * user will also introduce a security risk. Please refer to <a href= 36 * "http://www.owasp.org/index.php/Top_10_2007-Information_Leakage_and_Improper_Error_Handling" 37 * >Information Leakage and Improper Error Handling</a> for details. 38 * </p> 39 */ 40 public enum MessageType 41 { 42 // ****************************** Enumeration ******************************* 43 44 /** 45 * The suffix for the resource key to fetch title information. 46 * <p> 47 * This information is intended to provide a simple category to be displayed 48 * in window titles. More than one message has the same title. 49 * </p> 50 * <p> 51 * The value of this constant is <code>".title"</code>. 52 * </p> 53 */ 54 TITLE(".title"), 55 56 /** 57 * The suffix for the resource key to fetch summary information. 58 * <p> 59 * The summary is a short explanation of what has happened. 60 * </p> 61 * <p> 62 * The value of this constant is the empty string. 63 * </p> 64 */ 65 SUMMARY(""), 66 67 /** 68 * The suffix for the resource key to fetch details information. 69 * <p> 70 * The details information gives more details on what has happened. Typical 71 * usage of this message type is to add parameter values that were passed to 72 * the module that throwed the exception of to give some technical details 73 * only relevant to users of the system that track the failure to its root 74 * cause. 75 * </p> 76 * <p> 77 * The value of this constant is <code>".details"</code>. 78 * </p> 79 */ 80 DETAILS(".details"), 81 82 /** 83 * The suffix for the resource key to fetch information about what 84 * implications the abort of the current task has on the work of the user. 85 * <p> 86 * While the summary and details section of the message explained what has 87 * happend, the implications show what this means for the current task being 88 * aborted. The reader of the message can e.g. be assured that his transaction 89 * has been rolled back and no money transfer has taken place. 90 * </p> 91 * <p> 92 * The value of this constant is <code>".implications"</code>. 93 * </p> 94 */ 95 IMPLICATIONS_ON_CURRENT_TASK(".implications"), 96 97 /** 98 * The suffix for the resource key to fetch information about what the user 99 * can do now. 100 * <p> 101 * If the exception has been raised e.g. because of invalid user input this 102 * information can lead the user to what input has been expected. It can also 103 * give hints what configuration has lead to this problem in case a subsystem 104 * is not available. 105 * </p> 106 * <p> 107 * The value of this constant is <code>".todo"</code>. 108 * </p> 109 */ 110 WHAT_TO_DO_NOW(".todo"), 111 112 /** 113 * The suffix for the resource key to fetch an URL that links to further 114 * information on the problem. 115 * <p> 116 * The information references may provide even more details on the type of 117 * failure being reported. The referenced page may only have static content 118 * that does not quote parameters provided by the exception instance, but may 119 * provide structured information and links to related information on a help 120 * portal or FAQ. 121 * </p> 122 * <p> 123 * The value of this constant is <code>".url"</code>. 124 * </p> 125 */ 126 URL(".url"); 127 128 // ********************************* Fields ********************************* 129 130 // --- constants ------------------------------------------------------------ 131 132 // --- members -------------------------------------------------------------- 133 134 /** 135 * The suffix for the resource keys to fetch a specific type of message. 136 * 137 * @serial 138 */ 139 private final String messageKeySuffix; 140 141 // ****************************** Constructors ****************************** 142 143 /** 144 * Default constructor. 145 * 146 * @param messageKeySuffix the suffix for the resource keys to fetch a 147 * specific type of message. 148 */ 149 private MessageType(final String messageKeySuffix) 150 { 151 this.messageKeySuffix = messageKeySuffix; 152 } 153 154 // ********************************* Methods ******************************** 155 156 // --- init ----------------------------------------------------------------- 157 158 // --- get&set -------------------------------------------------------------- 159 160 /** 161 * Returns the suffix for the resource keys to fetch a specific type of 162 * message. 163 * 164 * @return the suffix for the resource keys to fetch a specific type of 165 * message. 166 */ 167 public String getMessageKeySuffix() 168 { 169 return this.messageKeySuffix; 170 } 171 172 // --- business ------------------------------------------------------------- 173 174 /** 175 * Returns the key with the message type resource key suffix ( 176 * {@link #getMessageKeySuffix()}) appended. 177 * 178 * @param keyPrefix the prefix of the key. 179 * @return the key with the given prefix and the message type suffix appended. 180 */ 181 public String createKey(final String keyPrefix) 182 { 183 return keyPrefix + this.messageKeySuffix; 184 } 185 186 /** 187 * Returns the index information specified in the message parameter for the 188 * message type. 189 * 190 * @param propertyName the name of the property whose message parameter is to 191 * be parsed. 192 * @param messageParam the annotation that contains the specific or default 193 * index with optional OGNL path. 194 * @return the specific or default index information set. If no index is set, 195 * the empty list is returned. 196 */ 197 public List<MessageParamInfo> getMessageParamInfos(final String propertyName, 198 final MessageParam messageParam) 199 { 200 final String value = messageParam.value(); 201 final List<MessageParamInfo> infos = 202 MessageParamParser.parse(propertyName, value); 203 return infos; 204 } 205 206 /** 207 * Returns the index information for the parent attributes specified in the 208 * message parameter for the message type. 209 * 210 * @param messageParam the annotation that contains the specific or default 211 * index with optional OGNL path for each parent attribute. 212 * @return the specific or default index information set for each attribute. 213 * If no index is set, the empty map is returned. The key is the name 214 * of the attribute, the value is the message parameter info as per 215 * {@link #getMessageParamInfos(MessageParam)}. 216 */ 217 public Map<String, List<MessageParamInfo>> getParentMessageParamInfos( 218 final ParentMessageParam messageParam) 219 { 220 final String value = messageParam.value(); 221 222 final Map<String, List<MessageParamInfo>> map = 223 MessageParamParser.parseParentMessageParam(value); 224 225 return map; 226 } 227 228 // --- object basics -------------------------------------------------------- 229 230 }