X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=9493e4993a708941c52a8afd8c1b00a18726e204;hb=bcb39bd9bde8071ac3a6741ce5d51ed9e763ba9c;hp=a43432ee11a3910128f5bd2f50f62c23fbbfb9a1;hpb=038cea23aa13e84430b55dda29fdea9fdf64828d;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java index a43432e..9493e49 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -38,7 +38,6 @@ import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; import com.indexdata.mkjsf.pazpar2.data.CommandError; -import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.mkjsf.utils.Utils; public class ServiceProxyClient implements SearchClient { @@ -48,7 +47,7 @@ public class ServiceProxyClient implements SearchClient { public static final String MODULENAME = "proxyclient"; public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS"; - private String selectedServiceUrl = ""; + private String serviceUrl = ""; private List initDocPaths = null; private Configuration config = null; @@ -70,7 +69,7 @@ public class ServiceProxyClient implements SearchClient { logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader)); try { config = configReader.getConfiguration(this); - selectedServiceUrl = config.get("SERVICE_PROXY_URL"); + serviceUrl = config.get("SERVICE_PROXY_URL"); this.initDocPaths = config.getMultiProperty(SP_INIT_DOC_PATHS,","); checkAuth = new AuthCommand(null); checkAuth.setParameterInState(new CommandParameter("action","=","check")); @@ -81,63 +80,11 @@ public class ServiceProxyClient implements SearchClient { c.printStackTrace(); } } - - - public boolean authenticate (ServiceProxyUser user) { - logger.info("Authenticating [" + user.getProperty("name") + "]"); - Pazpar2Command auth = new AuthCommand(null); - auth.setParametersInState(new CommandParameter("action","=","login"), - new CommandParameter("username","=",user.getProperty("name")), - new CommandParameter("password","=",user.getProperty("password"))); - ClientCommandResponse commandResponse = send(auth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.credentialsAuthenticationSucceeded(false); - return false; - } else { - user.credentialsAuthenticationSucceeded(true); - return true; - } - } - - public boolean checkAuthentication (ServiceProxyUser user) { - ClientCommandResponse commandResponse = send(checkAuth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.authenticationCheckFailed(); - return false; - } else { - return true; - } - } - - public boolean ipAuthenticate (ServiceProxyUser user) { - ClientCommandResponse commandResponse = send(ipAuth); - String responseStr = commandResponse.getResponseString(); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.ipAuthenticationSucceeded(false); - return false; - } else { - user.ipAuthenticationSucceeded(true); - return true; - } - } - + public boolean isAuthenticatingClient () { return true; } - - public boolean isAuthenticated (ServiceProxyUser user) { - if (user.getProperty("name") != null && user.getProperty("password") != null) { - return checkAuthentication(user); - } else { - return false; - } - } - + /** * Makes the request * @param request @@ -145,9 +92,9 @@ public class ServiceProxyClient implements SearchClient { * @throws ClientProtocolException * @throws IOException */ - private ClientCommandResponse send(Pazpar2Command command) { + public ClientCommandResponse send(Pazpar2Command command) { ClientCommandResponse commandResponse = null; - String url = selectedServiceUrl + "?" + command.getEncodedQueryString(); + String url = serviceUrl + "?" + command.getEncodedQueryString(); logger.info("Sending request "+url); HttpGet httpget = new HttpGet(url); byte[] response = null; @@ -205,7 +152,7 @@ public class ServiceProxyClient implements SearchClient { } @Override - public CommandResponse executeCommand(Pazpar2Command command) { + public HttpResponseWrapper executeCommand(Pazpar2Command command) { return send(command); } @@ -213,7 +160,7 @@ public class ServiceProxyClient implements SearchClient { logger.debug("Cloning Pz2Client"); ServiceProxyClient clone = new ServiceProxyClient(); clone.client = this.client; - clone.selectedServiceUrl = this.selectedServiceUrl; + clone.serviceUrl = this.serviceUrl; clone.initDocPaths = this.initDocPaths; return clone; } @@ -231,13 +178,13 @@ public class ServiceProxyClient implements SearchClient { @Override public List documentConfiguration () { List doc = new ArrayList(); - doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (selectedServiceUrl.length()>0 ? selectedServiceUrl : "[not defined yet]")); + doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (serviceUrl.length()>0 ? serviceUrl : "[not defined yet]")); return null; } public ClientCommandResponse postInitDoc (String filePath) throws IOException { logger.info("Looking to post the file in : [" + filePath +"]"); - HttpPost post = new HttpPost(selectedServiceUrl+"?command=init&includeDebug=yes"); + HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes"); File initDoc = new File(filePath); logger.info("Posting to SP: "); if (logger.isDebugEnabled()) { @@ -260,16 +207,33 @@ public class ServiceProxyClient implements SearchClient { return initDocPaths; } - public ClientCommandResponse postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException { - HttpPost post = new HttpPost(selectedServiceUrl+"?command=init" + (includeDebug? "&includeDebug=yes" : "")); + public HttpResponseWrapper postInitDoc(byte[] initDoc, boolean includeDebug) { + HttpPost post = new HttpPost(serviceUrl+"?command=init" + (includeDebug? "&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 new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType()); + ClientCommandResponse commandResponse = null; + byte[] response; + try { + response = client.execute(post, handler); + if (handler.getStatusCode()==200) { + commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType()); + } else { + logger.error("Service Proxy status code: " + handler.getStatusCode()); + commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.insertPazpar2ErrorXml("init", "Service Proxy error occurred", new String(response,"UTF-8")),"text/xml"); + } + } catch (ClientProtocolException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", "client protocol exception", e.getMessage()),"text/xml"); + } catch (IOException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", "IO", e.getMessage()),"text/xml"); + } + return commandResponse; } public void setServiceUrl (String url) { - selectedServiceUrl = url; + serviceUrl = url; } public Configuration getConfiguration () { @@ -278,12 +242,12 @@ public class ServiceProxyClient implements SearchClient { @Override public String getServiceUrl() { - return selectedServiceUrl; + return serviceUrl; } @Override public boolean hasServiceUrl() { - return selectedServiceUrl != null && selectedServiceUrl.length()>0; + return serviceUrl != null && serviceUrl.length()>0; } }