Sanitary
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / Pz2Session.java
index 2940307..17f8545 100644 (file)
@@ -1,5 +1,6 @@
 package com.indexdata.pz2utils4jsf.pazpar2;\r
 \r
+import java.io.ByteArrayOutputStream;\r
 import java.io.IOException;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
@@ -12,17 +13,20 @@ import javax.inject.Named;
 \r
 import org.apache.log4j.Logger;\r
 \r
+import com.indexdata.masterkey.pazpar2.client.ClientCommand;\r
 import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
 import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
 import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;\r
+import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
 import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
 import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
-import com.indexdata.pz2utils4jsf.errors.ApplicationError;\r
-import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
 import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\r
-import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse;\r
@@ -48,7 +52,7 @@ public class Pz2Session implements Pz2Interface {
   private TargetFilter targetFilter = null;  \r
   private ResultsPager pager = null; \r
   private ErrorHelper errorHelper = null;\r
-  private List<ApplicationError> configurationErrors = null;\r
+  private List<ErrorInterface> configurationErrors = null;\r
   \r
   public Pz2Session () {\r
     logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]");      \r
@@ -56,7 +60,7 @@ public class Pz2Session implements Pz2Interface {
     \r
   public void init(Pz2Configurator pz2conf) {\r
     if (client==null) {\r
-      configurationErrors = new ArrayList<ApplicationError>();\r
+      configurationErrors = new ArrayList<ErrorInterface>();\r
       errorHelper = new ErrorHelper(pz2conf);\r
       logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf));\r
       try {\r
@@ -64,17 +68,21 @@ public class Pz2Session implements Pz2Interface {
       } catch (ProxyErrorException pe) {\r
         logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
         configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper));\r
-      } catch (IOException io) {\r
+      } catch (ConfigurationException io) {\r
         logger.error("Could not configure Pazpar2 client: " + io.getMessage());\r
         configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper));\r
       }\r
-      try {\r
-        client = new Pazpar2ClientGeneric(cfg);     \r
-      } catch (ProxyErrorException pe) {\r
-        logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage());\r
-        configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper));                \r
-      } \r
-      logger.info("Got " + configurationErrors.size() + " configuration errors");\r
+      if (cfg != null) {\r
+        try {\r
+          client = new Pazpar2ClientGeneric(cfg);  \r
+        } catch (ProxyErrorException pe) {\r
+          logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage());\r
+          configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper));                \r
+        } \r
+        if (hasConfigurationErrors()) {\r
+          logger.info("Found " + configurationErrors.size() + " configuration errors");\r
+        }\r
+      }\r
       resetDataObjects();\r
     } else {\r
       logger.warn("Attempt to configure session but it already has a configured client");\r
@@ -142,11 +150,7 @@ public class Pz2Session implements Pz2Interface {
         return "0";\r
       }\r
     } else {\r
-      configurationErrors.add(\r
-          new ConfigurationError("Querying while errors",\r
-                                 "App halted",\r
-                                 "Cannot query Pazpar2 while there are configuration errors.",\r
-                                 errorHelper));\r
+      logger.error("Did not attempt to execute query since there are configuration errors.");\r
       return "0";\r
     }\r
     \r
@@ -293,8 +297,7 @@ public class Pz2Session implements Pz2Interface {
     return queryStates.getCurrentStateKey();\r
   }\r
       \r
-  public void setCurrentStateKey(String key) {\r
-    logger.debug("************** request to set state key to: [" + key + "]");    \r
+  public void setCurrentStateKey(String key) {       \r
     queryStates.setCurrentStateKey(key);\r
   }\r
   \r
@@ -323,8 +326,7 @@ public class Pz2Session implements Pz2Interface {
     return hasConfigurationErrors() || hasCommandErrors();\r
   }\r
 \r
-  public List<ApplicationError> getConfigurationErrors() {\r
-    logger.info("Returning " + configurationErrors.size() + " configuration errors");\r
+  public List<ErrorInterface> getConfigurationErrors() {    \r
     return configurationErrors;\r
   }\r
   \r
@@ -333,7 +335,7 @@ public class Pz2Session implements Pz2Interface {
    * error found for an arbitrary command, if any, otherwise\r
    * an empty dummy error. \r
    */    \r
-  public ApplicationError getCommandError() {\r
+  public ErrorInterface getCommandError() {\r
     CommandError error = new CommandError();    \r
     if (dataObjects.get("search").hasApplicationError()) {\r
       error = dataObjects.get("search").getApplicationError();                        \r
@@ -475,5 +477,4 @@ public class Pz2Session implements Pz2Interface {
     dataObjects.put("search", new SearchResponse());\r
   }\r
   \r
-\r
 }\r