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.config.Pz2ConfigureByMk2Config;
\r
14 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error;
\r
15 import com.indexdata.pz2utils4jsf.utils.Utils;
\r
17 public class ErrorHelper implements Serializable {
\r
19 public enum ErrorCode {PAZPAR2_404,
\r
20 PAZPAR2_UNEXPECTED_RESPONSE,
\r
23 LOCAL_SERVICE_DEF_FILE_NOT_FOUND,
\r
24 REMOTE_SERVICE_DEF_NOT_FOUND,
\r
25 LOCAL_SETTINGS_FILE_NOT_FOUND,
\r
26 MASTERKEY_CONFIG_FILE_NOT_FOUND,
\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(ApplicationError 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
74 return ErrorCode.NOT_RESOLVED;
\r
77 public ArrayList<String> getSuggestions(ApplicationError error) {
\r
78 ArrayList<String> suggestions = new ArrayList<String>();
\r
79 ErrorCode code = getErrorCode(error);
\r
82 suggestions.add("Pazpar2 service not found (404). ");
\r
83 suggestions.add("Please check the PAZPAR2_URL configuration and verify "
\r
84 + "that a pazpar2 service is running at the given address.");
\r
85 addConfigurationDocumentation(suggestions);
\r
87 case PAZPAR2_UNEXPECTED_RESPONSE:
\r
88 suggestions.add("Unexpected response code from Pazpar2. " + nl
\r
89 + "Please check the PAZPAR2_URL configuration and verify "
\r
90 + "that a pazpar2 service is running at the given address." + nl);
\r
92 case MASTERKEY_CONFIG_FILE_NOT_FOUND:
\r
93 suggestions.add("The main configuration file that is looked up using parameters" +
\r
94 " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" +
\r
95 " could not be found. Please check the web.xml parameters and the expected file system location. ");
\r
97 case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:
\r
98 suggestions.add("The service definition file could not be loaded.");
\r
99 suggestions.add("Please check the configuration and verify that the file exists");
\r
100 addConfigurationDocumentation(suggestions);
\r
102 case REMOTE_SERVICE_DEF_NOT_FOUND:
\r
104 case LOCAL_SETTINGS_FILE_NOT_FOUND:
\r
105 suggestions.add("A configuration using local target settings file was found, but " +
\r
106 " the file itself could not be found. Please check the configuration.");
\r
107 addConfigurationDocumentation(suggestions);
\r
110 suggestions.add("Unforeseen error situation. No suggestions prepared.");
\r
112 case SKIP_SUGGESTIONS:
\r
115 suggestions.add("The Pazpar2 service does not have a service definition with the requested ID ");
\r
116 suggestions.add("Please check the service ID set in the configuration and compare it with the " +
\r
117 " pazpar2 (server side) configuration.");
\r
118 addConfigurationDocumentation(suggestions);
\r
120 case PAZPAR2_ERRORS:
\r
121 if (error.hasPazpar2Error()) {
\r
122 if (error.getPazpar2Error().getCode().equals("0")) {
\r
125 suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");
\r
127 logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected.");
\r
131 return suggestions;
\r
134 private void addConfigurationDocumentation (ArrayList<String> suggestions) {
\r
135 suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator));
\r
136 suggestions.add("This configurator reports that following configuration was used: ");
\r
137 suggestions.addAll(configurator.document());
\r