Rearranges pz2/sp client logic to isolate error messaging
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2Client.java
index 78627d9..a68e67f 100644 (file)
@@ -24,6 +24,7 @@ import com.indexdata.mkjsf.config.Configuration;
 import com.indexdata.mkjsf.config.ConfigurationReader;\r
 import com.indexdata.mkjsf.errors.ConfigurationException;\r
 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.mkjsf.pazpar2.data.CommandError;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
 public class Pz2Client implements SearchClient {\r
@@ -82,16 +83,43 @@ public class Pz2Client implements SearchClient {
   \r
   @Override\r
   public void setSearchCommand(Pazpar2Command command) {\r
-    ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+    ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString());\r
     client.setSearchCommand(clientCommand);    \r
   }\r
 \r
   @Override\r
-  public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
-       throws Pazpar2ErrorException, IOException {\r
-    ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
-    Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
-    return new Pz2CommandResponse(pz2HttpResponse,baos);\r
+  public CommandResponse executeCommand(Pazpar2Command command) {\r
+    Pz2CommandResponse commandResponse = null;\r
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
+    ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString());\r
+    Pazpar2HttpResponse pz2HttpResponse = null;\r
+    long start = System.currentTimeMillis();\r
+    try {\r
+      pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
+      if (pz2HttpResponse.getStatusCode()==200) {\r
+        commandResponse = new Pz2CommandResponse(pz2HttpResponse,baos);\r
+      } else if (pz2HttpResponse.getStatusCode()==417) {\r
+        logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\r
+        commandResponse = new Pz2CommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml");                       \r
+      } else {\r
+        String resp = baos.toString("UTF-8");\r
+        logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp);\r
+        commandResponse = new Pz2CommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml");\r
+        throw new Pazpar2ErrorException(resp,pz2HttpResponse.getStatusCode(),resp,null);\r
+      }       \r
+    } catch (IOException e) {\r
+      logger.error(e.getMessage());\r
+      e.printStackTrace();\r
+      commandResponse = new Pz2CommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml");      \r
+    } catch (Pazpar2ErrorException e) {\r
+      logger.error(e.getMessage());\r
+      e.printStackTrace();\r
+      logger.error("Creating error XML");\r
+      commandResponse = new Pz2CommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml");\r
+    }\r
+    long end = System.currentTimeMillis();      \r
+    logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+    return commandResponse;\r
   }\r
 \r
   public Pz2Client cloneMe() {\r