1 package com.indexdata.mkjsf.pazpar2.data;
\r
3 import static com.indexdata.mkjsf.utils.Utils.nl;
\r
5 import java.util.ArrayList;
\r
6 import java.util.List;
\r
7 import java.util.regex.Pattern;
\r
9 import com.indexdata.mkjsf.errors.ErrorHelper;
\r
10 import com.indexdata.mkjsf.errors.ErrorHelper.ErrorCode;
\r
11 import com.indexdata.mkjsf.errors.ErrorInterface;
\r
12 import com.indexdata.utils.XmlUtils;
\r
15 * Holds an error encountered during the execution of a command.
\r
17 * An error can be received by a command thread as an exception message
\r
18 * or as an error XML. In both cases the error (string or xml) will be embedded
\r
19 * in a new 'applicationerror' element which in turn will be embedded in a
\r
20 * command XML (i.e. a 'search' or a 'show' response XML)
\r
22 * The command response XML is subsequently parsed by ResponseParser,
\r
23 * which will then create the CommandError object.
\r
25 * @author Niels Erik
\r
28 public class CommandError extends ResponseDataObject implements ErrorInterface {
\r
30 private static final long serialVersionUID = 8878776025779714122L;
\r
31 private static Pattern xmlDeclaration = Pattern.compile("<\\?xml.*\\?>");
\r
32 private ErrorCode applicationErrorCode;
\r
33 private ErrorHelper errorHelper = null;
\r
36 public CommandError () {
\r
39 public String getLabel() {
\r
40 return getOneElementValue("commandname");
\r
43 public String getMessage() {
\r
44 if (isServiceError()) {
\r
45 return getServiceError().getMsg();
\r
47 return getOneElementValue("errormessage");
\r
51 public String getException () {
\r
52 return getOneElementValue("exception");
\r
55 public List<String> getSuggestions() {
\r
56 if (errorHelper!=null) {
\r
57 return errorHelper.getSuggestions(this);
\r
59 List<String> nohelper = new ArrayList<String>();
\r
60 nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");
\r
66 * Creates an XML string error message, embedded in an XML string document named by the command
\r
67 * This is the XML that ResponseParser will turn into a CommandError object.
\r
68 * @param commandName
\r
70 * @param errorMessage
\r
73 public static String createErrorXml (String commandName, String statusCode, String exception, String errorMessage, String response) {
\r
74 StringBuilder errorXml = new StringBuilder("");
\r
75 errorXml.append("<" + commandName + ">"+nl);
\r
76 errorXml.append(" <applicationerror>"+nl);
\r
77 errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);
\r
78 errorXml.append(" <status>FAIL</status>"+nl);
\r
79 errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);
\r
80 errorXml.append(" <exception>" + (exception != null ? XmlUtils.escape(exception) : "") + "</exception>"+nl);
\r
81 errorXml.append(" <errormessage>" + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + "</errormessage>"+nl);
\r
82 errorXml.append(" <response>" + response + "</response>" + nl);
\r
83 errorXml.append(" </applicationerror>"+nl);
\r
84 errorXml.append("</" + commandName + ">"+nl);
\r
85 return errorXml.toString();
\r
89 * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of
\r
90 * a command response document (like 'search' or 'show').
\r
91 * This is the XML that ResponseParser will turn into a CommandError object.
\r
94 * @param commandName The name of the command during which's execution the error was encountered
\r
95 * @param exception The (possibly loosely defined) name of the exception that was thrown
\r
96 * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or
\r
97 * by the Pazpar2 client itself.
\r
100 public static String insertErrorXml (String commandName, String statusCode, String exception, String pazpar2ErrorXml) {
\r
101 StringBuilder errorXml = new StringBuilder("");
\r
102 errorXml.append("<" + commandName + ">"+nl);
\r
103 errorXml.append(" <applicationerror>"+nl);
\r
104 errorXml.append(" <commandname>" + commandName + "</commandname>"+nl);
\r
105 errorXml.append(" <statuscode>" + statusCode + "</statuscode>"+nl);
\r
106 errorXml.append(" <exception>" + XmlUtils.escape(exception) + "</exception>"+nl);
\r
107 errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl);
\r
108 errorXml.append(" </applicationerror>"+nl);
\r
109 errorXml.append("</" + commandName + ">"+nl);
\r
110 return errorXml.toString();
\r
115 * Sets the object that should be used to analyze the error
\r
118 public void setErrorHelper (ErrorHelper errorHelper) {
\r
119 this.errorHelper = errorHelper;
\r
123 public void setApplicationErrorCode(ErrorCode code) {
\r
124 this.applicationErrorCode = code;
\r
128 public ErrorCode getApplicationErrorCode() {
\r
129 return applicationErrorCode;
\r
132 public boolean isServiceError () {
\r
133 ServiceError pz2err = (ServiceError) getOneElement("error");
\r
134 return (pz2err != null);
\r
137 public ServiceError getServiceError() {
\r
138 return (ServiceError) getOneElement("error");
\r
141 public boolean isServiceProxyError () {
\r
142 return (isServiceError() && getServiceError().isServiceProxyError());
\r
145 public boolean isPazpar2Error () {
\r
146 return (isServiceError() && getServiceError().isPazpar2Error());
\r