Catches and reports missing web.xml init parameter
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / errors / ErrorHelper.java
1 package com.indexdata.pz2utils4jsf.errors;\r
2 \r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
4 \r
5 import java.io.Serializable;\r
6 import java.util.ArrayList;\r
7 import java.util.regex.Matcher;\r
8 import java.util.regex.Pattern;\r
9 \r
10 import org.apache.log4j.Logger;\r
11 \r
12 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
13 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error;\r
14 import com.indexdata.pz2utils4jsf.utils.Utils;\r
15 \r
16 public class ErrorHelper implements Serializable {\r
17 \r
18   public enum ErrorCode {PAZPAR2_404, \r
19                          PAZPAR2_UNEXPECTED_RESPONSE,\r
20                          PAZPAR2_12,\r
21                          PAZPAR2_ERRORS,\r
22                          LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
23                          REMOTE_SERVICE_DEF_NOT_FOUND,\r
24                          LOCAL_SETTINGS_FILE_NOT_FOUND,\r
25                          MASTERKEY_CONFIG_FILE_NOT_FOUND,\r
26                          MISSING_MANDATORY_PARAMETER,\r
27                          MISSING_MK2_CONFIG_INIT_PARAMETER,\r
28                          NOT_RESOLVED,\r
29                          SKIP_SUGGESTIONS};\r
30 \r
31   private static final long serialVersionUID = 2860804561068279131L;\r
32   private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
33   \r
34   private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
35   \r
36   private Pz2Configurator configurator = null;\r
37   \r
38   public ErrorHelper(Pz2Configurator configurator) {\r
39     this.configurator = configurator;\r
40   }\r
41   \r
42   public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) {\r
43     if (appError.hasPazpar2Error()) {\r
44       Pazpar2Error pz2err = appError.getPazpar2Error();\r
45       String pz2errcode = pz2err.getCode();\r
46       switch (pz2errcode) {\r
47       case "12": \r
48         return ErrorCode.PAZPAR2_12;\r
49       case "0":    \r
50         if (pz2err.getMsg().contains("target settings from file")) {\r
51           return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;\r
52         } else {\r
53           return ErrorCode.PAZPAR2_ERRORS;\r
54         }\r
55       default: \r
56         return ErrorCode.PAZPAR2_ERRORS;\r
57       }\r
58     } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {\r
59       Matcher m = httpResponsePattern.matcher(appError.getMessage());\r
60       if (m.matches()) {\r
61         String errorCode = m.group(1);\r
62         if (errorCode.equals("404")) {\r
63           return ErrorCode.PAZPAR2_404;\r
64         } else {\r
65           return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
66         }\r
67       }       \r
68     } else if (appError.getMessage().contains("Configuration file") & appError.getMessage().contains("properties")) {\r
69       return ErrorCode.MASTERKEY_CONFIG_FILE_NOT_FOUND; \r
70     } else if (appError.getMessage().contains("Error reading service definition XML")) {\r
71       return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;    \r
72     } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) {\r
73       return ErrorCode.SKIP_SUGGESTIONS;\r
74     } else if (appError.getMessage().contains("Missing mandatory parameter")) {\r
75       return ErrorCode.MISSING_MANDATORY_PARAMETER;\r
76     } else if (appError.getMessage().contains("Init parameter")\r
77                && appError.getMessage().contains("MASTERKEY")\r
78                && appError.getMessage().contains("missing in deployment descriptor")) {\r
79       return ErrorCode.MISSING_MK2_CONFIG_INIT_PARAMETER;\r
80     }\r
81     return ErrorCode.NOT_RESOLVED;\r
82   }\r
83     \r
84   public ArrayList<String> getSuggestions(ErrorInterface error) {\r
85     ArrayList<String> suggestions = new ArrayList<String>();\r
86     ErrorCode code = getErrorCode(error);\r
87     switch (code) {\r
88     case PAZPAR2_404:\r
89       suggestions.add("Pazpar2 service not found (404). ");\r
90       suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
91           + "that a pazpar2 service is running at the given address.");\r
92       addConfigurationDocumentation(suggestions);      \r
93       break;\r
94     case PAZPAR2_UNEXPECTED_RESPONSE:\r
95       suggestions.add("Unexpected response code from Pazpar2. " + nl\r
96           + "Please check the PAZPAR2_URL configuration and verify "\r
97           + "that a pazpar2 service is running at the given address." + nl);\r
98       break;     \r
99     case MASTERKEY_CONFIG_FILE_NOT_FOUND: \r
100       suggestions.add("The main configuration file that is looked up using parameters" +\r
101                 " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +\r
102                 " could not be found. Please check the web.xml parameters and the expected file system location. ");      \r
103       break;\r
104     case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
105       suggestions.add("The service definition file could not be loaded.");\r
106       suggestions.add("Please check the configuration and verify that the file exists");\r
107       addConfigurationDocumentation(suggestions);     \r
108       break;\r
109     case REMOTE_SERVICE_DEF_NOT_FOUND:\r
110       break;\r
111     case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
112       suggestions.add("A configuration using local target settings file was found, but " +\r
113                 " the file itself could not be found. Please check the configuration.");\r
114       addConfigurationDocumentation(suggestions);\r
115       break;\r
116     case MISSING_MANDATORY_PARAMETER:\r
117       suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" +\r
118                 " file used. Please check the property file for the parameter given in the error message ");\r
119       addConfigurationDocumentation(suggestions);\r
120       break;\r
121     case MISSING_MK2_CONFIG_INIT_PARAMETER:\r
122       suggestions.add("A mandatory init parameter was not found in the deployment descriptor (web.xml)." +\r
123                 " Following init parameters must be present in web.xml when using the Masterkey (MK2) configuration scheme:" +\r
124                 " MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " +\r
125                 "MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'");      \r
126       break;\r
127     case NOT_RESOLVED:\r
128       suggestions.add("Unforeseen error situation. No suggestions prepared.");\r
129       break;\r
130     case SKIP_SUGGESTIONS:\r
131       break;\r
132     case PAZPAR2_12: \r
133       suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");\r
134       suggestions.add("Please check the service ID set in the configuration and compare it with the " +\r
135                 " pazpar2 (server side) configuration.");\r
136       addConfigurationDocumentation(suggestions);    \r
137       break;\r
138     case PAZPAR2_ERRORS:\r
139       if (error.hasPazpar2Error()) {\r
140         if (error.getPazpar2Error().getCode().equals("0")) {\r
141           \r
142         }\r
143         suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");\r
144       } else {\r
145         logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");\r
146       }\r
147       break;\r
148     }\r
149     return suggestions;\r
150   }\r
151   \r
152   private void addConfigurationDocumentation (ArrayList<String> suggestions) {\r
153     suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));\r
154     suggestions.add("This configurator reports that following configuration was used: ");\r
155     suggestions.addAll(configurator.document());\r
156   }\r
157 }\r