X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2FPz2Client.java;h=6dfe77f7f570914465083725ddef3a47222333b8;hb=3cc9707fdd556a222b5d8e6bba61c84a320eb357;hp=2d03830f9e45d957e09d933dd9927a8c991a3d6b;hpb=ccb28ae8d5d46d29c40bd8b1637522c212b80636;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 2d03830..6dfe77f 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java @@ -24,19 +24,21 @@ import com.indexdata.mkjsf.config.Configuration; import com.indexdata.mkjsf.config.ConfigurationReader; import com.indexdata.mkjsf.errors.ConfigurationException; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.mkjsf.pazpar2.data.CommandError; import com.indexdata.mkjsf.utils.Utils; public class Pz2Client implements SearchClient { private static final long serialVersionUID = 5414266730169982028L; private static Logger logger = Logger.getLogger(Pz2Client.class); - private Pazpar2Client client = null; + private transient Pazpar2Client client = null; private Pazpar2ClientConfiguration cfg = null; public static final String MODULENAME = "pz2client"; public static Map DEFAULTS = new HashMap(); - Configuration config = null; + Configuration config = null; static { + DEFAULTS.put("PAZPAR2_URL", ""); DEFAULTS.put("PROXY_MODE","1"); DEFAULTS.put("SERIALIZE_REQUESTS", "false"); DEFAULTS.put("STREAMBUFF_SIZE", "4096"); @@ -49,7 +51,7 @@ public class Pz2Client implements SearchClient { public void configure(ConfigurationReader configReader) throws ConfigurationException { logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader)); try { - config = configReader.getConfiguration(this); + config = configReader.getConfiguration(this); cfg = new Pazpar2ClientConfiguration(new ConfigurationGetter(config)); } catch (ProxyErrorException pe) { logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); @@ -57,7 +59,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); @@ -82,16 +84,56 @@ public class Pz2Client implements SearchClient { @Override public void setSearchCommand(Pazpar2Command command) { - ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); + ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString()); client.setSearchCommand(clientCommand); } @Override - public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) - throws Pazpar2ErrorException, IOException { - ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); - Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos); - return new Pz2CommandResponse(pz2HttpResponse,baos); + public HttpResponseWrapper executeCommand(Pazpar2Command command) { + ClientCommandResponse commandResponse = null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString()); + Pazpar2HttpResponse pz2HttpResponse = null; + long start = System.currentTimeMillis(); + try { + 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"); + } else if (pz2HttpResponse.getContentType().contains("html")) { + String resp = baos.toString("UTF-8"); + logger.error("HTML response where XML was expected. Status code was " + pz2HttpResponse.getStatusCode() + ": " + resp); + String htmlStrippedOfTags = resp.replaceAll("\\<[^>]*>",""); + String errorXml = ""; + if (htmlStrippedOfTags.toLowerCase().contains("domain")) { + 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()), "Unexpected response type from Pazpar2: " + pz2HttpResponse.getContentType(),"Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags); + } + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,pz2HttpResponse.getContentType()); + } else { + String resp = baos.toString("UTF-8"); + logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp); + commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml"); + } + } catch (IOException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + 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(0,CommandError.createErrorXml(command.getCommandName(), "", "ServiceError", e.getMessage(),""),"text/xml"); + } + long end = System.currentTimeMillis(); + logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); + return commandResponse; } public Pz2Client cloneMe() { @@ -147,4 +189,32 @@ public class Pz2Client implements SearchClient { return config; } + @Override + public String getServiceUrl() { + return cfg.PAZPAR2_URL; + } + + @Override + public boolean hasServiceUrl() { + return cfg.PAZPAR2_URL != null && cfg.PAZPAR2_URL.length()>0; + } + + @Override + public void setServiceUrl (String serviceUrl) { + cfg.PAZPAR2_URL = serviceUrl; + } + + public String getServiceId () { + return cfg.PAZPAR2_SERVICE_ID; + } + + 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()); + } + } + }