Work in progress on error detect,report,troubleshoot
[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 java.io.Serializable;\r
4 import java.util.ArrayList;\r
5 import java.util.regex.Matcher;\r
6 import java.util.regex.Pattern;\r
7 \r
8 import org.apache.log4j.Logger;\r
9 \r
10 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
11 import com.indexdata.pz2utils4jsf.utils.Utils;\r
12 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
13 \r
14 public class ErrorHelper implements Serializable {\r
15 \r
16   public enum ErrorCode {PAZPAR2_404, \r
17                          PAZPAR2_UNEXPECTED_RESPONSE,\r
18                          LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
19                          REMOTE_SERVICE_DEF_NOT_FOUND,\r
20                          LOCAL_SETTINGS_FILE_NOT_FOUND,\r
21                          NOT_RESOLVED};\r
22 \r
23   private static final long serialVersionUID = 2860804561068279131L;\r
24   private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
25   private static Pattern missingLocalServiceDefFile = Pattern.compile(".*Error reading service definition XML.*");\r
26   private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
27   \r
28   private Pz2Configurator configurator = null;\r
29   \r
30   public ErrorHelper(Pz2Configurator configurator) {\r
31     this.configurator = configurator;\r
32   }\r
33   \r
34   public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) {    \r
35     if (error.getMessage().startsWith("Unexpected HTTP response")) {\r
36       Matcher m = httpResponsePattern.matcher(error.getMessage());\r
37       if (m.matches()) {\r
38         String errorCode = m.group(1);\r
39         if (errorCode.equals("404")) {\r
40           return ErrorCode.PAZPAR2_404;\r
41         } else {\r
42           return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;\r
43         }\r
44       }       \r
45     } else if (error.getMessage().contains("Error reading service definition XML")) {\r
46       return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;\r
47     }\r
48     return ErrorCode.NOT_RESOLVED;\r
49   }\r
50     \r
51   public ArrayList<String> getSuggestions(ApplicationError error) {\r
52     ArrayList<String> suggestions = new ArrayList<String>();\r
53     ErrorCode code = getErrorCode(error);\r
54     switch (code) {\r
55     case PAZPAR2_404:\r
56       suggestions.add("Pazpar2 service not found (404). ");\r
57       suggestions.add("Please check the PAZPAR2_URL configuration and verify "\r
58           + "that a pazpar2 service is running at the given address.");\r
59       suggestions.add("The application was configured using " + Utils.baseObjectName(configurator));\r
60       suggestions.add("The configurator reports following configuration was used: ");\r
61       suggestions.addAll(configurator.document());\r
62       break;\r
63     case PAZPAR2_UNEXPECTED_RESPONSE:\r
64       suggestions.add("Unexpected response code from Pazpar2. " + nl\r
65           + "Please check the PAZPAR2_URL configuration and verify "\r
66           + "that a pazpar2 service is running at the given address." + nl);\r
67       break;\r
68     case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:\r
69       suggestions.add("The service definition file could not be loaded.");\r
70       suggestions.add("Please check the configuration and verify that the file exists");\r
71       suggestions.add("The configurator reports following configuration was used: ");\r
72       suggestions.addAll(configurator.document());    \r
73       break;\r
74     case REMOTE_SERVICE_DEF_NOT_FOUND:\r
75       break;\r
76     case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
77       break;\r
78     case NOT_RESOLVED:\r
79       break;\r
80     }\r
81     return suggestions;\r
82   }\r
83 }\r