4aee1cf3e0c7b88b8f7c5dc036bc6f2eae344924
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / sp / ServiceProxyClient.java
1 package com.indexdata.pz2utils4jsf.pazpar2.sp;\r
2 \r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
4 \r
5 import java.io.BufferedReader;\r
6 import java.io.ByteArrayOutputStream;\r
7 import java.io.File;\r
8 import java.io.FileReader;\r
9 import java.io.IOException;\r
10 import java.util.ArrayList;\r
11 import java.util.HashMap;\r
12 import java.util.List;\r
13 import java.util.Map;\r
14 \r
15 import javax.enterprise.context.SessionScoped;\r
16 import javax.inject.Named;\r
17 \r
18 import org.apache.http.HttpEntity;\r
19 import org.apache.http.HttpResponse;\r
20 import org.apache.http.StatusLine;\r
21 import org.apache.http.client.ClientProtocolException;\r
22 import org.apache.http.client.HttpClient;\r
23 import org.apache.http.client.ResponseHandler;\r
24 import org.apache.http.client.methods.HttpGet;\r
25 import org.apache.http.client.methods.HttpPost;\r
26 import org.apache.http.conn.ClientConnectionManager;\r
27 import org.apache.http.conn.scheme.PlainSocketFactory;\r
28 import org.apache.http.conn.scheme.Scheme;\r
29 import org.apache.http.conn.scheme.SchemeRegistry;\r
30 import org.apache.http.entity.ByteArrayEntity;\r
31 import org.apache.http.entity.FileEntity;\r
32 import org.apache.http.impl.client.DefaultHttpClient;\r
33 import org.apache.http.impl.conn.PoolingClientConnectionManager;\r
34 import org.apache.http.util.EntityUtils;\r
35 import org.apache.log4j.Logger;\r
36 \r
37 import com.indexdata.masterkey.config.MissingMandatoryParameterException;\r
38 import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
39 import com.indexdata.pz2utils4jsf.config.Configuration;\r
40 import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
41 import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
42 import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
43 import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;\r
44 import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter;\r
45 import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
46 import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
47 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
48 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
49 import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager;\r
50 import com.indexdata.pz2utils4jsf.utils.Utils;\r
51 \r
52 @Named @SessionScoped \r
53 public class ServiceProxyClient implements SearchClient {\r
54     \r
55   private static final long serialVersionUID = -4031644009579840277L;\r
56   private static Logger logger = Logger.getLogger(ServiceProxyClient.class);\r
57   public static final String MODULENAME = "proxyclient";\r
58   public static final String SERVICE_PROXY_URL = "SERVICE_PROXY_URL";\r
59   public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
60   private String serviceUrl = "undefined";\r
61   private String[] initDocPaths = null;\r
62   private Configuration config = null;\r
63   \r
64   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
65   private HttpClient client;\r
66   private ServiceProxyUser user;\r
67 \r
68   public ServiceProxyClient () {\r
69     SchemeRegistry schemeRegistry = new SchemeRegistry();\r
70     schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));\r
71     ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);\r
72     client = new DefaultHttpClient(cm);    \r
73   }\r
74     \r
75   @Override\r
76   public void configure (ConfigurationReader configReader) {\r
77     logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
78     try {\r
79       config = configReader.getConfiguration(this);      \r
80       serviceUrl = config.getMandatory(SERVICE_PROXY_URL);  \r
81       this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));            \r
82     } catch (ConfigurationException c) {\r
83       c.printStackTrace();\r
84     } catch (MissingMandatoryParameterException mmp) {\r
85       mmp.printStackTrace();\r
86     }    \r
87   }\r
88   \r
89   private String[] getMultiProperty(String prop) {    \r
90     if (prop != null) {\r
91       return prop.split(",");\r
92     } else {\r
93       return null;\r
94     }\r
95   }\r
96   \r
97   public boolean authenticate (AuthenticationEntity user) {\r
98     try {      \r
99       logger.info("Authenticating [" + user.getProperty("name") + "]");\r
100       this.user = (ServiceProxyUser) user;\r
101       Pazpar2Command auth = new Pazpar2Command("auth",new StateManager());\r
102       auth.setParameters(new CommandParameter("action","=","login"), \r
103                          new CommandParameter("username","=",user.getProperty("name")), \r
104                          new CommandParameter("password","=",user.getProperty("password")));\r
105       byte[] response = send(auth);\r
106       String responseStr = new String(response,"UTF-8");\r
107       logger.info(responseStr);      \r
108       if (responseStr.contains("FAIL")) {\r
109         return false;\r
110       } else {\r
111         return true;\r
112       }      \r
113     } catch (ClientProtocolException e) {\r
114       // TODO Auto-generated catch block\r
115       e.printStackTrace();\r
116       return false;\r
117     } catch (IOException e) {\r
118       // TODO Auto-generated catch block\r
119       e.printStackTrace();\r
120       return false;\r
121     }        \r
122   }\r
123   \r
124   public boolean checkAuthentication () {\r
125     try {\r
126       Pazpar2Command check = new Pazpar2Command("auth",new StateManager());\r
127       check.setParameter(new CommandParameter("action","=","check"));\r
128       byte[] response = send(check);\r
129       logger.info(new String(response,"UTF-8"));\r
130     } catch (ClientProtocolException e) {\r
131       // TODO Auto-generated catch block\r
132       e.printStackTrace();\r
133       return false;\r
134     } catch (IOException e) {\r
135       // TODO Auto-generated catch block\r
136       e.printStackTrace();\r
137       return false;\r
138     }    \r
139     return true;\r
140     \r
141   }\r
142   \r
143   public boolean isAuthenticatingClient () {\r
144     return true;\r
145   }\r
146   \r
147   public boolean isAuthenticated () {\r
148     if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
149       return checkAuthentication();\r
150     } else {\r
151       return false;\r
152     }\r
153   }\r
154   \r
155   /**\r
156    * Makes the request\r
157    * @param request\r
158    * @return HTTP response as a String\r
159    * @throws ClientProtocolException\r
160    * @throws IOException\r
161    */\r
162   private byte[] send(CommandReadOnly command) throws ClientProtocolException, IOException {\r
163     String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
164     logger.info("Sending request "+url);    \r
165     HttpGet httpget = new HttpGet(url);     \r
166     byte[] response = client.execute(httpget, handler);    \r
167     return response;\r
168   }\r
169   \r
170   public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
171     private StatusLine statusLine = null;\r
172     public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
173       byte[] resp = null;\r
174       HttpEntity entity = response.getEntity();      \r
175       statusLine = response.getStatusLine();\r
176       if (entity != null) {        \r
177         resp = EntityUtils.toByteArray(entity);        \r
178       } \r
179       EntityUtils.consume(entity);\r
180       return resp;\r
181     }\r
182     public int getStatusCode() {\r
183       return statusLine.getStatusCode();\r
184     }    \r
185     public String getReasonPhrase() {\r
186       return statusLine.getReasonPhrase();\r
187     }\r
188   }\r
189 \r
190   public int getStatusCode () {\r
191     return handler.getStatusCode();\r
192   }\r
193   \r
194   public String getReasonPhrase() {\r
195     return handler.getReasonPhrase();\r
196   }\r
197 \r
198   @Override\r
199   public void setSearchCommand(CommandReadOnly command) {\r
200     // Do nothing, Service Proxy is handling this    \r
201   }\r
202 \r
203   @Override\r
204   public CommandResponse executeCommand(CommandReadOnly command,\r
205       ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
206     byte[] response = send(command);\r
207     baos.write(response);\r
208     return new ServiceProxyClientCommandResponse(getStatusCode(), new String(response,"UTF-8"));    \r
209   }\r
210 \r
211   public ServiceProxyClient cloneMe() {\r
212     logger.debug("Cloning Pz2Client");\r
213     ServiceProxyClient clone = new ServiceProxyClient();\r
214     clone.client = this.client;\r
215     clone.serviceUrl = this.serviceUrl;\r
216     clone.initDocPaths = this.initDocPaths;\r
217     return clone;\r
218   }\r
219 \r
220   @Override\r
221   public Map<String, String> getDefaults() {    \r
222     return new HashMap<String,String>();\r
223   }\r
224 \r
225   @Override\r
226   public String getModuleName() {\r
227     return MODULENAME;\r
228   }\r
229   \r
230   @Override\r
231   public List<String> documentConfiguration () {\r
232     List<String> doc = new ArrayList<String>();\r
233     doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl);\r
234     return null;\r
235   }\r
236   \r
237   public byte[] postInitDoc (String filePath) throws IOException {\r
238     logger.info("Looking to post the file in : [" + filePath +"]");\r
239     HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
240     File initDoc = new File(filePath);\r
241     logger.info("Posting to SP: ");\r
242     if (logger.isDebugEnabled()) {\r
243       BufferedReader reader = new BufferedReader(new FileReader(initDoc));\r
244       String line;\r
245       while ( (line = reader.readLine()) != null) {\r
246         System.out.println(line);\r
247       }\r
248       reader.close();\r
249     }\r
250     post.setEntity(new FileEntity(initDoc));\r
251     byte[] response = client.execute(post, handler);\r
252     logger.debug("Response on POST was: " + new String(response,"UTF-8"));    \r
253     return response;\r
254   }\r
255   \r
256   public String[] getInitDocPaths () {\r
257     logger.debug("Get init doc paths ");\r
258     logger.debug("length: " + initDocPaths.length);\r
259     return initDocPaths;\r
260   }\r
261   \r
262   public byte[] postInitDoc(byte[] initDoc) throws IOException {\r
263     HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
264     post.setEntity(new ByteArrayEntity(initDoc));\r
265     byte[] response = client.execute(post, handler);\r
266     logger.debug("Response on POST was: " + new String(response,"UTF-8"));    \r
267     return response;\r
268   }\r
269   \r
270   public void setServiceProxyUrl (String url) {\r
271     serviceUrl = url;\r
272   }\r
273   \r
274   public String getServiceProxyUrl () {\r
275     return serviceUrl;\r
276   }\r
277   \r
278   public Configuration getConfiguration () {\r
279     return config;\r
280   }\r
281   \r
282 }\r