Aligns naming. Moves most SP classes to sp package.
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 21 Mar 2013 00:29:02 +0000 (20:29 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 21 Mar 2013 00:29:02 +0000 (20:29 -0400)
12 files changed:
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java [deleted file]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2ClientCommandResponse.java [deleted file]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2CommandResponse.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java [deleted file]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2CommandResponse.java [deleted file]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClientCommandResponse.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyInterface.java [new file with mode: 0644]
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java [new file with mode: 0644]

diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2Client.java
deleted file mode 100644 (file)
index e6e96bc..0000000
+++ /dev/null
@@ -1,212 +0,0 @@
-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.inject.Named;\r
-\r
-import org.apache.http.HttpEntity;\r
-import org.apache.http.HttpResponse;\r
-import org.apache.http.StatusLine;\r
-import org.apache.http.client.ClientProtocolException;\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.Configuration;\r
-import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
-import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
-import com.indexdata.pz2utils4jsf.utils.Utils;\r
-\r
-@Named @SessionScoped \r
-public class ProxyPz2Client implements SearchClient {\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
-  private HttpClient client;\r
-  private ServiceProxyUser user;\r
-\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
-  @Override\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
-    } catch (ConfigurationException c) {\r
-      c.printStackTrace();\r
-    } catch (MissingMandatoryParameterException mmp) {\r
-      mmp.printStackTrace();\r
-    }    \r
-  }\r
-  \r
-  public boolean authenticate (AuthenticationEntity user) {\r
-    try {      \r
-      logger.info("Authenticating [" + user.getProperty("name") + "]");\r
-      this.user = (ServiceProxyUser) user;\r
-      Pazpar2Command auth = new Pazpar2Command("auth");\r
-      auth.setParameter(new CommandParameter("action","=","login"));\r
-      auth.setParameter(new CommandParameter("username","=",user.getProperty("name")));\r
-      auth.setParameter(new CommandParameter("password","=",user.getProperty("password")));\r
-      byte[] response = send(auth);\r
-      String responseStr = new String(response,"UTF-8");\r
-      logger.info(responseStr);      \r
-      if (responseStr.contains("FAIL")) {\r
-        return false;\r
-      } else {\r
-        return true;\r
-      }      \r
-    } catch (ClientProtocolException e) {\r
-      // TODO Auto-generated catch block\r
-      e.printStackTrace();\r
-      return false;\r
-    } catch (IOException e) {\r
-      // TODO Auto-generated catch block\r
-      e.printStackTrace();\r
-      return false;\r
-    }        \r
-  }\r
-  \r
-  public boolean checkAuthentication () {\r
-    try {\r
-      Pazpar2Command check = new Pazpar2Command("auth");\r
-      check.setParameter(new CommandParameter("action","=","check"));\r
-      byte[] response = send(check);\r
-      logger.info(new String(response,"UTF-8"));\r
-    } catch (ClientProtocolException e) {\r
-      // TODO Auto-generated catch block\r
-      e.printStackTrace();\r
-      return false;\r
-    } catch (IOException e) {\r
-      // TODO Auto-generated catch block\r
-      e.printStackTrace();\r
-      return false;\r
-    }    \r
-    return true;\r
-    \r
-  }\r
-  \r
-  public boolean isAuthenticatingClient () {\r
-    return true;\r
-  }\r
-  \r
-  public boolean isAuthenticated () {\r
-    if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
-      return checkAuthentication();\r
-    } else {\r
-      return false;\r
-    }\r
-  }\r
-  \r
-  /**\r
-   * Makes the request\r
-   * @param request\r
-   * @return HTTP response as a String\r
-   * @throws ClientProtocolException\r
-   * @throws IOException\r
-   */\r
-  private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\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 response;\r
-  }\r
-\r
-  \r
-  public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
-    private StatusLine statusLine = null;\r
-    public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
-      byte[] resp = null;\r
-      HttpEntity entity = response.getEntity();      \r
-      statusLine = response.getStatusLine();\r
-      if (entity != null) {        \r
-        resp = EntityUtils.toByteArray(entity);        \r
-      } \r
-      EntityUtils.consume(entity);\r
-      return resp;\r
-    }\r
-    public int getStatusCode() {\r
-      return statusLine.getStatusCode();\r
-    }    \r
-    public String getReasonPhrase() {\r
-      return statusLine.getReasonPhrase();\r
-    }\r
-  }\r
-\r
-  public int getStatusCode () {\r
-    return handler.getStatusCode();\r
-  }\r
-  \r
-  public String getReasonPhrase() {\r
-    return handler.getReasonPhrase();\r
-  }\r
-\r
-  @Override\r
-  public void setSearchCommand(Pazpar2Command command) {\r
-    // Do nothing, Service Proxy is handling this    \r
-  }\r
-\r
-  @Override\r
-  public CommandResponse executeCommand(Pazpar2Command command,\r
-      ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
-    byte[] response = send(command);\r
-    baos.write(response);\r
-    return new ProxyPz2ClientCommandResponse(getStatusCode(), new String(response,"UTF-8"));    \r
-  }\r
-\r
-  public ProxyPz2Client cloneMe() {\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
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2ClientCommandResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ProxyPz2ClientCommandResponse.java
deleted file mode 100644 (file)
index 67cbe45..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-package com.indexdata.pz2utils4jsf.pazpar2;\r
-\r
-public class ProxyPz2ClientCommandResponse implements CommandResponse {\r
-\r
-  private int statusCode = 0;\r
-  private String content = null;\r
-  \r
-  public ProxyPz2ClientCommandResponse(int statusCode, String content) {\r
-    this.statusCode = statusCode;\r
-    this.content = content;\r
-  }\r
-\r
-  @Override\r
-  public int getStatusCode() {\r
-    return statusCode;\r
-  }\r
-\r
-  @Override\r
-  public String getContentType() {\r
-    return "text/xml;charset=UTF-8";    \r
-  }\r
-\r
-  @Override\r
-  public String getResponseString() {\r
-    return content;\r
-  }\r
-\r
-}\r
index f908d80..a8d14cc 100644 (file)
@@ -40,7 +40,7 @@ public class Pz2Bean implements Pz2Interface, Serializable {
   public void initiatePz2Session() {\r
     logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next.");\r
     pz2 = new Pz2Session();\r
-    searchClient = new StraightPz2Client();\r
+    searchClient = new Pz2Client();\r
     logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
                           + Utils.objectId(configurator) + "] on session [" \r
                           + Utils.objectId(pz2) + "]" );    \r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java
new file mode 100644 (file)
index 0000000..bc10fab
--- /dev/null
@@ -0,0 +1,148 @@
+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.inject.Named;\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.Pazpar2ClientGeneric;\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.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 \r
+public class Pz2Client implements SearchClient {\r
+\r
+  private static final long serialVersionUID = 5414266730169982028L;\r
+  private static Logger logger = Logger.getLogger(Pz2Client.class);\r
+  private Pazpar2Client client = 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 Pz2Client() {}\r
+  \r
+  @Override\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
+      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 Pz2Client:  "+ pe.getMessage(),pe);\r
+    } \r
+    if (cfg != null) {\r
+      try {\r
+        client = new Pazpar2ClientGeneric(cfg);  \r
+      } catch (ProxyErrorException pe) {\r
+        logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
+        throw new ConfigurationException("Could not configure Pz2Client:  "+ pe.getMessage(),pe);\r
+      }\r
+    } else {\r
+      logger.error("There was a problem creating Pz2Client. Client is null after configuration.");\r
+      throw new ConfigurationException("Pazpar2Client is null after configuration");\r
+    } \r
+  }\r
+  \r
+  public boolean isAuthenticatingClient () {\r
+    return false;\r
+  }\r
+  \r
+  public boolean isAuthenticated() {\r
+    return false;\r
+  }\r
+  \r
+  public boolean authenticate() {\r
+    throw new UnsupportedOperationException("No authentication mechanism for straight pazpar2 client");\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
+\r
+  @Override\r
+  public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
+       throws Pazpar2ErrorException, IOException {\r
+    ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+    Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
+    return new Pz2CommandResponse(pz2HttpResponse,baos);\r
+  }\r
+\r
+  public Pz2Client cloneMe() {\r
+    logger.debug("Cloning Pz2Client");\r
+    Pz2Client clone = new Pz2Client();\r
+    clone.client = this.client;\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
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2CommandResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2CommandResponse.java
new file mode 100644 (file)
index 0000000..0c6ebad
--- /dev/null
@@ -0,0 +1,39 @@
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.UnsupportedEncodingException;\r
+\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
+\r
+public class Pz2CommandResponse implements CommandResponse {\r
+  \r
+  private Pazpar2HttpResponse pz2httpResponse = null;\r
+  private ByteArrayOutputStream content = null;\r
+  \r
+  public Pz2CommandResponse(Pazpar2HttpResponse pz2response, ByteArrayOutputStream content) {\r
+    pz2httpResponse = pz2response;\r
+    this.content = content;\r
+  }\r
+\r
+  @Override\r
+  public int getStatusCode() {    \r
+    return pz2httpResponse.getStatusCode();\r
+  }\r
+\r
+  @Override\r
+  public String getContentType() {\r
+    return pz2httpResponse.getContentType();\r
+  }\r
+\r
+  @Override\r
+  public String getResponseString() {\r
+    try {\r
+      return content.toString("UTF-8");\r
+    } catch (UnsupportedEncodingException e) {      \r
+      e.printStackTrace();\r
+      return null;\r
+    }\r
+  }\r
+\r
+\r
+}\r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java
new file mode 100644 (file)
index 0000000..473d332
--- /dev/null
@@ -0,0 +1,48 @@
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import javax.annotation.PostConstruct;\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
+import javax.inject.Inject;\r
+import javax.inject.Named;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.ServiceProxyClient;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.ServiceProxyInterface;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.ServiceProxySession;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+@Named("pz2") @SessionScoped @Alternative\r
+public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface {\r
+    \r
+  private static final long serialVersionUID = 4221824985678758225L;\r
+  private static Logger logger = Logger.getLogger(Pz2ProxyBean.class);\r
+    \r
+  @Inject ConfigurationReader configurator;\r
+  @Inject ServiceProxyUser user;\r
+  \r
+  public Pz2ProxyBean() {\r
+  }\r
+  \r
+  @PostConstruct\r
+  public void initiatePz2Session() {\r
+    logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next.");\r
+    pz2 = new ServiceProxySession();\r
+    searchClient = new ServiceProxyClient();\r
+    logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
+                          + Utils.objectId(configurator) + "] on session [" \r
+                          + Utils.objectId(pz2) + "]" );    \r
+    pz2.init(searchClient,configurator);\r
+  }\r
+\r
+\r
+  public String login(String navigateTo) {\r
+    logger.info("doing login");\r
+    ((ServiceProxySession) pz2).setUser(user);\r
+    return ((ServiceProxySession)pz2).login(navigateTo);\r
+  }\r
+\r
+}\r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2Client.java
deleted file mode 100644 (file)
index c7b0515..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-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.inject.Named;\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.Pazpar2ClientGeneric;\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.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 \r
-public class StraightPz2Client implements SearchClient {\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
-  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
-  @Override\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
-      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
-    } \r
-    if (cfg != null) {\r
-      try {\r
-        client = new Pazpar2ClientGeneric(cfg);  \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
-      }\r
-    } else {\r
-      logger.error("There was a problem creating StraightPz2Client. Client is null after configuration.");\r
-      throw new ConfigurationException("Pazpar2Client is null after configuration");\r
-    } \r
-  }\r
-  \r
-  public boolean isAuthenticatingClient () {\r
-    return false;\r
-  }\r
-  \r
-  public boolean isAuthenticated() {\r
-    return false;\r
-  }\r
-  \r
-  public boolean authenticate() {\r
-    throw new UnsupportedOperationException("No authentication mechanism for straight pazpar2 client");\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
-\r
-  @Override\r
-  public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
-       throws Pazpar2ErrorException, IOException {\r
-    ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
-    Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
-    return new StraightPz2CommandResponse(pz2HttpResponse,baos);\r
-  }\r
-\r
-  public StraightPz2Client cloneMe() {\r
-    logger.debug("Cloning StraightPz2Client");\r
-    StraightPz2Client clone = new StraightPz2Client();\r
-    clone.client = this.client;\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
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2CommandResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/StraightPz2CommandResponse.java
deleted file mode 100644 (file)
index d744e7b..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.indexdata.pz2utils4jsf.pazpar2;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.UnsupportedEncodingException;\r
-\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
-\r
-public class StraightPz2CommandResponse implements CommandResponse {\r
-  \r
-  private Pazpar2HttpResponse pz2httpResponse = null;\r
-  private ByteArrayOutputStream content = null;\r
-  \r
-  public StraightPz2CommandResponse(Pazpar2HttpResponse pz2response, ByteArrayOutputStream content) {\r
-    pz2httpResponse = pz2response;\r
-    this.content = content;\r
-  }\r
-\r
-  @Override\r
-  public int getStatusCode() {    \r
-    return pz2httpResponse.getStatusCode();\r
-  }\r
-\r
-  @Override\r
-  public String getContentType() {\r
-    return pz2httpResponse.getContentType();\r
-  }\r
-\r
-  @Override\r
-  public String getResponseString() {\r
-    try {\r
-      return content.toString("UTF-8");\r
-    } catch (UnsupportedEncodingException e) {      \r
-      e.printStackTrace();\r
-      return null;\r
-    }\r
-  }\r
-\r
-\r
-}\r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java
new file mode 100644 (file)
index 0000000..0602d4f
--- /dev/null
@@ -0,0 +1,216 @@
+package com.indexdata.pz2utils4jsf.pazpar2.sp;\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.inject.Named;\r
+\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.StatusLine;\r
+import org.apache.http.client.ClientProtocolException;\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.Configuration;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter;\r
+import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
+import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;\r
+import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+@Named @SessionScoped \r
+public class ServiceProxyClient implements SearchClient {\r
+    \r
+  private static final long serialVersionUID = -4031644009579840277L;\r
+  private static Logger logger = Logger.getLogger(ServiceProxyClient.class);\r
+  public static final String MODULENAME = "proxyclient";\r
+  private String serviceUrl = "undefined";\r
+  \r
+  ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
+  private HttpClient client;\r
+  private ServiceProxyUser user;\r
+\r
+  public ServiceProxyClient () {\r
+    SchemeRegistry schemeRegistry = new SchemeRegistry();\r
+    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));\r
+    ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);\r
+    client = new DefaultHttpClient(cm);    \r
+  }\r
+    \r
+  @Override\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
+    } catch (ConfigurationException c) {\r
+      c.printStackTrace();\r
+    } catch (MissingMandatoryParameterException mmp) {\r
+      mmp.printStackTrace();\r
+    }    \r
+  }\r
+  \r
+  public boolean authenticate (AuthenticationEntity user) {\r
+    try {      \r
+      logger.info("Authenticating [" + user.getProperty("name") + "]");\r
+      this.user = (ServiceProxyUser) user;\r
+      Pazpar2Command auth = new Pazpar2Command("auth");\r
+      auth.setParameter(new CommandParameter("action","=","login"));\r
+      auth.setParameter(new CommandParameter("username","=",user.getProperty("name")));\r
+      auth.setParameter(new CommandParameter("password","=",user.getProperty("password")));\r
+      byte[] response = send(auth);\r
+      String responseStr = new String(response,"UTF-8");\r
+      logger.info(responseStr);      \r
+      if (responseStr.contains("FAIL")) {\r
+        return false;\r
+      } else {\r
+        return true;\r
+      }      \r
+    } catch (ClientProtocolException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    } catch (IOException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    }        \r
+  }\r
+  \r
+  public boolean checkAuthentication () {\r
+    try {\r
+      Pazpar2Command check = new Pazpar2Command("auth");\r
+      check.setParameter(new CommandParameter("action","=","check"));\r
+      byte[] response = send(check);\r
+      logger.info(new String(response,"UTF-8"));\r
+    } catch (ClientProtocolException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    } catch (IOException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+      return false;\r
+    }    \r
+    return true;\r
+    \r
+  }\r
+  \r
+  public boolean isAuthenticatingClient () {\r
+    return true;\r
+  }\r
+  \r
+  public boolean isAuthenticated () {\r
+    if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
+      return checkAuthentication();\r
+    } else {\r
+      return false;\r
+    }\r
+  }\r
+  \r
+  /**\r
+   * Makes the request\r
+   * @param request\r
+   * @return HTTP response as a String\r
+   * @throws ClientProtocolException\r
+   * @throws IOException\r
+   */\r
+  private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\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 response;\r
+  }\r
+\r
+  \r
+  public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
+    private StatusLine statusLine = null;\r
+    public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
+      byte[] resp = null;\r
+      HttpEntity entity = response.getEntity();      \r
+      statusLine = response.getStatusLine();\r
+      if (entity != null) {        \r
+        resp = EntityUtils.toByteArray(entity);        \r
+      } \r
+      EntityUtils.consume(entity);\r
+      return resp;\r
+    }\r
+    public int getStatusCode() {\r
+      return statusLine.getStatusCode();\r
+    }    \r
+    public String getReasonPhrase() {\r
+      return statusLine.getReasonPhrase();\r
+    }\r
+  }\r
+\r
+  public int getStatusCode () {\r
+    return handler.getStatusCode();\r
+  }\r
+  \r
+  public String getReasonPhrase() {\r
+    return handler.getReasonPhrase();\r
+  }\r
+\r
+  @Override\r
+  public void setSearchCommand(Pazpar2Command command) {\r
+    // Do nothing, Service Proxy is handling this    \r
+  }\r
+\r
+  @Override\r
+  public CommandResponse executeCommand(Pazpar2Command command,\r
+      ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
+    byte[] response = send(command);\r
+    baos.write(response);\r
+    return new ServiceProxyClientCommandResponse(getStatusCode(), new String(response,"UTF-8"));    \r
+  }\r
+\r
+  public ServiceProxyClient cloneMe() {\r
+    logger.debug("Cloning Pz2Client");\r
+    ServiceProxyClient clone = new ServiceProxyClient();\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
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClientCommandResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClientCommandResponse.java
new file mode 100644 (file)
index 0000000..ef0c41d
--- /dev/null
@@ -0,0 +1,30 @@
+package com.indexdata.pz2utils4jsf.pazpar2.sp;\r
+\r
+import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
+\r
+public class ServiceProxyClientCommandResponse implements CommandResponse {\r
+\r
+  private int statusCode = 0;\r
+  private String content = null;\r
+  \r
+  public ServiceProxyClientCommandResponse(int statusCode, String content) {\r
+    this.statusCode = statusCode;\r
+    this.content = content;\r
+  }\r
+\r
+  @Override\r
+  public int getStatusCode() {\r
+    return statusCode;\r
+  }\r
+\r
+  @Override\r
+  public String getContentType() {\r
+    return "text/xml;charset=UTF-8";    \r
+  }\r
+\r
+  @Override\r
+  public String getResponseString() {\r
+    return content;\r
+  }\r
+\r
+}\r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyInterface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyInterface.java
new file mode 100644 (file)
index 0000000..5f4df13
--- /dev/null
@@ -0,0 +1,8 @@
+package com.indexdata.pz2utils4jsf.pazpar2.sp;\r
+\r
+import com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface;\r
+\r
+public interface ServiceProxyInterface extends Pz2Interface {\r
+\r
+  public String login(String navigateTo);\r
+}\r
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java
new file mode 100644 (file)
index 0000000..ce0f860
--- /dev/null
@@ -0,0 +1,33 @@
+package com.indexdata.pz2utils4jsf.pazpar2.sp;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session;\r
+import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+public class ServiceProxySession extends Pz2Session implements ServiceProxyInterface {\r
+\r
+  private ServiceProxyUser user; \r
+  private static final long serialVersionUID = -5770410029361522854L;\r
+  private static Logger logger = Logger.getLogger(ServiceProxySession.class);\r
+  \r
+  public ServiceProxySession() {\r
+    logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]");\r
+  }\r
+  \r
+  public void setUser(ServiceProxyUser user) {\r
+    this.user = user;\r
+  }\r
+\r
+  @Override\r
+  public String login(String navigateTo) {\r
+    if (((ServiceProxyClient)searchClient).authenticate(user)) {\r
+      return navigateTo;\r
+    } else {\r
+      return null;\r
+    }      \r
+  }\r
+  \r
+\r
+}\r