SP client almost functionable.
Also refactoring of configuration scheme - loosens tie to MasterKey
configuration scheme and to any particular configurable modules.
<dependency>\r
<groupId>org.apache.httpcomponents</groupId>\r
<artifactId>httpclient</artifactId>\r
- <version>4.0-beta1</version>\r
+ <version>4.2.3</version>\r
</dependency> \r
\r
<dependency>\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.config;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+\r
+public interface Configurable {\r
+\r
+ public void configure(ConfigurationReader reader) throws ConfigurationException;\r
+ public Map<String,String> getDefaults();\r
+ public String getModuleName();\r
+ public List<String> documentConfiguration(); \r
+ \r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.config;\r
+\r
+import java.io.Serializable;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+public class Configuration implements Serializable {\r
+\r
+ private static final long serialVersionUID = -6801241975338182197L;\r
+ private static Logger logger = Logger.getLogger(Configuration.class);\r
+ Map<String,String> properties = new HashMap<String,String>();\r
+ \r
+ public Configuration () {\r
+ logger.debug(Utils.objectId(this) + " being constructed with no argument"); \r
+ }\r
+ \r
+ public Configuration(Map<String,String> parameters) {\r
+ addAll(parameters);\r
+ }\r
+ \r
+ public void addAll(Map<String,String> parameters) {\r
+ for (String key : parameters.keySet()) {\r
+ properties.put(key, parameters.get(key));\r
+ } \r
+ }\r
+ \r
+ public void addAll(Map<String,String> defaults, Map<String,String> parameters) {\r
+ for (String key : defaults.keySet()) {\r
+ properties.put(key, defaults.get(key));\r
+ } \r
+ for (String key : parameters.keySet()) {\r
+ properties.put(key, parameters.get(key));\r
+ } \r
+ }\r
+ \r
+ public String get(String key) {\r
+ return properties.get(key); \r
+ }\r
+ \r
+ public void set(String key, String value) {\r
+ properties.put(key, value);\r
+ }\r
+ \r
+ public String get(String key, String defaultValue) {\r
+ if (properties.containsKey(key)) {\r
+ return properties.get(key);\r
+ } else {\r
+ return defaultValue;\r
+ }\r
+ }\r
+ \r
+ public String getMandatory(String key) throws MissingMandatoryParameterException {\r
+ if (properties.containsKey(key)) {\r
+ return properties.get(key);\r
+ } \r
+ throw new MissingMandatoryParameterException("Missing mandatory parameter: " + key); \r
+ }\r
+\r
+ public String getConfigFilePath() {
+ return get("configfilepath","nopathgiven");\r
+ }\r
+ \r
+ public Map<String,String> getConfigMap() {\r
+ return properties;\r
+ }\r
+ \r
+\r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.config;\r
+\r
+import java.io.Serializable;\r
+import java.util.List;\r
+\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+\r
+public interface ConfigurationReader extends Serializable {\r
+ \r
+ public Configuration getConfiguration(Configurable configurable) throws ConfigurationException;\r
+ \r
+ public List<String> document();\r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.config;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
+import javax.faces.context.ExternalContext;\r
+import javax.faces.context.FacesContext;\r
+import javax.inject.Named;\r
+import javax.servlet.ServletContext;\r
+import javax.servlet.http.HttpServletRequest;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.masterkey.config.MasterkeyConfiguration;\r
+import com.indexdata.masterkey.config.ModuleConfiguration;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
+@Named @SessionScoped @Alternative\r
+public class Mk2ConfigReader implements ConfigurationReader {\r
+\r
+ private static final long serialVersionUID = 8865086878660568870L;\r
+ private static Logger logger = Logger.getLogger(Mk2ConfigReader.class);\r
+ private Map<String,Configuration> configs = new HashMap<String,Configuration>(); \r
+ private Map<String,Configurable> configurables = new HashMap<String,Configurable>();\r
+\r
+ public Mk2ConfigReader () throws IOException {\r
+ logger.info(Utils.objectId(this) + " is instantiating Pazpar2 service configuration by MasterKey configuration scheme.");\r
+ }\r
+\r
+ @Override\r
+ public Configuration getConfiguration(Configurable configurable) throws ConfigurationException { \r
+ if (configs.get(configurable.getModuleName()) == null) {\r
+ Configuration config = readConfig(configurable);\r
+ configs.put(configurable.getModuleName(), config); \r
+ configurables.put(configurable.getModuleName(), configurable);\r
+ }\r
+ return configs.get(configurable.getModuleName()); \r
+ }\r
+ \r
+ private Configuration readConfig (Configurable configurable) throws ConfigurationException {\r
+ Configuration config = new Configuration();\r
+ ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();\r
+ ServletContext servletContext = (ServletContext) externalContext.getContext(); \r
+ MasterkeyConfiguration mkConfigContext;\r
+ try {\r
+ mkConfigContext = MasterkeyConfiguration.getInstance(servletContext,\r
+ "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName());\r
+ } catch (IOException e) {\r
+ throw new ConfigurationException(Mk2ConfigReader.class + " could not read configuration for '" + configurable.getModuleName() + "' using MasterKey configuration scheme: "+e.getMessage(),e);\r
+ } \r
+ try {\r
+ ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration(configurable.getModuleName()); \r
+ config.addAll(configurable.getDefaults(),moduleConfig.getConfigMap());\r
+ config.set("configpath", mkConfigContext.getConfigFileLocation().getConfigFilePath()); \r
+ } catch (IOException e) {\r
+ throw new ConfigurationException(Mk2ConfigReader.class + " could not read configuration for '"+ configurable.getModuleName() + "': "+e.getMessage(),e);\r
+ } \r
+ return config;\r
+ }\r
+ \r
+ public List<String> document() {\r
+ List<String> doc = new ArrayList<String>();\r
+ doc.add("Application properties as read by " + this.getClass());\r
+ for (String moduleName : configs.keySet()) {\r
+ doc.add(nl+"Module: " + moduleName);\r
+ Configurable module = configurables.get(moduleName);\r
+ Map<String,String> map = configs.get(moduleName).getConfigMap();\r
+ for (String key : map.keySet()) {\r
+ doc.add(nl+key+": "+ map.get(key) +\r
+ (module.getDefaults().containsKey(key) ? \r
+ (module.getDefaults().get(key).equals(map.get(key)) ? " [default]" : " [override]")\r
+ : "")); \r
+ }\r
+ }\r
+ return doc;\r
+ }\r
+ \r
+\r
+}\r
+++ /dev/null
-package com.indexdata.pz2utils4jsf.config;\r
-\r
-import java.io.IOException;\r
-import java.io.Serializable;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.inject.Named;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
-import com.indexdata.masterkey.config.ModuleConfiguration;\r
-import com.indexdata.masterkey.config.ModuleConfigurationGetter;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.utils.Utils;\r
-\r
-@Named @SessionScoped\r
-public class Pz2Config implements ModuleConfigurationGetter, Serializable {\r
-\r
- private static final long serialVersionUID = -6801241975338182197L;\r
- private static Logger logger = Logger.getLogger(Pz2Config.class);\r
- Map<String,String> properties = new HashMap<String,String>();\r
- ModuleConfiguration moduleConfig = null;\r
- \r
- public Pz2Config () {\r
- logger.debug(Utils.objectId(this) + " being constructed with no argument");\r
- setDefaults();\r
- }\r
- \r
- public Pz2Config (Map<String,String> parameters) { \r
- setDefaults();\r
- for (String key : parameters.keySet()) {\r
- properties.put(key, parameters.get(key));\r
- }\r
- }\r
- \r
- public Pz2Config (ModuleConfiguration moduleConfig) throws ConfigurationException {\r
- logger.debug(Utils.objectId(this) + " being constructed with moduleConfig argument.");\r
- this.moduleConfig = moduleConfig;\r
- try {\r
- for (String key : moduleConfig.getConfigMap().keySet()) {\r
- properties.put(key, moduleConfig.getConfigParameter(key));\r
- }\r
- } catch (IOException e) {\r
- throw new ConfigurationException("Could not instantiate Pazpar2 configuration: "+e.getMessage(),e);\r
- }\r
- }\r
- \r
- private void setDefaults () {\r
- properties.put("PROXY_MODE","1");\r
- properties.put("SERIALIZE_REQUESTS", "false");\r
- properties.put("STREAMBUFF_SIZE", "4096");\r
- properties.put("PARSE_RESPONSES", "true"); \r
- }\r
- \r
- @Override\r
- public String get(String key) {\r
- return properties.get(key); \r
- }\r
- \r
- public void set(String key, String value) {\r
- properties.put(key, value);\r
- }\r
- \r
- @Override\r
- public String get(String key, String defaultValue) {\r
- if (properties.containsKey(key)) {\r
- return properties.get(key);\r
- } else {\r
- return defaultValue;\r
- }\r
- }\r
-\r
- @Override\r
- public String getMandatory(String key) throws MissingMandatoryParameterException {\r
- if (properties.containsKey(key)) {\r
- return properties.get(key);\r
- } \r
- throw new MissingMandatoryParameterException("Missing mandatory parameter: " + key); \r
- }\r
-\r
- @Override\r
- public String getConfigFilePath() {
- return (moduleConfig != null ? moduleConfig.getConfigFilePath() : "nopath");\r
- }\r
- \r
- \r
-\r
-}\r
+++ /dev/null
-package com.indexdata.pz2utils4jsf.config;\r
-\r
-import java.io.Serializable;\r
-import java.util.List;\r
-\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-\r
-public interface Pz2Configurator extends Serializable {\r
- public Pz2Config getConfig() throws ConfigurationException;\r
- \r
- public List<String> document();\r
-}\r
+++ /dev/null
-package com.indexdata.pz2utils4jsf.config;\r
-\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.enterprise.inject.Alternative;\r
-import javax.faces.context.ExternalContext;\r
-import javax.faces.context.FacesContext;\r
-import javax.inject.Named;\r
-import javax.servlet.ServletContext;\r
-import javax.servlet.http.HttpServletRequest;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.masterkey.config.MasterkeyConfiguration;\r
-import com.indexdata.masterkey.config.ModuleConfiguration;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.utils.Utils;\r
-import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
-\r
-@Named @SessionScoped @Alternative\r
-public class Pz2ConfigureByMk2Config implements Pz2Configurator {\r
-\r
- private static final long serialVersionUID = 8865086878660568870L;\r
- private static Logger logger = Logger.getLogger(Pz2ConfigureByMk2Config.class);\r
- private Pz2Config pz2config = null;\r
- private String configFilePathAndName = "none";\r
-\r
- public Pz2ConfigureByMk2Config () throws IOException {\r
- logger.info(Utils.objectId(this) + " is instantiating Pazpar2 service configuration by MasterKey configuration scheme.");\r
- }\r
- \r
- @Override\r
- public Pz2Config getConfig() throws ConfigurationException {\r
- if (pz2config == null) {\r
- createConfig();\r
- }\r
- return pz2config;\r
- }\r
- \r
- private void createConfig () throws ConfigurationException {\r
- ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();\r
- ServletContext servletContext = (ServletContext) externalContext.getContext(); \r
- MasterkeyConfiguration mkConfigContext;\r
- try {\r
- mkConfigContext = MasterkeyConfiguration.getInstance(servletContext,\r
- "pazpar-application-jsf", ((HttpServletRequest) externalContext.getRequest()).getServerName());\r
- } catch (IOException e) {\r
- throw new ConfigurationException("Pz2ConfigureByMk2Config could not configure Pazpar2 client using MasterKey configuration scheme: "+e.getMessage(),e);\r
- }\r
- configFilePathAndName = mkConfigContext.getConfigFileLocation().getConfigFilePath(); \r
- try {\r
- ModuleConfiguration moduleConfig = mkConfigContext.getModuleConfiguration("pz2client");\r
- pz2config = new Pz2Config(moduleConfig);\r
- logger.info(document());\r
- } catch (IOException e) {\r
- throw new ConfigurationException("Pz2ConfigureByMk2Config could not get configuration for module 'pz2client': "+e.getMessage(),e);\r
- } \r
- }\r
- \r
- public List<String> document() {\r
- List<String> doc = new ArrayList<String>();\r
- doc.add(nl+"Attempted to configure service using the file " + configFilePathAndName);\r
- doc.add(nl+"-- Configured to access Pazpar2 at: " +pz2config.get("PAZPAR2_URL"));\r
- if (pz2config.get("PAZPAR2_SERVICE_XML") != null) {\r
- doc.add(nl+"-- Configured to use the service definition contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SERVICE_XML"));\r
- if (pz2config.get("PAZPAR2_SETTINGS_XML") != null) {\r
- doc.add(nl+"-- Configured to use the target settings contained in " + pz2config.getConfigFilePath() + "/" + pz2config.get("PAZPAR2_SETTINGS_XML"));\r
- } else {\r
- doc.add(nl+"-- Configured to use the server side target settings as defined in the service definition.");\r
- }\r
- } else if (pz2config.get("PAZPAR2_SERVICE_ID") != null) {\r
- doc.add(nl+"-- Configured to use the server side service definition identified by service id \""+pz2config.get("PAZPAR2_SERVICE_ID") + "\"");\r
- } else {\r
- doc.add(nl+"Error: Did not find service ID nor service definition XML file for setting up a pazpar2 service.");\r
- }\r
- return doc;\r
- }\r
- \r
-\r
-}\r
+++ /dev/null
-package com.indexdata.pz2utils4jsf.config;\r
-\r
-import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import javax.enterprise.context.SessionScoped;\r
-import javax.enterprise.inject.Alternative;\r
-import javax.faces.context.ExternalContext;\r
-import javax.faces.context.FacesContext;\r
-import javax.inject.Named;\r
-import javax.servlet.ServletContext;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-\r
-\r
-@Named @SessionScoped @Alternative\r
-public class Pz2ConfigureByWebXml implements Pz2Configurator {\r
-\r
- private static final long serialVersionUID = 144390224959311772L;\r
- private static Logger logger = Logger.getLogger(Pz2ConfigureByWebXml.class);\r
- private Pz2Config pz2config = null;\r
- private Map<String,String> parameters = new HashMap<String,String>(); \r
- \r
- public Pz2ConfigureByWebXml () {\r
- logger.info("Instantiating Pazpar2 service configuration by web.xml parameters");\r
- }\r
- \r
- @Override\r
- public Pz2Config getConfig() throws ConfigurationException {\r
- if (pz2config == null) {\r
- createConfig();\r
- }\r
- return pz2config;\r
- }\r
- \r
- private void createConfig () throws ConfigurationException {\r
- ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();\r
- ServletContext servletContext = (ServletContext) externalContext.getContext(); \r
- parameters.put("PAZPAR2_URL", servletContext.getInitParameter("PAZPAR2_URL"));\r
- if (parameters.get("PAZPAR2_URL")==null || parameters.get("PAZPAR2_URL").length()==0) { \r
- throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_URL'");\r
- }\r
- parameters.put("PAZPAR2_SERVICE_ID", servletContext.getInitParameter("PAZPAR2_SERVICE_ID"));\r
- if (parameters.get("PAZPAR2_SERVICE_ID")==null || parameters.get("PAZPAR2_SERVICE_ID").length()==0) { \r
- throw new ConfigurationException("Pz2ConfigureByWebXml could not find mandatory context-param 'PAZPAR2_SERVICE_ID'");\r
- }\r
- pz2config = new Pz2Config(parameters);\r
- }\r
- \r
- public List<String> document() {\r
- List<String> doc = new ArrayList<String>(); \r
- doc.add("Attempted to configure service using web.xml context-parameters ");\r
- doc.add(nl+"-- Configured to access Pazpar2 at [" +parameters.get("PAZPAR2_URL") + "]");\r
- doc.add(nl+"-- Configured to use the server side service definition identified by service id [" +parameters.get("PAZPAR2_SERVICE_ID") + "]");\r
- return doc; \r
- }\r
-}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.config;\r
+\r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Enumeration;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
+import javax.faces.context.ExternalContext;\r
+import javax.faces.context.FacesContext;\r
+import javax.inject.Named;\r
+import javax.servlet.ServletContext;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+\r
+@Named @SessionScoped @Alternative\r
+public class WebXmlConfigReader implements ConfigurationReader {\r
+\r
+ private static final long serialVersionUID = 144390224959311772L;\r
+ private static Logger logger = Logger.getLogger(WebXmlConfigReader.class);\r
+ private Configuration config = null;\r
+ private Map<String,String> parameters = new HashMap<String,String>(); \r
+ \r
+ public WebXmlConfigReader () {\r
+ logger.info("Instantiating Pazpar2 service configuration by web.xml parameters");\r
+ }\r
+ \r
+ public Configuration getConfiguration(Configurable configurable) throws ConfigurationException {\r
+ if (config == null) {\r
+ parameters.putAll(configurable.getDefaults());\r
+ parameters.putAll(readConfig());\r
+ config = new Configuration(parameters);\r
+ }\r
+ return config;\r
+ }\r
+ \r
+ private Map<String,String> readConfig () throws ConfigurationException {\r
+ Map<String,String> map = new HashMap<String,String>();\r
+ ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();\r
+ ServletContext servletContext = (ServletContext) externalContext.getContext(); \r
+ Enumeration<String> enumer = servletContext.getInitParameterNames();\r
+ while (enumer.hasMoreElements()) {\r
+ String name = enumer.nextElement();\r
+ map.put(name,servletContext.getInitParameter(name));\r
+ }\r
+ return map; \r
+ }\r
+ \r
+ public List<String> document() {\r
+ List<String> doc = new ArrayList<String>();\r
+ doc.add("Application properties as read by " + this.getClass());\r
+ for (String key : parameters.keySet()) {\r
+ doc.add(nl+key+": "+ parameters.get(key));\r
+ }\r
+ return doc;\r
+ }\r
+}\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
public class ErrorHelper implements Serializable {\r
\r
private static Logger logger = Logger.getLogger(ErrorHelper.class);\r
\r
- private Pz2Configurator configurator = null;\r
+ private ConfigurationReader configurator = null;\r
\r
- public ErrorHelper(Pz2Configurator configurator) {\r
+ public ErrorHelper(ConfigurationReader configurator) {\r
this.configurator = configurator;\r
}\r
\r
return ErrorCode.SKIP_SUGGESTIONS;\r
} else if (errmsg.contains("Missing mandatory parameter")) {\r
return ErrorCode.MISSING_MANDATORY_PROPERTY;\r
- } else if (errmsg.contains("Pz2ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { \r
+ } else if (errmsg.contains("ConfigureByMk2Config") && errmsg.contains("Init parameter") && (errmsg.contains("missing"))) { \r
return ErrorCode.MISSING_MK2_CONFIG_INIT_PARAMETER;\r
- } else if (appError.getMessage().contains("Pz2ConfigureByWebXml could not find mandatory context-param")) {\r
+ } else if (appError.getMessage().contains("WebXmlConfigReader could not find mandatory context-param")) {\r
return ErrorCode.MISSING_CONTEXT_PARAMETER;\r
}\r
return ErrorCode.NOT_RESOLVED;\r
switch (code) {\r
case MISSING_MK2_CONFIG_INIT_PARAMETER:\r
suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." +\r
- " Following init parameters must be present when using the MasterKey configuration scheme (Pz2ConfigureByMk2Config):" +\r
+ " Following init parameters must be present when using the MasterKey configuration scheme (ConfigureByMk2Config):" +\r
" MASTERKEY_ROOT_CONFIG_DIR (i.e. '/etc/masterkey'), MASTERKEY_COMPONENT_CONFIG_DIR (i.e. '/myapp'), " +\r
" MASTERKEY_CONFIG_FILE_NAME (i.e. 'myapp.properties'"); \r
break;\r
case MISSING_CONTEXT_PARAMETER:\r
suggestions.add("A mandatory init parameter (context-param) was not found in the deployment descriptor (web.xml)." +\r
- " Following init parameters must be present when using Pz2ConfigureByWebXml:" +\r
+ " Following init parameters must be present when using WebXmlConfigReader:" +\r
" PAZPAR2_URL, PAZPAR2_SERVICE_ID"); \r
break;\r
case MISSING_MANDATORY_PROPERTY:\r
package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
import java.io.ByteArrayOutputStream;\r
import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
\r
import javax.enterprise.context.SessionScoped;\r
import javax.enterprise.inject.Alternative;\r
import org.apache.http.client.HttpClient;\r
import org.apache.http.client.ResponseHandler;\r
import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.conn.ClientConnectionManager;\r
+import org.apache.http.conn.scheme.PlainSocketFactory;\r
+import org.apache.http.conn.scheme.Scheme;\r
+import org.apache.http.conn.scheme.SchemeRegistry;\r
import org.apache.http.impl.client.DefaultHttpClient;\r
+import org.apache.http.impl.conn.PoolingClientConnectionManager;\r
import org.apache.http.util.EntityUtils;\r
import org.apache.log4j.Logger;\r
\r
+import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.Configuration;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
@Named @SessionScoped @Alternative\r
\r
private static final long serialVersionUID = -4031644009579840277L;\r
private static Logger logger = Logger.getLogger(ProxyPz2Client.class);\r
+ public static final String MODULENAME = "proxyclient";\r
+ private String serviceUrl = "undefined";\r
\r
ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
- HttpClient client = new DefaultHttpClient(); \r
+ private HttpClient client;\r
\r
- public ProxyPz2Client(HttpClient client) {\r
+ public ProxyPz2Client () {\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
}\r
- \r
+ \r
@Override\r
- public void configure (Pz2Configurator configurator) {\r
- logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator));\r
-\r
+ public void configure (ConfigurationReader configReader) {\r
+ logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
+ try {\r
+ Configuration config = configReader.getConfiguration(this); \r
+ serviceUrl = config.getMandatory("SERVICE_PROXY_URL");\r
+ authenticate();\r
+ } catch (ConfigurationException c) {\r
+ // TODO Auto-generated catch block\r
+ c.printStackTrace();\r
+ } catch (MissingMandatoryParameterException mmp) {\r
+ mmp.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ public void authenticate () {\r
+ try {\r
+ Pazpar2Command auth = new Pazpar2Command("auth");\r
+ auth.setParameter(new CommandParameter("action","=","login"));\r
+ auth.setParameter(new CommandParameter("username","=","demo"));\r
+ auth.setParameter(new CommandParameter("password","=","demo"));\r
+ send(auth);\r
+ } catch (ClientProtocolException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ \r
}\r
\r
/**\r
* @throws IOException\r
*/\r
private String send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
- String url = command.getEncodedQueryString(); \r
- logger.debug("Sending request "+url); \r
+ String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
+ logger.info("Sending request "+url); \r
HttpGet httpget = new HttpGet(url); \r
byte[] response = client.execute(httpget, handler); \r
return new String(response);\r
public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
private StatusLine statusLine = null;\r
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
+ byte[] resp = null;\r
HttpEntity entity = response.getEntity(); \r
statusLine = response.getStatusLine();\r
if (entity != null) { \r
- return EntityUtils.toByteArray(entity);\r
- } else {\r
- return null;\r
- }\r
+ resp = EntityUtils.toByteArray(entity); \r
+ } \r
+ EntityUtils.consume(entity);\r
+ return resp;\r
}\r
public int getStatusCode() {\r
return statusLine.getStatusCode();\r
}\r
\r
public ProxyPz2Client cloneMe() {\r
- return this;\r
+ logger.debug("Cloning StraightPz2Client");\r
+ ProxyPz2Client clone = new ProxyPz2Client();\r
+ clone.client = this.client;\r
+ clone.serviceUrl = this.serviceUrl;\r
+ return clone;\r
+ }\r
+\r
+ @Override\r
+ public Map<String, String> getDefaults() { \r
+ return new HashMap<String,String>();\r
+ }\r
+\r
+ @Override\r
+ public String getModuleName() {\r
+ return MODULENAME;\r
+ }\r
+ \r
+ @Override\r
+ public List<String> documentConfiguration () {\r
+ List<String> doc = new ArrayList<String>();\r
+ doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl);\r
+ return null;\r
}\r
\r
}\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
private static Logger logger = Logger.getLogger(Pz2Bean.class);\r
\r
Pz2Session pz2; \r
- @Inject Pz2Configurator configurator;\r
- @Inject SearchClient searchClient;\r
+ @Inject ConfigurationReader configurator;\r
+ @Inject SearchClient searchClient;\r
\r
public Pz2Bean () {\r
logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]");\r
pz2 = new Pz2Session();\r
logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
+ Utils.objectId(configurator) + "] on session [" \r
- + Utils.objectId(pz2) + "]" );\r
+ + Utils.objectId(pz2) + "]" ); \r
pz2.init(searchClient,configurator);\r
}\r
\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); \r
}\r
\r
- public void init(SearchClient searchClient, Pz2Configurator configurator) {\r
+ public void init(SearchClient searchClient, ConfigurationReader configReader) {\r
configurationErrors = new ArrayList<ErrorInterface>();\r
- errorHelper = new ErrorHelper(configurator); \r
+ errorHelper = new ErrorHelper(configReader); \r
logger.debug(Utils.objectId(this) + " will configure search client for the session");\r
try {\r
- searchClient.configure(configurator);\r
- \r
- // The cloning is a hack: \r
+ searchClient.configure(configReader); \r
// At the time of writing this search client is injected using Weld. \r
// However, the client is used for asynchronously sending off requests\r
- // to the server AND propagation of context to threads is not supported.\r
- // Trying so will throw a WELD-001303 error. To avoid that, a context\r
- // free client is spawned from the context dependent one. \r
+ // to the server AND propagation of context to threads is currently \r
+ // not supported. Trying to do so throws a WELD-001303 error. \r
+ // To avoid that, a context free client is cloned from the context \r
+ // dependent one. \r
+ // If propagation to threads gets supported, the cloning can go. \r
this.searchClient = searchClient.cloneMe();\r
\r
} catch (ConfigurationException e) {\r
+ // TODO: set errors\r
logger.info("Found " + configurationErrors.size() + " configuration errors"); \r
- } \r
+ } \r
+ logger.info(configReader.document());\r
resetDataObjects();\r
}\r
\r
import java.io.IOException;\r
import java.io.Serializable;\r
\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.config.Configurable;\r
\r
-\r
-public interface SearchClient extends Serializable {\r
-\r
- public void configure(Pz2Configurator configurator) throws ConfigurationException;\r
+public interface SearchClient extends Configurable, Serializable {\r
+ \r
public void setSearchCommand(Pazpar2Command command);\r
public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException;\r
public SearchClient cloneMe();\r
package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
+import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
+\r
import java.io.ByteArrayOutputStream;\r
import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
\r
import javax.enterprise.context.SessionScoped;\r
import javax.enterprise.inject.Alternative;\r
\r
import org.apache.log4j.Logger;\r
\r
+import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
+import com.indexdata.masterkey.config.ModuleConfigurationGetter;\r
import com.indexdata.masterkey.pazpar2.client.ClientCommand;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
-import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.Configuration;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
private static final long serialVersionUID = 5414266730169982028L;\r
private static Logger logger = Logger.getLogger(StraightPz2Client.class);\r
private Pazpar2Client client = null;\r
- private Pazpar2ClientConfiguration cfg = null; \r
+ private Pazpar2ClientConfiguration cfg = null;\r
+ public static final String MODULENAME = "pz2client";\r
+ public static Map<String,String> DEFAULTS = new HashMap<String,String>();\r
+ \r
+ static { \r
+ DEFAULTS.put("PROXY_MODE","1");\r
+ DEFAULTS.put("SERIALIZE_REQUESTS", "false");\r
+ DEFAULTS.put("STREAMBUFF_SIZE", "4096");\r
+ DEFAULTS.put("PARSE_RESPONSES", "true"); \r
+ }\r
\r
public StraightPz2Client() {}\r
\r
- public void configure(Pz2Configurator configurator) throws ConfigurationException {\r
- logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator));\r
+ public void configure(ConfigurationReader configReader) throws ConfigurationException { \r
+ logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
try {\r
- cfg = new Pazpar2ClientConfiguration(configurator.getConfig());\r
+ Configuration config = configReader.getConfiguration(this);\r
+ cfg = new Pazpar2ClientConfiguration(new ConfigurationGetter(config));\r
} catch (ProxyErrorException pe) {\r
logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe);\r
throw new ConfigurationException("Pazpar2Client is null after configuration");\r
} \r
}\r
-\r
+ \r
+ \r
@Override\r
public void setSearchCommand(Pazpar2Command command) {\r
ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
- client.setSearchCommand(clientCommand);\r
- \r
+ client.setSearchCommand(clientCommand); \r
}\r
\r
@Override\r
clone.cfg = this.cfg;\r
return clone;\r
}\r
+\r
+ @Override\r
+ public Map<String, String> getDefaults() {\r
+ return DEFAULTS;\r
+ }\r
+\r
+ @Override\r
+ public String getModuleName() {\r
+ return MODULENAME;\r
+ }\r
+ \r
+ class ConfigurationGetter implements ModuleConfigurationGetter {\r
+ Configuration config = null;\r
+ ConfigurationGetter(Configuration configuration) {\r
+ config = configuration;\r
+ }\r
+ @Override\r
+ public String get(String value) {\r
+ return config.get(value);\r
+ }\r
+ @Override\r
+ public String get(String value, String defaultValue) {\r
+ return config.get(value,defaultValue);\r
+ }\r
+ @Override\r
+ public String getMandatory(String name)\r
+ throws MissingMandatoryParameterException {\r
+ return config.getMandatory(name);\r
+ }\r
+ @Override\r
+ public String getConfigFilePath() {\r
+ return config.getConfigFilePath();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public List<String> documentConfiguration() {\r
+ List<String> doc = new ArrayList<String>();\r
+ doc.add(nl+ MODULENAME + " was configured to access Pazpar2 at : " + cfg.PAZPAR2_URL); \r
+ return new ArrayList<String>();\r
+ }\r
+\r
}\r