From bcb39bd9bde8071ac3a6741ce5d51ed9e763ba9c Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Fri, 10 May 2013 18:48:54 -0400 Subject: [PATCH] Cleans up SP auth, parse resps, store for display, etc --- .../mkjsf/pazpar2/ServiceProxyClient.java | 59 +------------------- .../mkjsf/pazpar2/ServiceProxyExtensions.java | 43 ++++++++++++-- .../mkjsf/pazpar2/commands/Pazpar2Commands.java | 15 ++++- .../mkjsf/pazpar2/commands/sp/AuthCommand.java | 3 - .../pazpar2/commands/sp/ServiceProxyCommands.java | 28 ++++++++++ .../indexdata/mkjsf/pazpar2/data/AuthResponse.java | 25 +++++++++ .../mkjsf/pazpar2/data/ResponseParser.java | 15 +++-- .../indexdata/mkjsf/pazpar2/data/Responses.java | 14 ++--- .../mkjsf/pazpar2/state/Pazpar2State.java | 6 +- .../mkjsf/pazpar2/state/StateManager.java | 2 +- 10 files changed, 127 insertions(+), 83 deletions(-) create mode 100644 src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java create mode 100644 src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java index 79d6153..9493e49 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -38,7 +38,6 @@ import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; import com.indexdata.mkjsf.pazpar2.data.CommandError; -import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.mkjsf.utils.Utils; public class ServiceProxyClient implements SearchClient { @@ -81,63 +80,11 @@ public class ServiceProxyClient implements SearchClient { c.printStackTrace(); } } - - - public boolean authenticate (ServiceProxyUser user) { - logger.info("Authenticating [" + user.getProperty("name") + "]"); - Pazpar2Command auth = new AuthCommand(null); - auth.setParametersInState(new CommandParameter("action","=","login"), - new CommandParameter("username","=",user.getProperty("name")), - new CommandParameter("password","=",user.getProperty("password"))); - ClientCommandResponse commandResponse = send(auth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.credentialsAuthenticationSucceeded(false); - return false; - } else { - user.credentialsAuthenticationSucceeded(true); - return true; - } - } - - public boolean checkAuthentication (ServiceProxyUser user) { - ClientCommandResponse commandResponse = send(checkAuth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.authenticationCheckFailed(); - return false; - } else { - return true; - } - } - - public boolean ipAuthenticate (ServiceProxyUser user) { - ClientCommandResponse commandResponse = send(ipAuth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.ipAuthenticationSucceeded(false); - return false; - } else { - user.ipAuthenticationSucceeded(true); - return true; - } - } - + public boolean isAuthenticatingClient () { return true; } - - public boolean isAuthenticated (ServiceProxyUser user) { - if (user.getProperty("name") != null && user.getProperty("password") != null) { - return checkAuthentication(user); - } else { - return false; - } - } - + /** * Makes the request * @param request @@ -145,7 +92,7 @@ public class ServiceProxyClient implements SearchClient { * @throws ClientProtocolException * @throws IOException */ - private ClientCommandResponse send(Pazpar2Command command) { + public ClientCommandResponse send(Pazpar2Command command) { ClientCommandResponse commandResponse = null; String url = serviceUrl + "?" + command.getEncodedQueryString(); logger.info("Sending request "+url); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java index 560edb6..0ee2825 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java @@ -12,8 +12,11 @@ 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.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; @@ -41,7 +44,8 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab // 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."); @@ -52,16 +56,34 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab login("dummy"); } } + */ - @Override + + @Override public String login(String navigateTo) { logger.info("doing login by " + user + " using " + pz2 + " and client " + pz2.getSpClient()); pz2.resetSearchAndRecordCommands(); pzresp.resetAllSessionData(); - pz2.getSpClient().authenticate(user); + 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())); + 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); + } return navigateTo; } + public void ipAuthenticate (ServiceProxyUser user) { if (!user.isIpAuthenticated()) { if (user.isAuthenticated()) { @@ -69,7 +91,20 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab } pz2.resetSearchAndRecordCommands(); pzresp.resetAllSessionData(); - pz2.getSpClient().ipAuthenticate(user); + 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); + } } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java index 9e04d6e..2524d47 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java @@ -9,6 +9,7 @@ import javax.inject.Named; import org.apache.log4j.Logger; +import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands; import com.indexdata.mkjsf.pazpar2.state.StateManager; import com.indexdata.mkjsf.utils.Utils; @@ -17,7 +18,7 @@ public class Pazpar2Commands implements Serializable { private static final long serialVersionUID = -5172466320351302413L; private static Logger logger = Logger.getLogger(Pazpar2Commands.class); - + public static final String INIT = "init"; public static final String PING = "ping"; public static final String SETTINGS = "settings"; @@ -27,8 +28,11 @@ public class Pazpar2Commands implements Serializable { public static final String RECORD = "record"; public static final String TERMLIST = "termlist"; public static final String BYTARGET = "bytarget"; + + private ServiceProxyCommands sp = null; - @Inject StateManager stateMgr; + @Inject + protected StateManager stateMgr; public Pazpar2Commands() { logger.info("Initializing Pazpar2Commands [" + Utils.objectId(this) + "]"); @@ -78,5 +82,12 @@ public class Pazpar2Commands implements Serializable { public Pazpar2Command getCommand(String name) { return stateMgr.getCommand(name); } + + public ServiceProxyCommands getSp() { + if (sp == null) { + sp = new ServiceProxyCommands(stateMgr); + } + return sp; + } } 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 698f265..4d72d61 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 @@ -9,7 +9,6 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand { public AuthCommand(StateManager stateMgr) { super("auth", stateMgr); - // TODO Auto-generated constructor stub } public AuthCommand copy () { @@ -24,6 +23,4 @@ public class AuthCommand extends Pazpar2Command implements ServiceProxyCommand { public ServiceProxyCommand getSp() { return this; } - - } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java new file mode 100644 index 0000000..cac8100 --- /dev/null +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java @@ -0,0 +1,28 @@ +package com.indexdata.mkjsf.pazpar2.commands.sp; + +import java.io.Serializable; + +import org.apache.log4j.Logger; + +import com.indexdata.mkjsf.pazpar2.state.StateManager; +import com.indexdata.mkjsf.utils.Utils; + +public class ServiceProxyCommands implements Serializable { + + public static final String AUTH = "auth"; + private static final long serialVersionUID = 6223527018096841188L; + private static Logger logger = Logger.getLogger(ServiceProxyCommands.class); + private StateManager stateMgr = null; + + public ServiceProxyCommands(StateManager stateMgr) { + logger.info("Initializing ServiceProxyCommands [" + Utils.objectId(this) + "]"); + this.stateMgr = stateMgr; + } + + + public AuthCommand getAuth() { + return (AuthCommand) (stateMgr.getCommand(AUTH)); + } + + +} diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java new file mode 100644 index 0000000..a0a015f --- /dev/null +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/AuthResponse.java @@ -0,0 +1,25 @@ +package com.indexdata.mkjsf.pazpar2.data; + +public class AuthResponse extends ResponseDataObject { + + private static final long serialVersionUID = 8006774126022849936L; + + public String getDisplayName () { + return getOneElementValue("displayName"); + } + + public String getRealm () { + return getOneElementValue("realm"); + } + + public String getStatus () { + return getOneElementValue("status"); + } + + public String getAuthenticationType () { + return getOneElementValue("type"); + } + + + +} diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java index 4af0ec2..29a5fb7 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java @@ -27,10 +27,10 @@ public class ResponseParser extends DefaultHandler { private String xml = null; private static Logger logger = Logger.getLogger(ResponseParser.class); - public static final List docTypes = - Arrays.asList("bytarget","termlist","show","stat","record","search","init"); + public static List docTypes = Arrays.asList( "bytarget","termlist","show","stat","record","search","init", + /* SP extras */ "response" ); - public ResponseParser() { + public ResponseParser() { try { initSax(); } catch (ParserConfigurationException e) { @@ -66,13 +66,10 @@ public class ResponseParser extends DefaultHandler { try { xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")))); } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return result; @@ -118,7 +115,9 @@ public class ResponseParser extends DefaultHandler { } else if (localName.equals("applicationerror")) { currentElement = new CommandError(); } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) { - currentElement = new Pazpar2Error(); + currentElement = new Pazpar2Error(); + } else if (localName.equals("response")) { // Note, document element not named 'auth' + currentElement = new AuthResponse(); } else { currentElement = new ResponseDataObject(); } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java index a442805..8e4078d 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java @@ -16,7 +16,6 @@ import org.apache.log4j.Logger; import com.indexdata.mkjsf.errors.ErrorHelper; import com.indexdata.mkjsf.errors.ErrorInterface; -import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses; import com.indexdata.mkjsf.utils.Utils; @Named("pzresp") @SessionScoped @@ -89,12 +88,16 @@ public class Responses implements Serializable { dataObjects = new ConcurrentHashMap(); resetSearchResponses(); dataObjects.put("init", new InitResponse()); + dataObjects.put("auth", new AuthResponse()); } - public InitResponse getInit () { - logger.info("Request to show init response from " + Utils.objectId(this)); + public InitResponse getInit () { return ((InitResponse) dataObjects.get("init")); } + + public AuthResponse getAuth () { + return ((AuthResponse) dataObjects.get("auth")); + } public ShowResponse getShow () { return ((ShowResponse) dataObjects.get("show")); @@ -147,11 +150,6 @@ public class Responses implements Serializable { } } - public SpResponses getSp() { - // TODO: - return null; - } - public void download(String commandName) throws UnsupportedEncodingException, IOException { logger.info(Utils.objectId(this) + " got a download request for " + commandName); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java b/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java index dc2d2e1..2551c3b 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java @@ -14,6 +14,8 @@ import com.indexdata.mkjsf.pazpar2.commands.SettingsCommand; import com.indexdata.mkjsf.pazpar2.commands.ShowCommand; import com.indexdata.mkjsf.pazpar2.commands.StatCommand; import com.indexdata.mkjsf.pazpar2.commands.TermlistCommand; +import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; +import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands; /** * Holds a 'pazpar2 state', understood as a full set of pazpar2 commands and @@ -36,7 +38,9 @@ public class Pazpar2State { commands.put(Pazpar2Commands.SHOW, new ShowCommand(mgr)); commands.put(Pazpar2Commands.RECORD, new RecordCommand(mgr)); commands.put(Pazpar2Commands.TERMLIST, new TermlistCommand(mgr)); - commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr)); + commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr)); + + commands.put(ServiceProxyCommands.AUTH, new AuthCommand(mgr)); key = "#1"; } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java b/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java index c7661e0..7106f6d 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java @@ -21,7 +21,7 @@ public class StateManager implements Serializable { Map states = new HashMap(); String currentKey = ""; - private static List allCommands = new ArrayList(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")); + private static List allCommands = new ArrayList(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget","auth")); Map pendingStateChanges = new HashMap(); private static Logger logger = Logger.getLogger(StateManager.class); private List listeners = new ArrayList(); -- 1.7.10.4