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