--- /dev/null
+package com.indexdata.mkjsf.pazpar2;\r
+\r
+import static com.indexdata.mkjsf.utils.Utils.nl;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.apache.http.Header;\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.StatusLine;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.HttpClient;\r
+import org.apache.http.client.ResponseHandler;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpPost;\r
+import org.apache.http.conn.ClientConnectionManager;\r
+import org.apache.http.conn.scheme.PlainSocketFactory;\r
+import org.apache.http.conn.scheme.Scheme;\r
+import org.apache.http.conn.scheme.SchemeRegistry;\r
+import org.apache.http.entity.ByteArrayEntity;\r
+import org.apache.http.entity.FileEntity;\r
+import org.apache.http.impl.client.DefaultHttpClient;\r
+import org.apache.http.impl.conn.PoolingClientConnectionManager;\r
+import org.apache.http.util.EntityUtils;\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.config.Configuration;\r
+import com.indexdata.mkjsf.config.ConfigurationReader;\r
+import com.indexdata.mkjsf.errors.ConfigurationException;\r
+import com.indexdata.mkjsf.errors.MissingConfigurationContextException;\r
+import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;\r
+import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
+import com.indexdata.mkjsf.pazpar2.data.CommandError;\r
+import com.indexdata.mkjsf.utils.Utils;\r
+\r
+/**\r
+ * Search client handling Service Proxy requests. \r
+ * \r
+ * @author Niels Erik\r
+ *\r
+ */\r
+public class ServiceProxyClient implements SearchClient {\r
+ \r
+ private static final long serialVersionUID = -4031644009579840277L;\r
+ private static Logger logger = Logger.getLogger(ServiceProxyClient.class);\r
+ public static final String MODULENAME = "proxyclient";\r
+ \r
+ public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
+ private String serviceUrl = "";\r
+ \r
+ private List<String> initDocPaths = null;\r
+ private Configuration config = null;\r
+ \r
+ ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
+ private transient HttpClient client; \r
+ private Pazpar2Command checkAuth = null;\r
+ private Pazpar2Command ipAuth = null;\r
+\r
+ public ServiceProxyClient () {\r
+ SchemeRegistry schemeRegistry = new SchemeRegistry();\r
+ schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));\r
+ ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);\r
+ client = new DefaultHttpClient(cm);\r
+ }\r
+ \r
+ @Override\r
+ public void configure (ConfigurationReader configReader) throws MissingConfigurationContextException {\r
+ logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
+ try {\r
+ config = configReader.getConfiguration(this); \r
+ serviceUrl = config.get("SERVICE_PROXY_URL");\r
+ this.initDocPaths = config.getMultiProperty(SP_INIT_DOC_PATHS,",");\r
+ checkAuth = new AuthCommand();\r
+ checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
+ ipAuth = new AuthCommand();\r
+ ipAuth.setParameterInState(new CommandParameter("action","=","ipauth"));\r
+ } catch (MissingConfigurationContextException mcce) {\r
+ throw mcce;\r
+ } catch (ConfigurationException ce) {\r
+ logger.error("Failed to configure Service Proxy client");\r
+ ce.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ public boolean isAuthenticatingClient () {\r
+ return true;\r
+ }\r
+ \r
+ /**\r
+ * Makes the request\r
+ * @param request\r
+ * @return HTTP response as a String\r
+ * @throws ClientProtocolException\r
+ * @throws IOException\r
+ */\r
+ public ClientCommandResponse send(Pazpar2Command command) {\r
+ ClientCommandResponse commandResponse = null;\r
+ String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
+ logger.info("Sending request "+url); \r
+ HttpGet httpget = new HttpGet(url); \r
+ byte[] response = null;\r
+ try {\r
+ response = client.execute(httpget, handler);\r
+ if (handler.getStatusCode()==200 && (handler.getContentType().contains("xml") || handler.getContentType().contains("octet-stream"))) {\r
+ logger.trace("Creating command response holding content of type " + handler.getContentType());\r
+ commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
+ } else {\r
+ logger.error("Service Proxy status code: " + handler.getStatusCode());\r
+ String errorXml = "";\r
+ if (handler.getContentType().contains("xml")) {\r
+ errorXml = CommandError.insertErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Service Proxy error: "+handler.getStatusCode(), new String(response,"UTF-8")); \r
+ } else {\r
+ if (handler.getContentType().contains("html")) {\r
+ String htmlStrippedOfTags = (new String(response,"UTF-8")).replaceAll("\\<[^>]*>","");\r
+ if (htmlStrippedOfTags.toLowerCase().contains("domain")) {\r
+ 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
+ } else {\r
+ errorXml = CommandError.createErrorXml(command.getCommandName(), String.valueOf(handler.getStatusCode()), "Unexpected response type from Service Proxy", "Expected XML from SP but got HTML", htmlStrippedOfTags); \r
+ }\r
+ } else {\r
+ 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
+ } \r
+ }\r
+ commandResponse = new ClientCommandResponse(handler.getStatusCode(),errorXml,handler.getContentType());\r
+ } \r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ 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
+ }\r
+ return commandResponse; \r
+ }\r
+ \r
+ public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
+ private StatusLine statusLine = null;\r
+ private Header contentType = null;\r
+ public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
+ byte[] resp = null;\r
+ HttpEntity entity = response.getEntity(); \r
+ statusLine = response.getStatusLine();\r
+ if (entity != null) { \r
+ resp = EntityUtils.toByteArray(entity); \r
+ contentType = response.getEntity().getContentType(); \r
+ } \r
+ EntityUtils.consume(entity); \r
+ return resp;\r
+ }\r
+ public int getStatusCode() {\r
+ return statusLine.getStatusCode();\r
+ } \r
+ public String getReasonPhrase() {\r
+ return statusLine.getReasonPhrase();\r
+ }\r
+ public String getContentType () {\r
+ return (contentType != null ? contentType.getValue() : "Content-Type not known"); \r
+ }\r
+ }\r
+\r
+ public int getStatusCode () {\r
+ return handler.getStatusCode();\r
+ }\r
+ \r
+ public String getReasonPhrase() {\r
+ return handler.getReasonPhrase();\r
+ }\r
+\r
+ /**\r
+ * Does nothing in Service Proxy context\r
+ */\r
+ @Override\r
+ public void setSearchCommand(Pazpar2Command command) {\r
+ // Do nothing, Service Proxy is handling this \r
+ }\r
+\r
+ @Override\r
+ public HttpResponseWrapper executeCommand(Pazpar2Command command) {\r
+ return send(command);\r
+ }\r
+\r
+ public ServiceProxyClient cloneMe() {\r
+ logger.debug("Cloning Pz2Client");\r
+ ServiceProxyClient clone = new ServiceProxyClient();\r
+ clone.client = this.client;\r
+ clone.serviceUrl = this.serviceUrl;\r
+ clone.initDocPaths = this.initDocPaths;\r
+ return clone;\r
+ }\r
+\r
+ /**\r
+ * Returns default configuration parameters for the client.\r
+ */\r
+ @Override\r
+ public Map<String, String> getDefaults() { \r
+ return new HashMap<String,String>();\r
+ }\r
+\r
+ /**\r
+ * Returns the configuration name of the client\r
+ */\r
+ @Override\r
+ public String getModuleName() {\r
+ return MODULENAME;\r
+ }\r
+ \r
+ @Override\r
+ public List<String> documentConfiguration () {\r
+ List<String> doc = new ArrayList<String>();\r
+ doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + (serviceUrl.length()>0 ? serviceUrl : "[not defined yet]"));\r
+ return null;\r
+ }\r
+ \r
+ public ClientCommandResponse postInitDoc (String filePath) throws IOException {\r
+ logger.info("Looking to post the file in : [" + filePath +"]");\r
+ HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
+ File initDoc = new File(filePath);\r
+ logger.info("Posting to SP: ");\r
+ if (logger.isDebugEnabled()) {\r
+ BufferedReader reader = new BufferedReader(new FileReader(initDoc));\r
+ String line;\r
+ while ( (line = reader.readLine()) != null) {\r
+ System.out.println(line);\r
+ }\r
+ reader.close();\r
+ }\r
+ post.setEntity(new FileEntity(initDoc));\r
+ byte[] response = client.execute(post, handler);\r
+ logger.debug("Response on POST was: " + new String(response,"UTF-8")); \r
+ return new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType()); \r
+ }\r
+ \r
+ public List<String> getInitDocPaths () {\r
+ logger.debug("Get init doc paths ");\r
+ logger.debug("length: " + initDocPaths.size());\r
+ return initDocPaths;\r
+ }\r
+ \r
+ public HttpResponseWrapper postInitDoc(byte[] initDoc, boolean includeDebug) {\r
+ HttpPost post = new HttpPost(serviceUrl+"?command=init" + (includeDebug? "&includeDebug=yes" : ""));\r
+ post.setEntity(new ByteArrayEntity(initDoc));\r
+ ClientCommandResponse commandResponse = null;\r
+ byte[] response;\r
+ try {\r
+ response = client.execute(post, handler);\r
+ if (handler.getStatusCode()==200) {\r
+ commandResponse = new ClientCommandResponse(handler.getStatusCode(),response,handler.getContentType());\r
+ } else {\r
+ logger.error("Service Proxy status code: " + handler.getStatusCode());\r
+ 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
+ }\r
+ } catch (ClientProtocolException e) {\r
+ logger.error(e.getMessage());\r
+ e.printStackTrace();\r
+ commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "Client protocol exception", e.getMessage(), e.getStackTrace().toString()),"text/xml"); \r
+ } catch (IOException e) {\r
+ logger.error(e.getMessage());\r
+ e.printStackTrace();\r
+ commandResponse = new ClientCommandResponse(-1,CommandError.createErrorXml("init", String.valueOf(handler.getStatusCode()), "IO exception", e.getMessage(),e.getStackTrace().toString()),"text/xml"); \r
+ }\r
+ return commandResponse; \r
+ }\r
+ \r
+ /**\r
+ * Sets the URL of the Service Proxy that should service requests. \r
+ */\r
+ public void setServiceUrl (String url) { \r
+ serviceUrl = url;\r
+ }\r
+ \r
+ public Configuration getConfiguration () {\r
+ return config;\r
+ }\r
+\r
+ @Override\r
+ public String getServiceUrl() { \r
+ return serviceUrl;\r
+ }\r
+\r
+ /**\r
+ * Returns true if a Service Proxy URL was defined yet.\r
+ */\r
+ @Override\r
+ public boolean hasServiceUrl() {\r
+ return serviceUrl != null && serviceUrl.length()>0;\r
+ }\r
+ \r
+}\r