Work in progress on error detect,report,troubleshoot
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / errors / ErrorHelper.java
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java
new file mode 100644 (file)
index 0000000..9aa8fec
--- /dev/null
@@ -0,0 +1,83 @@
+package com.indexdata.pz2utils4jsf.errors;\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.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
+public class ErrorHelper implements Serializable {\r
+\r
+  public enum ErrorCode {PAZPAR2_404, \r
+                         PAZPAR2_UNEXPECTED_RESPONSE,\r
+                         LOCAL_SERVICE_DEF_FILE_NOT_FOUND,\r
+                         REMOTE_SERVICE_DEF_NOT_FOUND,\r
+                         LOCAL_SETTINGS_FILE_NOT_FOUND,\r
+                         NOT_RESOLVED};\r
+\r
+  private static final long serialVersionUID = 2860804561068279131L;\r
+  private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
+  private static Pattern missingLocalServiceDefFile = Pattern.compile(".*Error reading service definition XML.*");\r
+  private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
+  \r
+  private Pz2Configurator configurator = null;\r
+  \r
+  public ErrorHelper(Pz2Configurator configurator) {\r
+    this.configurator = configurator;\r
+  }\r
+  \r
+  public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) {    \r
+    if (error.getMessage().startsWith("Unexpected HTTP response")) {\r
+      Matcher m = httpResponsePattern.matcher(error.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 (error.getMessage().contains("Error reading service definition XML")) {\r
+      return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;\r
+    }\r
+    return ErrorCode.NOT_RESOLVED;\r
+  }\r
+    \r
+  public ArrayList<String> getSuggestions(ApplicationError error) {\r
+    ArrayList<String> suggestions = new ArrayList<String>();\r
+    ErrorCode code = getErrorCode(error);\r
+    switch (code) {\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
+      suggestions.add("The application was configured using " + Utils.baseObjectName(configurator));\r
+      suggestions.add("The configurator reports following configuration was used: ");\r
+      suggestions.addAll(configurator.document());\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 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
+      suggestions.add("The configurator reports following configuration was used: ");\r
+      suggestions.addAll(configurator.document());    \r
+      break;\r
+    case REMOTE_SERVICE_DEF_NOT_FOUND:\r
+      break;\r
+    case LOCAL_SETTINGS_FILE_NOT_FOUND:\r
+      break;\r
+    case NOT_RESOLVED:\r
+      break;\r
+    }\r
+    return suggestions;\r
+  }\r
+}\r