Avoids npe in case of main config file missing
[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.config.Pz2ConfigureByMk2Config;\r
14 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error;\r
15 import com.indexdata.pz2utils4jsf.utils.Utils;\r
16 \r
17 public class ErrorHelper implements Serializable {\r
18 \r
19   public enum ErrorCode {PAZPAR2_404, \r
20                          PAZPAR2_UNEXPECTED_RESPONSE,\r
21                          PAZPAR2_12,\r
22                          PAZPAR2_ERRORS,\r
23                          LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
24                          REMOTE_SERVICE_DEF_NOT_FOUND,\r
25                          LOCAL_SETTINGS_FILE_NOT_FOUND,\r
26                          MASTERKEY_CONFIG_FILE_NOT_FOUND,\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 Pz2Configurator configurator = null;\r
36   \r
37   public ErrorHelper(Pz2Configurator configurator) {\r
38     this.configurator = configurator;\r
39   }\r
40   \r
41   public ErrorHelper.ErrorCode getErrorCode(ApplicationError appError) {\r
42     if (appError.hasPazpar2Error()) {\r
43       Pazpar2Error pz2err = appError.getPazpar2Error();\r
44       String pz2errcode = pz2err.getCode();\r
45       switch (pz2errcode) {\r
46       case "12": \r
47         return ErrorCode.PAZPAR2_12;\r
48       case "0":    \r
49         if (pz2err.getMsg().contains("target settings from file")) {\r
50           return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;\r
51         } else {\r
52           return ErrorCode.PAZPAR2_ERRORS;\r
53         }\r
54       default: \r
55         return ErrorCode.PAZPAR2_ERRORS;\r
56       }\r
57     } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {\r
58       Matcher m = httpResponsePattern.matcher(appError.getMessage());\r
59       if (m.matches()) {\r
60         String errorCode = m.group(1);\r
61         if (errorCode.equals("404")) {\r
62           return ErrorCode.PAZPAR2_404;\r
63         } else {\r
64           return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
65         }\r
66       }       \r
67     } else if (appError.getMessage().contains("Configuration file") & appError.getMessage().contains("properties")) {\r
68       return ErrorCode.MASTERKEY_CONFIG_FILE_NOT_FOUND; \r
69     } else if (appError.getMessage().contains("Error reading service definition XML")) {\r
70       return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;    \r
71     } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) {\r
72       return ErrorCode.SKIP_SUGGESTIONS;\r
73     }\r
74     return ErrorCode.NOT_RESOLVED;\r
75   }\r
76     \r
77   public ArrayList<String> getSuggestions(ApplicationError error) {\r
78     ArrayList<String> suggestions = new ArrayList<String>();\r
79     ErrorCode code = getErrorCode(error);\r
80     switch (code) {\r
81     case PAZPAR2_404:\r
82       suggestions.add("Pazpar2 service not found (404). ");\r
83       suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
84           + "that a pazpar2 service is running at the given address.");\r
85       addConfigurationDocumentation(suggestions);      \r
86       break;\r
87     case PAZPAR2_UNEXPECTED_RESPONSE:\r
88       suggestions.add("Unexpected response code from Pazpar2. " + nl\r
89           + "Please check the PAZPAR2_URL configuration and verify "\r
90           + "that a pazpar2 service is running at the given address." + nl);\r
91       break;     \r
92     case MASTERKEY_CONFIG_FILE_NOT_FOUND: \r
93       suggestions.add("The main configuration file that is looked up using parameters" +\r
94                 " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +\r
95                 " could not be found. Please check the web.xml parameters and the expected file system location. ");      \r
96       break;\r
97     case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
98       suggestions.add("The service definition file could not be loaded.");\r
99       suggestions.add("Please check the configuration and verify that the file exists");\r
100       addConfigurationDocumentation(suggestions);     \r
101       break;\r
102     case REMOTE_SERVICE_DEF_NOT_FOUND:\r
103       break;\r
104     case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
105       suggestions.add("A configuration using local target settings file was found, but " +\r
106                 " the file itself could not be found. Please check the configuration.");\r
107       addConfigurationDocumentation(suggestions);\r
108       break;\r
109     case NOT_RESOLVED:\r
110       suggestions.add("Unforeseen error situation. No suggestions prepared.");\r
111       break;\r
112     case SKIP_SUGGESTIONS:\r
113       break;\r
114     case PAZPAR2_12: \r
115       suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");\r
116       suggestions.add("Please check the service ID set in the configuration and compare it with the " +\r
117                 " pazpar2 (server side) configuration.");\r
118       addConfigurationDocumentation(suggestions);    \r
119       break;\r
120     case PAZPAR2_ERRORS:\r
121       if (error.hasPazpar2Error()) {\r
122         if (error.getPazpar2Error().getCode().equals("0")) {\r
123           \r
124         }\r
125         suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");\r
126       } else {\r
127         logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");\r
128       }\r
129       break;\r
130     }\r
131     return suggestions;\r
132   }\r
133   \r
134   private void addConfigurationDocumentation (ArrayList<String> suggestions) {\r
135     suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));\r
136     suggestions.add("This configurator reports that following configuration was used: ");\r
137     suggestions.addAll(configurator.document());\r
138   }\r
139 }\r