From 0f4faab803c26eeebe511e9895013ecadfdd3ebe Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Wed, 13 Mar 2013 11:09:17 -0400 Subject: [PATCH] Adding errors to be handled. Organizing error handling. --- src/META-INF/resources/pz2utils/pz2watch.xhtml | 6 - .../indexdata/pz2utils4jsf/config/Pz2Config.java | 11 +- .../pz2utils4jsf/config/Pz2Configurator.java | 5 +- .../config/Pz2ConfigureByMk2Config.java | 29 ++-- .../pz2utils4jsf/config/Pz2ConfigureByWebXml.java | 29 ++-- .../errors/ConfigurationException.java | 32 ++++ .../indexdata/pz2utils4jsf/errors/ErrorHelper.java | 167 ++++++++++---------- .../indexdata/pz2utils4jsf/pazpar2/Pz2Session.java | 3 +- 8 files changed, 168 insertions(+), 114 deletions(-) create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationException.java diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index 965e01f..9cbdda2 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -38,9 +38,6 @@ - #{error.label} - - @@ -50,9 +47,6 @@ - - - diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java index fc23ae2..128525b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java @@ -13,6 +13,7 @@ import org.apache.log4j.Logger; import com.indexdata.masterkey.config.MissingMandatoryParameterException; import com.indexdata.masterkey.config.ModuleConfiguration; import com.indexdata.masterkey.config.ModuleConfigurationGetter; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.utils.Utils; @Named @SessionScoped @@ -36,11 +37,15 @@ public class Pz2Config implements ModuleConfigurationGetter, Serializable { } } - public Pz2Config (ModuleConfiguration moduleConfig) throws IOException { + public Pz2Config (ModuleConfiguration moduleConfig) throws ConfigurationException { logger.debug(Utils.objectId(this) + " being constructed with moduleConfig argument."); this.moduleConfig = moduleConfig; - for (String key : moduleConfig.getConfigMap().keySet()) { - properties.put(key, moduleConfig.getConfigParameter(key)); + try { + for (String key : moduleConfig.getConfigMap().keySet()) { + properties.put(key, moduleConfig.getConfigParameter(key)); + } + } catch (IOException e) { + throw new ConfigurationException("Could not instantiate Pazpar2 configuration: "+e.getMessage(),e); } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java index b4e8eab..9749aa1 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java @@ -1,11 +1,12 @@ package com.indexdata.pz2utils4jsf.config; -import java.io.IOException; import java.io.Serializable; import java.util.List; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; + public interface Pz2Configurator extends Serializable { - public Pz2Config getConfig() throws IOException; + public Pz2Config getConfig() throws ConfigurationException; public List document(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java index e809486..95f69fe 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java @@ -16,6 +16,7 @@ import org.apache.log4j.Logger; import com.indexdata.masterkey.config.MasterkeyConfiguration; import com.indexdata.masterkey.config.ModuleConfiguration; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.utils.Utils; import static com.indexdata.pz2utils4jsf.utils.Utils.nl; @@ -32,29 +33,37 @@ public class Pz2ConfigureByMk2Config implements Pz2Configurator { } @Override - public Pz2Config getConfig() throws IOException { + public Pz2Config getConfig() throws ConfigurationException { if (pz2config == null) { createConfig(); } return pz2config; } - private void createConfig () throws IOException { + private void createConfig () throws ConfigurationException { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); ServletContext servletContext = (ServletContext) externalContext.getContext(); - MasterkeyConfiguration mkConfigContext = - MasterkeyConfiguration.getInstance(servletContext, - "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName()); - configFilePathAndName = mkConfigContext.getConfigFileLocation().getConfigFilePath(); - ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration("pz2client"); - pz2config = new Pz2Config(moduleConfig); - logger.info(document()); + MasterkeyConfiguration mkConfigContext; + try { + mkConfigContext = MasterkeyConfiguration.getInstance(servletContext, + "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName()); + } catch (IOException e) { + throw new ConfigurationException("Pz2ConfigureByMk2Config could not configure Pazpar2 client using MasterKey configuration scheme: "+e.getMessage(),e); + } + configFilePathAndName = mkConfigContext.getConfigFileLocation().getConfigFilePath(); + try { + ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration("pz2client"); + pz2config = new Pz2Config(moduleConfig); + logger.info(document()); + } catch (IOException e) { + throw new ConfigurationException("Pz2ConfigureByMk2Config could not get configuration for module 'pz2client': "+e.getMessage(),e); + } } public List document() { List doc = new ArrayList(); - doc.add("Attempted to configure service using the file " + configFilePathAndName); + doc.add(":"+nl+"Attempted to configure service using the file " + configFilePathAndName); doc.add(nl+"-- Configured to access Pazpar2 at: " +pz2config.get("PAZPAR2_URL")); if (pz2config.get("PAZPAR2_SERVICE_XML") != null) { doc.add(nl+"-- Configured to use the service definition contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SERVICE_XML")); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java index 4845ad0..0fc81d4 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java @@ -1,6 +1,7 @@ package com.indexdata.pz2utils4jsf.config; -import java.io.IOException; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -15,6 +16,8 @@ import javax.servlet.ServletContext; import org.apache.log4j.Logger; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; + @Named @SessionScoped @Alternative public class Pz2ConfigureByWebXml implements Pz2Configurator { @@ -22,31 +25,39 @@ public class Pz2ConfigureByWebXml implements Pz2Configurator { private static final long serialVersionUID = 144390224959311772L; private static Logger logger = Logger.getLogger(Pz2ConfigureByWebXml.class); private Pz2Config pz2config = null; + private Map parameters = new HashMap(); public Pz2ConfigureByWebXml () { logger.info("Instantiating Pazpar2 service configuration by web.xml parameters"); } @Override - public Pz2Config getConfig() throws IOException { + public Pz2Config getConfig() throws ConfigurationException { if (pz2config == null) { createConfig(); } return pz2config; } - private void createConfig () throws IOException { + private void createConfig () throws ConfigurationException { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); - ServletContext servletContext = (ServletContext) externalContext.getContext(); - Map parameters = new HashMap(); + ServletContext servletContext = (ServletContext) externalContext.getContext(); parameters.put("PAZPAR2_URL", servletContext.getInitParameter("PAZPAR2_URL")); + if (parameters.get("PAZPAR2_URL")==null || parameters.get("PAZPAR2_URL").length()==0) { + throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_URL'"); + } parameters.put("PAZPAR2_SERVICE_ID", servletContext.getInitParameter("PAZPAR2_SERVICE_ID")); - pz2config = new Pz2Config(parameters); + if (parameters.get("PAZPAR2_SERVICE_ID")==null || parameters.get("PAZPAR2_SERVICE_ID").length()==0) { + throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_SERVICE_ID'"); + } + pz2config = new Pz2Config(parameters); } public List document() { - List doc = new ArrayList(); - doc.add("No documentation written yet for this configurator"); - return doc; + List doc = new ArrayList(); + doc.add("Attempted to configure service using web.xml context-parameters "); + doc.add(nl+"-- Configured to access Pazpar2 at [" +parameters.get("PAZPAR2_URL") + "]"); + doc.add(nl+"-- Configured to use the server side service definition identified by service id [" +parameters.get("PAZPAR2_SERVICE_ID") + "]"); + return doc; } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationException.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationException.java new file mode 100644 index 0000000..960933b --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationException.java @@ -0,0 +1,32 @@ +package com.indexdata.pz2utils4jsf.errors; + +public class ConfigurationException extends Exception { + + private static final long serialVersionUID = -2406313397798065423L; + + public ConfigurationException() { + // TODO Auto-generated constructor stub + } + + public ConfigurationException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public ConfigurationException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public ConfigurationException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public ConfigurationException(String message, Throwable cause, + boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java index 0fcc526..e78fb4d 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -10,21 +10,20 @@ import java.util.regex.Pattern; import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; -import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; import com.indexdata.pz2utils4jsf.utils.Utils; public class ErrorHelper implements Serializable { public enum ErrorCode {PAZPAR2_404, PAZPAR2_UNEXPECTED_RESPONSE, - PAZPAR2_12, PAZPAR2_ERRORS, LOCAL_SERVICE_DEF_FILE_NOT_FOUND, REMOTE_SERVICE_DEF_NOT_FOUND, LOCAL_SETTINGS_FILE_NOT_FOUND, MASTERKEY_CONFIG_FILE_NOT_FOUND, - MISSING_MANDATORY_PARAMETER, + MISSING_MANDATORY_PROPERTY, MISSING_MK2_CONFIG_INIT_PARAMETER, + MISSING_CONTEXT_PARAMETER, NOT_RESOLVED, SKIP_SUGGESTIONS}; @@ -40,22 +39,14 @@ public class ErrorHelper implements Serializable { } public ErrorHelper.ErrorCode getErrorCode(ErrorInterface appError) { + String errmsg = appError.getMessage(); if (appError.hasPazpar2Error()) { - Pazpar2Error pz2err = appError.getPazpar2Error(); - String pz2errcode = pz2err.getCode(); - switch (pz2errcode) { - case "12": - return ErrorCode.PAZPAR2_12; - case "0": - if (pz2err.getMsg().contains("target settings from file")) { - return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND; - } else { - return ErrorCode.PAZPAR2_ERRORS; - } - default: + if (appError.getPazpar2Error().getMsg().contains("target settings from file")) { + return ErrorCode.LOCAL_SETTINGS_FILE_NOT_FOUND; + } else { return ErrorCode.PAZPAR2_ERRORS; } - } else if (appError.getMessage().startsWith("Unexpected HTTP response")) { + } else if (errmsg.startsWith("Unexpected HTTP response")) { Matcher m = httpResponsePattern.matcher(appError.getMessage()); if (m.matches()) { String errorCode = m.group(1); @@ -65,18 +56,18 @@ public class ErrorHelper implements Serializable { return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE; } } - } else if (appError.getMessage().contains("Configuration file") & appError.getMessage().contains("properties")) { + } else if (errmsg.contains("Configuration file") & appError.getMessage().contains("properties")) { return ErrorCode.MASTERKEY_CONFIG_FILE_NOT_FOUND; - } else if (appError.getMessage().contains("Error reading service definition XML")) { + } else if (errmsg.contains("Error reading service definition XML")) { return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND; - } else if (appError.getMessage().contains("Cannot query Pazpar2 while there are configuration errors")) { + } else if (errmsg.contains("Cannot query Pazpar2 while there are configuration errors")) { return ErrorCode.SKIP_SUGGESTIONS; - } else if (appError.getMessage().contains("Missing mandatory parameter")) { - return ErrorCode.MISSING_MANDATORY_PARAMETER; - } else if (appError.getMessage().contains("Init parameter") - && appError.getMessage().contains("MASTERKEY") - && appError.getMessage().contains("missing in deployment descriptor")) { + } else if (errmsg.contains("Missing mandatory parameter")) { + return ErrorCode.MISSING_MANDATORY_PROPERTY; + } else if (errmsg.contains("Pz2ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { return ErrorCode.MISSING_MK2_CONFIG_INIT_PARAMETER; + } else if (appError.getMessage().contains("Pz2ConfigureByWebXml could not find mandatory context-param")) { + return ErrorCode.MISSING_CONTEXT_PARAMETER; } return ErrorCode.NOT_RESOLVED; } @@ -85,66 +76,76 @@ public class ErrorHelper implements Serializable { ArrayList suggestions = new ArrayList(); ErrorCode code = getErrorCode(error); switch (code) { - case PAZPAR2_404: - suggestions.add("Pazpar2 service not found (404). "); - suggestions.add("Please check the PAZPAR2_URL configuration and verify " - + "that a pazpar2 service is running at the given address."); - addConfigurationDocumentation(suggestions); - break; - case PAZPAR2_UNEXPECTED_RESPONSE: - suggestions.add("Unexpected response code from Pazpar2. " + nl - + "Please check the PAZPAR2_URL configuration and verify " - + "that a pazpar2 service is running at the given address." + nl); - break; - case MASTERKEY_CONFIG_FILE_NOT_FOUND: - suggestions.add("The main configuration file that is looked up using parameters" + - " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" + - " could not be found. Please check the web.xml parameters and the expected file system location. "); - break; - case LOCAL_SERVICE_DEF_FILE_NOT_FOUND: - suggestions.add("The service definition file could not be loaded."); - suggestions.add("Please check the configuration and verify that the file exists"); - addConfigurationDocumentation(suggestions); - break; - case REMOTE_SERVICE_DEF_NOT_FOUND: - break; - case LOCAL_SETTINGS_FILE_NOT_FOUND: - suggestions.add("A configuration using local target settings file was found, but " + - " the file itself could not be found. Please check the configuration."); - addConfigurationDocumentation(suggestions); - break; - case MISSING_MANDATORY_PARAMETER: - suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" + - " file used. Please check the property file for the parameter given in the error message "); - addConfigurationDocumentation(suggestions); - break; - case MISSING_MK2_CONFIG_INIT_PARAMETER: - suggestions.add("A mandatory init parameter was not found in the deployment descriptor (web.xml)." + - " Following init parameters must be present in web.xml when using the Masterkey (MK2) configuration scheme:" + - " MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " + - "MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'"); - break; - case NOT_RESOLVED: - suggestions.add("Unforeseen error situation. No suggestions prepared."); - break; - case SKIP_SUGGESTIONS: - break; - case PAZPAR2_12: - suggestions.add("The Pazpar2 service does not have a service definition with the requested ID "); - suggestions.add("Please check the service ID set in the configuration and compare it with the " + - " pazpar2 (server side) configuration."); - addConfigurationDocumentation(suggestions); - break; - case PAZPAR2_ERRORS: - if (error.hasPazpar2Error()) { - if (error.getPazpar2Error().getCode().equals("0")) { - + case MISSING_MK2_CONFIG_INIT_PARAMETER: + suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." + + " Following init parameters must be present when using the MasterKey configuration scheme (Pz2ConfigureByMk2Config):" + + " MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " + + " MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'"); + break; + case MISSING_CONTEXT_PARAMETER: + suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." + + " Following init parameters must be present when using Pz2ConfigureByWebXml:" + + " PAZPAR2_URL, PAZPAR2_SERVICE_ID"); + break; + case MISSING_MANDATORY_PROPERTY: + suggestions.add("A mandatory configuration parameter was not found in the MK2 config properties" + + " file used. Please check the property file for the parameter given in the error message "); + addConfigurationDocumentation(suggestions); + break; + case MASTERKEY_CONFIG_FILE_NOT_FOUND: + suggestions.add("The main configuration file that is looked up using parameters" + + " in web.xml (MASTERKEY_ROOT_CONFIG_DIR,MASTERKEY_COMPONENT_CONFIG_DIR,MASTERKEY_CONFIG_FILE_NAME)" + + " could not be found. Please check the web.xml parameters and the expected file system location. "); + break; + case LOCAL_SERVICE_DEF_FILE_NOT_FOUND: + suggestions.add("The service definition file could not be loaded."); + suggestions.add("Please check the configuration and verify that the file exists"); + addConfigurationDocumentation(suggestions); + break; + case REMOTE_SERVICE_DEF_NOT_FOUND: + break; + case LOCAL_SETTINGS_FILE_NOT_FOUND: + suggestions.add("A configuration using local target settings file was found, but " + + " the file itself could not be found. Please check the configuration."); + addConfigurationDocumentation(suggestions); + break; + case PAZPAR2_404: + suggestions.add("Pazpar2 service not found (404). "); + suggestions.add("Please check the PAZPAR2_URL configuration and verify " + + "that a pazpar2 service is running at the given address."); + addConfigurationDocumentation(suggestions); + break; + case PAZPAR2_UNEXPECTED_RESPONSE: + suggestions.add("Unexpected response code from Pazpar2. " + nl + + "Please check the PAZPAR2_URL configuration and verify " + + "that a pazpar2 service is running at the given address." + nl); + break; + case PAZPAR2_ERRORS: + if (error.hasPazpar2Error()) { + String pz2code = error.getPazpar2Error().getCode(); + switch (pz2code) { + case "3": + suggestions.add("Query terms not supported."); + break; + case "12": + suggestions.add("The Pazpar2 server does not have a service defined by the requested ID "); + suggestions.add("Please check the service ID set in the configuration and compare it with the " + + " configuration on the Pazpar2 server-side."); + addConfigurationDocumentation(suggestions); + break; + default: + suggestions.add("Pazpar2 error: " + error.getPazpar2Error().getMsg() + " (Pazpar2 # "+error.getPazpar2Error().getCode()+")"); + } + break; + } else { + logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected."); } - suggestions.add("Encountered Pazpar2 error: " + error.getPazpar2Error().getMsg() + " ("+error.getPazpar2Error().getCode()+")"); - } else { - logger.error("Programming problem. An application error was categorized as a Papzar2 error yet does not have Pazpar2 error information as expected."); - } - break; + break; + case SKIP_SUGGESTIONS: + break; + case NOT_RESOLVED: + suggestions.add("Sorry, no troubleshooting suggestions were written for this error scenario just yet."); + break; } return suggestions; } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 1bac38b..267f1b4 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -18,6 +18,7 @@ import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric; import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ConfigurationError; @@ -64,7 +65,7 @@ public class Pz2Session implements Pz2Interface { } catch (ProxyErrorException pe) { logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper)); - } catch (IOException io) { + } catch (ConfigurationException io) { logger.error("Could not configure Pazpar2 client: " + io.getMessage()); configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper)); } -- 1.7.10.4