From c6430ad85e3b04ea2823df38c15e8473342ff95b Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Wed, 17 Apr 2013 12:28:39 -0400 Subject: [PATCH] Adds demo auth method. Tweaks polling & state fields setup. Adds a method for automatic authentication from demo. Tweaks naming and location for specialty fields for pazpar2 polling and browser history handling. --- src/META-INF/resources/pz2utils/pz2watch.xhtml | 2 +- .../java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java | 27 +++++++++++-- .../com/indexdata/mkjsf/pazpar2/Pz2Interface.java | 6 ++- .../com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java | 11 ++++++ .../mkjsf/pazpar2/commands/CommandParameter.java | 7 +++- .../mkjsf/pazpar2/commands/Expression.java | 5 ++- .../mkjsf/pazpar2/commands/InitCommand.java | 4 +- .../mkjsf/pazpar2/commands/Pazpar2Command.java | 25 ++++++------ .../mkjsf/pazpar2/sp/ServiceProxyClient.java | 41 ++++++++++++++------ .../mkjsf/pazpar2/sp/auth/ServiceProxyUser.java | 21 ++++++++-- .../indexdata/mkjsf/pz2utils/ListenerFieldIds.java | 30 -------------- 11 files changed, 113 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/com/indexdata/mkjsf/pz2utils/ListenerFieldIds.java diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index aafdbef..f18e46d 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -44,7 +44,7 @@ State: - + diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java index 43578f8..d617f14 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java @@ -192,14 +192,13 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { public void setCurrentStateKey(String key) { stateMgr.setCurrentStateKey(key); } - - - + protected boolean hasQuery() { return pzreq.getCommand("search").hasParameterSet("query"); } + @Override public ResultsPager getPager () { if (pager == null) { pager = new ResultsPager(pzresp); @@ -207,6 +206,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { return pager; } + @Override public ResultsPager setPager (int pageRange) { pager = new ResultsPager(pzresp,pageRange,pzreq); return pager; @@ -241,6 +241,25 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { update(commandName); } } + + @Override + public boolean getAuthenticationRequired () { + return searchClient.isAuthenticatingClient(); + } - + @Override + public String getCheckHistory () { + return ":pz2watch:stateForm:windowlocationhash"; + } + + @Override + public String getWatchActiveclients () { + return ":pz2watch:activeclientsForm:activeclientsField"; + } + + @Override + public String getWatchActiveclientsRecord () { + return ":pz2watch:activeclientsForm:activeclientsFieldRecord"; + } + } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Interface.java index 1ffd33c..bf8aa40 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Interface.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Interface.java @@ -5,7 +5,7 @@ import java.io.Serializable; import com.indexdata.mkjsf.controls.ResultsPager; public interface Pz2Interface extends Serializable { - + /** * Executes a Pazpar2 search using the given query string * @@ -96,6 +96,10 @@ public interface Pz2Interface extends Serializable { */ public void setCurrentStateKey(String key); + public boolean getAuthenticationRequired (); + public String getCheckHistory (); + public String getWatchActiveclients (); + public String getWatchActiveclientsRecord (); } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java index 16b4319..fce82ca 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java @@ -46,6 +46,17 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { "during construction of parent object Pz2Bean."); } } + + public void login(String un, String pw) { + if (user.isAuthenticated() && user.getName().equals(un) && ((ServiceProxyClient) searchClient).checkAuthentication()) { + 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"); + } + } @Override public String login(String navigateTo) { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java index 69c224a..f97497f 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java @@ -3,7 +3,9 @@ package com.indexdata.mkjsf.pazpar2.commands; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.log4j.Logger; @@ -14,11 +16,12 @@ public class CommandParameter implements Serializable { private static Logger logger = Logger.getLogger(CommandParameter.class); - private static final long serialVersionUID = 625502285668766749L; + private static final long serialVersionUID = 625502285668766749L; String name = null; String operator = null; String value = null; Map expressions = new HashMap(); + private static List nologparams = Arrays.asList("password"); public CommandParameter (String name) { logger.debug("Instantiating command parameter '" + name + "'"); @@ -36,7 +39,7 @@ public class CommandParameter implements Serializable { } public CommandParameter (String name, String operator, String value) { - logger.debug("Instantiating command parameter '" + name + "' with String: [" + value + "]"); + if (!nologparams.contains(name)) logger.debug("Instantiating command parameter '" + name + "' with String: [" + value + "]"); this.name = name; this.operator = operator; this.value = value; diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java index d95ff64..3e93e49 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java @@ -1,9 +1,12 @@ package com.indexdata.mkjsf.pazpar2.commands; +import java.io.Serializable; + import com.indexdata.mkjsf.pazpar2.commands.Expression; -public class Expression { +public class Expression implements Serializable { + private static final long serialVersionUID = -751704027842027769L; String leftEntity; String operator; String rightEntity; diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/InitCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/InitCommand.java index f1f31ef..2ae51a5 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/InitCommand.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/InitCommand.java @@ -11,11 +11,11 @@ public class InitCommand extends Pazpar2Command { } public void setClear(String clear) { - setParameter(new CommandParameter("clear","=",clear)); + setParameterInState(new CommandParameter("clear","=",clear)); } public void setService(String serviceId) { - setParameter(new CommandParameter("service","=",serviceId)); + setParameterInState(new CommandParameter("service","=",serviceId)); } @Override 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 4fbe159..9a3695b 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java @@ -19,12 +19,7 @@ public class Pazpar2Command implements Serializable { public Pazpar2Command (String name, StateManager stateMgr) { this.name = name; - if (stateMgr == null) { - // Sets throw-away state - this.stateMgr = new StateManager(); - } else { - this.stateMgr = stateMgr; - } + this.stateMgr = stateMgr; } public Pazpar2Command copy () { @@ -43,7 +38,7 @@ public class Pazpar2Command implements Serializable { Pazpar2Command copy = this.copy(); logger.debug(name + " command: setting parameter [" + parameter.getName() + "=" + parameter.getValueWithExpressions() + "]"); copy.parameters.put(parameter.getName(),parameter); - stateMgr.checkIn(copy); + checkInState(copy); } public void setParameters (CommandParameter... params) { @@ -52,7 +47,7 @@ public class Pazpar2Command implements Serializable { logger.debug(name + " command: setting parameter [" + param.getName() + "=" + param.getValueWithExpressions() + "]"); copy.parameters.put(param.getName(),param); } - stateMgr.checkIn(copy); + checkInState(copy); } public void setParametersInState (CommandParameter... params) { @@ -75,13 +70,13 @@ public class Pazpar2Command implements Serializable { public void removeParameter (String name) { Pazpar2Command copy = this.copy(); copy.parameters.remove(name); - stateMgr.checkIn(copy); + checkInState(copy); } public void removeParameters() { Pazpar2Command copy = this.copy(); copy.parameters = new HashMap(); - stateMgr.checkIn(copy); + checkInState(copy); } public void removeParametersInState() { @@ -144,5 +139,13 @@ public class Pazpar2Command implements Serializable { public String getSession() { return getParameterValue("session"); - } + } + + private void checkInState(Pazpar2Command command) { + if (stateMgr != null) { + stateMgr.checkIn(command); + } else { + logger.info("Command '" + command.getName() + "' not affecting state (history) as no state manager was defined for this command."); + } + } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java index 3724362..244496a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java @@ -59,12 +59,14 @@ public class ServiceProxyClient implements SearchClient { ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler(); private HttpClient client; private ServiceProxyUser user; + private Pazpar2Command checkAuth = null; + public ServiceProxyClient () { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); - client = new DefaultHttpClient(cm); + client = new DefaultHttpClient(cm); } @Override @@ -73,7 +75,9 @@ public class ServiceProxyClient implements SearchClient { try { config = configReader.getConfiguration(this); serviceUrl = config.getMandatory(SERVICE_PROXY_URL); - this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); + this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); + checkAuth = new Pazpar2Command("auth",null); + checkAuth.setParameterInState(new CommandParameter("action","=","check")); } catch (ConfigurationException c) { c.printStackTrace(); } catch (MissingMandatoryParameterException mmp) { @@ -92,17 +96,19 @@ public class ServiceProxyClient implements SearchClient { public boolean authenticate (AuthenticationEntity user) { try { logger.info("Authenticating [" + user.getProperty("name") + "]"); - this.user = (ServiceProxyUser) user; + this.user = (ServiceProxyUser) user; Pazpar2Command auth = new Pazpar2Command("auth",null); auth.setParametersInState(new CommandParameter("action","=","login"), new CommandParameter("username","=",user.getProperty("name")), - new CommandParameter("password","=",user.getProperty("password"))); + new CommandParameter("password","=",user.getProperty("password"))); byte[] response = send(auth); String responseStr = new String(response,"UTF-8"); logger.info(responseStr); if (responseStr.contains("FAIL")) { + this.user.isAuthenticated(false); return false; } else { + this.user.isAuthenticated(true); return true; } } catch (ClientProtocolException e) { @@ -116,12 +122,18 @@ public class ServiceProxyClient implements SearchClient { } } - public boolean checkAuthentication () { + public boolean checkAuthentication () { try { - Pazpar2Command check = new Pazpar2Command("auth",null); - check.setParameter(new CommandParameter("action","=","check")); - byte[] response = send(check); + byte[] response = send(checkAuth); logger.info(new String(response,"UTF-8")); + String responseStr = new String(response,"UTF-8"); + if (responseStr.contains("FAIL")) { + this.user.isAuthenticated(false); + return false; + } else { + this.user.isAuthenticated(true); + return true; + } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -130,9 +142,7 @@ public class ServiceProxyClient implements SearchClient { // TODO Auto-generated catch block e.printStackTrace(); return false; - } - return true; - + } } public boolean isAuthenticatingClient () { @@ -162,6 +172,15 @@ public class ServiceProxyClient implements SearchClient { return response; } + private byte[] send (String queryString) throws ClientProtocolException, IOException { + String url = serviceUrl + "?" + queryString; + logger.info("Sending request "+url); + HttpGet httpget = new HttpGet(url); + byte[] response = client.execute(httpget, handler); + return response; + + } + public class ProxyPz2ResponseHandler implements ResponseHandler { private StatusLine statusLine = null; public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java index ea0be27..ed6d0ba 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/auth/ServiceProxyUser.java @@ -8,17 +8,24 @@ import java.util.Map; import javax.enterprise.context.SessionScoped; import javax.inject.Named; +import org.apache.log4j.Logger; + +import com.indexdata.mkjsf.utils.Utils; + @Named("user") @SessionScoped public class ServiceProxyUser implements AuthenticationEntity { private static final long serialVersionUID = 2351542518778803071L; private List possibleProperties = Arrays.asList("name","password","realm"); private Map actualProperties = new HashMap(); + private static Logger logger = Logger.getLogger(ServiceProxyUser.class); + private boolean authenticated = false; - public ServiceProxyUser() {} + public ServiceProxyUser() { + logger.debug("ServiceProxyUser instantiated: " + Utils.objectId(this)); + } - public void setAuthenticationMethod() { - + public void setAuthenticationMethod() { } public String getName() { @@ -44,6 +51,14 @@ public class ServiceProxyUser implements AuthenticationEntity { public String getRealm() { return actualProperties.get("realm"); } + + public void isAuthenticated(boolean authenticated) { + this.authenticated = authenticated; + } + + public boolean isAuthenticated() { + return authenticated; + } @Override diff --git a/src/main/java/com/indexdata/mkjsf/pz2utils/ListenerFieldIds.java b/src/main/java/com/indexdata/mkjsf/pz2utils/ListenerFieldIds.java deleted file mode 100644 index df3652f..0000000 --- a/src/main/java/com/indexdata/mkjsf/pz2utils/ListenerFieldIds.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.indexdata.mkjsf.pz2utils; - -import java.io.Serializable; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Named; - -@Named("pz2watch") -@ApplicationScoped -public class ListenerFieldIds implements Serializable { - - private static final long serialVersionUID = -57079241763914538L; - - public String getHistory () { - return ":pz2watch:stateForm:windowlocationhash"; - } - - public String getActiveclients () { - return ":pz2watch:activeclientsForm:activeclientsField"; - } - - public String getActiveclientsRecord () { - return ":pz2watch:activeclientsForm:activeclientsFieldRecord"; - } - - public String getErrorMessages () { - return ":pz2watch:activeclientsForm:errorMessages"; - } - -} -- 1.7.10.4