import com.indexdata.mkjsf.errors.ErrorCentral;\r
import com.indexdata.mkjsf.errors.ErrorHelper;\r
import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
+import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;\r
import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
pzreq.getRecord().removeParametersInState(); \r
pzreq.getShow().setParameterInState(new CommandParameter("start","=",0)); \r
logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));\r
- doCommand("search"); \r
+ doCommand("search"); \r
+ }\r
+ \r
+ public String doRecord() {\r
+ ResponseDataObject responseObject = doCommand("record");\r
+ if (pzreq.getRecord().hasParameterValue("offset") ||\r
+ pzreq.getRecord().hasParameterValue("checksum")) {\r
+ RecordResponse recordResponse = new RecordResponse();\r
+ recordResponse.setType("record");\r
+ recordResponse.setXml(responseObject.getXml());\r
+ recordResponse.setAttribute("activeclients", "0");\r
+ pzresp.put("record", recordResponse);\r
+ }\r
+ return pzresp.getRecord().getActiveClients(); \r
}\r
\r
/**\r
public String update (String commands) {\r
logger.info("Request to update: " + commands);\r
try {\r
- if (! validateUpdateRequest(commands)) {\r
+ if (!validateUpdateRequest(commands)) {\r
return "0";\r
+ } else if (commands.equals("search")) {\r
+ doSearch();\r
+ return "";\r
+ } else if (commands.equals("record")) {\r
+ return doRecord();\r
+ } else if (pzresp.getSearch().isNew()) {\r
+ logger.info("New search. Marking it old, then returning 'new' to trigger another round-trip.");\r
+ pzresp.getSearch().setIsNew(false);\r
+ return "new";\r
} else {\r
handleQueryStateChanges(commands);\r
- if (!commands.equals("search") && pzresp.getSearch().hasApplicationError()) {\r
+ if (pzresp.getSearch().hasApplicationError()) {\r
logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error.");\r
return "0";\r
} else {\r
logger.debug("Processing request for " + commands); \r
+ \r
List<CommandThread> threadList = new ArrayList<CommandThread>();\r
StringTokenizer tokens = new StringTokenizer(commands,",");\r
while (tokens.hasMoreElements()) { \r
if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
pzresp.put(commandName, responseObject);\r
} else {\r
- if (commandName.equals("record") && \r
- (pzreq.getRecord().hasParameterValue("offset") ||\r
- pzreq.getRecord().hasParameterValue("checksum"))) {\r
- RecordResponse recordResponse = new RecordResponse();\r
- recordResponse.setType("record");\r
- recordResponse.setXml(responseObject.getXml());\r
- recordResponse.setAttribute("activeclients", "0");\r
- pzresp.put(commandName, recordResponse);\r
- }\r
+ logger.info("Unknown doc type [" + responseObject.getType() + "]. Was not cached.");\r
}\r
}\r
- if (commands.equals("record")) {\r
- return pzresp.getRecord().getActiveClients();\r
- } else {\r
- return pzresp.getActiveClients();\r
- }\r
+ return pzresp.getActiveClients();\r
}\r
} \r
} catch (ClassCastException cce) {\r
return "";\r
} else {\r
pzreq.getRecord().setId(recId);\r
- return doCommand("record");\r
+ doCommand("record");\r
+ return pzresp.getRecord().getActiveClients();\r
}\r
}\r
\r
logger.debug("Found pending record ID change. Doing record before updating " + commands);\r
stateMgr.hasPendingStateChange("record",false);\r
if (pzreq.getCommand("record").hasParameterValue("id")) {\r
- update("record");\r
+ doRecord();\r
} else { \r
pzresp.put("record", new RecordResponse());\r
}\r
}\r
}\r
\r
- protected String doCommand(String commandName) { \r
- logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
- return update(commandName);\r
+ /**\r
+ * Validates the request then executes the command and parses the response.\r
+ * If the parsed response is of a known type it will be cached in 'pzresp'\r
+ * \r
+ * @param commandName The command to be executed\r
+ * @return An XML response parsed to form a response data object\r
+ */\r
+ protected ResponseDataObject doCommand(String commandName) {\r
+ ResponseDataObject responseObject = null; \r
+ if (validateUpdateRequest(commandName)) {\r
+ logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
+ Pazpar2Command command = pzreq.getCommand(commandName);\r
+ long start = System.currentTimeMillis();\r
+ HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
+ long end = System.currentTimeMillis();\r
+ logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+ responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
+ responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
+ if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+ pzresp.put(commandName, responseObject);\r
+ } \r
+ }\r
+ return responseObject;\r
}\r
- \r
+ \r
@Override\r
public void stateUpdated(String commandName) {\r
logger.debug("State change reported for [" + commandName + "]");\r