From edf26614e68f0c3f17d245c1f00f01667fe6ac05 Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Fri, 15 Mar 2013 16:12:02 -0400 Subject: [PATCH] Service proxy client and configuration schemes SP client almost functionable. Also refactoring of configuration scheme - loosens tie to MasterKey configuration scheme and to any particular configurable modules. --- pom.xml | 2 +- .../pz2utils4jsf/config/Configurable.java | 15 +++ .../pz2utils4jsf/config/Configuration.java | 73 +++++++++++++++ .../pz2utils4jsf/config/ConfigurationReader.java | 13 +++ .../pz2utils4jsf/config/Mk2ConfigReader.java | 86 ++++++++++++++++++ .../indexdata/pz2utils4jsf/config/Pz2Config.java | 91 ------------------- .../pz2utils4jsf/config/Pz2Configurator.java | 12 --- .../config/Pz2ConfigureByMk2Config.java | 83 ----------------- .../pz2utils4jsf/config/Pz2ConfigureByWebXml.java | 63 ------------- .../pz2utils4jsf/config/WebXmlConfigReader.java | 63 +++++++++++++ .../indexdata/pz2utils4jsf/errors/ErrorHelper.java | 14 +-- .../pz2utils4jsf/pazpar2/ProxyPz2Client.java | 96 +++++++++++++++++--- .../indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java | 8 +- .../indexdata/pz2utils4jsf/pazpar2/Pz2Session.java | 22 +++-- .../pz2utils4jsf/pazpar2/SearchClient.java | 10 +- .../pz2utils4jsf/pazpar2/StraightPz2Client.java | 77 ++++++++++++++-- 16 files changed, 428 insertions(+), 300 deletions(-) create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Configurable.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Configuration.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/ConfigurationReader.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Mk2ConfigReader.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/WebXmlConfigReader.java diff --git a/pom.xml b/pom.xml index 5eda446..567fde3 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ org.apache.httpcomponents httpclient - 4.0-beta1 + 4.2.3 diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Configurable.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Configurable.java new file mode 100644 index 0000000..26586ba --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Configurable.java @@ -0,0 +1,15 @@ +package com.indexdata.pz2utils4jsf.config; + +import java.util.List; +import java.util.Map; + +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; + +public interface Configurable { + + public void configure(ConfigurationReader reader) throws ConfigurationException; + public Map getDefaults(); + public String getModuleName(); + public List documentConfiguration(); + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Configuration.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Configuration.java new file mode 100644 index 0000000..adab4bb --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Configuration.java @@ -0,0 +1,73 @@ +package com.indexdata.pz2utils4jsf.config; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.masterkey.config.MissingMandatoryParameterException; +import com.indexdata.pz2utils4jsf.utils.Utils; + +public class Configuration implements Serializable { + + private static final long serialVersionUID = -6801241975338182197L; + private static Logger logger = Logger.getLogger(Configuration.class); + Map properties = new HashMap(); + + public Configuration () { + logger.debug(Utils.objectId(this) + " being constructed with no argument"); + } + + public Configuration(Map parameters) { + addAll(parameters); + } + + public void addAll(Map parameters) { + for (String key : parameters.keySet()) { + properties.put(key, parameters.get(key)); + } + } + + public void addAll(Map defaults, Map parameters) { + for (String key : defaults.keySet()) { + properties.put(key, defaults.get(key)); + } + for (String key : parameters.keySet()) { + properties.put(key, parameters.get(key)); + } + } + + public String get(String key) { + return properties.get(key); + } + + public void set(String key, String value) { + properties.put(key, value); + } + + public String get(String key, String defaultValue) { + if (properties.containsKey(key)) { + return properties.get(key); + } else { + return defaultValue; + } + } + + public String getMandatory(String key) throws MissingMandatoryParameterException { + if (properties.containsKey(key)) { + return properties.get(key); + } + throw new MissingMandatoryParameterException("Missing mandatory parameter: " + key); + } + + public String getConfigFilePath() { + return get("configfilepath","nopathgiven"); + } + + public Map getConfigMap() { + return properties; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/ConfigurationReader.java b/src/main/java/com/indexdata/pz2utils4jsf/config/ConfigurationReader.java new file mode 100644 index 0000000..528e8bd --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/ConfigurationReader.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.config; + +import java.io.Serializable; +import java.util.List; + +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; + +public interface ConfigurationReader extends Serializable { + + public Configuration getConfiguration(Configurable configurable) throws ConfigurationException; + + public List document(); +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Mk2ConfigReader.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Mk2ConfigReader.java new file mode 100644 index 0000000..430cc73 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/Mk2ConfigReader.java @@ -0,0 +1,86 @@ +package com.indexdata.pz2utils4jsf.config; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.enterprise.context.SessionScoped; +import javax.enterprise.inject.Alternative; +import javax.faces.context.ExternalContext; +import javax.faces.context.FacesContext; +import javax.inject.Named; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import org.apache.log4j.Logger; + +import com.indexdata.masterkey.config.MasterkeyConfiguration; +import com.indexdata.masterkey.config.ModuleConfiguration; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.utils.Utils; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + +@Named @SessionScoped @Alternative +public class Mk2ConfigReader implements ConfigurationReader { + + private static final long serialVersionUID = 8865086878660568870L; + private static Logger logger = Logger.getLogger(Mk2ConfigReader.class); + private Map configs = new HashMap(); + private Map configurables = new HashMap(); + + public Mk2ConfigReader () throws IOException { + logger.info(Utils.objectId(this) + " is instantiating Pazpar2 service configuration by MasterKey configuration scheme."); + } + + @Override + public Configuration getConfiguration(Configurable configurable) throws ConfigurationException { + if (configs.get(configurable.getModuleName()) == null) { + Configuration config = readConfig(configurable); + configs.put(configurable.getModuleName(), config); + configurables.put(configurable.getModuleName(), configurable); + } + return configs.get(configurable.getModuleName()); + } + + private Configuration readConfig (Configurable configurable) throws ConfigurationException { + Configuration config = new Configuration(); + ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); + ServletContext servletContext = (ServletContext) externalContext.getContext(); + MasterkeyConfiguration mkConfigContext; + try { + mkConfigContext = MasterkeyConfiguration.getInstance(servletContext, + "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName()); + } catch (IOException e) { + throw new ConfigurationException(Mk2ConfigReader.class + " could not read configuration for '" + configurable.getModuleName() + "' using MasterKey configuration scheme: "+e.getMessage(),e); + } + try { + ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration(configurable.getModuleName()); + config.addAll(configurable.getDefaults(),moduleConfig.getConfigMap()); + config.set("configpath", mkConfigContext.getConfigFileLocation().getConfigFilePath()); + } catch (IOException e) { + throw new ConfigurationException(Mk2ConfigReader.class + " could not read configuration for '"+ configurable.getModuleName() + "': "+e.getMessage(),e); + } + return config; + } + + public List document() { + List doc = new ArrayList(); + doc.add("Application properties as read by " + this.getClass()); + for (String moduleName : configs.keySet()) { + doc.add(nl+"Module: " + moduleName); + Configurable module = configurables.get(moduleName); + Map map = configs.get(moduleName).getConfigMap(); + for (String key : map.keySet()) { + doc.add(nl+key+": "+ map.get(key) + + (module.getDefaults().containsKey(key) ? + (module.getDefaults().get(key).equals(map.get(key)) ? " [default]" : " [override]") + : "")); + } + } + return doc; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java deleted file mode 100644 index c6af028..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Config.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.indexdata.pz2utils4jsf.config; - -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -import javax.enterprise.context.SessionScoped; -import javax.inject.Named; - -import org.apache.log4j.Logger; - -import com.indexdata.masterkey.config.MissingMandatoryParameterException; -import com.indexdata.masterkey.config.ModuleConfiguration; -import com.indexdata.masterkey.config.ModuleConfigurationGetter; -import com.indexdata.pz2utils4jsf.errors.ConfigurationException; -import com.indexdata.pz2utils4jsf.utils.Utils; - -@Named @SessionScoped -public class Pz2Config implements ModuleConfigurationGetter, Serializable { - - private static final long serialVersionUID = -6801241975338182197L; - private static Logger logger = Logger.getLogger(Pz2Config.class); - Map properties = new HashMap(); - ModuleConfiguration moduleConfig = null; - - public Pz2Config () { - logger.debug(Utils.objectId(this) + " being constructed with no argument"); - setDefaults(); - } - - public Pz2Config (Map parameters) { - setDefaults(); - for (String key : parameters.keySet()) { - properties.put(key, parameters.get(key)); - } - } - - public Pz2Config (ModuleConfiguration moduleConfig) throws ConfigurationException { - logger.debug(Utils.objectId(this) + " being constructed with moduleConfig argument."); - this.moduleConfig = moduleConfig; - try { - for (String key : moduleConfig.getConfigMap().keySet()) { - properties.put(key, moduleConfig.getConfigParameter(key)); - } - } catch (IOException e) { - throw new ConfigurationException("Could not instantiate Pazpar2 configuration: "+e.getMessage(),e); - } - } - - private void setDefaults () { - properties.put("PROXY_MODE","1"); - properties.put("SERIALIZE_REQUESTS", "false"); - properties.put("STREAMBUFF_SIZE", "4096"); - properties.put("PARSE_RESPONSES", "true"); - } - - @Override - public String get(String key) { - return properties.get(key); - } - - public void set(String key, String value) { - properties.put(key, value); - } - - @Override - public String get(String key, String defaultValue) { - if (properties.containsKey(key)) { - return properties.get(key); - } else { - return defaultValue; - } - } - - @Override - public String getMandatory(String key) throws MissingMandatoryParameterException { - if (properties.containsKey(key)) { - return properties.get(key); - } - throw new MissingMandatoryParameterException("Missing mandatory parameter: " + key); - } - - @Override - public String getConfigFilePath() { - return (moduleConfig != null ? moduleConfig.getConfigFilePath() : "nopath"); - } - - - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java deleted file mode 100644 index 9749aa1..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2Configurator.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.indexdata.pz2utils4jsf.config; - -import java.io.Serializable; -import java.util.List; - -import com.indexdata.pz2utils4jsf.errors.ConfigurationException; - -public interface Pz2Configurator extends Serializable { - public Pz2Config getConfig() throws ConfigurationException; - - public List document(); -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java deleted file mode 100644 index e6418fc..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByMk2Config.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.indexdata.pz2utils4jsf.config; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import javax.enterprise.context.SessionScoped; -import javax.enterprise.inject.Alternative; -import javax.faces.context.ExternalContext; -import javax.faces.context.FacesContext; -import javax.inject.Named; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; - -import org.apache.log4j.Logger; - -import com.indexdata.masterkey.config.MasterkeyConfiguration; -import com.indexdata.masterkey.config.ModuleConfiguration; -import com.indexdata.pz2utils4jsf.errors.ConfigurationException; -import com.indexdata.pz2utils4jsf.utils.Utils; -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; - -@Named @SessionScoped @Alternative -public class Pz2ConfigureByMk2Config implements Pz2Configurator { - - private static final long serialVersionUID = 8865086878660568870L; - private static Logger logger = Logger.getLogger(Pz2ConfigureByMk2Config.class); - private Pz2Config pz2config = null; - private String configFilePathAndName = "none"; - - public Pz2ConfigureByMk2Config () throws IOException { - logger.info(Utils.objectId(this) + " is instantiating Pazpar2 service configuration by MasterKey configuration scheme."); - } - - @Override - public Pz2Config getConfig() throws ConfigurationException { - if (pz2config == null) { - createConfig(); - } - return pz2config; - } - - private void createConfig () throws ConfigurationException { - ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); - ServletContext servletContext = (ServletContext) externalContext.getContext(); - MasterkeyConfiguration mkConfigContext; - try { - mkConfigContext = MasterkeyConfiguration.getInstance(servletContext, - "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName()); - } catch (IOException e) { - throw new ConfigurationException("Pz2ConfigureByMk2Config could not configure Pazpar2 client using MasterKey configuration scheme: "+e.getMessage(),e); - } - configFilePathAndName = mkConfigContext.getConfigFileLocation().getConfigFilePath(); - try { - ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration("pz2client"); - pz2config = new Pz2Config(moduleConfig); - logger.info(document()); - } catch (IOException e) { - throw new ConfigurationException("Pz2ConfigureByMk2Config could not get configuration for module 'pz2client': "+e.getMessage(),e); - } - } - - public List document() { - List doc = new ArrayList(); - doc.add(nl+"Attempted to configure service using the file " + configFilePathAndName); - doc.add(nl+"-- Configured to access Pazpar2 at: " +pz2config.get("PAZPAR2_URL")); - if (pz2config.get("PAZPAR2_SERVICE_XML") != null) { - doc.add(nl+"-- Configured to use the service definition contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SERVICE_XML")); - if (pz2config.get("PAZPAR2_SETTINGS_XML") != null) { - doc.add(nl+"-- Configured to use the target settings contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SETTINGS_XML")); - } else { - doc.add(nl+"-- Configured to use the server side target settings as defined in the service definition."); - } - } else if (pz2config.get("PAZPAR2_SERVICE_ID") != null) { - doc.add(nl+"-- Configured to use the server side service definition identified by service id \""+pz2config.get("PAZPAR2_SERVICE_ID") + "\""); - } else { - doc.add(nl+"Error: Did not find service ID nor service definition XML file for setting up a pazpar2 service."); - } - return doc; - } - - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java b/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java deleted file mode 100644 index 0fc81d4..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/config/Pz2ConfigureByWebXml.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.indexdata.pz2utils4jsf.config; - -import static com.indexdata.pz2utils4jsf.utils.Utils.nl; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.enterprise.context.SessionScoped; -import javax.enterprise.inject.Alternative; -import javax.faces.context.ExternalContext; -import javax.faces.context.FacesContext; -import javax.inject.Named; -import javax.servlet.ServletContext; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.errors.ConfigurationException; - - -@Named @SessionScoped @Alternative -public class Pz2ConfigureByWebXml implements Pz2Configurator { - - private static final long serialVersionUID = 144390224959311772L; - private static Logger logger = Logger.getLogger(Pz2ConfigureByWebXml.class); - private Pz2Config pz2config = null; - private Map parameters = new HashMap(); - - public Pz2ConfigureByWebXml () { - logger.info("Instantiating Pazpar2 service configuration by web.xml parameters"); - } - - @Override - public Pz2Config getConfig() throws ConfigurationException { - if (pz2config == null) { - createConfig(); - } - return pz2config; - } - - private void createConfig () throws ConfigurationException { - ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); - ServletContext servletContext = (ServletContext) externalContext.getContext(); - parameters.put("PAZPAR2_URL", servletContext.getInitParameter("PAZPAR2_URL")); - if (parameters.get("PAZPAR2_URL")==null || parameters.get("PAZPAR2_URL").length()==0) { - throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_URL'"); - } - parameters.put("PAZPAR2_SERVICE_ID", servletContext.getInitParameter("PAZPAR2_SERVICE_ID")); - if (parameters.get("PAZPAR2_SERVICE_ID")==null || parameters.get("PAZPAR2_SERVICE_ID").length()==0) { - throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_SERVICE_ID'"); - } - pz2config = new Pz2Config(parameters); - } - - public List document() { - List doc = new ArrayList(); - doc.add("Attempted to configure service using web.xml context-parameters "); - doc.add(nl+"-- Configured to access Pazpar2 at [" +parameters.get("PAZPAR2_URL") + "]"); - doc.add(nl+"-- Configured to use the server side service definition identified by service id [" +parameters.get("PAZPAR2_SERVICE_ID") + "]"); - return doc; - } -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/WebXmlConfigReader.java b/src/main/java/com/indexdata/pz2utils4jsf/config/WebXmlConfigReader.java new file mode 100644 index 0000000..f3669ae --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/WebXmlConfigReader.java @@ -0,0 +1,63 @@ +package com.indexdata.pz2utils4jsf.config; + +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.enterprise.context.SessionScoped; +import javax.enterprise.inject.Alternative; +import javax.faces.context.ExternalContext; +import javax.faces.context.FacesContext; +import javax.inject.Named; +import javax.servlet.ServletContext; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; + +@Named @SessionScoped @Alternative +public class WebXmlConfigReader implements ConfigurationReader { + + private static final long serialVersionUID = 144390224959311772L; + private static Logger logger = Logger.getLogger(WebXmlConfigReader.class); + private Configuration config = null; + private Map parameters = new HashMap(); + + public WebXmlConfigReader () { + logger.info("Instantiating Pazpar2 service configuration by web.xml parameters"); + } + + public Configuration getConfiguration(Configurable configurable) throws ConfigurationException { + if (config == null) { + parameters.putAll(configurable.getDefaults()); + parameters.putAll(readConfig()); + config = new Configuration(parameters); + } + return config; + } + + private Map readConfig () throws ConfigurationException { + Map map = new HashMap(); + ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); + ServletContext servletContext = (ServletContext) externalContext.getContext(); + Enumeration enumer = servletContext.getInitParameterNames(); + while (enumer.hasMoreElements()) { + String name = enumer.nextElement(); + map.put(name,servletContext.getInitParameter(name)); + } + return map; + } + + public List document() { + List doc = new ArrayList(); + doc.add("Application properties as read by " + this.getClass()); + for (String key : parameters.keySet()) { + doc.add(nl+key+": "+ parameters.get(key)); + } + return doc; + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java index e78fb4d..c80c366 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/errors/ErrorHelper.java @@ -9,7 +9,7 @@ import java.util.regex.Pattern; import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.utils.Utils; public class ErrorHelper implements Serializable { @@ -32,9 +32,9 @@ public class ErrorHelper implements Serializable { private static Logger logger = Logger.getLogger(ErrorHelper.class); - private Pz2Configurator configurator = null; + private ConfigurationReader configurator = null; - public ErrorHelper(Pz2Configurator configurator) { + public ErrorHelper(ConfigurationReader configurator) { this.configurator = configurator; } @@ -64,9 +64,9 @@ public class ErrorHelper implements Serializable { return ErrorCode.SKIP_SUGGESTIONS; } else if (errmsg.contains("Missing mandatory parameter")) { return ErrorCode.MISSING_MANDATORY_PROPERTY; - } else if (errmsg.contains("Pz2ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { + } else if (errmsg.contains("ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { return ErrorCode.MISSING_MK2_CONFIG_INIT_PARAMETER; - } else if (appError.getMessage().contains("Pz2ConfigureByWebXml could not find mandatory context-param")) { + } else if (appError.getMessage().contains("WebXmlConfigReader could not find mandatory context-param")) { return ErrorCode.MISSING_CONTEXT_PARAMETER; } return ErrorCode.NOT_RESOLVED; @@ -78,13 +78,13 @@ public class ErrorHelper implements Serializable { switch (code) { case MISSING_MK2_CONFIG_INIT_PARAMETER: suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." + - " Following init parameters must be present when using the MasterKey configuration scheme (Pz2ConfigureByMk2Config):" + + " Following init parameters must be present when using the MasterKey configuration scheme (ConfigureByMk2Config):" + " MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " + " MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'"); break; case MISSING_CONTEXT_PARAMETER: suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." + - " Following init parameters must be present when using Pz2ConfigureByWebXml:" + + " Following init parameters must be present when using WebXmlConfigReader:" + " PAZPAR2_URL, PAZPAR2_SERVICE_ID"); break; case MISSING_MANDATORY_PROPERTY: diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java index 349b0ff..db1fe91 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java @@ -1,7 +1,13 @@ package com.indexdata.pz2utils4jsf.pazpar2; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.enterprise.context.SessionScoped; import javax.enterprise.inject.Alternative; @@ -14,12 +20,20 @@ import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.scheme.PlainSocketFactory; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; +import com.indexdata.masterkey.config.MissingMandatoryParameterException; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.Configuration; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.utils.Utils; @Named @SessionScoped @Alternative @@ -27,17 +41,49 @@ 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"; + private String serviceUrl = "undefined"; ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler(); - HttpClient client = new DefaultHttpClient(); + private HttpClient client; - public ProxyPz2Client(HttpClient client) { + public ProxyPz2Client () { + SchemeRegistry schemeRegistry = new SchemeRegistry(); + schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); + ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); + client = new DefaultHttpClient(cm); } - + @Override - public void configure (Pz2Configurator configurator) { - logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator)); - + public void configure (ConfigurationReader configReader) { + 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(); + } catch (ConfigurationException c) { + // TODO Auto-generated catch block + c.printStackTrace(); + } catch (MissingMandatoryParameterException mmp) { + mmp.printStackTrace(); + } + } + + public void authenticate () { + try { + 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); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } /** @@ -48,8 +94,8 @@ public class ProxyPz2Client implements SearchClient { * @throws IOException */ private String send(Pazpar2Command command) throws ClientProtocolException, IOException { - String url = command.getEncodedQueryString(); - logger.debug("Sending request "+url); + 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); @@ -59,13 +105,14 @@ public class ProxyPz2Client implements SearchClient { public class ProxyPz2ResponseHandler implements ResponseHandler { private StatusLine statusLine = null; public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { + byte[] resp = null; HttpEntity entity = response.getEntity(); statusLine = response.getStatusLine(); if (entity != null) { - return EntityUtils.toByteArray(entity); - } else { - return null; - } + resp = EntityUtils.toByteArray(entity); + } + EntityUtils.consume(entity); + return resp; } public int getStatusCode() { return statusLine.getStatusCode(); @@ -96,7 +143,28 @@ public class ProxyPz2Client implements SearchClient { } public ProxyPz2Client cloneMe() { - return this; + logger.debug("Cloning StraightPz2Client"); + ProxyPz2Client clone = new ProxyPz2Client(); + clone.client = this.client; + clone.serviceUrl = this.serviceUrl; + return clone; + } + + @Override + public Map getDefaults() { + return new HashMap(); + } + + @Override + public String getModuleName() { + return MODULENAME; + } + + @Override + public List documentConfiguration () { + List doc = new ArrayList(); + doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl); + return null; } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index dff3142..16b36e0 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -10,7 +10,7 @@ import javax.inject.Named; import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; import com.indexdata.pz2utils4jsf.errors.ErrorInterface; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; @@ -28,8 +28,8 @@ public class Pz2Bean implements Pz2Interface, Serializable { private static Logger logger = Logger.getLogger(Pz2Bean.class); Pz2Session pz2; - @Inject Pz2Configurator configurator; - @Inject SearchClient searchClient; + @Inject ConfigurationReader configurator; + @Inject SearchClient searchClient; public Pz2Bean () { logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); @@ -41,7 +41,7 @@ public class Pz2Bean implements Pz2Interface, Serializable { pz2 = new Pz2Session(); logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" + Utils.objectId(configurator) + "] on session [" - + Utils.objectId(pz2) + "]" ); + + Utils.objectId(pz2) + "]" ); pz2.init(searchClient,configurator); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 58b296f..f35523e 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -11,7 +11,7 @@ import javax.inject.Named; import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.errors.ErrorHelper; @@ -48,24 +48,26 @@ public class Pz2Session implements Pz2Interface { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); } - public void init(SearchClient searchClient, Pz2Configurator configurator) { + public void init(SearchClient searchClient, ConfigurationReader configReader) { configurationErrors = new ArrayList(); - errorHelper = new ErrorHelper(configurator); + errorHelper = new ErrorHelper(configReader); logger.debug(Utils.objectId(this) + " will configure search client for the session"); try { - searchClient.configure(configurator); - - // The cloning is a hack: + searchClient.configure(configReader); // At the time of writing this search client is injected using Weld. // However, the client is used for asynchronously sending off requests - // to the server AND propagation of context to threads is not supported. - // Trying so will throw a WELD-001303 error. To avoid that, a context - // free client is spawned from the context dependent one. + // to the server AND propagation of context to threads is currently + // not supported. Trying to do so throws a WELD-001303 error. + // 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(); } catch (ConfigurationException e) { + // TODO: set errors logger.info("Found " + configurationErrors.size() + " configuration errors"); - } + } + logger.info(configReader.document()); resetDataObjects(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java index 7534150..ea83a12 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java @@ -4,15 +4,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.Serializable; -import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; -import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.config.Configurable; - -public interface SearchClient extends Serializable { - - public void configure(Pz2Configurator configurator) throws ConfigurationException; +public interface SearchClient extends Configurable, Serializable { + public void setSearchCommand(Pazpar2Command command); public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException; public SearchClient cloneMe(); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java index 187b928..3ae05ec 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java @@ -1,7 +1,13 @@ package com.indexdata.pz2utils4jsf.pazpar2; +import static com.indexdata.pz2utils4jsf.utils.Utils.nl; + import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.enterprise.context.SessionScoped; import javax.enterprise.inject.Alternative; @@ -9,6 +15,8 @@ import javax.inject.Named; import org.apache.log4j.Logger; +import com.indexdata.masterkey.config.MissingMandatoryParameterException; +import com.indexdata.masterkey.config.ModuleConfigurationGetter; import com.indexdata.masterkey.pazpar2.client.ClientCommand; import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration; @@ -16,7 +24,8 @@ import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric; import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.Configuration; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.utils.Utils; @@ -26,14 +35,24 @@ public class StraightPz2Client implements SearchClient { private static final long serialVersionUID = 5414266730169982028L; private static Logger logger = Logger.getLogger(StraightPz2Client.class); private Pazpar2Client client = null; - private Pazpar2ClientConfiguration cfg = null; + private Pazpar2ClientConfiguration cfg = null; + public static final String MODULENAME = "pz2client"; + public static Map DEFAULTS = new HashMap(); + + static { + DEFAULTS.put("PROXY_MODE","1"); + DEFAULTS.put("SERIALIZE_REQUESTS", "false"); + DEFAULTS.put("STREAMBUFF_SIZE", "4096"); + DEFAULTS.put("PARSE_RESPONSES", "true"); + } public StraightPz2Client() {} - public void configure(Pz2Configurator configurator) throws ConfigurationException { - logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator)); + public void configure(ConfigurationReader configReader) throws ConfigurationException { + logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader)); try { - cfg = new Pazpar2ClientConfiguration(configurator.getConfig()); + Configuration config = configReader.getConfiguration(this); + cfg = new Pazpar2ClientConfiguration(new ConfigurationGetter(config)); } catch (ProxyErrorException pe) { logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe); @@ -50,12 +69,12 @@ public class StraightPz2Client implements SearchClient { throw new ConfigurationException("Pazpar2Client is null after configuration"); } } - + + @Override public void setSearchCommand(Pazpar2Command command) { ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); - client.setSearchCommand(clientCommand); - + client.setSearchCommand(clientCommand); } @Override @@ -73,4 +92,46 @@ public class StraightPz2Client implements SearchClient { clone.cfg = this.cfg; return clone; } + + @Override + public Map getDefaults() { + return DEFAULTS; + } + + @Override + public String getModuleName() { + return MODULENAME; + } + + class ConfigurationGetter implements ModuleConfigurationGetter { + Configuration config = null; + ConfigurationGetter(Configuration configuration) { + config = configuration; + } + @Override + public String get(String value) { + return config.get(value); + } + @Override + public String get(String value, String defaultValue) { + return config.get(value,defaultValue); + } + @Override + public String getMandatory(String name) + throws MissingMandatoryParameterException { + return config.getMandatory(name); + } + @Override + public String getConfigFilePath() { + return config.getConfigFilePath(); + } + } + + @Override + public List documentConfiguration() { + List doc = new ArrayList(); + doc.add(nl+ MODULENAME + " was configured to access Pazpar2 at : " + cfg.PAZPAR2_URL); + return new ArrayList(); + } + } -- 1.7.10.4