From: Niels Erik G. Nielsen Date: Thu, 21 Mar 2013 00:12:27 +0000 (-0400) Subject: Generalizes and distinguishes - pz2 and sp. Adds auth. X-Git-Tag: v0.0.7~199 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=6ecfe8692fcb0f9840b28325f413fc698093fa88;p=mkjsf-moved-to-github.git Generalizes and distinguishes - pz2 and sp. Adds auth. Work in progress on class structure for pazpar2 and SP sessions respectively. Currently based on injecting either Pz2Bean or SpBean as Alternative's. --- diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java index c80c366..874c50f 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -132,7 +132,13 @@ public class ErrorHelper implements Serializable { suggestions.add("Please check the service ID set in the configuration and compare it with the " + " configuration on the Pazpar2 server-side."); addConfigurationDocumentation(suggestions); - break; + break; + case "100": + suggestions.add("Pazpar2 Service Proxy error"); + suggestions.add("A request was made to the Pazpar2 Service Proxy, but the Service Proxy reports "); + suggestions.add(" that authentication is lacking. Could be no successful authentication request was made or"); + suggestions.add(" that the Service Proxy session timed out."); + break; default: suggestions.add("Pazpar2 error: " + error.getPazpar2Error().getMsg() + " (Pazpar2 # "+error.getPazpar2Error().getCode()+")"); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java index a2c10b8..01b999c 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -41,9 +41,9 @@ public class CommandThread extends Thread { CommandResponse commandResponse = client.executeCommand(command, baos); if (commandResponse.getStatusCode()==200) { response.append(commandResponse.getResponseString()); - } else if (commandResponse.getStatusCode()==417) { + } else if (commandResponse.getStatusCode()==417) { logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8")); - response.append(CommandError.insertPazpar2ErrorXml(command.getName(), "Expectation failed (417)", baos.toString("UTF-8"))); + response.append(CommandError.insertPazpar2ErrorXml(command.getName(), "Expectation failed (417)", commandResponse.getResponseString())); } else { String resp = baos.toString("UTF-8"); logger.error("Pazpar2 status code was " + commandResponse.getStatusCode() + ": " + resp); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java index db1fe91..e6e96bc 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Map; import javax.enterprise.context.SessionScoped; -import javax.enterprise.inject.Alternative; import javax.inject.Named; import org.apache.http.HttpEntity; @@ -34,11 +33,13 @@ import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.pz2utils4jsf.config.Configuration; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity; +import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.pz2utils4jsf.utils.Utils; -@Named @SessionScoped @Alternative +@Named @SessionScoped public class ProxyPz2Client implements SearchClient { - + private static final long serialVersionUID = -4031644009579840277L; private static Logger logger = Logger.getLogger(ProxyPz2Client.class); public static final String MODULENAME = "proxyclient"; @@ -46,12 +47,13 @@ public class ProxyPz2Client implements SearchClient { ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler(); private HttpClient client; + private ServiceProxyUser user; public ProxyPz2Client () { 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 @@ -59,33 +61,72 @@ public class ProxyPz2Client implements SearchClient { logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader)); try { Configuration config = configReader.getConfiguration(this); - serviceUrl = config.getMandatory("SERVICE_PROXY_URL"); - authenticate(); + serviceUrl = config.getMandatory("SERVICE_PROXY_URL"); } catch (ConfigurationException c) { - // TODO Auto-generated catch block c.printStackTrace(); } catch (MissingMandatoryParameterException mmp) { mmp.printStackTrace(); - } + } } - public void authenticate () { - try { + public boolean authenticate (AuthenticationEntity user) { + try { + logger.info("Authenticating [" + user.getProperty("name") + "]"); + this.user = (ServiceProxyUser) user; Pazpar2Command auth = new Pazpar2Command("auth"); auth.setParameter(new CommandParameter("action","=","login")); - auth.setParameter(new CommandParameter("username","=","demo")); - auth.setParameter(new CommandParameter("password","=","demo")); - send(auth); + auth.setParameter(new CommandParameter("username","=",user.getProperty("name"))); + auth.setParameter(new CommandParameter("password","=",user.getProperty("password"))); + byte[] response = send(auth); + String responseStr = new String(response,"UTF-8"); + logger.info(responseStr); + if (responseStr.contains("FAIL")) { + return false; + } else { + return true; + } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); + return false; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } + return false; + } + } + + public boolean checkAuthentication () { + try { + Pazpar2Command check = new Pazpar2Command("auth"); + check.setParameter(new CommandParameter("action","=","check")); + byte[] response = send(check); + logger.info(new String(response,"UTF-8")); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + return true; } + public boolean isAuthenticatingClient () { + return true; + } + + public boolean isAuthenticated () { + if (user.getProperty("name") != null && user.getProperty("password") != null) { + return checkAuthentication(); + } else { + return false; + } + } + /** * Makes the request * @param request @@ -93,12 +134,12 @@ public class ProxyPz2Client implements SearchClient { * @throws ClientProtocolException * @throws IOException */ - private String send(Pazpar2Command command) throws ClientProtocolException, IOException { + private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException { String url = serviceUrl + "?" + command.getEncodedQueryString(); logger.info("Sending request "+url); HttpGet httpget = new HttpGet(url); byte[] response = client.execute(httpget, handler); - return new String(response); + return response; } @@ -138,8 +179,9 @@ public class ProxyPz2Client implements SearchClient { @Override public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException { - String response = send(command); - return new ProxyPz2ClientCommandResponse(getStatusCode(), response); + byte[] response = send(command); + baos.write(response); + return new ProxyPz2ClientCommandResponse(getStatusCode(), new String(response,"UTF-8")); } public ProxyPz2Client cloneMe() { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index 16b36e0..f908d80 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -5,6 +5,7 @@ import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; +import javax.enterprise.inject.Alternative; import javax.inject.Inject; import javax.inject.Named; @@ -21,15 +22,15 @@ import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; import com.indexdata.pz2utils4jsf.utils.Utils; -@Named("pz2") @SessionScoped +@Named("pz2") @SessionScoped @Alternative public class Pz2Bean implements Pz2Interface, Serializable { private static final long serialVersionUID = 3440277287081557861L; private static Logger logger = Logger.getLogger(Pz2Bean.class); - Pz2Session pz2; @Inject ConfigurationReader configurator; - @Inject SearchClient searchClient; + protected Pz2Session pz2; + protected SearchClient searchClient; public Pz2Bean () { logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); @@ -39,12 +40,14 @@ public class Pz2Bean implements Pz2Interface, Serializable { public void initiatePz2Session() { logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next."); pz2 = new Pz2Session(); + searchClient = new StraightPz2Client(); logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" + Utils.objectId(configurator) + "] on session [" + Utils.objectId(pz2) + "]" ); pz2.init(searchClient,configurator); } + /* (non-Javadoc) * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch(java.lang.String) */ diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java index dbe5f44..0e17724 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java @@ -332,5 +332,5 @@ public interface Pz2Interface extends Serializable { * @return */ public List getConfigurationErrors(); - + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index bec9874..d506678 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -36,14 +36,14 @@ public class Pz2Session implements Pz2Interface { private static final long serialVersionUID = 3947514708343320514L; private static Logger logger = Logger.getLogger(Pz2Session.class); - private Map dataObjects = new ConcurrentHashMap(); - private QueryStates queryStates = new QueryStates(); - private ErrorHelper errorHelper = null; - - private List configurationErrors = null; - private SearchClient searchClient = null; - private TargetFilter targetFilter = null; - private ResultsPager pager = null; + protected Map dataObjects = new ConcurrentHashMap(); + protected QueryStates queryStates = new QueryStates(); + protected ErrorHelper errorHelper = null; + + protected List configurationErrors = null; + protected SearchClient searchClient = null; + protected TargetFilter targetFilter = null; + protected ResultsPager pager = null; public Pz2Session () { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); @@ -62,7 +62,7 @@ public class Pz2Session implements Pz2Interface { // To avoid that, a context free client is cloned from the context // dependent one. // If propagation to threads gets supported, the cloning can go. - this.searchClient = searchClient.cloneMe(); + this.searchClient = searchClient.cloneMe(); } catch (ConfigurationException e) { configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); @@ -70,7 +70,7 @@ public class Pz2Session implements Pz2Interface { logger.info(configReader.document()); resetDataObjects(); } - + public void doSearch(String query) { setCommandParameter("search",new CommandParameter("query","=",query)); doSearch(); @@ -336,11 +336,11 @@ public class Pz2Session implements Pz2Interface { } - private boolean hasTargetFilter(TargetFilter targetFilter) { + protected boolean hasTargetFilter(TargetFilter targetFilter) { return hasTargetFilter() && targetFilter.equals(this.targetFilter); } - private boolean hasQuery() { + protected boolean hasQuery() { return !(getCommand("search").getParameter("query") == null); } @@ -366,7 +366,7 @@ public class Pz2Session implements Pz2Interface { return errorHelper; } - private void handleQueryStateChanges (String commands) { + protected void handleQueryStateChanges (String commands) { if (queryStates.hasPendingStateChange("search")) { logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); @@ -383,7 +383,7 @@ public class Pz2Session implements Pz2Interface { } } - private String getActiveClients() { + protected String getActiveClients() { if (getShow()!=null) { logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); @@ -392,25 +392,25 @@ public class Pz2Session implements Pz2Interface { } } - private Pazpar2Command getCommand(String name) { + protected Pazpar2Command getCommand(String name) { return queryStates.getCurrentState().getCommand(name); } - private void setCommandParameter(String commandName, CommandParameter parameter) { + protected void setCommandParameter(String commandName, CommandParameter parameter) { logger.debug("Setting parameter for " + commandName + ": " + parameter); queryStates.getCurrentState().setCommandParameter(commandName, parameter, queryStates); } - private void removeCommandParameter(String commandName, String parameterName) { + protected void removeCommandParameter(String commandName, String parameterName) { queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); } - private void removeCommand (String commandName) { + protected void removeCommand (String commandName) { queryStates.getCurrentState().removeCommand(commandName, queryStates); } - private String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -421,7 +421,7 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -433,7 +433,7 @@ public class Pz2Session implements Pz2Interface { } - private int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { + protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -444,13 +444,13 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { + protected String doCommand(String commandName) { Pazpar2Command command = getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); - return update(commandName); + return update(commandName); } - private void resetDataObjects() { + protected void resetDataObjects() { logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); dataObjects = new ConcurrentHashMap(); dataObjects.put("show", new ShowResponse()); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java index ea83a12..cc45e76 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java @@ -12,5 +12,5 @@ public interface SearchClient extends Configurable, Serializable { public void setSearchCommand(Pazpar2Command command); public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException; public SearchClient cloneMe(); - + public boolean isAuthenticatingClient(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java index 475aa15..c7b0515 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Map; import javax.enterprise.context.SessionScoped; -import javax.enterprise.inject.Alternative; import javax.inject.Named; import org.apache.log4j.Logger; @@ -29,7 +28,7 @@ import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.utils.Utils; -@Named @SessionScoped @Alternative +@Named @SessionScoped public class StraightPz2Client implements SearchClient { private static final long serialVersionUID = 5414266730169982028L; @@ -71,6 +70,17 @@ public class StraightPz2Client implements SearchClient { } } + public boolean isAuthenticatingClient () { + return false; + } + + public boolean isAuthenticated() { + return false; + } + + public boolean authenticate() { + throw new UnsupportedOperationException("No authentication mechanism for straight pazpar2 client"); + } @Override public void setSearchCommand(Pazpar2Command command) { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java index 2b5fae2..05cd506 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/CommandError.java @@ -88,8 +88,8 @@ public class CommandError extends Pazpar2ResponseData implements ErrorInterface * * @param commandName The name of the command during which's execution the error was encountered * @param exceptionName The (possibly loosely defined) name of the exception that was thrown - * @param pazpar2ErrorXml The error document as created by Pazpar2 -- or, for some errors, by the - * Pazpar2 client. + * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or + * by the Pazpar2 client itself. * @return */ public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/AuthenticationEntity.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/AuthenticationEntity.java new file mode 100644 index 0000000..cd9ee6c --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/AuthenticationEntity.java @@ -0,0 +1,16 @@ +package com.indexdata.pz2utils4jsf.pazpar2.sp.auth; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +public interface AuthenticationEntity extends Serializable{ + + + public String getProperty(String key); + + public Map getPropertyMap(); + + public List getPossibleProperties(); + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/ServiceProxyUser.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/ServiceProxyUser.java new file mode 100644 index 0000000..dbde7bc --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/auth/ServiceProxyUser.java @@ -0,0 +1,65 @@ +package com.indexdata.pz2utils4jsf.pazpar2.sp.auth; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Named; + +@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(); + + public ServiceProxyUser() {} + + public void setAuthenticationMethod() { + + } + + public String getName() { + return actualProperties.get("name"); + } + + public void setName(String newValue) { + actualProperties.put("name", newValue); + } + + public String getPassword() { + return actualProperties.get("password"); + } + + public void setPassword(String newValue) { + actualProperties.put("password", newValue); + } + + public void setRealm(String realm) { + actualProperties.put("realm", realm); + } + + public String getRealm() { + return actualProperties.get("realm"); + } + + + @Override + public String getProperty(String key) { + return actualProperties.get(key); + } + + @Override + public Map getPropertyMap() { + return actualProperties; + } + + @Override + public List getPossibleProperties() { + return possibleProperties; + } + + +}