From: Niels Erik G. Nielsen Date: Fri, 8 Mar 2013 22:38:11 +0000 (-0500) Subject: Work in progress on error detect,report,troubleshoot X-Git-Tag: v0.0.7~216 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=2aa1c4adceb245c1f59f62e9adb77fda9f02291a;p=mkjsf-moved-to-github.git Work in progress on error detect,report,troubleshoot Adds configuration errors (had command errors) Resolves more errors Adds more suggestions --- diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index dfa628d..de46cc7 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -36,12 +36,29 @@ -

- - - #{suggestion} - - + + + #{error.label} + + + + + + + + #{suggestion} + + + + + +

+ + + #{suggestion} + + +
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java index f10aa1e..dccda3b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java @@ -18,8 +18,6 @@ import com.indexdata.masterkey.config.MasterkeyConfiguration; import com.indexdata.masterkey.config.ModuleConfiguration; import com.indexdata.pz2utils4jsf.utils.Utils; -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; - @Named @SessionScoped @Alternative public class Pz2ConfigureByMk2Config implements Pz2Configurator { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java new file mode 100644 index 0000000..450df7a --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java @@ -0,0 +1,19 @@ +package com.indexdata.pz2utils4jsf.errors; + +import java.io.Serializable; +import java.util.List; + +import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; + + +public interface ApplicationError extends Serializable { + + public String getLabel(); + public String getMessage(); + public String getException(); + public void setApplicationErrorCode(ErrorCode code); + public ErrorCode getApplicationErrorCode(); + public List getSuggestions(); + public void setErrorHelper(ErrorHelper helper); + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java new file mode 100644 index 0000000..c27f3a3 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java @@ -0,0 +1,60 @@ +package com.indexdata.pz2utils4jsf.errors; + +import java.util.List; + +import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; + + +public class ConfigurationError implements ApplicationError { + + private static final long serialVersionUID = -6599667223782130838L; + private String label; + private String message; + private String exception; + private ErrorHelper helper; + private ErrorCode applicationErrorCode; + + public ConfigurationError(String label, String exception, String message, ErrorHelper helper) { + this.label = label; + this.message = message; + this.helper = helper; + this.exception = exception; + } + + public List getSuggestions() { + return helper.getSuggestions(this); + } + + @Override + public String getLabel() { + return label; + } + + @Override + public String getMessage() { + return message; + } + + @Override + public String getException() { + return exception; + } + + @Override + public void setErrorHelper (ErrorHelper helper) { + this.helper = helper; + } + + @Override + public void setApplicationErrorCode(ErrorCode code) { + this.applicationErrorCode = code; + } + + @Override + public ErrorCode getApplicationErrorCode() { + return applicationErrorCode; + } + + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java new file mode 100644 index 0000000..9aa8fec --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -0,0 +1,83 @@ +package com.indexdata.pz2utils4jsf.errors; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.utils.Utils; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + +public class ErrorHelper implements Serializable { + + public enum ErrorCode {PAZPAR2_404, + PAZPAR2_UNEXPECTED_RESPONSE, + LOCAL_SERVICE_DEF_FILE_NOT_FOUND, + REMOTE_SERVICE_DEF_NOT_FOUND, + LOCAL_SETTINGS_FILE_NOT_FOUND, + NOT_RESOLVED}; + + private static final long serialVersionUID = 2860804561068279131L; + private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*"); + private static Pattern missingLocalServiceDefFile = Pattern.compile(".*Error reading service definition XML.*"); + private static Logger logger = Logger.getLogger(ErrorHelper.class); + + private Pz2Configurator configurator = null; + + public ErrorHelper(Pz2Configurator configurator) { + this.configurator = configurator; + } + + public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) { + if (error.getMessage().startsWith("Unexpected HTTP response")) { + Matcher m = httpResponsePattern.matcher(error.getMessage()); + if (m.matches()) { + String errorCode = m.group(1); + if (errorCode.equals("404")) { + return ErrorCode.PAZPAR2_404; + } else { + return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE; + } + } + } else if (error.getMessage().contains("Error reading service definition XML")) { + return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND; + } + return ErrorCode.NOT_RESOLVED; + } + + public ArrayList getSuggestions(ApplicationError error) { + 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."); + suggestions.add("The application was configured using " + Utils.baseObjectName(configurator)); + suggestions.add("The configurator reports following configuration was used: "); + suggestions.addAll(configurator.document()); + 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 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"); + suggestions.add("The configurator reports following configuration was used: "); + suggestions.addAll(configurator.document()); + break; + case REMOTE_SERVICE_DEF_NOT_FOUND: + break; + case LOCAL_SETTINGS_FILE_NOT_FOUND: + break; + case NOT_RESOLVED: + break; + } + return suggestions; + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ApplicationTroubleshooter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ApplicationTroubleshooter.java deleted file mode 100644 index a30bc54..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ApplicationTroubleshooter.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2; - -import java.util.ArrayList; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; -import com.indexdata.pz2utils4jsf.utils.Utils; -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; - -public class ApplicationTroubleshooter { - - private static Pattern httpResponsePattern = Pattern.compile("Unexpected HTTP response code \\(([0-9]*)\\).*"); - private static Logger logger = Logger.getLogger(ApplicationTroubleshooter.class); - - private Pz2Configurator configurator = null; - - public ApplicationTroubleshooter(Pz2Configurator configurator) { - this.configurator = configurator; - } - - public ArrayList getSuggestions(String commandName, String errorMessage) { - ArrayList suggestions = new ArrayList(); - if (errorMessage.startsWith("Unexpected HTTP response")) { - Matcher m = httpResponsePattern.matcher(errorMessage); - if (m.matches()) { - String errorCode = m.group(1); - if (errorCode.equals("404")) { - suggestions.add("Pazpar2 service not found (response code 404). "); - suggestions.add("Please check the PAZPAR2_URL configuration and verify " + - "that a pazpar2 service is running at the given address."); - suggestions.add("The application was configured using " + Utils.baseObjectName(configurator)); - suggestions.add("The configurator reports following configuration was used: "); - suggestions.addAll(configurator.document()); - } else { - suggestions.add("Response code was " + errorCode + ". " + nl + - "Please check the PAZPAR2_URL configuration and verify " + - "that a pazpar2 service is running at the given address." + nl); - } - } else { - logger.warn("Found message but no pattern match"); - } - } - if (errorMessage == null || errorMessage.length()==0) { - logger.debug("No error message found, no suggestions made."); - } else { - logger.info("No suggestions yet for message " + errorMessage); - } - return suggestions; - } -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java index 50ec2b6..ea52988 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -7,8 +7,9 @@ import org.apache.log4j.Logger; import com.indexdata.masterkey.pazpar2.client.ClientCommand; import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; +import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; -import com.indexdata.pz2utils4jsf.pazpar2.data.ApplicationError; +import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; public class CommandThread extends Thread { @@ -16,7 +17,7 @@ public class CommandThread extends Thread { Pazpar2Command command; Pazpar2Client client; private ByteArrayOutputStream baos = new ByteArrayOutputStream(); - private StringBuilder response = new StringBuilder(""); + private StringBuilder response = new StringBuilder(""); public CommandThread (Pazpar2Command command, Pazpar2Client client) { this.command = command; @@ -40,16 +41,24 @@ public class CommandThread extends Thread { } try { long start = System.currentTimeMillis(); - client.executeCommand(clientCommand, baos); - response.append(baos.toString("UTF-8")); + Pazpar2HttpResponse httpResponse = client.executeCommand(clientCommand, baos); + if (httpResponse.getStatusCode()==200) { + response.append(baos.toString("UTF-8")); + } else { + String resp = baos.toString("UTF-8"); + throw new Pazpar2ErrorException(resp,httpResponse.getStatusCode(),resp,null); + } long end = System.currentTimeMillis(); logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." ); } catch (IOException e) { - response.append(ApplicationError.createErrorXml(command.getName(), "io", e.getMessage())); + response.append(CommandError.createErrorXml(command.getName(), "io", e.getMessage())); logger.error(response.toString()); } catch (Pazpar2ErrorException e) { - response.append(ApplicationError.createErrorXml(command.getName(), "pazpar2error", e.getMessage())); + response.append(CommandError.createErrorXml(command.getName(), "pazpar2error", e.getMessage())); logger.error(response.toString()); + } catch (Exception e) { + response.append(CommandError.createErrorXml(command.getName(), "general", e.getMessage())); + logger.error(response.toString()); } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index abab426..dcf75a7 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -12,7 +12,7 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.Pz2Configurator; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.pazpar2.data.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ApplicationError; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; @@ -278,9 +278,22 @@ public class Pz2Bean implements Pz2Interface, Serializable { return pz2.hasErrors(); } - public ApplicationError getOneError() { - return pz2.getOneError(); + public ApplicationError getCommandError() { + return pz2.getCommandError(); } + public List getConfigurationErrors () { + return pz2.getConfigurationErrors(); + } + + @Override + public boolean hasCommandErrors() { + return pz2.hasCommandErrors(); + } + + @Override + public boolean hasConfigurationErrors() { + return pz2.hasConfigurationErrors(); + } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java index d7fb64c..3de5b68 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java @@ -4,8 +4,7 @@ import java.io.Serializable; import java.util.List; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; -import com.indexdata.pz2utils4jsf.pazpar2.data.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ApplicationError; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; @@ -300,8 +299,38 @@ public interface Pz2Interface extends Serializable { */ public void setCurrentStateKey(String key); + /** + * @return true if any errors encountered so far + */ public boolean hasErrors(); - - public ApplicationError getOneError(); + + /** + * + * @return true if errors encountered during execution of commands + */ + public boolean hasCommandErrors(); + + /** + * + * @return true if errors encountered when configuring the service + */ + public boolean hasConfigurationErrors(); + + /** + * Returns one (of possibly multiple) errors encountered during execution of commands + * Will prefer to show the search errors - if any - as the search command is usually + * executed first. + * + * @return + */ + public ApplicationError getCommandError(); + + /** + * Returns all errors encountered during configuration of the application, in particular + * the Pazpar2 client. + * + * @return + */ + public List getConfigurationErrors(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 74980bc..cae075f 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -12,10 +12,16 @@ import javax.inject.Named; import org.apache.log4j.Logger; +import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; +import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration; +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.pazpar2.data.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; +import com.indexdata.pz2utils4jsf.errors.ConfigurationError; +import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser; @@ -37,11 +43,12 @@ public class Pz2Session implements Pz2Interface { private QueryStates queryStates = new QueryStates(); private static final long serialVersionUID = 3947514708343320514L; - private com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration cfg = null; - private com.indexdata.masterkey.pazpar2.client.Pazpar2Client client = null; + private Pazpar2ClientConfiguration cfg = null; + private Pazpar2Client client = null; private TargetFilter targetFilter = null; private ResultsPager pager = null; - private ApplicationTroubleshooter errorHelper = null; + private ErrorHelper errorHelper = null; + private List configurationErrors = null; public Pz2Session () { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); @@ -49,17 +56,26 @@ public class Pz2Session implements Pz2Interface { public void init(Pz2Configurator pz2conf) { if (client==null) { + configurationErrors = new ArrayList(); + errorHelper = new ErrorHelper(pz2conf); logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf)); try { - cfg = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig()); - client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg); - errorHelper = new ApplicationTroubleshooter(pz2conf); - resetDataObjects(); - } catch (ProxyErrorException e) { - e.printStackTrace(); - } catch (IOException ioe) { - ioe.printStackTrace(); + cfg = new Pazpar2ClientConfiguration(pz2conf.getConfig()); + } 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) { + logger.error("Could not configure Pazpar2 client: " + io.getMessage()); + configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper)); } + try { + client = new Pazpar2ClientGeneric(cfg); + } catch (ProxyErrorException pe) { + logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage()); + configurationErrors.add(new ConfigurationError("Pz2Client","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); + } + logger.info("Got " + configurationErrors.size() + " configuration errors"); + resetDataObjects(); } else { logger.warn("Attempt to configure session but it already has a configured client"); } @@ -95,33 +111,42 @@ public class Pz2Session implements Pz2Interface { * @return Number of activeclients at the time of the 'show' command */ public String update (String commands) { - if (hasQuery()) { - handleQueryStateChanges(commands); - logger.debug("Processing request for " + commands); - List threadList = new ArrayList(); - StringTokenizer tokens = new StringTokenizer(commands,","); - while (tokens.hasMoreElements()) { - threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); - } - for (CommandThread thread : threadList) { - thread.start(); - } - for (CommandThread thread : threadList) { - try { - thread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); + if (! hasConfigurationErrors()) { + if (hasQuery()) { + handleQueryStateChanges(commands); + logger.debug("Processing request for " + commands); + List threadList = new ArrayList(); + StringTokenizer tokens = new StringTokenizer(commands,","); + while (tokens.hasMoreElements()) { + threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); } + for (CommandThread thread : threadList) { + thread.start(); + } + for (CommandThread thread : threadList) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + for (CommandThread thread : threadList) { + String commandName = thread.getCommand().getName(); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse()); + dataObjects.put(commandName, responseObject); + } + return getActiveClients(); + } else { + logger.debug("Skipped requests for " + commands + " as there's not yet a query."); + resetDataObjects(); + return "0"; } - for (CommandThread thread : threadList) { - String commandName = thread.getCommand().getName(); - Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse()); - dataObjects.put(commandName, responseObject); - } - return getActiveClients(); } else { - logger.debug("Skipped requests for " + commands + " as there's not yet a query."); - resetDataObjects(); + configurationErrors.add( + new ConfigurationError("Querying while errors", + "App halted", + "Cannot query Pazpar2 while there are configuration errors.", + errorHelper)); return "0"; } @@ -273,10 +298,11 @@ public class Pz2Session implements Pz2Interface { queryStates.setCurrentStateKey(key); } - /** - * Returns true if application error found in any response data objects - */ - public boolean hasErrors () { + public boolean hasConfigurationErrors () { + return (configurationErrors.size()>0); + } + + public boolean hasCommandErrors () { if (dataObjects.get("search").hasApplicationError()) { logger.info("Error detected in search"); return true; @@ -287,17 +313,28 @@ public class Pz2Session implements Pz2Interface { return true; } } - return false; + return false; + } + + /** + * Returns true if application error found in any response data objects + */ + public boolean hasErrors () { + return hasConfigurationErrors() || hasCommandErrors(); } + public List getConfigurationErrors() { + logger.info("Returning " + configurationErrors.size() + " configuration errors"); + return configurationErrors; + } /** * Returns a search command error, if any, otherwise the first * error found for an arbitrary command, if any, otherwise * an empty dummy error. */ - public ApplicationError getOneError() { - ApplicationError error = new ApplicationError(); + public ApplicationError getCommandError() { + CommandError error = new CommandError(); if (dataObjects.get("search").hasApplicationError()) { error = dataObjects.get("search").getApplicationError(); } else { @@ -308,7 +345,7 @@ public class Pz2Session implements Pz2Interface { } } } - error.setTroubleshooter(errorHelper); + error.setErrorHelper(errorHelper); return error; } @@ -339,7 +376,7 @@ public class Pz2Session implements Pz2Interface { return pager; } - protected ApplicationTroubleshooter getTroubleshooter() { + protected ErrorHelper getTroubleshooter() { return errorHelper; } @@ -421,10 +458,10 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { + private String doCommand(String commandName) { Pazpar2Command command = getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); - return update(commandName); + return update(commandName); } private void resetDataObjects() { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ApplicationError.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ApplicationError.java deleted file mode 100644 index 5e95712..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ApplicationError.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2.data; - -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; - -import java.util.ArrayList; -import java.util.List; - -import com.indexdata.pz2utils4jsf.pazpar2.ApplicationTroubleshooter; -import com.indexdata.utils.XmlUtils; - -public class ApplicationError extends Pazpar2ResponseData { - - private static final long serialVersionUID = 8878776025779714122L; - private ApplicationTroubleshooter errorHelper = null; - - - public ApplicationError () { - } - - public String getCommandName() { - return getOneElementValue("commandname"); - } - - public String getErrorMessage() { - return getOneElementValue("errormessage"); - } - - public String getException () { - return getOneElementValue("exception"); - } - - public List getSuggestions() { - if (errorHelper!=null) { - return errorHelper.getSuggestions(getCommandName(), getErrorMessage()); - } else { - List nohelper = new ArrayList(); - nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set"); - return nohelper; - } - } - - /** - * Creates an XML string error message, embedded in an XML string document named by the command - * @param commandName - * @param exceptionName - * @param errorMessage - * @return - */ - public static String createErrorXml (String commandName, String exceptionName, String errorMessage) { - StringBuilder errorXml = new StringBuilder(""); - errorXml.append("<" + commandName + ">"+nl); - errorXml.append(" "+nl); - errorXml.append(" " + commandName + ""); - errorXml.append(" " + XmlUtils.escape(exceptionName) + ""+nl); - errorXml.append(" " + XmlUtils.escape(errorMessage) + ""+nl); - errorXml.append(" "+nl); - errorXml.append(""+nl); - return errorXml.toString(); - } - - public void setTroubleshooter (ApplicationTroubleshooter errorHelper) { - this.errorHelper = errorHelper; - } - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java new file mode 100644 index 0000000..2421f38 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java @@ -0,0 +1,86 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; +import com.indexdata.utils.XmlUtils; + +/** + * Captures errors encountered during the execution of a command. + * Is parsed by Pazpar2ResponseParser, piggybacked in a (seemingly) + * regular command respond. + * + * @author Niels Erik + * + */ +public class CommandError extends Pazpar2ResponseData implements ApplicationError { + + private static final long serialVersionUID = 8878776025779714122L; + private ErrorCode applicationErrorCode; + private ErrorHelper errorHelper = null; + + + public CommandError () { + } + + public String getLabel() { + return getOneElementValue("commandname"); + } + + public String getMessage() { + return getOneElementValue("errormessage"); + } + + public String getException () { + return getOneElementValue("exception"); + } + + public List getSuggestions() { + if (errorHelper!=null) { + return errorHelper.getSuggestions(this); + } else { + List nohelper = new ArrayList(); + nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set"); + return nohelper; + } + } + + /** + * Creates an XML string error message, embedded in an XML string document named by the command + * @param commandName + * @param exceptionName + * @param errorMessage + * @return + */ + public static String createErrorXml (String commandName, String exceptionName, String errorMessage) { + StringBuilder errorXml = new StringBuilder(""); + errorXml.append("<" + commandName + ">"+nl); + errorXml.append(" "+nl); + errorXml.append(" " + commandName + ""+nl); + errorXml.append(" " + XmlUtils.escape(exceptionName) + ""+nl); + errorXml.append(" " + XmlUtils.escape(errorMessage) + ""+nl); + errorXml.append(" "+nl); + errorXml.append(""+nl); + return errorXml.toString(); + } + + public void setErrorHelper (ErrorHelper errorHelper) { + this.errorHelper = errorHelper; + } + + @Override + public void setApplicationErrorCode(ErrorCode code) { + this.applicationErrorCode = code; + } + + @Override + public ErrorCode getApplicationErrorCode() { + return applicationErrorCode; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java index 8be2d24..d6ee2bb 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java @@ -14,7 +14,7 @@ public class Pazpar2ResponseData implements Serializable { HashMap attributes = new HashMap(); HashMap> elements = new HashMap>(); String textContent = ""; - ApplicationError error = null; + CommandError error = null; public void setType (String type) { this.type = type; @@ -98,8 +98,8 @@ public class Pazpar2ResponseData implements Serializable { return (getOneElement("applicationerror") != null); } - public ApplicationError getApplicationError() { - return (ApplicationError) getOneElement("applicationerror"); + public CommandError getApplicationError() { + return (CommandError) getOneElement("applicationerror"); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java index 40cf412..2e0ef39 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java @@ -72,17 +72,17 @@ public class Pazpar2ResponseParser extends DefaultHandler { * @return Response data object */ public Pazpar2ResponseData getDataObject (String response) { - try { + try { xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")))); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(); } return result; } @@ -123,7 +123,7 @@ public class Pazpar2ResponseParser extends DefaultHandler { } else if (localName.equals("search")) { currentElement = new SearchResponse(); } else if (localName.equals("applicationerror")) { - currentElement = new ApplicationError(); + currentElement = new CommandError(); } else { currentElement = new Pazpar2ResponseData(); }