1 package com.indexdata.pz2utils4jsf.pazpar2;
\r
3 import java.io.ByteArrayOutputStream;
\r
4 import java.io.IOException;
\r
6 import javax.enterprise.context.SessionScoped;
\r
7 import javax.enterprise.inject.Alternative;
\r
8 import javax.inject.Named;
\r
10 import org.apache.log4j.Logger;
\r
12 import com.indexdata.masterkey.pazpar2.client.ClientCommand;
\r
13 import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;
\r
14 import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;
\r
15 import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;
\r
16 import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;
\r
17 import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;
\r
18 import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;
\r
19 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;
\r
20 import com.indexdata.pz2utils4jsf.errors.ConfigurationException;
\r
21 import com.indexdata.pz2utils4jsf.utils.Utils;
\r
23 @Named @SessionScoped @Alternative
\r
24 public class StraightPz2Client implements SearchClient {
\r
26 private static final long serialVersionUID = 5414266730169982028L;
\r
27 private static Logger logger = Logger.getLogger(StraightPz2Client.class);
\r
28 private Pazpar2Client client = null;
\r
29 private Pazpar2ClientConfiguration cfg = null;
\r
31 public StraightPz2Client() {}
\r
33 public void configure(Pz2Configurator configurator) throws ConfigurationException {
\r
34 logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator));
\r
36 cfg = new Pazpar2ClientConfiguration(configurator.getConfig());
\r
37 } catch (ProxyErrorException pe) {
\r
38 logger.error("Could not configure Pazpar2 client: " + pe.getMessage());
\r
39 throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe);
\r
43 client = new Pazpar2ClientGeneric(cfg);
\r
44 } catch (ProxyErrorException pe) {
\r
45 logger.error("Could not configure Pazpar2 client: " + pe.getMessage());
\r
46 throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe);
\r
49 logger.error("There was a problem creating StraightPz2Client. Client is null after configuration.");
\r
50 throw new ConfigurationException("Pazpar2Client is null after configuration");
\r
55 public void setSearchCommand(Pazpar2Command command) {
\r
56 ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());
\r
57 client.setSearchCommand(clientCommand);
\r
62 public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos)
\r
63 throws Pazpar2ErrorException, IOException {
\r
64 ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());
\r
65 Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);
\r
66 return new StraightPz2CommandResponse(pz2HttpResponse,baos);
\r
69 public StraightPz2Client cloneMe() {
\r
70 logger.debug("Cloning StraightPz2Client");
\r
71 StraightPz2Client clone = new StraightPz2Client();
\r
72 clone.client = this.client;
\r
73 clone.cfg = this.cfg;
\r