187b9280b2789a726ecf4cb0a3d0d887af6e4230
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / StraightPz2Client.java
1 package com.indexdata.pz2utils4jsf.pazpar2;\r
2 \r
3 import java.io.ByteArrayOutputStream;\r
4 import java.io.IOException;\r
5 \r
6 import javax.enterprise.context.SessionScoped;\r
7 import javax.enterprise.inject.Alternative;\r
8 import javax.inject.Named;\r
9 \r
10 import org.apache.log4j.Logger;\r
11 \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
22 \r
23 @Named @SessionScoped @Alternative\r
24 public class StraightPz2Client implements SearchClient {\r
25 \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
30   \r
31   public StraightPz2Client() {}\r
32   \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
35     try {\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
40     } \r
41     if (cfg != null) {\r
42       try {\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
47       }\r
48     } else {\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
51     } \r
52   }\r
53 \r
54   @Override\r
55   public void setSearchCommand(Pazpar2Command command) {\r
56     ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
57     client.setSearchCommand(clientCommand);\r
58     \r
59   }\r
60 \r
61   @Override\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
67   }\r
68 \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
74     return clone;\r
75   }\r
76 }\r