X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FServiceProxyClient.java;h=e5d7307c08713f33013468f7fc883aec5deae0d1;hb=dff9a63520eeb6c4d6fd6b5e286e8f6a06c5608a;hp=152ee33475067678a4db954046f2f6d91398537e;hpb=6da388f43fedda3e61e630f826244608d2da6301;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 152ee33..e5d7307 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyClient.java @@ -34,12 +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.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; @@ -65,20 +86,22 @@ 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(); - } + } catch (MissingConfigurationContextException mcce) { + throw mcce; + } catch (ConfigurationException ce) { + logger.error("Failed to configure Service Proxy client"); + ce.printStackTrace(); + } } public boolean isAuthenticatingClient () { @@ -100,13 +123,14 @@ public class ServiceProxyClient implements SearchClient { byte[] response = null; try { response = client.execute(httpget, handler); - if (handler.getStatusCode()==200 && handler.getContentType().contains("xml")) { + 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()); 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")); + 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("\\<[^>]*>",""); @@ -117,9 +141,9 @@ public class ServiceProxyClient implements SearchClient { } } 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()); + } } + commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType()); } } catch (Exception e) { e.printStackTrace(); @@ -161,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 @@ -180,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; @@ -222,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; @@ -247,6 +282,9 @@ public class ServiceProxyClient implements SearchClient { return commandResponse; } + /** + * Sets the URL of the Service Proxy that should service requests. + */ public void setServiceUrl (String url) { serviceUrl = url; } @@ -260,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;