1 package com.indexdata.pz2utils4jsf.errors;
\r
3 import java.io.Serializable;
\r
4 import java.util.ArrayList;
\r
5 import java.util.regex.Matcher;
\r
6 import java.util.regex.Pattern;
\r
8 import org.apache.log4j.Logger;
\r
10 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;
\r
11 import com.indexdata.pz2utils4jsf.utils.Utils;
\r
12 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
\r
14 public class ErrorHelper implements Serializable {
\r
16 public enum ErrorCode {PAZPAR2_404,
\r
17 PAZPAR2_UNEXPECTED_RESPONSE,
\r
18 LOCAL_SERVICE_DEF_FILE_NOT_FOUND,
\r
19 REMOTE_SERVICE_DEF_NOT_FOUND,
\r
20 LOCAL_SETTINGS_FILE_NOT_FOUND,
\r
23 private static final long serialVersionUID = 2860804561068279131L;
\r
24 private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*");
\r
25 private static Pattern missingLocalServiceDefFile = Pattern.compile(".*Error reading service definition XML.*");
\r
26 private static Logger logger = Logger.getLogger(ErrorHelper.class);
\r
28 private Pz2Configurator configurator = null;
\r
30 public ErrorHelper(Pz2Configurator configurator) {
\r
31 this.configurator = configurator;
\r
34 public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) {
\r
35 if (error.getMessage().startsWith("Unexpected HTTP response")) {
\r
36 Matcher m = httpResponsePattern.matcher(error.getMessage());
\r
38 String errorCode = m.group(1);
\r
39 if (errorCode.equals("404")) {
\r
40 return ErrorCode.PAZPAR2_404;
\r
42 return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE;
\r
45 } else if (error.getMessage().contains("Error reading service definition XML")) {
\r
46 return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND;
\r
48 return ErrorCode.NOT_RESOLVED;
\r
51 public ArrayList<String> getSuggestions(ApplicationError error) {
\r
52 ArrayList<String> suggestions = new ArrayList<String>();
\r
53 ErrorCode code = getErrorCode(error);
\r
56 suggestions.add("Pazpar2 service not found (404). ");
\r
57 suggestions.add("Please check the PAZPAR2_URL configuration and verify "
\r
58 + "that a pazpar2 service is running at the given address.");
\r
59 suggestions.add("The application was configured using " + Utils.baseObjectName(configurator));
\r
60 suggestions.add("The configurator reports following configuration was used: ");
\r
61 suggestions.addAll(configurator.document());
\r
63 case PAZPAR2_UNEXPECTED_RESPONSE:
\r
64 suggestions.add("Unexpected response code from Pazpar2. " + nl
\r
65 + "Please check the PAZPAR2_URL configuration and verify "
\r
66 + "that a pazpar2 service is running at the given address." + nl);
\r
68 case LOCAL_SERVICE_DEF_FILE_NOT_FOUND:
\r
69 suggestions.add("The service definition file could not be loaded.");
\r
70 suggestions.add("Please check the configuration and verify that the file exists");
\r
71 suggestions.add("The configurator reports following configuration was used: ");
\r
72 suggestions.addAll(configurator.document());
\r
74 case REMOTE_SERVICE_DEF_NOT_FOUND:
\r
76 case LOCAL_SETTINGS_FILE_NOT_FOUND:
\r