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=0840a36b3e7f59d062e9b9df15d9b77f0ccfad3d;hpb=5be18609f7d860a3759ef04278a7be397dd7175a;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 0840a36..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,21 +2,15 @@ package com.indexdata.pz2utils4jsf.pazpar2.sp; import static com.indexdata.pz2utils4jsf.utils.Utils.nl; -import java.io.BufferedWriter; +import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileWriter; +import java.io.FileReader; 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; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -30,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; @@ -41,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; @@ -98,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); @@ -123,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")); @@ -239,26 +234,34 @@ public class ServiceProxyClient implements SearchClient { 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()); - } - } + 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; }