1 package com.indexdata.pz2utils4jsf.errors;
\r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
\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
10 import org.apache.log4j.Logger;
\r
12 import com.indexdata.pz2utils4jsf.config.ConfigurationReader;
\r
13 import com.indexdata.pz2utils4jsf.utils.Utils;
\r
15 public class ErrorHelper implements Serializable {
\r
17 public enum ErrorCode {PAZPAR2_404,
\r
18 PAZPAR2_UNEXPECTED_RESPONSE,
\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
30 private static final long serialVersionUID = 2860804561068279131L;
\r
31 private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");
\r
33 private static Logger logger = Logger.getLogger(ErrorHelper.class);
\r
35 private ConfigurationReader configurator = null;
\r
37 public ErrorHelper(ConfigurationReader configurator) {
\r
38 this.configurator = configurator;
\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
47 return ErrorCode.PAZPAR2_ERRORS;
\r
49 } else if (errmsg.startsWith("Unexpected HTTP response")) {
\r
50 Matcher m = httpResponsePattern.matcher(appError.getMessage());
\r
52 String errorCode = m.group(1);
\r
53 if (errorCode.equals("404")) {
\r
54 return ErrorCode.PAZPAR2_404;
\r
56 return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;
\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
72 return ErrorCode.NOT_RESOLVED;
\r
75 public ArrayList<String> getSuggestions(ErrorInterface error) {
\r
76 ArrayList<String> suggestions = new ArrayList<String>();
\r
77 ErrorCode code = getErrorCode(error);
\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
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
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
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
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
105 case REMOTE_SERVICE_DEF_NOT_FOUND:
\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
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
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
123 case PAZPAR2_ERRORS:
\r
124 if (error.hasPazpar2Error()) {
\r
125 String pz2code = error.getPazpar2Error().getCode();
\r
128 suggestions.add("Query terms not supported.");
\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
137 suggestions.add("Pazpar2 error: " + error.getPazpar2Error().getMsg() + " (Pazpar2 # "+error.getPazpar2Error().getCode()+")");
\r
141 logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");
\r
144 case SKIP_SUGGESTIONS:
\r
147 suggestions.add("Sorry, no troubleshooting suggestions were written for this error scenario just yet.");
\r
150 return suggestions;
\r
153 private void addConfigurationDocumentation (ArrayList<String> suggestions) {
\r
154 suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));
\r
155 suggestions.add("This configurator reports that following configuration was used: ");
\r
156 suggestions.addAll(configurator.document());
\r