From: Niels Erik G. Nielsen Date: Sat, 9 Mar 2013 04:20:18 +0000 (-0500) Subject: Embeds pz2 error XML, if any, in the app error XML X-Git-Tag: v0.0.7~215 X-Git-Url: http://sru.miketaylor.org.uk/?a=commitdiff_plain;h=b8de1214df604ebb0485b0d4b22d28733c963933;p=mkjsf-moved-to-github.git Embeds pz2 error XML, if any, in the app error XML --- diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index de46cc7..965e01f 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -36,24 +36,27 @@ - + - #{error.label} + #{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 dccda3b..ac05c0a 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java @@ -52,16 +52,16 @@ public class Pz2ConfigureByMk2Config implements Pz2Configurator { public List document() { List doc = new ArrayList(); - doc.add("Set to access Pazpar2 at: " +pz2config.get("PAZPAR2_URL")); + doc.add("-- Set to access Pazpar2 at: " +pz2config.get("PAZPAR2_URL")); if (pz2config.get("PAZPAR2_SERVICE_XML") != null) { - doc.add("Set to use the service definition contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SERVICE_XML")); + doc.add("-- App set to use the service definition contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SERVICE_XML")); if (pz2config.get("PAZPAR2_SETTINGS_XML") != null) { - doc.add("Set to use the target settings contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SETTINGS_XML")); + doc.add("-- App set to use the target settings contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SETTINGS_XML")); } else { - doc.add("Set to use the server side target settings as defined in the service definition."); + doc.add("-- App set to use the server side target settings as defined in the service definition."); } } else if (pz2config.get("PAZPAR2_SERVICE_ID") != null) { - doc.add("Set to use the server side service definition identified by service id "+pz2config.get("PAZPAR2_SERVICE_ID")); + doc.add("-- App set to use the server side service definition identified by service id \""+pz2config.get("PAZPAR2_SERVICE_ID") + "\""); } else { doc.add("Error: Did not find service ID nor service definition XML file to set up pazpar2 service."); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java index 450df7a..7ececce 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ApplicationError.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.List; import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; public interface ApplicationError extends Serializable { @@ -15,5 +16,7 @@ public interface ApplicationError extends Serializable { public ErrorCode getApplicationErrorCode(); public List getSuggestions(); public void setErrorHelper(ErrorHelper helper); + public boolean hasPazpar2Error(); + public Pazpar2Error getPazpar2Error(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java index c27f3a3..2cafa40 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ConfigurationError.java @@ -3,6 +3,7 @@ package com.indexdata.pz2utils4jsf.errors; import java.util.List; import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Error; public class ConfigurationError implements ApplicationError { @@ -55,6 +56,12 @@ public class ConfigurationError implements ApplicationError { return applicationErrorCode; } - + public boolean hasPazpar2Error () { + return false; + } + public Pazpar2Error getPazpar2Error() { + return null; + } + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java index 9aa8fec..f74407d 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -1,5 +1,7 @@ package com.indexdata.pz2utils4jsf.errors; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + import java.io.Serializable; import java.util.ArrayList; import java.util.regex.Matcher; @@ -8,21 +10,24 @@ 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; -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; 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, - NOT_RESOLVED}; + NOT_RESOLVED, + SKIP_SUGGESTIONS}; 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; @@ -31,9 +36,24 @@ public class ErrorHelper implements Serializable { this.configurator = configurator; } - public ErrorHelper.ErrorCode getErrorCode(ApplicationError error) { - if (error.getMessage().startsWith("Unexpected HTTP response")) { - Matcher m = httpResponsePattern.matcher(error.getMessage()); + public ErrorHelper.ErrorCode getErrorCode(ApplicationError appError) { + 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: + return ErrorCode.PAZPAR2_ERRORS; + } + } else if (appError.getMessage().startsWith("Unexpected HTTP response")) { + Matcher m = httpResponsePattern.matcher(appError.getMessage()); if (m.matches()) { String errorCode = m.group(1); if (errorCode.equals("404")) { @@ -42,8 +62,10 @@ public class ErrorHelper implements Serializable { return ErrorCode.PAZPAR2_UNEXPECTED_RESPONSE; } } - } else if (error.getMessage().contains("Error reading service definition XML")) { - return ErrorCode.LOCAL_SERVICE_DEF_FILE_NOT_FOUND; + } else if (appError.getMessage().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")) { + return ErrorCode.SKIP_SUGGESTIONS; } return ErrorCode.NOT_RESOLVED; } @@ -56,9 +78,7 @@ public class ErrorHelper implements Serializable { 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()); + addConfigurationDocumentation(suggestions); break; case PAZPAR2_UNEXPECTED_RESPONSE: suggestions.add("Unexpected response code from Pazpar2. " + nl @@ -68,16 +88,43 @@ public class ErrorHelper implements Serializable { 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()); + 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 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")) { + + } + 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; } return suggestions; } + + private void addConfigurationDocumentation (ArrayList suggestions) { + suggestions.add("The application was configured using the configurator " + Utils.baseObjectName(configurator)); + suggestions.add("This configurator reports that following configuration was used: "); + suggestions.addAll(configurator.document()); + } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java index ea52988..95dcf1e 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -44,8 +44,12 @@ public class CommandThread extends Thread { Pazpar2HttpResponse httpResponse = client.executeCommand(clientCommand, baos); if (httpResponse.getStatusCode()==200) { response.append(baos.toString("UTF-8")); + } else if (httpResponse.getStatusCode()==417) { + logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8")); + response.append(CommandError.insertPazpar2ErrorXml(command.getName(), "Expectation failed (417)", baos.toString("UTF-8"))); } else { String resp = baos.toString("UTF-8"); + logger.error("Pazpar2 status code was " + httpResponse.getStatusCode() + ": " + resp); throw new Pazpar2ErrorException(resp,httpResponse.getStatusCode(),resp,null); } long end = System.currentTimeMillis(); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index cae075f..2940307 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -72,7 +72,7 @@ public class Pz2Session implements Pz2Interface { 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)); + configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); } logger.info("Got " + configurationErrors.size() + " configuration errors"); resetDataObjects(); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java index 2421f38..016f68e 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java @@ -33,7 +33,11 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro } public String getMessage() { - return getOneElementValue("errormessage"); + if (hasPazpar2Error()) { + return getPazpar2Error().getMsg(); + } else { + return getOneElementValue("errormessage"); + } } public String getException () { @@ -69,6 +73,19 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro return errorXml.toString(); } + public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) { + StringBuilder errorXml = new StringBuilder(""); + errorXml.append("<" + commandName + ">"+nl); + errorXml.append(" "+nl); + errorXml.append(" " + commandName + ""+nl); + errorXml.append(" " + XmlUtils.escape(exceptionName) + ""+nl); + errorXml.append(pazpar2ErrorXml+nl); + errorXml.append(" "+nl); + errorXml.append(""+nl); + return errorXml.toString(); + + } + public void setErrorHelper (ErrorHelper errorHelper) { this.errorHelper = errorHelper; } @@ -82,5 +99,14 @@ public class CommandError extends Pazpar2ResponseData implements ApplicationErro public ErrorCode getApplicationErrorCode() { return applicationErrorCode; } + + public boolean hasPazpar2Error () { + return ( getOneElement("error") != null); + } + + public Pazpar2Error getPazpar2Error() { + return (Pazpar2Error) getOneElement("error"); + } + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2Error.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2Error.java new file mode 100644 index 0000000..9c2a725 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2Error.java @@ -0,0 +1,14 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +public class Pazpar2Error extends Pazpar2ResponseData { + + private static final long serialVersionUID = -7060267782024414318L; + + public String getCode() { + return getAttribute("code"); + } + + public String getMsg() { + return getAttribute("msg"); + } +} 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 d6ee2bb..c4f7b99 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java @@ -102,7 +102,8 @@ public class Pazpar2ResponseData implements Serializable { return (CommandError) getOneElement("applicationerror"); } - - - + public boolean hasPazpar2Error() { + return hasApplicationError() && getApplicationError().hasPazpar2Error(); + } + } 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 2e0ef39..e69b397 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java @@ -124,6 +124,8 @@ public class Pazpar2ResponseParser extends DefaultHandler { currentElement = new SearchResponse(); } else if (localName.equals("applicationerror")) { currentElement = new CommandError(); + } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) { + currentElement = new Pazpar2Error(); } else { currentElement = new Pazpar2ResponseData(); } @@ -151,6 +153,4 @@ public class Pazpar2ResponseParser extends DefaultHandler { dataElements.pop(); } } - - }