1 package com.indexdata.pz2utils4jsf.errors;
\r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
\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
10 import org.apache.log4j.Logger;
\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
16 public class ErrorHelper implements Serializable {
\r
18 public enum ErrorCode {PAZPAR2_404,
\r
19 PAZPAR2_UNEXPECTED_RESPONSE,
\r
22 LOCAL_SERVICE_DEF_FILE_NOT_FOUND,
\r
23 REMOTE_SERVICE_DEF_NOT_FOUND,
\r
24 LOCAL_SETTINGS_FILE_NOT_FOUND,
\r
25 MASTERKEY_CONFIG_FILE_NOT_FOUND,
\r
26 MISSING_MANDATORY_PARAMETER,
\r
30 private static final long serialVersionUID = 2860804561068279131L;
\r
31 private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");
\r
33 private static Logger logger = Logger.getLogger(ErrorHelper.class);
\r
35 private Pz2Configurator configurator = null;
\r
37 public ErrorHelper(Pz2Configurator configurator) {
\r
38 this.configurator = configurator;
\r
41 public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) {
\r
42 if (appError.hasPazpar2Error()) {
\r
43 Pazpar2Error pz2err = appError.getPazpar2Error();
\r
44 String pz2errcode = pz2err.getCode();
\r
45 switch (pz2errcode) {
\r
47 return ErrorCode.PAZPAR2_12;
\r
49 if (pz2err.getMsg().contains("target settings from file")) {
\r
50 return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;
\r
52 return ErrorCode.PAZPAR2_ERRORS;
\r
55 return ErrorCode.PAZPAR2_ERRORS;
\r
57 } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {
\r
58 Matcher m = httpResponsePattern.matcher(appError.getMessage());
\r
60 String errorCode = m.group(1);
\r
61 if (errorCode.equals("404")) {
\r
62 return ErrorCode.PAZPAR2_404;
\r
64 return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;
\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 } else if (appError.getMessage().contains("Missing mandatory parameter")) {
\r
74 return ErrorCode.MISSING_MANDATORY_PARAMETER;
\r
76 return ErrorCode.NOT_RESOLVED;
\r
79 public ArrayList<String> getSuggestions(ErrorInterface error) {
\r
80 ArrayList<String> suggestions = new ArrayList<String>();
\r
81 ErrorCode code = getErrorCode(error);
\r
84 suggestions.add("Pazpar2 service not found (404). ");
\r
85 suggestions.add("Please check the PAZPAR2_URL configuration and verify "
\r
86 + "that a pazpar2 service is running at the given address.");
\r
87 addConfigurationDocumentation(suggestions);
\r
89 case PAZPAR2_UNEXPECTED_RESPONSE:
\r
90 suggestions.add("Unexpected response code from Pazpar2. " + nl
\r
91 + "Please check the PAZPAR2_URL configuration and verify "
\r
92 + "that a pazpar2 service is running at the given address." + nl);
\r
94 case MASTERKEY_CONFIG_FILE_NOT_FOUND:
\r
95 suggestions.add("The main configuration file that is looked up using parameters" +
\r
96 " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +
\r
97 " could not be found. Please check the web.xml parameters and the expected file system location. ");
\r
99 case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:
\r
100 suggestions.add("The service definition file could not be loaded.");
\r
101 suggestions.add("Please check the configuration and verify that the file exists");
\r
102 addConfigurationDocumentation(suggestions);
\r
104 case REMOTE_SERVICE_DEF_NOT_FOUND:
\r
106 case LOCAL_SETTINGS_FILE_NOT_FOUND:
\r
107 suggestions.add("A configuration using local target settings file was found, but " +
\r
108 " the file itself could not be found. Please check the configuration.");
\r
109 addConfigurationDocumentation(suggestions);
\r
111 case MISSING_MANDATORY_PARAMETER:
\r
112 suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" +
\r
113 " file used. Please check the property file for the parameter given in the error message ");
\r
114 addConfigurationDocumentation(suggestions);
\r
117 suggestions.add("Unforeseen error situation. No suggestions prepared.");
\r
119 case SKIP_SUGGESTIONS:
\r
122 suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");
\r
123 suggestions.add("Please check the service ID set in the configuration and compare it with the " +
\r
124 " pazpar2 (server side) configuration.");
\r
125 addConfigurationDocumentation(suggestions);
\r
127 case PAZPAR2_ERRORS:
\r
128 if (error.hasPazpar2Error()) {
\r
129 if (error.getPazpar2Error().getCode().equals("0")) {
\r
132 suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");
\r
134 logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");
\r
138 return suggestions;
\r
141 private void addConfigurationDocumentation (ArrayList<String> suggestions) {
\r
142 suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));
\r
143 suggestions.add("This configurator reports that following configuration was used: ");
\r
144 suggestions.addAll(configurator.document());
\r