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
28 private static final long serialVersionUID = 2860804561068279131L;
\r
29 private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");
\r
31 private static Logger logger = Logger.getLogger(ErrorHelper.class);
\r
33 private Pz2Configurator configurator = null;
\r
35 public ErrorHelper(Pz2Configurator configurator) {
\r
36 this.configurator = configurator;
\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
45 return ErrorCode.PAZPAR2_12;
\r
47 if (pz2err.getMsg().contains("target settings from file")) {
\r
48 return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND;
\r
50 return ErrorCode.PAZPAR2_ERRORS;
\r
53 return ErrorCode.PAZPAR2_ERRORS;
\r
55 } else if (appError.getMessage().startsWith("Unexpected HTTP response")) {
\r
56 Matcher m = httpResponsePattern.matcher(appError.getMessage());
\r
58 String errorCode = m.group(1);
\r
59 if (errorCode.equals("404")) {
\r
60 return ErrorCode.PAZPAR2_404;
\r
62 return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;
\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
70 return ErrorCode.NOT_RESOLVED;
\r
73 public ArrayList<String> getSuggestions(ApplicationError error) {
\r
74 ArrayList<String> suggestions = new ArrayList<String>();
\r
75 ErrorCode code = getErrorCode(error);
\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
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
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
93 case REMOTE_SERVICE_DEF_NOT_FOUND:
\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
101 suggestions.add("Unforeseen error situation. No suggestions prepared.");
\r
103 case SKIP_SUGGESTIONS:
\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
111 case PAZPAR2_ERRORS:
\r
112 if (error.hasPazpar2Error()) {
\r
113 if (error.getPazpar2Error().getCode().equals("0")) {
\r
116 suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")");
\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
122 return suggestions;
\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