Adds a method for automatic authentication from demo.
Tweaks naming and location for specialty fields for pazpar2 polling
and browser history handling.
<h:form id="stateForm" prependId="false" rendered="${cc.attrs.trackHistory == 'true'}" style="${cc.attrs.debug == 'true' ? '' : 'display:none;'}">\r
State: \r
<h:inputText id="windowlocationhash" value="#{pz2.currentStateKey}" size="100">\r
- <f:ajax event="change" render="#{cc.attrs.renderOnHistoryChange} #{pz2watch.activeclients}"/>\r
+ <f:ajax event="change" render="#{cc.attrs.renderOnHistoryChange} #{pz2.watchActiveclients}"/>\r
</h:inputText>\r
</h:form>\r
</cc:implementation>\r
public void setCurrentStateKey(String key) { \r
stateMgr.setCurrentStateKey(key);\r
}\r
- \r
- \r
- \r
+ \r
protected boolean hasQuery() { \r
return pzreq.getCommand("search").hasParameterSet("query"); \r
}\r
\r
\r
+ @Override\r
public ResultsPager getPager () {\r
if (pager == null) {\r
pager = new ResultsPager(pzresp); \r
return pager; \r
}\r
\r
+ @Override\r
public ResultsPager setPager (int pageRange) {\r
pager = new ResultsPager(pzresp,pageRange,pzreq);\r
return pager;\r
update(commandName);\r
} \r
}\r
+ \r
+ @Override\r
+ public boolean getAuthenticationRequired () {\r
+ return searchClient.isAuthenticatingClient();\r
+ }\r
\r
-\r
+ @Override\r
+ public String getCheckHistory () {\r
+ return ":pz2watch:stateForm:windowlocationhash";\r
+ }\r
+ \r
+ @Override\r
+ public String getWatchActiveclients () {\r
+ return ":pz2watch:activeclientsForm:activeclientsField";\r
+ }\r
+ \r
+ @Override\r
+ public String getWatchActiveclientsRecord () {\r
+ return ":pz2watch:activeclientsForm:activeclientsFieldRecord";\r
+ }\r
+ \r
}\r
import com.indexdata.mkjsf.controls.ResultsPager;\r
\r
public interface Pz2Interface extends Serializable {\r
-\r
+ \r
/**\r
* Executes a Pazpar2 search using the given query string\r
* \r
*/\r
public void setCurrentStateKey(String key);\r
\r
+ public boolean getAuthenticationRequired ();\r
\r
+ public String getCheckHistory ();\r
+ public String getWatchActiveclients ();\r
+ public String getWatchActiveclientsRecord ();\r
\r
}\r
"during construction of parent object Pz2Bean.");\r
}\r
}\r
+ \r
+ public void login(String un, String pw) {\r
+ if (user.isAuthenticated() && user.getName().equals(un) && ((ServiceProxyClient) searchClient).checkAuthentication()) {\r
+ logger.info("Repeat request from UI to authenticate user. Auth verified for given user name so skipping log-in.");\r
+ } else {\r
+ logger.info("doing un/pw login");\r
+ user.setName(un);\r
+ user.setPassword(pw);\r
+ login("dummy");\r
+ }\r
+ }\r
\r
@Override\r
public String login(String navigateTo) {\r
import java.io.Serializable;\r
import java.io.UnsupportedEncodingException;\r
import java.net.URLEncoder;\r
+import java.util.Arrays;\r
import java.util.HashMap;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.apache.log4j.Logger;\r
\r
private static Logger logger = Logger.getLogger(CommandParameter.class);\r
\r
- private static final long serialVersionUID = 625502285668766749L;\r
+ private static final long serialVersionUID = 625502285668766749L; \r
String name = null;\r
String operator = null;\r
String value = null;\r
Map<String,Expression> expressions = new HashMap<String,Expression>();\r
+ private static List<String> nologparams = Arrays.asList("password");\r
\r
public CommandParameter (String name) {\r
logger.debug("Instantiating command parameter '" + name + "'");\r
}\r
\r
public CommandParameter (String name, String operator, String value) {\r
- logger.debug("Instantiating command parameter '" + name + "' with String: [" + value + "]");\r
+ if (!nologparams.contains(name)) logger.debug("Instantiating command parameter '" + name + "' with String: [" + value + "]"); \r
this.name = name;\r
this.operator = operator;\r
this.value = value; \r
package com.indexdata.mkjsf.pazpar2.commands;\r
\r
+import java.io.Serializable;\r
+\r
import com.indexdata.mkjsf.pazpar2.commands.Expression;\r
\r
-public class Expression {\r
+public class Expression implements Serializable {\r
\r
+ private static final long serialVersionUID = -751704027842027769L;\r
String leftEntity;\r
String operator;\r
String rightEntity;\r
}\r
\r
public void setClear(String clear) { \r
- setParameter(new CommandParameter("clear","=",clear));\r
+ setParameterInState(new CommandParameter("clear","=",clear));\r
}\r
\r
public void setService(String serviceId) { \r
- setParameter(new CommandParameter("service","=",serviceId));\r
+ setParameterInState(new CommandParameter("service","=",serviceId));\r
}\r
\r
@Override\r
\r
public Pazpar2Command (String name, StateManager stateMgr) {\r
this.name = name;\r
- if (stateMgr == null) {\r
- // Sets throw-away state\r
- this.stateMgr = new StateManager();\r
- } else {\r
- this.stateMgr = stateMgr;\r
- }\r
+ this.stateMgr = stateMgr;\r
}\r
\r
public Pazpar2Command copy () {\r
Pazpar2Command copy = this.copy();\r
logger.debug(name + " command: setting parameter [" + parameter.getName() + "=" + parameter.getValueWithExpressions() + "]");\r
copy.parameters.put(parameter.getName(),parameter);\r
- stateMgr.checkIn(copy);\r
+ checkInState(copy);\r
}\r
\r
public void setParameters (CommandParameter... params) {\r
logger.debug(name + " command: setting parameter [" + param.getName() + "=" + param.getValueWithExpressions() + "]");\r
copy.parameters.put(param.getName(),param);\r
}\r
- stateMgr.checkIn(copy);\r
+ checkInState(copy);\r
}\r
\r
public void setParametersInState (CommandParameter... params) { \r
public void removeParameter (String name) {\r
Pazpar2Command copy = this.copy();\r
copy.parameters.remove(name);\r
- stateMgr.checkIn(copy);\r
+ checkInState(copy);\r
}\r
\r
public void removeParameters() {\r
Pazpar2Command copy = this.copy();\r
copy.parameters = new HashMap<String,CommandParameter>();\r
- stateMgr.checkIn(copy);\r
+ checkInState(copy);\r
}\r
\r
public void removeParametersInState() {\r
\r
public String getSession() {\r
return getParameterValue("session");\r
- } \r
+ } \r
+ \r
+ private void checkInState(Pazpar2Command command) {\r
+ if (stateMgr != null) {\r
+ stateMgr.checkIn(command);\r
+ } else {\r
+ logger.info("Command '" + command.getName() + "' not affecting state (history) as no state manager was defined for this command.");\r
+ }\r
+ }\r
}\r
ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
private HttpClient client;\r
private ServiceProxyUser user;\r
+ private Pazpar2Command checkAuth = null;\r
+\r
\r
public ServiceProxyClient () {\r
SchemeRegistry schemeRegistry = new SchemeRegistry();\r
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));\r
ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);\r
- client = new DefaultHttpClient(cm); \r
+ client = new DefaultHttpClient(cm); \r
}\r
\r
@Override\r
try {\r
config = configReader.getConfiguration(this); \r
serviceUrl = config.getMandatory(SERVICE_PROXY_URL); \r
- this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); \r
+ this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));\r
+ checkAuth = new Pazpar2Command("auth",null);\r
+ checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
} catch (ConfigurationException c) {\r
c.printStackTrace();\r
} catch (MissingMandatoryParameterException mmp) {\r
public boolean authenticate (AuthenticationEntity user) {\r
try { \r
logger.info("Authenticating [" + user.getProperty("name") + "]");\r
- this.user = (ServiceProxyUser) user;\r
+ this.user = (ServiceProxyUser) user; \r
Pazpar2Command auth = new Pazpar2Command("auth",null);\r
auth.setParametersInState(new CommandParameter("action","=","login"), \r
new CommandParameter("username","=",user.getProperty("name")), \r
- new CommandParameter("password","=",user.getProperty("password")));\r
+ new CommandParameter("password","=",user.getProperty("password"))); \r
byte[] response = send(auth);\r
String responseStr = new String(response,"UTF-8");\r
logger.info(responseStr); \r
if (responseStr.contains("FAIL")) {\r
+ this.user.isAuthenticated(false);\r
return false;\r
} else {\r
+ this.user.isAuthenticated(true);\r
return true;\r
} \r
} catch (ClientProtocolException e) {\r
} \r
}\r
\r
- public boolean checkAuthentication () {\r
+ public boolean checkAuthentication () { \r
try {\r
- Pazpar2Command check = new Pazpar2Command("auth",null);\r
- check.setParameter(new CommandParameter("action","=","check"));\r
- byte[] response = send(check);\r
+ byte[] response = send(checkAuth);\r
logger.info(new String(response,"UTF-8"));\r
+ String responseStr = new String(response,"UTF-8"); \r
+ if (responseStr.contains("FAIL")) {\r
+ this.user.isAuthenticated(false);\r
+ return false;\r
+ } else {\r
+ this.user.isAuthenticated(true);\r
+ return true;\r
+ } \r
} catch (ClientProtocolException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
return false;\r
- } \r
- return true;\r
- \r
+ } \r
}\r
\r
public boolean isAuthenticatingClient () {\r
return response;\r
}\r
\r
+ private byte[] send (String queryString) throws ClientProtocolException, IOException {\r
+ String url = serviceUrl + "?" + queryString; \r
+ logger.info("Sending request "+url); \r
+ HttpGet httpget = new HttpGet(url); \r
+ byte[] response = client.execute(httpget, handler); \r
+ return response;\r
+ \r
+ }\r
+ \r
public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
private StatusLine statusLine = null;\r
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
import javax.enterprise.context.SessionScoped;\r
import javax.inject.Named;\r
\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.utils.Utils;\r
+\r
@Named("user") @SessionScoped\r
public class ServiceProxyUser implements AuthenticationEntity {\r
\r
private static final long serialVersionUID = 2351542518778803071L;\r
private List<String> possibleProperties = Arrays.asList("name","password","realm");\r
private Map<String,String> actualProperties = new HashMap<String,String>();\r
+ private static Logger logger = Logger.getLogger(ServiceProxyUser.class);\r
+ private boolean authenticated = false;\r
\r
- public ServiceProxyUser() {}\r
+ public ServiceProxyUser() {\r
+ logger.debug("ServiceProxyUser instantiated: " + Utils.objectId(this));\r
+ }\r
\r
- public void setAuthenticationMethod() {\r
- \r
+ public void setAuthenticationMethod() { \r
}\r
\r
public String getName() { \r
public String getRealm() {\r
return actualProperties.get("realm");\r
}\r
+ \r
+ public void isAuthenticated(boolean authenticated) {\r
+ this.authenticated = authenticated;\r
+ }\r
+ \r
+ public boolean isAuthenticated() {\r
+ return authenticated;\r
+ }\r
\r
\r
@Override\r
+++ /dev/null
-package com.indexdata.mkjsf.pz2utils;\r
-\r
-import java.io.Serializable;\r
-\r
-import javax.enterprise.context.ApplicationScoped;\r
-import javax.inject.Named;\r
-\r
-@Named("pz2watch")\r
-@ApplicationScoped\r
-public class ListenerFieldIds implements Serializable {\r
- \r
- private static final long serialVersionUID = -57079241763914538L;\r
- \r
- public String getHistory () {\r
- return ":pz2watch:stateForm:windowlocationhash";\r
- }\r
- \r
- public String getActiveclients () {\r
- return ":pz2watch:activeclientsForm:activeclientsField";\r
- }\r
- \r
- public String getActiveclientsRecord () {\r
- return ":pz2watch:activeclientsForm:activeclientsFieldRecord";\r
- }\r
- \r
- public String getErrorMessages () {\r
- return ":pz2watch:activeclientsForm:errorMessages";\r
- }\r
-\r
-}\r