More error handling/reporting. Normalizing auth resp
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Sun, 12 May 2013 17:49:16 +0000 (13:49 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Sun, 12 May 2013 17:49:16 +0000 (13:49 -0400)
Parses HTML reponses where XML were expected, for instance HTML pages
returned when domain is not found or HTML error pages from servlet
container when service-proxy app not found.

Also normalizes auth responses, renaming the document element 'response'
to 'auth' before handing the XML string to the parser (other responses
are named by their corresponding command, which is what the parser
expects). Thie original auth response is still preserved, though.

src/main/java/com/indexdata/mkjsf/pazpar2/ClientCommandResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java

index 6e0d9e3..e33ce1e 100644 (file)
@@ -11,6 +11,7 @@ public class ClientCommandResponse implements HttpResponseWrapper {
   private String contentType;\r
   private byte[] content = null;\r
   private String contentString = null;\r
+  private byte[] bytesForParsing = null;\r
   \r
   public ClientCommandResponse(Pazpar2HttpResponse pz2response, ByteArrayOutputStream content) {    \r
     this.content = content.toByteArray();\r
@@ -58,7 +59,30 @@ public class ClientCommandResponse implements HttpResponseWrapper {
   public byte[] getBytes() {\r
     return content;\r
   }\r
+  \r
+  public void setResponseToParse(String parseString) {    \r
+    try {\r
+      this.bytesForParsing = parseString.getBytes("UTF-8");\r
+    } catch (UnsupportedEncodingException e) {      \r
+      e.printStackTrace();\r
+    }\r
+  }\r
 \r
+  public byte[] getResponseToParse() {\r
+    if (bytesForParsing != null) {\r
+      return bytesForParsing;\r
+    } else if (content != null) {\r
+      return content;\r
+    } else {\r
+      try {\r
+        return contentString.getBytes("UTF-8");\r
+      } catch (UnsupportedEncodingException e) {\r
+        e.printStackTrace();\r
+        return null;\r
+      }\r
+    }\r
+  }\r
+  \r
   @Override\r
   public boolean isBinary() {    \r
     return !contentType.contains("xml");\r
index 902a497..4faef7b 100644 (file)
@@ -161,9 +161,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
           }\r
           for (CommandThread thread : threadList) {\r
              String commandName = thread.getCommand().getCommandName();\r
-             HttpResponseWrapper response = thread.getCommandResponse();\r
+             ClientCommandResponse response = (ClientCommandResponse) thread.getCommandResponse();\r
              responseLogger.debug("Response was: " + response.getResponseString());\r
-             ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response.getResponseString());\r
+             ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response);\r
              if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
                pzresp.put(commandName, responseObject);\r
              } else {\r
index cbdcf6b..fc919f3 100644 (file)
@@ -101,22 +101,22 @@ public class Pz2Client implements SearchClient {
         commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
       } else if (pz2HttpResponse.getStatusCode()==417) {\r
         logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\r
-        commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml");                       \r
+        commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()) ,"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 ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml");\r
+        commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "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 ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml");      \r
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "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 ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml");\r
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "io", e.getMessage()),"text/xml");\r
     }\r
     long end = System.currentTimeMillis();      \r
     logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
index 9493e49..51afab2 100644 (file)
@@ -100,15 +100,30 @@ public class ServiceProxyClient implements SearchClient {
     byte[] response = null;\r
     try {\r
       response = client.execute(httpget, handler);\r
-      if (handler.getStatusCode()==200) {\r
+      if (handler.getStatusCode()==200 && handler.getContentType().contains("xml")) {\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(command.getCommandName(), "Service Proxy error occurred", new String(response,"UTF-8")),"text/xml");                       \r
+        String errorXml = "";\r
+        if (handler.getContentType().contains("xml")) {\r
+          errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8"));          \r
+        } else {\r
+          if (handler.getContentType().contains("html")) {\r
+            String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>","");\r
+            if (htmlStrippedOfTags.toLowerCase().contains("domain")) {\r
+              errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got HTML with word 'domain' in, probably domain not found.", htmlStrippedOfTags);              \r
+            } else {\r
+              errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got HTML", htmlStrippedOfTags);              \r
+            }\r
+          } else {\r
+            errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Error: Expected XML response from Service Proxy, got: "+handler.getContentType(), new String(response,"UTF-8"));\r
+          }\r
+          commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType());\r
+        }\r
       }       \r
     } catch (Exception e) {\r
       e.printStackTrace();\r
-      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : "")),"text/xml");\r
+      commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : "")),handler.getContentType());\r
     }\r
     return commandResponse; \r
   }\r
@@ -218,16 +233,16 @@ public class ServiceProxyClient implements SearchClient {
         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
+        commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.insertErrorXml("init", String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), 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
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "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
+      commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO", e.getMessage()),"text/xml");      \r
     }\r
     return commandResponse;    \r
   }\r
index 0ee2825..dae3a17 100644 (file)
@@ -69,7 +69,9 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
                               new CommandParameter("username","=",user.getName()),\r
                               new CommandParameter("password","=",user.getPassword()));\r
     ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);\r
-    AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString()));\r
+    String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth");\r
+    commandResponse.setResponseToParse(renamedResponse);\r
+    AuthResponse responseObject = (AuthResponse) ResponseParser.getParser().getDataObject(commandResponse);\r
     if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
       pzresp.put(auth.getCommandName(), responseObject);\r
     }\r
@@ -93,8 +95,10 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
       pzresp.resetAllSessionData();\r
       AuthCommand auth = pzreq.getSp().getAuth(); \r
       auth.setParameterInState(new CommandParameter("action","=","ipAuth"));\r
-      ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);\r
-      AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString()));\r
+      ClientCommandResponse commandResponse = pz2.getSpClient().send(auth);      \r
+      String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth");\r
+      commandResponse.setResponseToParse(renamedResponse);\r
+      ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(commandResponse);\r
       if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
         pzresp.put(auth.getCommandName(), responseObject);\r
       }\r
@@ -107,6 +111,12 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
       }      \r
     }\r
   }\r
+  \r
+  private String renameResponseElement(String responseString, String newName) {\r
+    responseString = responseString.replace("<response>", "<" + newName + ">");\r
+    responseString = responseString.replace("</response>", "</" + newName + ">");\r
+    return responseString;\r
+  }\r
     \r
   public String getInitDocPath () {\r
     return pz2.getSpClient().getConfiguration().get("INIT_DOC_PATH");\r
@@ -156,8 +166,8 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
   }\r
   \r
   public void submitInitDoc () throws IOException {\r
-    HttpResponseWrapper response =  initDocUpload.submit();\r
-    ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response.getResponseString());\r
+    ClientCommandResponse response =  (ClientCommandResponse) initDocUpload.submit();\r
+    ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response);\r
     logger.info("Putting init response to : " + Utils.objectId(pzresp));\r
     pzresp.put("init", responseObject);\r
   }\r
@@ -175,8 +185,4 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
   public InitDocUpload getInitDocUpload () {\r
     return initDocUpload;\r
   }\r
-\r
-  \r
-  \r
-  \r
 }\r
index e3fd7b7..9fbe0d0 100644 (file)
@@ -70,11 +70,12 @@ public class CommandError extends ResponseDataObject implements ErrorInterface {
    * @param errorMessage\r
    * @return\r
    */\r
-  public static String createErrorXml (String commandName, String exceptionName, String errorMessage) {\r
+  public static String createErrorXml (String commandName, String statusCode, String exceptionName, String errorMessage) {\r
     StringBuilder errorXml = new StringBuilder("");\r
     errorXml.append("<" + commandName + ">"+nl);\r
     errorXml.append(" <applicationerror>"+nl);\r
     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
+    errorXml.append("  <statuscode>" + statusCode + "</statuscode>"+nl);\r
     errorXml.append("  <exception>" + (exceptionName != null ? XmlUtils.escape(exceptionName) : "") + "</exception>"+nl);    \r
     errorXml.append("  <errormessage>" + (errorMessage != null  ? XmlUtils.escape(errorMessage) : "") + "</errormessage>"+nl);    \r
     errorXml.append(" </applicationerror>"+nl);\r
@@ -94,11 +95,12 @@ public class CommandError extends ResponseDataObject implements ErrorInterface {
    *                        by the Pazpar2 client itself. \r
    * @return\r
    */\r
-  public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) {\r
+  public static String insertErrorXml (String commandName, String statusCode, String exceptionName, String pazpar2ErrorXml) {\r
     StringBuilder errorXml = new StringBuilder("");\r
     errorXml.append("<" + commandName + ">"+nl);\r
     errorXml.append(" <applicationerror>"+nl);\r
     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
+    errorXml.append("  <statuscode>" + statusCode + "</statuscode>"+nl);\r
     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
     errorXml.append(xmlDeclaration.matcher(pazpar2ErrorXml).replaceAll("")+nl);    \r
     errorXml.append(" </applicationerror>"+nl);\r
index 29a5fb7..852ebb1 100644 (file)
@@ -18,6 +18,8 @@ import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;\r
 import org.xml.sax.helpers.DefaultHandler;\r
 \r
+import com.indexdata.mkjsf.pazpar2.ClientCommandResponse;\r
+\r
 public class ResponseParser extends DefaultHandler {\r
 \r
   private XMLReader xmlReader = null;\r
@@ -28,7 +30,7 @@ public class ResponseParser extends DefaultHandler {
   private static Logger logger = Logger.getLogger(ResponseParser.class);\r
 \r
   public static List<String> docTypes = Arrays.asList(  "bytarget","termlist","show","stat","record","search","init",\r
-                                        /* SP extras */ "response" );                                        \r
+                                        /* SP extras */ "auth" );                                        \r
   \r
   public ResponseParser() {\r
     try {\r
@@ -61,10 +63,10 @@ public class ResponseParser extends DefaultHandler {
    * @param response XML response string from Pazpar2\r
    * @return Response data object\r
    */\r
-  public ResponseDataObject getDataObject (String response) {\r
-    this.xml = response;\r
+  public ResponseDataObject getDataObject (ClientCommandResponse response) {\r
+    this.xml = response.getResponseString();\r
     try {      \r
-      xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))));\r
+      xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getResponseToParse())));\r
     } catch (UnsupportedEncodingException e) {\r
       e.printStackTrace(); \r
     } catch (IOException e) {\r
@@ -116,7 +118,7 @@ public class ResponseParser extends DefaultHandler {
       currentElement = new CommandError();\r
     } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) {\r
       currentElement = new Pazpar2Error(); \r
-    } else if (localName.equals("response")) {  // Note, document element not named 'auth'\r
+    } else if (localName.equals("auth")) {  \r
       currentElement = new AuthResponse();\r
     } else {\r
       currentElement = new ResponseDataObject();\r