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
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
}\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
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
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
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
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
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
} \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
}\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
public InitDocUpload getInitDocUpload () {\r
return initDocUpload;\r
}\r
-\r
- \r
- \r
- \r
}\r
* @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
* 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
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
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
* @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
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