From: Niels Erik G. Nielsen Date: Sat, 25 May 2013 02:24:26 +0000 (-0400) Subject: Cleans up SP extension commands X-Git-Tag: v0.0.7~79 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=95b6d2c90bde9b9946b79479f42233f6d7645f77;p=mkjsf-moved-to-github.git Cleans up SP extension commands .. as for straight pz2 commands, request logic is moved from the main SP extensions bean into individual commands. Also adds options (pz2 and sp) for adding service command parameters as parameters to the run() command itself. Useful for this approach in a form: --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java index 6595059..51ab55b 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java @@ -12,13 +12,9 @@ import org.apache.log4j.Logger; import org.apache.myfaces.custom.fileupload.UploadedFile; import com.indexdata.mkjsf.config.ConfigurationReader; -import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; -import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; import com.indexdata.mkjsf.pazpar2.commands.sp.InitDocUpload; import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; import com.indexdata.mkjsf.pazpar2.data.ResponseParser; -import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse; -import com.indexdata.mkjsf.pazpar2.data.sp.SpResponseDataObject; import com.indexdata.mkjsf.utils.Utils; @Named("pz2x") @SessionScoped @@ -37,48 +33,14 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab // TODO: //stateMgr.addStateListener(this); } - - public void authenticate() { - if (Pz2Bean.get().getPzresp().getSp().getAuth().unsupportedCommand()) { - logger.warn("Running seemingly unsupported command [auth] against SP."); - } - Pz2Bean.get().resetSearchAndRecordCommands(); - Pz2Bean.get().getPzresp().getSp().resetAuthAndBeyond(true); - Pz2Bean.get().getPzreq().getSp().getAuth().run(); - } - - public void login(String un, String pw) { - login(un,pw,""); - } - - public void login(String un, String pw, String navigateTo) { - Pz2Bean.get().getPzreq().getSp().getAuth().setUsername(un); - Pz2Bean.get().getPzreq().getSp().getAuth().setPassword(pw); - login(""); - } - - @Override - public String login(String navigateTo) { - AuthCommand auth = Pz2Bean.get().getPzreq().getSp().getAuth(); - auth.setParameterInState(new CommandParameter("action","=","login")); - authenticate(); - return navigateTo; - } - - public void ipAuthenticate () { - AuthCommand auth = Pz2Bean.get().getPzreq().getSp().getAuth(); - auth.setParameterInState(new CommandParameter("action","=","ipAuth")); - authenticate(); - } - + public String getInitDocPath () { return Pz2Bean.get().getSpClient().getConfiguration().get("INIT_DOC_PATH"); } @Override public void setInitFileName(String fileName) { - this.initDocFileName = fileName; - + this.initDocFileName = fileName; } @Override @@ -138,31 +100,5 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab return initDocUpload; } - public CategoriesResponse getCategories () { - String command="categories"; - if (Pz2Bean.get().isServiceProxyService()) { - if (Pz2Bean.get().getPzresp().getSp().getCategories().unsupportedCommand()) { - logger.info("Skipping seemingly unsupported command: " + command); - return new CategoriesResponse(); - } else { - SpResponseDataObject response = (SpResponseDataObject) Pz2Bean.get().getPzreq().getSp().getCategories().run(); - if (response.unsupportedCommand()) { - logger.warn("Command 'categories' not supported by this Service Proxy"); - } else if (response.hasApplicationError()) { - logger.error(response.getXml()); - } - try { - return (CategoriesResponse) response; - } catch (Exception e) { - e.printStackTrace(); - logger.debug(response.getXml()); - return new CategoriesResponse(); - } - } - } else { - return new CategoriesResponse(); - } - } - - + } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyInterface.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyInterface.java index c2d8c48..3a2e6a2 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyInterface.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyInterface.java @@ -4,8 +4,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; -public interface ServiceProxyInterface { - public String login(String navigateTo); +public interface ServiceProxyInterface { public void setInitFileName (String fileName); public String getInitFileName(); public HttpResponseWrapper postInit() throws UnsupportedEncodingException, IOException; diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java index 41e3326..fd74c37 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java @@ -3,6 +3,7 @@ package com.indexdata.mkjsf.pazpar2.commands; import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import java.util.StringTokenizer; import org.apache.log4j.Logger; @@ -39,11 +40,22 @@ public abstract class Pazpar2Command implements Serializable { return name; } - public ResponseDataObject run() { + public ResponseDataObject run() { return run(Pz2Bean.get().getSearchClient(), Pz2Bean.get().getPzresp()); } + public ResponseDataObject runWith(String... parameters) { + for (String parameter : parameters) { + StringTokenizer tokenizer = new StringTokenizer(parameter,"="); + String name = (String) tokenizer.nextElement(); + String value = (String) tokenizer.nextElement(); + CommandParameter commandParameter = new CommandParameter(name,"=",value); + setParameterInState(commandParameter); + } + return run(); + } + /** * For running the command in a thread. Client and Responses must be * provided because at this point the CDI bean cannot be retrieved @@ -54,7 +66,7 @@ public abstract class Pazpar2Command implements Serializable { * @return */ public ResponseDataObject run(SearchClient client,Responses pzresp) { - logger.info("Running " + getCommandName() + " using " + client); + logger.debug("Running " + getCommandName() + " using " + client); HttpResponseWrapper httpResponse = client.executeCommand(this); logger.debug("Parsing response for " + getCommandName()); ResponseDataObject responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse) httpResponse); @@ -178,6 +190,10 @@ public abstract class Pazpar2Command implements Serializable { Pz2Bean.get().getStateMgr().checkIn(command); } + public String navigateTo (String target) { + return target; + } + public abstract ServiceProxyCommand getSp(); public abstract boolean spOnly(); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java index 3c9617e..723bc6a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/AuthCommand.java @@ -19,6 +19,8 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand { } public SpResponseDataObject run() { + Pz2Bean.get().resetSearchAndRecordCommands(); + Pz2Bean.get().getPzresp().getSp().resetAuthAndBeyond(true); ClientCommandResponse response = (ClientCommandResponse) Pz2Bean.get().getSearchClient().executeCommand(this); String renamedResponse = renameResponseElement(response.getResponseString(), "auth"); response.setResponseToParse(renamedResponse); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java index ad5723f..989c21a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java @@ -1,14 +1,42 @@ package com.indexdata.mkjsf.pazpar2.commands.sp; +import org.apache.log4j.Logger; + +import com.indexdata.mkjsf.pazpar2.Pz2Bean; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse; public class CategoriesCommand extends Pazpar2Command implements ServiceProxyCommand { private static final long serialVersionUID = 5023993689780291641L; + private static Logger logger = Logger.getLogger(CategoriesCommand.class); public CategoriesCommand() { super("categories"); } + + @Override + public CategoriesResponse run () { + if (Pz2Bean.get().getPzresp().getSp().getCategories().unsupportedCommand()) { + logger.info("Skipping seemingly unsupported categories command"); + return new CategoriesResponse(); + } else { + if (Pz2Bean.get().isServiceProxyService()) { + try { + CategoriesResponse response = (CategoriesResponse) super.run(); + if (response.unsupportedCommand()) { + logger.warn("Command 'categories' not supported by this Service Proxy"); + } + return response; + } catch (Exception e) { + e.printStackTrace(); + return new CategoriesResponse(); + } + } else { + return new CategoriesResponse(); + } + } + } @Override public Pazpar2Command copy() {