26e5c9a05ba3a6e978f8fb14e261847bb0eac50a
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / ServiceProxyClient.java
1 package com.indexdata.mkjsf.pazpar2;\r
2 \r
3 import static com.indexdata.mkjsf.utils.Utils.nl;\r
4 \r
5 import java.io.BufferedReader;\r
6 import java.io.File;\r
7 import java.io.FileReader;\r
8 import java.io.IOException;\r
9 import java.util.ArrayList;\r
10 import java.util.HashMap;\r
11 import java.util.List;\r
12 import java.util.Map;\r
13 \r
14 import org.apache.http.Header;\r
15 import org.apache.http.HttpEntity;\r
16 import org.apache.http.HttpResponse;\r
17 import org.apache.http.StatusLine;\r
18 import org.apache.http.client.ClientProtocolException;\r
19 import org.apache.http.client.HttpClient;\r
20 import org.apache.http.client.ResponseHandler;\r
21 import org.apache.http.client.methods.HttpGet;\r
22 import org.apache.http.client.methods.HttpPost;\r
23 import org.apache.http.conn.ClientConnectionManager;\r
24 import org.apache.http.conn.scheme.PlainSocketFactory;\r
25 import org.apache.http.conn.scheme.Scheme;\r
26 import org.apache.http.conn.scheme.SchemeRegistry;\r
27 import org.apache.http.entity.ByteArrayEntity;\r
28 import org.apache.http.entity.FileEntity;\r
29 import org.apache.http.impl.client.DefaultHttpClient;\r
30 import org.apache.http.impl.conn.PoolingClientConnectionManager;\r
31 import org.apache.http.util.EntityUtils;\r
32 import org.apache.log4j.Logger;\r
33 \r
34 import com.indexdata.mkjsf.config.Configuration;\r
35 import com.indexdata.mkjsf.config.ConfigurationReader;\r
36 import com.indexdata.mkjsf.errors.ConfigurationException;\r
37 import com.indexdata.mkjsf.errors.MissingConfigurationContextException;\r
38 import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
39 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
40 import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
41 import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommand;\r
42 import com.indexdata.mkjsf.pazpar2.data.CommandError;\r
43 import com.indexdata.mkjsf.utils.Utils;\r
44 \r
45 /**\r
46  * Search client handling Service Proxy requests. \r
47  *   \r
48  * @author Niels Erik\r
49  *\r
50  */\r
51 public class ServiceProxyClient implements SearchClient {\r
52     \r
53   private static final long serialVersionUID = -4031644009579840277L;\r
54   private static Logger logger = Logger.getLogger(ServiceProxyClient.class);\r
55   public static final String MODULENAME = "proxyclient";\r
56   \r
57   public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
58   private String serviceUrl = "";\r
59   \r
60   private List<String> initDocPaths = null;\r
61   private Configuration config = null;\r
62   \r
63   ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
64   private transient HttpClient client;  \r
65   private Pazpar2Command checkAuth = null;\r
66   private Pazpar2Command ipAuth = null;\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) throws MissingConfigurationContextException {\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.get("SERVICE_PROXY_URL");\r
81       this.initDocPaths = config.getMultiProperty(SP_INIT_DOC_PATHS,",");\r
82       checkAuth = new AuthCommand();\r
83       checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
84       ipAuth = new AuthCommand();\r
85       ipAuth.setParameterInState(new CommandParameter("action","=","ipauth"));\r
86     } catch (MissingConfigurationContextException mcce) {\r
87       throw mcce;\r
88     } catch (ConfigurationException ce) {\r
89       logger.error("Failed to configure Service Proxy client");\r
90       ce.printStackTrace();\r
91     }\r
92   }\r
93     \r
94   public boolean isAuthenticatingClient () {\r
95     return true;\r
96   }\r
97     \r
98   /**\r
99    * Makes the request\r
100    * @param request\r
101    * @return HTTP response as a String\r
102    * @throws ClientProtocolException\r
103    * @throws IOException\r
104    */\r
105   public ClientCommandResponse send(Pazpar2Command command) {\r
106     ClientCommandResponse commandResponse = null;\r
107     String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
108     logger.info("Sending request "+url);    \r
109     HttpGet httpget = new HttpGet(url);     \r
110     byte[] response = null;\r
111     try {\r
112       response = client.execute(httpget, handler);\r
113       if (handler.getStatusCode()==200 && (handler.getContentType().contains("xml") || handler.getContentType().contains("octet-stream"))) {\r
114         logger.trace("Creating command response holding content of type " + handler.getContentType());\r
115         commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
116       } else {\r
117         logger.error("Service Proxy status code: " + handler.getStatusCode());\r
118         String errorXml = "";\r
119         if (handler.getContentType().contains("xml")) {\r
120           errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8"));        \r
121         } else {\r
122           if (handler.getContentType().contains("html")) {\r
123             String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>","");\r
124             if (htmlStrippedOfTags.toLowerCase().contains("domain")) {\r
125               errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML. It contains the word domain suggesting that the service address was not found.", htmlStrippedOfTags);              \r
126             } else {\r
127               errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML", htmlStrippedOfTags);              \r
128             }\r
129           } else {\r
130             errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy: "+handler.getContentType(), "Could not process non-XML response from Service Proxy", new String(response,"UTF-8"));\r
131           }          \r
132         }\r
133         commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType());\r
134       }       \r
135     } catch (Exception e) {\r
136       e.printStackTrace();\r
137       commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : ""), e.getStackTrace().toString()),handler.getContentType());\r
138     }\r
139     return commandResponse; \r
140   }\r
141   \r
142   public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
143     private StatusLine statusLine = null;\r
144     private Header contentType = null;\r
145     public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
146       byte[] resp = null;\r
147       HttpEntity entity = response.getEntity();      \r
148       statusLine = response.getStatusLine();\r
149       if (entity != null) {        \r
150         resp = EntityUtils.toByteArray(entity);        \r
151         contentType = response.getEntity().getContentType();        \r
152       }       \r
153       EntityUtils.consume(entity);      \r
154       return resp;\r
155     }\r
156     public int getStatusCode() {\r
157       return statusLine.getStatusCode();\r
158     }    \r
159     public String getReasonPhrase() {\r
160       return statusLine.getReasonPhrase();\r
161     }\r
162     public String getContentType () {\r
163       return (contentType != null ? contentType.getValue() : "Content-Type not known"); \r
164     }\r
165   }\r
166 \r
167   public int getStatusCode () {\r
168     return handler.getStatusCode();\r
169   }\r
170   \r
171   public String getReasonPhrase() {\r
172     return handler.getReasonPhrase();\r
173   }\r
174 \r
175   /**\r
176    * Does nothing in Service Proxy context\r
177    */\r
178   @Override\r
179   public void setSearchCommand(Pazpar2Command command) {\r
180     // Do nothing, Service Proxy is handling this    \r
181   }\r
182 \r
183   @Override\r
184   public HttpResponseWrapper executeCommand(Pazpar2Command command) {\r
185     return send(command);\r
186   }\r
187 \r
188   public ServiceProxyClient cloneMe() {\r
189     logger.debug("Cloning Pz2Client");\r
190     ServiceProxyClient clone = new ServiceProxyClient();\r
191     clone.client = this.client;\r
192     clone.serviceUrl = this.serviceUrl;\r
193     clone.initDocPaths = this.initDocPaths;\r
194     return clone;\r
195   }\r
196 \r
197   /**\r
198    * Returns default configuration parameters for the client.\r
199    */\r
200   @Override\r
201   public Map<String, String> getDefaults() {    \r
202     return new HashMap<String,String>();\r
203   }\r
204 \r
205   /**\r
206    * Returns the configuration name of the client\r
207    */\r
208   @Override\r
209   public String getModuleName() {\r
210     return MODULENAME;\r
211   }\r
212   \r
213   @Override\r
214   public List<String> documentConfiguration () {\r
215     List<String> doc = new ArrayList<String>();\r
216     doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (serviceUrl.length()>0 ? serviceUrl : "[not defined yet]"));\r
217     return null;\r
218   }\r
219   \r
220   public ClientCommandResponse postInitDoc (String filePath) throws IOException {\r
221     logger.info("Looking to post the file in : [" + filePath +"]");\r
222     HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
223     File initDoc = new File(filePath);\r
224     logger.info("Posting to SP: ");\r
225     if (logger.isDebugEnabled()) {\r
226       BufferedReader reader = new BufferedReader(new FileReader(initDoc));\r
227       String line;\r
228       while ( (line = reader.readLine()) != null) {\r
229         System.out.println(line);\r
230       }\r
231       reader.close();\r
232     }\r
233     post.setEntity(new FileEntity(initDoc));\r
234     byte[] response = client.execute(post, handler);\r
235     logger.debug("Response on POST was: " + new String(response,"UTF-8"));    \r
236     return new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());    \r
237   }\r
238   \r
239   public List<String> getInitDocPaths () {\r
240     logger.debug("Get init doc paths ");\r
241     logger.debug("length: " + initDocPaths.size());\r
242     return initDocPaths;\r
243   }\r
244   \r
245   public HttpResponseWrapper postInitDoc(byte[] initDoc, Pazpar2Command command) {\r
246     String requestParameters = command.getEncodedQueryString();\r
247     logger.info("Initiating session with init doc and [" + requestParameters +"]");\r
248     HttpPost post = new HttpPost(serviceUrl+"?" + requestParameters);\r
249     post.setEntity(new ByteArrayEntity(initDoc));\r
250     ClientCommandResponse commandResponse = null;\r
251     byte[] response;\r
252     try {\r
253       response = client.execute(post, handler);\r
254       if (handler.getStatusCode()==200) {\r
255         commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
256       } else {\r
257         logger.error("Service Proxy status code: " + handler.getStatusCode());\r
258         commandResponse = new ClientCommandResponse(handler.getStatusCode(),CommandError.insertErrorXml("init", String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8")),"text/xml");                               \r
259       }\r
260     } catch (ClientProtocolException e) {\r
261       logger.error(e.getMessage());\r
262       e.printStackTrace();\r
263       commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "Client protocol exception", e.getMessage(), e.getStackTrace().toString()),"text/xml");      \r
264     } catch (IOException e) {\r
265       logger.error(e.getMessage());\r
266       e.printStackTrace();\r
267       commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO exception", e.getMessage(),e.getStackTrace().toString()),"text/xml");      \r
268     }\r
269     return commandResponse;    \r
270   }\r
271   \r
272   /**\r
273    * Sets the URL of the Service Proxy that should service requests. \r
274    */\r
275   public void setServiceUrl (String url) {    \r
276     serviceUrl = url;\r
277   }\r
278           \r
279   public Configuration getConfiguration () {\r
280     return config;\r
281   }\r
282 \r
283   @Override\r
284   public String getServiceUrl() {    \r
285     return serviceUrl;\r
286   }\r
287 \r
288   /**\r
289    * Returns true if a Service Proxy URL was defined yet.\r
290    */\r
291   @Override\r
292   public boolean hasServiceUrl() {\r
293     return serviceUrl != null && serviceUrl.length()>0;\r
294   }\r
295   \r
296 }\r