View Javadoc

1   /*
2    * Copyright 2007-2011 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   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
41   * @version $Revision$
42   */
43  public enum MessageType
44  {
45    // ****************************** Enumeration *******************************
46  
47    /**
48     * The suffix for the resource key to fetch title information.
49     * <p>
50     * This information is intended to provide a simple category to be displayed
51     * in window titles. More than one message has the same title.
52     * <p>
53     * The value of this constant is <code>".title"</code>.
54     * </p>
55     */
56    TITLE(".title"),
57  
58    /**
59     * The suffix for the resource key to fetch summary information.
60     * <p>
61     * The summary is a short explanation of what has happened.
62     * </p>
63     * <p>
64     * The value of this constant is the empty string.
65     * </p>
66     */
67    SUMMARY(""),
68  
69    /**
70     * The suffix for the resource key to fetch details information.
71     * <p>
72     * The details information gives more details on what has happened. Typical
73     * usage of this message type is to add parameter values that were passed to
74     * the module that throwed the exception of to give some technical details
75     * only relevant to users of the system that track the failure to its root
76     * cause.
77     * </p>
78     * <p>
79     * The value of this constant is <code>".details"</code>.
80     * </p>
81     */
82    DETAILS(".details"),
83  
84    /**
85     * The suffix for the resource key to fetch information about what
86     * implications the abort of the current task has on the work of the user.
87     * <p>
88     * While the summary and details section of the message explained what has
89     * happend, the implications show what this means for the current task being
90     * aborted. The reader of the message can e.g. be assured that his transaction
91     * has been rolled back and no money transfer has taken place.
92     * </p>
93     * <p>
94     * The value of this constant is <code>".implications"</code>.
95     * </p>
96     */
97    IMPLICATIONS_ON_CURRENT_TASK(".implications"),
98  
99    /**
100    * The suffix for the resource key to fetch information about what the user
101    * can do now.
102    * <p>
103    * If the exception has been raised e.g. because of invalid user input this
104    * information can lead the user to what input has been expected. It can also
105    * give hints what configuration has lead to this problem in case a subsystem
106    * is not available.
107    * </p>
108    * <p>
109    * The value of this constant is <code>".todo"</code>.
110    * </p>
111    */
112   WHAT_TO_DO_NOW(".todo"),
113 
114   /**
115    * The suffix for the resource key to fetch an URL that links to further
116    * information on the problem.
117    * <p>
118    * The information references may provide even more details on the type of
119    * failure being reported. The referenced page may only have static content
120    * that does not quote parameters provided by the exception instance, but may
121    * provide structured information and links to related information on a help
122    * portal or FAQ.
123    * </p>
124    * <p>
125    * The value of this constant is <code>".url"</code>.
126    * </p>
127    */
128   URL(".url");
129 
130   // ********************************* Fields *********************************
131 
132   // --- constants ------------------------------------------------------------
133 
134   // --- members --------------------------------------------------------------
135 
136   /**
137    * The suffix for the resource keys to fetch a specific type of message.
138    *
139    * @serial
140    */
141   private final String messageKeySuffix;
142 
143   // ****************************** Constructors ******************************
144 
145   /**
146    * Default constructor.
147    *
148    * @param messageKeySuffix the suffix for the resource keys to fetch a
149    *          specific type of message.
150    */
151   private MessageType(final String messageKeySuffix)
152   {
153     this.messageKeySuffix = messageKeySuffix;
154   }
155 
156   // ********************************* Methods ********************************
157 
158   // --- init -----------------------------------------------------------------
159 
160   // --- get&set --------------------------------------------------------------
161 
162   /**
163    * Returns the suffix for the resource keys to fetch a specific type of
164    * message.
165    *
166    * @return the suffix for the resource keys to fetch a specific type of
167    *         message.
168    */
169   public String getMessageKeySuffix()
170   {
171     return this.messageKeySuffix;
172   }
173 
174   // --- business -------------------------------------------------------------
175 
176   /**
177    * Returns the key with the message type resource key suffix (
178    * {@link #getMessageKeySuffix()}) appended.
179    *
180    * @param keyPrefix the prefix of the key.
181    * @return the key with the given prefix and the message type suffix appended.
182    */
183   public String createKey(final String keyPrefix)
184   {
185     return keyPrefix + this.messageKeySuffix;
186   }
187 
188   /**
189    * Returns the index information specified in the message parameter for the
190    * message type.
191    *
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(
198       final MessageParam messageParam)
199   {
200     String specificIndexString;
201     if (TITLE == this)
202     {
203       specificIndexString = messageParam.headerParamIndex();
204     }
205     else if (SUMMARY == this)
206     {
207       specificIndexString = messageParam.summaryParamIndex();
208     }
209     else if (DETAILS == this)
210     {
211       specificIndexString = messageParam.detailsParamIndex();
212     }
213     else if (IMPLICATIONS_ON_CURRENT_TASK == this)
214     {
215       specificIndexString = messageParam.implicationsParamIndex();
216     }
217     else if (WHAT_TO_DO_NOW == this)
218     {
219       specificIndexString = messageParam.todoParamIndex();
220     }
221     else if (URL == this)
222     {
223       specificIndexString = messageParam.urlParamIndex();
224     }
225     else
226     {
227       assert false : "Unexpected enumeration type: " + this;
228       specificIndexString = null; // NOPMD
229     }
230 
231     if ("".equals(specificIndexString))
232     {
233       specificIndexString = messageParam.value();
234     }
235     final List<MessageParamInfo> infos =
236         MessageParamParser.parse(specificIndexString);
237 
238     return infos;
239   }
240 
241   /**
242    * Returns the index information for the parent attributes specified in the
243    * message parameter for the message type.
244    *
245    * @param messageParam the annotation that contains the specific or default
246    *          index with optional OGNL path for each parent attribute.
247    * @return the specific or default index information set for each attribute.
248    *         If no index is set, the empty map is returned. The key is the name
249    *         of the attribute, the value is the message parameter info as per
250    *         {@link #getMessageParamInfos(MessageParam)}.
251    */
252   public Map<String, List<MessageParamInfo>> getParentMessageParamInfos(
253       final ParentMessageParam messageParam)
254   {
255     String specificIndexString;
256     if (TITLE == this)
257     {
258       specificIndexString = messageParam.headerParamIndex();
259     }
260     else if (SUMMARY == this)
261     {
262       specificIndexString = messageParam.summaryParamIndex();
263     }
264     else if (DETAILS == this)
265     {
266       specificIndexString = messageParam.detailsParamIndex();
267     }
268     else if (IMPLICATIONS_ON_CURRENT_TASK == this)
269     {
270       specificIndexString = messageParam.implicationsParamIndex();
271     }
272     else if (WHAT_TO_DO_NOW == this)
273     {
274       specificIndexString = messageParam.todoParamIndex();
275     }
276     else if (URL == this)
277     {
278       specificIndexString = messageParam.urlParamIndex();
279     }
280     else
281     {
282       assert false : "Unexpected enumeration type: " + this;
283       specificIndexString = null; // NOPMD
284     }
285 
286     if ("".equals(specificIndexString))
287     {
288       specificIndexString = messageParam.value();
289     }
290 
291     final Map<String, List<MessageParamInfo>> map =
292         MessageParamParser.parseParentMessageParam(specificIndexString);
293 
294     return map;
295   }
296 
297   // --- object basics --------------------------------------------------------
298 
299 }