X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2Fsp%2FServiceProxyClient.java;h=8c2c2e259e647845fe832fee70052635a5ef9963;hb=801fbed2d559e224160d473e0860dd765354569f;hp=0602d4f8de35a4d97719f0a94ac3a0634847622e;hpb=aa4634e1a3c9eec2103b299dda20d916dcba8b20;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java index 0602d4f..8c2c2e2 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java @@ -2,16 +2,16 @@ package com.indexdata.pz2utils4jsf.pazpar2.sp; import static com.indexdata.pz2utils4jsf.utils.Utils.nl; +import java.io.BufferedReader; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; 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; @@ -19,10 +19,13 @@ 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.client.methods.HttpPost; 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.entity.ByteArrayEntity; +import org.apache.http.entity.FileEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; @@ -33,21 +36,25 @@ 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.commands.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; 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"; + public static final String SERVICE_PROXY_URL = "SERVICE_PROXY_URL"; + public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS"; private String serviceUrl = "undefined"; + private String[] initDocPaths = null; + private Configuration config = null; ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler(); private HttpClient client; @@ -64,8 +71,9 @@ public class ServiceProxyClient implements SearchClient { 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"); + config = configReader.getConfiguration(this); + serviceUrl = config.getMandatory(SERVICE_PROXY_URL); + this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); } catch (ConfigurationException c) { c.printStackTrace(); } catch (MissingMandatoryParameterException mmp) { @@ -73,14 +81,22 @@ public class ServiceProxyClient implements SearchClient { } } + private String[] getMultiProperty(String prop) { + if (prop != null) { + return prop.split(","); + } else { + return null; + } + } + 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"))); + Pazpar2Command auth = new Pazpar2Command("auth",null); + auth.setParametersInState(new CommandParameter("action","=","login"), + new CommandParameter("username","=",user.getProperty("name")), + new CommandParameter("password","=",user.getProperty("password"))); byte[] response = send(auth); String responseStr = new String(response,"UTF-8"); logger.info(responseStr); @@ -102,7 +118,7 @@ public class ServiceProxyClient implements SearchClient { public boolean checkAuthentication () { try { - Pazpar2Command check = new Pazpar2Command("auth"); + Pazpar2Command check = new Pazpar2Command("auth",null); check.setParameter(new CommandParameter("action","=","check")); byte[] response = send(check); logger.info(new String(response,"UTF-8")); @@ -145,7 +161,6 @@ public class ServiceProxyClient implements SearchClient { byte[] response = client.execute(httpget, handler); return response; } - public class ProxyPz2ResponseHandler implements ResponseHandler { private StatusLine statusLine = null; @@ -193,6 +208,7 @@ public class ServiceProxyClient implements SearchClient { ServiceProxyClient clone = new ServiceProxyClient(); clone.client = this.client; clone.serviceUrl = this.serviceUrl; + clone.initDocPaths = this.initDocPaths; return clone; } @@ -212,5 +228,50 @@ public class ServiceProxyClient implements SearchClient { doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl); return null; } - + + public byte[] postInitDoc (String filePath) throws IOException { + logger.info("Looking to post the file in : [" + filePath +"]"); + HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes"); + File initDoc = new File(filePath); + logger.info("Posting to SP: "); + if (logger.isDebugEnabled()) { + BufferedReader reader = new BufferedReader(new FileReader(initDoc)); + String line; + while ( (line = reader.readLine()) != null) { + System.out.println(line); + } + reader.close(); + } + post.setEntity(new FileEntity(initDoc)); + byte[] response = client.execute(post, handler); + logger.debug("Response on POST was: " + new String(response,"UTF-8")); + return response; + } + + public String[] getInitDocPaths () { + logger.debug("Get init doc paths "); + logger.debug("length: " + initDocPaths.length); + return initDocPaths; + } + + public byte[] postInitDoc(byte[] initDoc) throws IOException { + HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes"); + post.setEntity(new ByteArrayEntity(initDoc)); + byte[] response = client.execute(post, handler); + logger.debug("Response on POST was: " + new String(response,"UTF-8")); + return response; + } + + public void setServiceProxyUrl (String url) { + serviceUrl = url; + } + + public String getServiceProxyUrl () { + return serviceUrl; + } + + public Configuration getConfiguration () { + return config; + } + }