X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FPz2Client.java;h=9e02137d14a54bfa19860f16218afe8ca2e2ed0c;hb=1ab622be3bcde018e6b7816d4af8b79f80159630;hp=3656828477e93187c6d02d67fbd61585b411532a;hpb=7689ef2cae342cd80a0e504b2d68bde4f5348850;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java index 3656828..9e02137 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java @@ -27,6 +27,35 @@ import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; import com.indexdata.mkjsf.pazpar2.data.CommandError; import com.indexdata.mkjsf.utils.Utils; +/** + * Search client handling straight Pazpar2 requests. + * + *

Although it is described here as straight Pazpar2, the client itself + * actually represents a layer between Pazpar2 and the JSF application because it + * uses the Pazpar2 client from the library masterkey-common.

+ * That client, which is the one also used by the Service Proxy, does perform certain + * types of session handling, bootstraps lost sessions, avoids repeating already + * executed queries etc, so it is -- in other words -- still a mediated interaction + * with Pazpar2 that takes place. At least for now.

+ * + *

Configuration

+ * + * Configuration name: pz2client. + * + *

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.

+ * + *

Pz2Client will acknowledge following configuration parameters: + * + *

+ * + * @author Niels Erik + * + */ public class Pz2Client implements SearchClient { private static final long serialVersionUID = 5414266730169982028L; @@ -34,6 +63,10 @@ public class Pz2Client implements SearchClient { private transient Pazpar2Client client = null; private Pazpar2ClientConfiguration cfg = null; public static final String MODULENAME = "pz2client"; + + /** + * PAZPAR2_URL=none, PROXY_MODE=1, SERIALIZE_REQUESTS=false, STREAMBUFF_SIZE=4096, PARSE_RESPONSES=true + */ public static Map DEFAULTS = new HashMap(); Configuration config = null; @@ -59,7 +92,7 @@ public class Pz2Client implements SearchClient { } if (cfg != null) { try { - client = new Pazpar2ClientGeneric(cfg); + client = new Pazpar2ClientGeneric(cfg); } catch (ProxyErrorException pe) { logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); throw new ConfigurationException("Could not configure Pz2Client: "+ pe.getMessage(),pe); @@ -88,6 +121,13 @@ public class Pz2Client implements SearchClient { client.setSearchCommand(clientCommand); } + /** + * Runs the give Pazpar2 command and returns a response wrapper with either the received response or + * with some form of error message. + * + * It is intended that this method never throws an exception. All events are supposed to be captured and + * returned in some form of response. + */ @Override public HttpResponseWrapper executeCommand(Pazpar2Command command) { ClientCommandResponse commandResponse = null; @@ -99,6 +139,9 @@ public class Pz2Client implements SearchClient { pz2HttpResponse = client.executeCommand(clientCommand, baos); if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("xml")) { commandResponse = new ClientCommandResponse(pz2HttpResponse,baos); + } else if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("octet-stream")) { + commandResponse = new ClientCommandResponse(pz2HttpResponse,baos); + logger.info("Content type: " + commandResponse.getContentType() + ". isBinary?: " + commandResponse.isBinary()); } else if (pz2HttpResponse.getStatusCode()==417) { logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8")); commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()) ,"Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml"); @@ -108,11 +151,11 @@ public class Pz2Client implements SearchClient { String htmlStrippedOfTags = resp.replaceAll("\\<[^>]*>",""); String errorXml = ""; if (htmlStrippedOfTags.toLowerCase().contains("domain")) { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Error: Expected XML response from Pazpar2, got HTML with the word domain. Could be unrecognized address for Pazpar2.", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2", "Error: Expected XML response from Pazpar2 but got HTML. The HTML contains the word domain suggesting that the Pazpar2 address was not found.", htmlStrippedOfTags); } else { - errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Error: Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags); + errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2: " + pz2HttpResponse.getContentType(),"Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags); } - commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,"text/xml"); + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,pz2HttpResponse.getContentType()); } else { String resp = baos.toString("UTF-8"); logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp); @@ -121,15 +164,15 @@ public class Pz2Client implements SearchClient { } catch (IOException e) { logger.error(e.getMessage()); e.printStackTrace(); - commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "io", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "IO exception", e.getMessage(), ""),"text/xml"); } catch (Pazpar2ErrorException e) { logger.error(e.getMessage()); e.printStackTrace(); logger.error("Creating error XML"); - commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "-1", "Pazpar2Error", e.getMessage()),"text/xml"); + commandResponse = new ClientCommandResponse(0,CommandError.createErrorXml(command.getCommandName(), "", "ServiceError", e.getMessage(),""),"text/xml"); } long end = System.currentTimeMillis(); - logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); + logger.info("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); return commandResponse; } @@ -141,11 +184,17 @@ public class Pz2Client implements SearchClient { return clone; } + /** + * Returns default configuration parameters for the client. + */ @Override public Map getDefaults() { return DEFAULTS; } + /** + * Returns the configuration name of the client + */ @Override public String getModuleName() { return MODULENAME; @@ -175,6 +224,9 @@ public class Pz2Client implements SearchClient { } } + /** + * Provides configuration documentation -- mostly for diagnosing problems + */ @Override public List documentConfiguration() { List doc = new ArrayList(); @@ -186,20 +238,48 @@ public class Pz2Client implements SearchClient { return config; } + /** + * Returns the currently configured Papzar2 URL. + */ @Override public String getServiceUrl() { return cfg.PAZPAR2_URL; } + /** + * Returns true if a Papzar2 URL was defined yet. + */ @Override public boolean hasServiceUrl() { return cfg.PAZPAR2_URL != null && cfg.PAZPAR2_URL.length()>0; } + /** + * Sets the Pazpar2 URL to use for requests. + */ @Override public void setServiceUrl (String serviceUrl) { - cfg.PAZPAR2_URL = serviceUrl; - + cfg.PAZPAR2_URL = serviceUrl; + } + + /** + * Returns the Pazpar2 Service ID + */ + public String getServiceId () { + return cfg.PAZPAR2_SERVICE_ID; + } + + /** + * Sets the service ID that Pazpar2 should use when servicing requests + * @param serviceId + */ + public void setServiceId(String serviceId) { + cfg.PAZPAR2_SERVICE_ID = serviceId; + try { + client = new Pazpar2ClientGeneric(cfg); + } catch (ProxyErrorException pe) { + logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); + } } }