--- /dev/null
+package com.indexdata.mkjsf.errors;\r
+\r
+import static com.indexdata.mkjsf.utils.Utils.nl;\r
+\r
+import java.io.Serializable;\r
+import java.util.ArrayList;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.config.ConfigurationReader;\r
+import com.indexdata.mkjsf.utils.Utils;\r
+\r
+public class ErrorHelper implements Serializable {\r
+\r
+ public enum ErrorCode {PAZPAR2_404, \r
+ PAZPAR2_UNEXPECTED_RESPONSE,\r
+ PAZPAR2_ERRORS,\r
+ LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
+ REMOTE_SERVICE_DEF_NOT_FOUND,\r
+ LOCAL_SETTINGS_FILE_NOT_FOUND,\r
+ MASTERKEY_CONFIG_FILE_NOT_FOUND,\r
+ MISSING_MANDATORY_PROPERTY,\r
+ MISSING_MK2_CONFIG_INIT_PARAMETER,\r
+ MISSING_CONTEXT_PARAMETER,\r
+ NOT_RESOLVED,\r
+ SKIP_SUGGESTIONS};\r
+\r
+ private static final long serialVersionUID = 2860804561068279131L;\r
+ private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
+ \r
+ private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
+ \r
+ private ConfigurationReader configurator = null;\r
+ \r
+ public ErrorHelper(ConfigurationReader configurator) {\r
+ this.configurator = configurator;\r
+ }\r
+ \r
+ public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) {\r
+ String errmsg = appError.getMessage();\r
+ if (appError.isServiceError()) {\r
+ if (appError.getServiceError().getMsg().contains("target settings from file")) {\r
+ return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;\r
+ } else {\r
+ return ErrorCode.PAZPAR2_ERRORS;\r
+ }\r
+ } else if (errmsg.startsWith("Unexpected HTTP response")) {\r
+ Matcher m = httpResponsePattern.matcher(appError.getMessage());\r
+ if (m.matches()) {\r
+ String errorCode = m.group(1);\r
+ if (errorCode.equals("404")) {\r
+ return ErrorCode.PAZPAR2_404;\r
+ } else {\r
+ return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
+ }\r
+ } \r
+ } else if (errmsg.contains("Configuration file") & appError.getMessage().contains("properties")) {\r
+ return ErrorCode.MASTERKEY_CONFIG_FILE_NOT_FOUND; \r
+ } else if (errmsg.contains("Error reading service definition XML")) {\r
+ return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND; \r
+ } else if (errmsg.contains("Cannot query Pazpar2 while there are configuration errors")) {\r
+ return ErrorCode.SKIP_SUGGESTIONS;\r
+ } else if (errmsg.contains("Missing mandatory parameter")) {\r
+ return ErrorCode.MISSING_MANDATORY_PROPERTY;\r
+ } else if (errmsg.contains("ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { \r
+ return ErrorCode.MISSING_MK2_CONFIG_INIT_PARAMETER;\r
+ } else if (appError.getMessage().contains("WebXmlConfigReader could not find mandatory context-param")) {\r
+ return ErrorCode.MISSING_CONTEXT_PARAMETER;\r
+ }\r
+ return ErrorCode.NOT_RESOLVED;\r
+ }\r
+ \r
+ public ArrayList<String> getSuggestions(ErrorInterface error) {\r
+ ArrayList<String> suggestions = new ArrayList<String>();\r
+ ErrorCode code = getErrorCode(error);\r
+ switch (code) {\r
+ case MISSING_MK2_CONFIG_INIT_PARAMETER:\r
+ suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." +\r
+ " Following init parameters must be present when using the MasterKey configuration scheme (ConfigureByMk2Config):" +\r
+ " MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " +\r
+ " MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'"); \r
+ break;\r
+ case MISSING_CONTEXT_PARAMETER:\r
+ suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." +\r
+ " Following init parameters must be present when using WebXmlConfigReader:" +\r
+ " PAZPAR2_URL, PAZPAR2_SERVICE_ID"); \r
+ break;\r
+ case MISSING_MANDATORY_PROPERTY:\r
+ suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" +\r
+ " file used. Please check the property file for the parameter given in the error message ");\r
+ addConfigurationDocumentation(suggestions);\r
+ break; \r
+ case MASTERKEY_CONFIG_FILE_NOT_FOUND: \r
+ suggestions.add("The main configuration file that is looked up using parameters" +\r
+ " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +\r
+ " could not be found. Please check the web.xml parameters and the expected file system location. "); \r
+ break;\r
+ case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
+ suggestions.add("The service definition file could not be loaded.");\r
+ suggestions.add("Please check the configuration and verify that the file exists");\r
+ addConfigurationDocumentation(suggestions); \r
+ break;\r
+ case REMOTE_SERVICE_DEF_NOT_FOUND:\r
+ break;\r
+ case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
+ suggestions.add("A configuration using local target settings file was found, but " +\r
+ " the file itself could not be found. Please check the configuration.");\r
+ addConfigurationDocumentation(suggestions);\r
+ break;\r
+ case PAZPAR2_404:\r
+ suggestions.add("Pazpar2 service not found (404). ");\r
+ suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
+ + "that a pazpar2 service is running at the given address.");\r
+ addConfigurationDocumentation(suggestions); \r
+ break;\r
+ case PAZPAR2_UNEXPECTED_RESPONSE:\r
+ suggestions.add("Unexpected response code from Pazpar2. " + nl\r
+ + "Please check the PAZPAR2_URL configuration and verify "\r
+ + "that a pazpar2 service is running at the given address." + nl);\r
+ break; \r
+ case PAZPAR2_ERRORS:\r
+ if (error.isServiceError()) { \r
+ int pz2code = Integer.parseInt(error.getServiceError().getCode());\r
+ switch (pz2code) {\r
+ case 3:\r
+ suggestions.add("The search experienced a problem with the query terms.");\r
+ break;\r
+ case 12:\r
+ suggestions.add("The Pazpar2 server does not have a service defined by the requested ID ");\r
+ suggestions.add("Please check the service ID set in the configuration and compare it with the " +\r
+ " configuration on the Pazpar2 server-side.");\r
+ addConfigurationDocumentation(suggestions); \r
+ break;\r
+ case 100:\r
+ suggestions.add("Pazpar2 Service Proxy error");\r
+ suggestions.add("A request was made to the Pazpar2 Service Proxy, but the Service Proxy reports ");\r
+ suggestions.add(" that authentication is lacking. Could be no successful authentication request was made or");\r
+ suggestions.add(" that the Service Proxy session timed out.");\r
+ break;\r
+ default:\r
+ suggestions.add("Pazpar2 error: " + error.getServiceError().getMsg() + " (Pazpar2 # "+error.getServiceError().getCode()+")");\r
+ }\r
+ break;\r
+ } else {\r
+ logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");\r
+ }\r
+ break;\r
+ case SKIP_SUGGESTIONS:\r
+ break; \r
+ case NOT_RESOLVED:\r
+ suggestions.add("Sorry, no troubleshooting suggestions were written for this error scenario just yet.");\r
+ break; \r
+ }\r
+ return suggestions;\r
+ }\r
+ \r
+ private void addConfigurationDocumentation (ArrayList<String> suggestions) {\r
+ suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));\r
+ suggestions.add("This configurator reports that following configuration was used: ");\r
+ suggestions.addAll(configurator.document());\r
+ }\r
+}\r