Work on error reporting. Adds troubleshooter.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / ApplicationTroubleshooter.java
1 package com.indexdata.pz2utils4jsf.pazpar2;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.regex.Matcher;\r
5 import java.util.regex.Pattern;\r
6 \r
7 import org.apache.log4j.Logger;\r
8 \r
9 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
10 import com.indexdata.pz2utils4jsf.utils.Utils;\r
11 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
12 \r
13 public class ApplicationTroubleshooter {\r
14 \r
15   private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");\r
16   private static Logger logger = Logger.getLogger(ApplicationTroubleshooter.class);\r
17   \r
18   private Pz2Configurator configurator = null;\r
19   \r
20   public ApplicationTroubleshooter(Pz2Configurator configurator) {\r
21     this.configurator = configurator;\r
22   }\r
23     \r
24   public ArrayList<String> getSuggestions(String commandName, String errorMessage) {\r
25     ArrayList<String> suggestions = new ArrayList<String>();\r
26     if (errorMessage.startsWith("Unexpected HTTP response")) {\r
27       Matcher m = httpResponsePattern.matcher(errorMessage);\r
28       if (m.matches()) {\r
29         String errorCode = m.group(1);\r
30         if (errorCode.equals("404")) {\r
31           suggestions.add("Pazpar2 service not found (response code 404). ");\r
32           suggestions.add("Please check the PAZPAR2_URL configuration and verify " +\r
33                         "that a pazpar2 service is running at the given address."); \r
34           suggestions.add("The application was configured using " + Utils.baseObjectName(configurator));\r
35           suggestions.add("The configurator reports following configuration was used: ");\r
36           suggestions.addAll(configurator.document());          \r
37         } else {\r
38           suggestions.add("Response code was " + errorCode + ". " + nl +\r
39               "Please check the PAZPAR2_URL configuration and verify " + \r
40               "that a pazpar2 service is running at the given address." + nl);           \r
41         }        \r
42       } else {\r
43         logger.warn("Found message but no pattern match");        \r
44       }      \r
45     }\r
46     if (errorMessage == null || errorMessage.length()==0) {\r
47       logger.debug("No error message found, no suggestions made.");\r
48     } else {\r
49       logger.info("No suggestions yet for message " + errorMessage);\r
50     }\r
51     return suggestions;\r
52   }\r
53 }\r