X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2Fsp%2FServiceProxyClient.java;h=0840a36b3e7f59d062e9b9df15d9b77f0ccfad3d;hb=5be18609f7d860a3759ef04278a7be397dd7175a;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..0840a36 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java @@ -2,12 +2,18 @@ package com.indexdata.pz2utils4jsf.pazpar2.sp; import static com.indexdata.pz2utils4jsf.utils.Utils.nl; +import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Scanner; import javax.enterprise.context.SessionScoped; import javax.inject.Named; @@ -19,10 +25,12 @@ 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.FileEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; @@ -47,7 +55,11 @@ 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 +76,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,6 +86,14 @@ 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") + "]"); @@ -145,7 +166,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 +213,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 +233,42 @@ 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: "); + Path path = Paths.get(filePath); + if (logger.isDebugEnabled()) { + try (Scanner scanner = new Scanner(path, "UTF-8")){ + while (scanner.hasNextLine()){ + System.out.println(scanner.nextLine()); + } + } + } + post.setEntity(new FileEntity(initDoc)); + byte[] response = client.execute(post, handler); + logger.info("Response on POST was: " + new String(response,"UTF-8")); + return response; + } + + public String[] getInitDocPaths () { + logger.info("Get init doc paths "); + logger.info("length: " + initDocPaths.length); + return initDocPaths; + } + + public void setServiceProxyUrl (String url) { + serviceUrl = url; + } + + public String getServiceProxyUrl () { + return serviceUrl; + } + + public Configuration getConfiguration () { + return config; + } + }