X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FPz2Bean.java;h=5b45551a1b1aa476e1899b4e901e4768ae4c60e3;hb=d739ecb22a85d8f982add6d429e069edf7e0dde2;hp=9c43ddd3ad7d5050c74de0eb341d99b7f3b20795;hpb=980d3604794a7135590c02beb9420dc8931e703c;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java index 9c43ddd..5b45551 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java @@ -26,6 +26,7 @@ import com.indexdata.mkjsf.errors.ErrorHelper; import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands; +import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommand; import com.indexdata.mkjsf.pazpar2.data.RecordResponse; import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; import com.indexdata.mkjsf.pazpar2.data.ResponseParser; @@ -113,6 +114,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria public void doSearch() { if (errors.hasConfigurationErrors()) { logger.error("Ignoring search request due to configuration errors."); + } else if (searchClient == null){ + logger.error("No search client defined. A client must either be pre-configured or selected before searching."); + errors.addConfigurationError(new ConfigurationError("No client defined","Client is null","No search client defined. A client must be pre-configured or selected runtime, prior to searching.")); } else { stateMgr.hasPendingStateChange("search",false); pzresp.resetSearchResponses(); @@ -137,15 +141,8 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria logger.debug("Ignoring record request due search error."); return ""; } else { - ResponseDataObject responseObject = doCommand("record"); - if (pzreq.getRecord().hasParameterValue("offset") || - pzreq.getRecord().hasParameterValue("checksum")) { - RecordResponse recordResponse = new RecordResponse(); - recordResponse.setType("record"); - recordResponse.setXml(responseObject.getXml()); - recordResponse.setAttribute("activeclients", "0"); - pzresp.put("record", recordResponse); - } + logger.debug("Executing record command"); + doCommand("record"); return pzresp.getRecord().getActiveClients(); } } @@ -164,7 +161,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria logger.error("Ignoring show,stat,termlist,bytarget commands due to problem with most recent search."); return ""; } else if (!hasQuery()) { - logger.error("Ignoring show,stat,termlist,bytarget commands because there is not yet a query."); + logger.debug("Ignoring show,stat,termlist,bytarget commands because there is not yet a query."); return ""; } else { return update("show,stat,termlist,bytarget"); @@ -178,7 +175,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria * @return Number of activeclients at the time of the 'show' command */ public String update (String commands) { - logger.info("Request to update: " + commands); + logger.debug("Request to update: " + commands); try { if (commands.equals("search")) { doSearch(); @@ -192,11 +189,13 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria } else { handleQueryStateChanges(commands); if (pzresp.getSearch().hasApplicationError()) { - logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error."); + logger.error("The command(s) " + commands + " cancelled because the latest search command had an error."); + return "0"; + } else if (errors.hasConfigurationErrors()) { + logger.error("The command(s) " + commands + " cancelled due to configuration errors."); return "0"; } else { logger.debug("Processing request for " + commands); - List threadList = new ArrayList(); StringTokenizer tokens = new StringTokenizer(commands,","); while (tokens.hasMoreElements()) { @@ -282,7 +281,19 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria pager = new ResultsPager(pzresp,pageRange,pzreq); return pager; } - + + /** + * This methods main purpose is to support browser history. + * + * When the browsers back or forward buttons are pressed, a + * re-search /might/ be required - namely if the query changes. + * So, as the UI requests updates of the page (show,facets, + * etc) this method checks if a search must be executed + * before those updates are performed. + * + * @see {@link com.indexdata.mkjsf.pazpar2.state.StateManager#setCurrentStateKey} + * @param commands + */ protected void handleQueryStateChanges (String commands) { if (stateMgr.hasPendingStateChange("search") && hasQuery()) { logger.info("Triggered search: Found pending search change [" + pzreq.getCommand("search").toString() + "], doing search before updating " + commands); @@ -300,26 +311,53 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria } /** - * Validates the request then executes the command and parses the response. + * Executes the command and parses the response to create data objects. * If the parsed response is of a known type it will be cached in 'pzresp' * * @param commandName The command to be executed * @return An XML response parsed to form a response data object */ protected ResponseDataObject doCommand(String commandName) { - ResponseDataObject responseObject = null; - logger.info("Request "+commandName + ": "+ pzreq.getCommand("search").toString()); - Pazpar2Command command = pzreq.getCommand(commandName); - long start = System.currentTimeMillis(); - HttpResponseWrapper commandResponse = searchClient.executeCommand(command); - long end = System.currentTimeMillis(); - logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); - responseLogger.debug("Response was: " + commandResponse.getResponseString()); - responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse); - if (ResponseParser.docTypes.contains(responseObject.getType())) { - pzresp.put(commandName, responseObject); - } - return responseObject; + if (pzreq.getCommand(commandName).spOnly() && isPazpar2Service()) { + logger.warn("Skipping " + commandName + " - SP-only command, un-supported by Pazpar2"); + return new ResponseDataObject(); + } else { + ResponseDataObject responseObject = null; + logger.info("Request "+commandName + ": "+ pzreq.getCommand("search").toString()); + Pazpar2Command command = pzreq.getCommand(commandName); + long start = System.currentTimeMillis(); + HttpResponseWrapper commandResponse = searchClient.executeCommand(command); + long end = System.currentTimeMillis(); + logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); + responseLogger.debug("Response was: " + commandResponse.getResponseString()); + if (commandResponse.getContentType().contains("xml")) { + responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse); + if (ResponseParser.docTypes.contains(responseObject.getType())) { + logger.debug("Storing " + responseObject.getType() + " in pzresp. "); + pzresp.put(commandName, responseObject); + } else { + if (commandName.equals("record")) { + logger.debug("Command was 'record' but response not '' - assuming raw record response."); + ResponseDataObject recordResponse = new RecordResponse(); + recordResponse.setType("record"); + recordResponse.setXml(responseObject.getXml()); + recordResponse.setAttribute("activeclients", "0"); + pzresp.put("record", recordResponse); + } + } + } else if (commandResponse.isBinary()) { + responseObject = new RecordResponse(); + responseObject.setType(commandName); + logger.info("Binary response"); + responseObject.setAttribute("activeclients", "0"); + responseObject.setXml("binary response"); + responseObject.setBinary(commandResponse.getBytes()); + pzresp.put("record", responseObject); + } else { + logger.error("Response was not found to be XML or binary. The response was not handled."); + } + return responseObject; + } } @Override @@ -375,6 +413,17 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria return (searchClient!=null ? searchClient.getServiceUrl() : ""); } + public void setServiceId () { + pzreq.getRecord().removeParametersInState(); + pzreq.getSearch().removeParametersInState(); + pzresp.resetSearchResponses(); + pz2Client.setServiceId(pzreq.getInit().getService()); + } + + public String getServiceId () { + return pzreq.getInit().getService(); + } + public boolean getServiceUrlIsDefined() { return (searchClient != null && searchClient.hasServiceUrl()); } @@ -452,6 +501,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria serviceProxyUrls = config.getMultiProperty(SERVICE_PROXY_URL_LIST,","); pazpar2Urls = config.getMultiProperty(PAZPAR2_URL_LIST, ","); } + logger.info(reader.document()); logger.info("Service Type is configured to " + serviceType); }