X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2Fsp%2FServiceProxyClient.java;h=8c2c2e259e647845fe832fee70052635a5ef9963;hb=801fbed2d559e224160d473e0860dd765354569f;hp=d9f89fc136484e5f1abffbd9805b2d3461193cef;hpb=a78346a10c75cd39bd0009517aebabc73871e2c3;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 d9f89fc..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,17 +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; @@ -25,6 +24,7 @@ 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; @@ -36,15 +36,15 @@ 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; @@ -54,6 +54,7 @@ public class ServiceProxyClient implements SearchClient { 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; @@ -70,7 +71,7 @@ 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); + config = configReader.getConfiguration(this); serviceUrl = config.getMandatory(SERVICE_PROXY_URL); this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); } catch (ConfigurationException c) { @@ -92,10 +93,10 @@ public class ServiceProxyClient implements SearchClient { 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); @@ -117,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")); @@ -160,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; @@ -233,18 +233,35 @@ public class ServiceProxyClient implements SearchClient { 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.info("Response on POST was: " + new String(response,"UTF-8")); + logger.debug("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); + 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; } @@ -252,5 +269,9 @@ public class ServiceProxyClient implements SearchClient { public String getServiceProxyUrl () { return serviceUrl; } - + + public Configuration getConfiguration () { + return config; + } + }