+ public HttpResponseWrapper executeCommand(Pazpar2Command command) {\r
+ ClientCommandResponse 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 && pz2HttpResponse.getContentType().contains("xml")) {\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+ } else if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("octet-stream")) {\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+ logger.info("Content type: " + commandResponse.getContentType() + ". isBinary?: " + commandResponse.isBinary());\r
+ } else if (pz2HttpResponse.getStatusCode()==417) {\r
+ logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\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 if (pz2HttpResponse.getContentType().contains("html")) {\r
+ String resp = baos.toString("UTF-8");\r
+ logger.error("HTML response where XML was expected. Status code was " + pz2HttpResponse.getStatusCode() + ": " + resp);\r
+ String htmlStrippedOfTags = resp.replaceAll("\\<[^>]*>","");\r
+ String errorXml = "";\r
+ if (htmlStrippedOfTags.toLowerCase().contains("domain")) {\r
+ errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2", "Error: Expected XML response from Pazpar2 but got HTML. The HTML contains the word domain suggesting that the Pazpar2 address was not found.", htmlStrippedOfTags);\r
+ } else { \r
+ errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2: " + pz2HttpResponse.getContentType(),"Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags);\r
+ } \r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,pz2HttpResponse.getContentType()); \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.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml"); \r
+ } \r
+ } catch (IOException e) {\r
+ logger.error(e.getMessage());\r
+ e.printStackTrace();\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "IO exception", 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(0,CommandError.createErrorXml(command.getCommandName(), "", "Pazpar2Error", e.getMessage(),""),"text/xml");\r
+ }\r
+ long end = System.currentTimeMillis(); \r
+ logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+ return commandResponse;\r