X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=26e5c9a05ba3a6e978f8fb14e261847bb0eac50a;hb=b45b0c8165fa016246ac1ff0cf2f69a89013d356;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..26e5c9a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -34,13 +34,20 @@ 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.commands.sp.ServiceProxyCommand; 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. + * + * @author Niels Erik + * + */ public class ServiceProxyClient implements SearchClient { private static final long serialVersionUID = -4031644009579840277L; @@ -48,7 +55,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; @@ -66,78 +73,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); - 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 = 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,23 +102,39 @@ 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; 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,13 +172,16 @@ 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 } @Override - public CommandResponse executeCommand(Pazpar2Command command) { + public HttpResponseWrapper executeCommand(Pazpar2Command command) { return send(command); } @@ -213,16 +189,22 @@ 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; } + /** + * 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; @@ -231,13 +213,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 +242,38 @@ 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, 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)); - 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; } + /** + * Sets the URL of the Service Proxy that should service requests. + */ public void setServiceUrl (String url) { - selectedServiceUrl = url; + serviceUrl = url; } public Configuration getConfiguration () { @@ -278,12 +282,15 @@ public class ServiceProxyClient implements SearchClient { @Override public String getServiceUrl() { - return selectedServiceUrl; + return serviceUrl; } + /** + * Returns true if a Service Proxy URL was defined yet. + */ @Override public boolean hasServiceUrl() { - return selectedServiceUrl != null && selectedServiceUrl.length()>0; + return serviceUrl != null && serviceUrl.length()>0; } }