Catches init doc upload errors
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / ServiceProxyClient.java
index 1f68715..79d6153 100644 (file)
@@ -48,7 +48,7 @@ public class ServiceProxyClient implements SearchClient {
   public static final String MODULENAME = "proxyclient";\r
   \r
   public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
-  private String selectedServiceUrl = "";\r
+  private String serviceUrl = "";\r
   \r
   private List<String> initDocPaths = null;\r
   private Configuration config = null;\r
@@ -70,7 +70,7 @@ public class ServiceProxyClient implements SearchClient {
     logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
     try {\r
       config = configReader.getConfiguration(this);      \r
-      selectedServiceUrl = config.get("SERVICE_PROXY_URL");\r
+      serviceUrl = config.get("SERVICE_PROXY_URL");\r
       this.initDocPaths = config.getMultiProperty(SP_INIT_DOC_PATHS,",");\r
       checkAuth = new AuthCommand(null);\r
       checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
@@ -147,7 +147,7 @@ public class ServiceProxyClient implements SearchClient {
    */\r
   private ClientCommandResponse send(Pazpar2Command command) {\r
     ClientCommandResponse commandResponse = null;\r
-    String url = selectedServiceUrl + "?" + command.getEncodedQueryString(); \r
+    String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
     logger.info("Sending request "+url);    \r
     HttpGet httpget = new HttpGet(url);     \r
     byte[] response = null;\r
@@ -213,7 +213,7 @@ public class ServiceProxyClient implements SearchClient {
     logger.debug("Cloning Pz2Client");\r
     ServiceProxyClient clone = new ServiceProxyClient();\r
     clone.client = this.client;\r
-    clone.selectedServiceUrl = this.selectedServiceUrl;\r
+    clone.serviceUrl = this.serviceUrl;\r
     clone.initDocPaths = this.initDocPaths;\r
     return clone;\r
   }\r
@@ -231,13 +231,13 @@ public class ServiceProxyClient implements SearchClient {
   @Override\r
   public List<String> documentConfiguration () {\r
     List<String> doc = new ArrayList<String>();\r
-    doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (selectedServiceUrl.length()>0 ? selectedServiceUrl : "[not defined yet]"));\r
+    doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (serviceUrl.length()>0 ? serviceUrl : "[not defined yet]"));\r
     return null;\r
   }\r
   \r
   public ClientCommandResponse postInitDoc (String filePath) throws IOException {\r
     logger.info("Looking to post the file in : [" + filePath +"]");\r
-    HttpPost post = new HttpPost(selectedServiceUrl+"?command=init&includeDebug=yes");\r
+    HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
     File initDoc = new File(filePath);\r
     logger.info("Posting to SP: ");\r
     if (logger.isDebugEnabled()) {\r
@@ -260,16 +260,33 @@ public class ServiceProxyClient implements SearchClient {
     return initDocPaths;\r
   }\r
   \r
-  public ClientCommandResponse postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException {\r
-    HttpPost post = new HttpPost(selectedServiceUrl+"?command=init" + (includeDebug? "&includeDebug=yes" : ""));\r
+  public HttpResponseWrapper postInitDoc(byte[] initDoc, boolean includeDebug) {\r
+    HttpPost post = new HttpPost(serviceUrl+"?command=init" + (includeDebug? "&includeDebug=yes" : ""));\r
     post.setEntity(new ByteArrayEntity(initDoc));\r
-    byte[] response = client.execute(post, handler);\r
-    logger.debug("Response on POST was: " + new String(response,"UTF-8"));    \r
-    return new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());    \r
+    ClientCommandResponse commandResponse = null;\r
+    byte[] response;\r
+    try {\r
+      response = client.execute(post, handler);\r
+      if (handler.getStatusCode()==200) {\r
+        commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
+      } else {\r
+        logger.error("Service Proxy status code: " + handler.getStatusCode());\r
+        commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.insertPazpar2ErrorXml("init", "Service Proxy error occurred", new String(response,"UTF-8")),"text/xml");                               \r
+      }\r
+    } catch (ClientProtocolException e) {\r
+      logger.error(e.getMessage());\r
+      e.printStackTrace();\r
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", "client protocol exception", e.getMessage()),"text/xml");      \r
+    } catch (IOException e) {\r
+      logger.error(e.getMessage());\r
+      e.printStackTrace();\r
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", "IO", e.getMessage()),"text/xml");      \r
+    }\r
+    return commandResponse;    \r
   }\r
   \r
   public void setServiceUrl (String url) {    \r
-    selectedServiceUrl = url;\r
+    serviceUrl = url;\r
   }\r
           \r
   public Configuration getConfiguration () {\r
@@ -278,12 +295,12 @@ public class ServiceProxyClient implements SearchClient {
 \r
   @Override\r
   public String getServiceUrl() {    \r
-    return selectedServiceUrl;\r
+    return serviceUrl;\r
   }\r
 \r
   @Override\r
   public boolean hasServiceUrl() {\r
-    return selectedServiceUrl != null && selectedServiceUrl.length()>0;\r
+    return serviceUrl != null && serviceUrl.length()>0;\r
   }\r
   \r
 }\r