import com.indexdata.mkjsf.config.ConfigurationReader;\r
import com.indexdata.mkjsf.errors.ConfigurationException;\r
import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.mkjsf.pazpar2.data.CommandError;\r
import com.indexdata.mkjsf.utils.Utils;\r
\r
public class Pz2Client implements SearchClient {\r
\r
private static final long serialVersionUID = 5414266730169982028L;\r
private static Logger logger = Logger.getLogger(Pz2Client.class);\r
- private Pazpar2Client client = null;\r
+ private transient Pazpar2Client client = null;\r
private Pazpar2ClientConfiguration cfg = null;\r
public static final String MODULENAME = "pz2client";\r
public static Map<String,String> DEFAULTS = new HashMap<String,String>();\r
- Configuration config = null;\r
+ Configuration config = null; \r
\r
static { \r
+ DEFAULTS.put("PAZPAR2_URL", "");\r
DEFAULTS.put("PROXY_MODE","1");\r
DEFAULTS.put("SERIALIZE_REQUESTS", "false");\r
DEFAULTS.put("STREAMBUFF_SIZE", "4096");\r
public void configure(ConfigurationReader configReader) throws ConfigurationException { \r
logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
try {\r
- config = configReader.getConfiguration(this);\r
+ config = configReader.getConfiguration(this); \r
cfg = new Pazpar2ClientConfiguration(new ConfigurationGetter(config));\r
} catch (ProxyErrorException pe) {\r
logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
} \r
if (cfg != null) {\r
try {\r
- client = new Pazpar2ClientGeneric(cfg); \r
+ client = new Pazpar2ClientGeneric(cfg); \r
} catch (ProxyErrorException pe) {\r
logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
throw new ConfigurationException("Could not configure Pz2Client: "+ pe.getMessage(),pe);\r
\r
@Override\r
public void setSearchCommand(Pazpar2Command command) {\r
- ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+ ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString());\r
client.setSearchCommand(clientCommand); \r
}\r
\r
@Override\r
- public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
- throws Pazpar2ErrorException, IOException {\r
- ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
- Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
- return new Pz2CommandResponse(pz2HttpResponse,baos);\r
+ public HttpResponseWrapper executeCommand(Pazpar2Command command) {\r
+ ClientCommandResponse commandResponse = null;\r
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
+ ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString());\r
+ Pazpar2HttpResponse pz2HttpResponse = null;\r
+ long start = System.currentTimeMillis();\r
+ try {\r
+ pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
+ if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("xml")) {\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+ } else if (pz2HttpResponse.getStatusCode()==200 && pz2HttpResponse.getContentType().contains("octet-stream")) {\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse,baos);\r
+ logger.info("Content type: " + commandResponse.getContentType() + ". isBinary?: " + commandResponse.isBinary());\r
+ } else if (pz2HttpResponse.getStatusCode()==417) {\r
+ logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()) ,"Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml"); \r
+ } else if (pz2HttpResponse.getContentType().contains("html")) {\r
+ String resp = baos.toString("UTF-8");\r
+ logger.error("HTML response where XML was expected. Status code was " + pz2HttpResponse.getStatusCode() + ": " + resp);\r
+ String htmlStrippedOfTags = resp.replaceAll("\\<[^>]*>","");\r
+ String errorXml = "";\r
+ if (htmlStrippedOfTags.toLowerCase().contains("domain")) {\r
+ 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);\r
+ } else { \r
+ errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Unexpected response type from Pazpar2: " + pz2HttpResponse.getContentType(),"Expected XML response from Pazpar2, got HTML", htmlStrippedOfTags);\r
+ } \r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),errorXml,pz2HttpResponse.getContentType()); \r
+ } else {\r
+ String resp = baos.toString("UTF-8");\r
+ logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp);\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml"); \r
+ } \r
+ } catch (IOException e) {\r
+ logger.error(e.getMessage());\r
+ e.printStackTrace();\r
+ commandResponse = new ClientCommandResponse(pz2HttpResponse.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(pz2HttpResponse.getStatusCode()), "IO exception", e.getMessage(), ""),"text/xml"); \r
+ } catch (Pazpar2ErrorException e) {\r
+ logger.error(e.getMessage());\r
+ e.printStackTrace();\r
+ logger.error("Creating error XML");\r
+ commandResponse = new ClientCommandResponse(0,CommandError.createErrorXml(command.getCommandName(), "", "Pazpar2Error", e.getMessage(),""),"text/xml");\r
+ }\r
+ long end = System.currentTimeMillis(); \r
+ logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+ return commandResponse;\r
}\r
\r
public Pz2Client cloneMe() {\r
return config;\r
}\r
\r
+ @Override\r
+ public String getServiceUrl() {\r
+ return cfg.PAZPAR2_URL; \r
+ }\r
+\r
+ @Override\r
+ public boolean hasServiceUrl() {\r
+ return cfg.PAZPAR2_URL != null && cfg.PAZPAR2_URL.length()>0;\r
+ }\r
+ \r
+ @Override \r
+ public void setServiceUrl (String serviceUrl) { \r
+ cfg.PAZPAR2_URL = serviceUrl; \r
+ }\r
+ \r
+ public String getServiceId () {\r
+ return cfg.PAZPAR2_SERVICE_ID;\r
+ }\r
+ \r
+ public void setServiceId(String serviceId) {\r
+ cfg.PAZPAR2_SERVICE_ID = serviceId;\r
+ try {\r
+ client = new Pazpar2ClientGeneric(cfg); \r
+ } catch (ProxyErrorException pe) {\r
+ logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); \r
+ }\r
+ }\r
+\r
}\r