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