X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=aa54403aad6b8907e31717c5e645ecf47b7c4b91;hb=31a027596723261b413d69c74428b176def3627f;hp=79d61530d620436aafdc48a3f9c1048abf789781;hpb=16f342276da7b336c96e285005db5c1401f2a6c6;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 79d6153..aa54403 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 { @@ -72,72 +71,20 @@ public class ServiceProxyClient implements SearchClient { config = configReader.getConfiguration(this); serviceUrl = config.get("SERVICE_PROXY_URL"); this.initDocPaths = config.getMultiProperty(SP_INIT_DOC_PATHS,","); - checkAuth = new AuthCommand(null); + checkAuth = new AuthCommand(); checkAuth.setParameterInState(new CommandParameter("action","=","check")); - ipAuth = new AuthCommand(null); + ipAuth = new AuthCommand(); ipAuth.setParameterInState(new CommandParameter("action","=","ipauth")); } catch (ConfigurationException c) { // TODO: 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.trace("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; } @@ -271,16 +234,16 @@ public class ServiceProxyClient implements SearchClient { 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"); + 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", "client protocol exception", e.getMessage()),"text/xml"); + 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", "IO", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO exception", e.getMessage(),e.getStackTrace().toString()),"text/xml"); } return commandResponse; }