X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=2db45586c21fa76071465c92d50acea8ef846e38;hb=3d72901f9f5e443837bf67603024de8c7d06a68e;hp=ca76a6a213a3c5c0a83223c21bb3df3ca25aadf4;hpb=752d17396ab3de88156763d4cda636e2eaa44086;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 ca76a6a..2db4558 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 { @@ -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,7 +92,7 @@ public class ServiceProxyClient implements SearchClient { * @throws ClientProtocolException * @throws IOException */ - private ClientCommandResponse send(Pazpar2Command command) { + public ClientCommandResponse send(Pazpar2Command command) { ClientCommandResponse commandResponse = null; String url = serviceUrl + "?" + command.getEncodedQueryString(); logger.info("Sending request "+url); @@ -153,15 +100,31 @@ public class ServiceProxyClient implements SearchClient { byte[] response = null; try { response = client.execute(httpget, handler); - if (handler.getStatusCode()==200) { + if (handler.getStatusCode()==200 && (handler.getContentType().contains("xml") || handler.getContentType().contains("octet-stream"))) { + logger.debug("Creating command response holding content of type " + handler.getContentType()); commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType()); } else { logger.error("Service Proxy status code: " + handler.getStatusCode()); - commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Service Proxy error occurred", new String(response,"UTF-8")),"text/xml"); + String errorXml = ""; + if (handler.getContentType().contains("xml")) { + errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8")); + } else { + if (handler.getContentType().contains("html")) { + String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>",""); + if (htmlStrippedOfTags.toLowerCase().contains("domain")) { + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML. It contains the word domain suggesting that the service address was not found.", htmlStrippedOfTags); + } else { + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML", htmlStrippedOfTags); + } + } else { + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy: "+handler.getContentType(), "Could not process non-XML response from Service Proxy", new String(response,"UTF-8")); + } + } + commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType()); } } catch (Exception e) { e.printStackTrace(); - commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : "")),"text/xml"); + commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : ""), e.getStackTrace().toString()),handler.getContentType()); } return commandResponse; } @@ -260,12 +223,29 @@ public class ServiceProxyClient implements SearchClient { return initDocPaths; } - public ClientCommandResponse postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException { + 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.insertErrorXml("init", String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8")),"text/xml"); + } + } catch (ClientProtocolException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "Client protocol exception", e.getMessage(), e.getStackTrace().toString()),"text/xml"); + } catch (IOException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO exception", e.getMessage(),e.getStackTrace().toString()),"text/xml"); + } + return commandResponse; } public void setServiceUrl (String url) {