X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=e5d7307c08713f33013468f7fc883aec5deae0d1;hb=d5df0bd2f94a72c8af9bdc350e46b4c3af378ceb;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..e5d7307 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -34,13 +34,33 @@ import org.apache.log4j.Logger; import com.indexdata.mkjsf.config.Configuration; import com.indexdata.mkjsf.config.ConfigurationReader; import com.indexdata.mkjsf.errors.ConfigurationException; +import com.indexdata.mkjsf.errors.MissingConfigurationContextException; 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; +/** + * Search client handling Service Proxy requests. + * + *

Configuration

+ * + * Configuration name: proxyclient + * + *

When configuring the client using the Mk2Config scheme, this is the prefix to + * use in the .properties file. When using web.xml context parameters for configuration + * the configuration name has no effect.

+ * + *

ServiceProxyClient will acknowledge following configuration parameters: + * + *

+ * + * @author Niels Erik + * + */ public class ServiceProxyClient implements SearchClient { private static final long serialVersionUID = -4031644009579840277L; @@ -66,78 +86,28 @@ public class ServiceProxyClient implements SearchClient { } @Override - public void configure (ConfigurationReader configReader) { + public void configure (ConfigurationReader configReader) throws MissingConfigurationContextException { logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader)); try { 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; - } + } catch (MissingConfigurationContextException mcce) { + throw mcce; + } catch (ConfigurationException ce) { + logger.error("Failed to configure Service Proxy client"); + ce.printStackTrace(); + } } - + 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 +115,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 +123,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; } @@ -199,6 +185,9 @@ public class ServiceProxyClient implements SearchClient { return handler.getReasonPhrase(); } + /** + * Does nothing in Service Proxy context + */ @Override public void setSearchCommand(Pazpar2Command command) { // Do nothing, Service Proxy is handling this @@ -218,11 +207,17 @@ public class ServiceProxyClient implements SearchClient { return clone; } + /** + * Returns default configuration parameters for the client. + */ @Override public Map getDefaults() { return new HashMap(); } + /** + * Returns the configuration name of the client + */ @Override public String getModuleName() { return MODULENAME; @@ -260,8 +255,10 @@ public class ServiceProxyClient implements SearchClient { return initDocPaths; } - public HttpResponseWrapper postInitDoc(byte[] initDoc, boolean includeDebug) { - HttpPost post = new HttpPost(serviceUrl+"?command=init" + (includeDebug? "&includeDebug=yes" : "")); + public HttpResponseWrapper postInitDoc(byte[] initDoc, Pazpar2Command command) { + String requestParameters = command.getEncodedQueryString(); + logger.info("Initiating session with init doc and [" + requestParameters +"]"); + HttpPost post = new HttpPost(serviceUrl+"?" + requestParameters); post.setEntity(new ByteArrayEntity(initDoc)); ClientCommandResponse commandResponse = null; byte[] response; @@ -271,20 +268,23 @@ 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; } + /** + * Sets the URL of the Service Proxy that should service requests. + */ public void setServiceUrl (String url) { serviceUrl = url; } @@ -298,6 +298,9 @@ public class ServiceProxyClient implements SearchClient { return serviceUrl; } + /** + * Returns true if a Service Proxy URL was defined yet. + */ @Override public boolean hasServiceUrl() { return serviceUrl != null && serviceUrl.length()>0;