X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FCommandThread.java;h=95dcf1e3be5ded660f1ef36879312f4c6b79b74c;hb=b8de1214df604ebb0485b0d4b22d28733c963933;hp=5764777447ff2a14dae005d56fdb49e2bc135d52;hpb=ab7a23c769e77affa25a47112d2e5e93ae23d7bc;p=mkjsf-moved-to-github.git
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java
index 5764777..95dcf1e 100644
--- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java
+++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java
@@ -5,12 +5,11 @@ import java.io.IOException;
import org.apache.log4j.Logger;
-import com.indexdata.pz2utils4jsf.pazpar2.CommandThread;
-import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;
-import com.indexdata.utils.XmlUtils;
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.CommandError;
public class CommandThread extends Thread {
@@ -18,13 +17,23 @@ 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;
this.client = client;
}
+ /**
+ * Runs the specified command using the specified Pazpar2 client
+ * Sets the Pazpar2 response as an XML response string to be retrieved by
+ * getResponse().
+ *
+ * In case of an exception, an error response is generated, the document
+ * element being the same as it would have been if successful (named after
+ * the command, that is).
+ *
+ */
public void run() {
ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());
if (command.getName().equals("search")) {
@@ -32,25 +41,39 @@ 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 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();
logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." );
} catch (IOException e) {
- logger.error("Message: " + e.getMessage());
- response = new StringBuilder("<"+command.getName()+">"+XmlUtils.escape(e.getMessage())+""+command.getName()+">");
+ response.append(CommandError.createErrorXml(command.getName(), "io", e.getMessage()));
logger.error(response.toString());
} catch (Pazpar2ErrorException e) {
- logger.error(e.getMessage());
- response = new StringBuilder("<"+command.getName()+">"+XmlUtils.escape(e.getMessage())+""+command.getName()+">");
+ 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());
}
}
+ /**
+ *
+ * @return Pazpar2 response as an XML string, possibly a generated error XML
+ */
public String getResponse () {
return response.toString();
}
-
+
public Pazpar2Command getCommand() {
return command;
}