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