-package com.indexdata.pz2utils4jsf.pazpar2.sp;\r
-\r
-import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.ByteArrayOutputStream;\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.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.masterkey.config.MissingMandatoryParameterException;\r
-import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
-import com.indexdata.pz2utils4jsf.config.Configuration;\r
-import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
-import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
-import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
-import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
-import com.indexdata.pz2utils4jsf.utils.Utils;\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
- public static final String SERVICE_PROXY_URL = "SERVICE_PROXY_URL";\r
- public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";\r
- private String serviceUrl = "undefined";\r
- private String[] initDocPaths = null;\r
- private Configuration config = null;\r
- \r
- ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
- private HttpClient client;\r
- private ServiceProxyUser user;\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) {\r
- logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
- try {\r
- config = configReader.getConfiguration(this); \r
- serviceUrl = config.getMandatory(SERVICE_PROXY_URL); \r
- this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS)); \r
- } catch (ConfigurationException c) {\r
- c.printStackTrace();\r
- } catch (MissingMandatoryParameterException mmp) {\r
- mmp.printStackTrace();\r
- } \r
- }\r
- \r
- private String[] getMultiProperty(String prop) { \r
- if (prop != null) {\r
- return prop.split(",");\r
- } else {\r
- return null;\r
- }\r
- }\r
- \r
- public boolean authenticate (AuthenticationEntity user) {\r
- try { \r
- logger.info("Authenticating [" + user.getProperty("name") + "]");\r
- this.user = (ServiceProxyUser) user;\r
- Pazpar2Command auth = new Pazpar2Command("auth",null);\r
- auth.setParametersInState(new CommandParameter("action","=","login"), \r
- new CommandParameter("username","=",user.getProperty("name")), \r
- new CommandParameter("password","=",user.getProperty("password")));\r
- byte[] response = send(auth);\r
- String responseStr = new String(response,"UTF-8");\r
- logger.info(responseStr); \r
- if (responseStr.contains("FAIL")) {\r
- return false;\r
- } else {\r
- return true;\r
- } \r
- } catch (ClientProtocolException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- return false;\r
- } catch (IOException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- return false;\r
- } \r
- }\r
- \r
- public boolean checkAuthentication () {\r
- try {\r
- Pazpar2Command check = new Pazpar2Command("auth",null);\r
- check.setParameter(new CommandParameter("action","=","check"));\r
- byte[] response = send(check);\r
- logger.info(new String(response,"UTF-8"));\r
- } catch (ClientProtocolException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- return false;\r
- } catch (IOException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- return false;\r
- } \r
- return true;\r
- \r
- }\r
- \r
- public boolean isAuthenticatingClient () {\r
- return true;\r
- }\r
- \r
- public boolean isAuthenticated () {\r
- if (user.getProperty("name") != null && user.getProperty("password") != null) {\r
- return checkAuthentication();\r
- } else {\r
- return false;\r
- }\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
- private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
- String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
- logger.info("Sending request "+url); \r
- HttpGet httpget = new HttpGet(url); \r
- byte[] response = client.execute(httpget, handler); \r
- return response;\r
- }\r
- \r
- public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
- private StatusLine statusLine = 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
- } \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
- }\r
-\r
- public int getStatusCode () {\r
- return handler.getStatusCode();\r
- }\r
- \r
- public String getReasonPhrase() {\r
- return handler.getReasonPhrase();\r
- }\r
-\r
- @Override\r
- public void setSearchCommand(Pazpar2Command command) {\r
- // Do nothing, Service Proxy is handling this \r
- }\r
-\r
- @Override\r
- public CommandResponse executeCommand(Pazpar2Command command,\r
- ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
- byte[] response = send(command);\r
- baos.write(response);\r
- return new ServiceProxyClientCommandResponse(getStatusCode(), new String(response,"UTF-8")); \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
- @Override\r
- public Map<String, String> getDefaults() { \r
- return new HashMap<String,String>();\r
- }\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);\r
- return null;\r
- }\r
- \r
- public byte[] 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 response;\r
- }\r
- \r
- public String[] getInitDocPaths () {\r
- logger.debug("Get init doc paths ");\r
- logger.debug("length: " + initDocPaths.length);\r
- return initDocPaths;\r
- }\r
- \r
- public byte[] postInitDoc(byte[] initDoc) throws IOException {\r
- HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");\r
- post.setEntity(new ByteArrayEntity(initDoc));\r
- byte[] response = client.execute(post, handler);\r
- logger.debug("Response on POST was: " + new String(response,"UTF-8")); \r
- return response;\r
- }\r
- \r
- public void setServiceProxyUrl (String url) {\r
- serviceUrl = url;\r
- }\r
- \r
- public String getServiceProxyUrl () {\r
- return serviceUrl;\r
- }\r
- \r
- public Configuration getConfiguration () {\r
- return config;\r
- }\r
- \r
-}\r