X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyExtensions.java;h=d085c316ce950c2c5b3803647681265e23155fbe;hb=11ff48b9d37193a89a4b74da75db8c58415c46a6;hp=0ee282552ee8981e62318d86021e9396aea62be3;hpb=bcb39bd9bde8071ac3a6741ce5d51ed9e763ba9c;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java index 0ee2825..d085c31 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java @@ -13,14 +13,12 @@ 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.Pazpar2Commands; import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; import com.indexdata.mkjsf.pazpar2.commands.sp.InitDocUpload; -import com.indexdata.mkjsf.pazpar2.data.AuthResponse; import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; import com.indexdata.mkjsf.pazpar2.data.ResponseParser; -import com.indexdata.mkjsf.pazpar2.data.Responses; -import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; +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 @@ -33,79 +31,63 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab private InitDocUpload initDocUpload; @Inject ConfigurationReader configurator; - @Inject ServiceProxyUser user; @Inject Pz2Bean pz2; - @Inject Pazpar2Commands pzreq; - @Inject Responses pzresp; - - + public ServiceProxyExtensions() { this.initDocUpload = new InitDocUpload(this); // TODO: //stateMgr.addStateListener(this); } - - /* - public void login(String un, String pw) { - if (user.isAuthenticated() && user.getName().equals(un) && pz2.spClient.checkAuthentication(user)) { - logger.info("Repeat request from UI to authenticate user. Auth verified for given user name so skipping log-in."); - } else { - logger.info("doing un/pw login"); - user.setName(un); - user.setPassword(pw); - login("dummy"); + + public void authenticate() { + if (pz2.getPzresp().getSp().getAuth().unsupportedCommand()) { + logger.warn("Running seemingly unsupported command [auth] against SP."); } - } - */ - - - @Override - public String login(String navigateTo) { - logger.info("doing login by " + user + " using " + pz2 + " and client " + pz2.getSpClient()); pz2.resetSearchAndRecordCommands(); - pzresp.resetAllSessionData(); - AuthCommand auth = pzreq.getSp().getAuth(); - auth.setParametersInState(new CommandParameter("action","=","login"), - new CommandParameter("username","=",user.getName()), - new CommandParameter("password","=",user.getPassword())); - ClientCommandResponse commandResponse = pz2.getSpClient().send(auth); - AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString())); + pz2.getPzresp().getSp().resetAuthAndBeyond(true); + AuthCommand auth = pz2.getPzreq().getSp().getAuth(); + ClientCommandResponse commandResponse = pz2.getSpClient().send(auth); + String renamedResponse = renameResponseElement(commandResponse.getResponseString(), "auth"); + commandResponse.setResponseToParse(renamedResponse); + SpResponseDataObject responseObject = (SpResponseDataObject) ResponseParser.getParser().getDataObject(commandResponse); if (ResponseParser.docTypes.contains(responseObject.getType())) { - pzresp.put(auth.getCommandName(), responseObject); + pz2.getPzresp().put(auth.getCommandName(), responseObject); + } + if (responseObject.unsupportedCommand()) { + logger.error("auth command does not seem to be supported by this Service Proxy"); } String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.credentialsAuthenticationSucceeded(false); - } else { - user.credentialsAuthenticationSucceeded(true); - } - return navigateTo; + logger.info(responseStr); } + public void login(String un, String pw) { + login(un,pw,""); + } - public void ipAuthenticate (ServiceProxyUser user) { - if (!user.isIpAuthenticated()) { - if (user.isAuthenticated()) { - user.clear(); - } - pz2.resetSearchAndRecordCommands(); - pzresp.resetAllSessionData(); - AuthCommand auth = pzreq.getSp().getAuth(); - auth.setParameterInState(new CommandParameter("action","=","ipAuth")); - ClientCommandResponse commandResponse = pz2.getSpClient().send(auth); - AuthResponse responseObject = (AuthResponse) (ResponseParser.getParser().getDataObject(commandResponse.getResponseString())); - if (ResponseParser.docTypes.contains(responseObject.getType())) { - pzresp.put(auth.getCommandName(), responseObject); - } - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.credentialsAuthenticationSucceeded(false); - } else { - user.credentialsAuthenticationSucceeded(true); - } - } + public void login(String un, String pw, String navigateTo) { + pz2.getPzreq().getSp().getAuth().setUsername(un); + pz2.getPzreq().getSp().getAuth().setPassword(pw); + login(""); + } + + @Override + public String login(String navigateTo) { + AuthCommand auth = pz2.getPzreq().getSp().getAuth(); + auth.setParameterInState(new CommandParameter("action","=","login")); + authenticate(); + return navigateTo; + } + + public void ipAuthenticate () { + AuthCommand auth = pz2.getPzreq().getSp().getAuth(); + auth.setParameterInState(new CommandParameter("action","=","ipAuth")); + authenticate(); + } + + private String renameResponseElement(String responseString, String newName) { + responseString = responseString.replace("", "<" + newName + ">"); + responseString = responseString.replace("", ""); + return responseString; } public String getInitDocPath () { @@ -127,9 +109,9 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab public ClientCommandResponse postInit() throws UnsupportedEncodingException, IOException { String initDocPath = pz2.getSpClient().getInitDocPaths().get(0); logger.info("Paths: " + pz2.getSpClient().getInitDocPaths()); - logger.info("Path: " + initDocPath); + logger.info("Path: " + initDocPath); pz2.resetSearchAndRecordCommands(); - pzresp.resetAllSessionData(); + pz2.getPzresp().getSp().resetAuthAndBeyond(true); ClientCommandResponse response = pz2.getSpClient().postInitDoc(initDocPath + getInitFileName()); return response; } @@ -137,7 +119,7 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab @Override public HttpResponseWrapper postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException { pz2.resetSearchAndRecordCommands(); - pzresp.resetAllSessionData(); + pz2.getPzresp().getSp().resetAuthAndBeyond(true); HttpResponseWrapper response = pz2.getSpClient().postInitDoc(initDoc,includeDebug); return response; } @@ -156,10 +138,10 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab } public void submitInitDoc () throws IOException { - HttpResponseWrapper response = initDocUpload.submit(); - ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response.getResponseString()); - logger.info("Putting init response to : " + Utils.objectId(pzresp)); - pzresp.put("init", responseObject); + ClientCommandResponse response = (ClientCommandResponse) initDocUpload.submit(); + ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response); + logger.info("Putting init response to : " + Utils.objectId(pz2.getPzresp())); + pz2.getPzresp().put("init", responseObject); } public void setIncludeInitDocDebug(boolean bool) { @@ -171,12 +153,36 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab return initDocUpload.getIncludeDebug(); } - // TODO: Remove when possible + // TODO: Remove when obsolete public InitDocUpload getInitDocUpload () { return initDocUpload; } - + public CategoriesResponse getCategories () { + String command="categories"; + if (pz2.isServiceProxyService()) { + if (pz2.getPzresp().getSp().getCategories().unsupportedCommand()) { + logger.info("Skipping seemingly unsupported command: " + command); + return new CategoriesResponse(); + } else { + SpResponseDataObject response = (SpResponseDataObject) pz2.doCommand(command); + 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(); + } + } }