<groupId>org.jboss.weld.servlet</groupId>\r
<artifactId>weld-servlet</artifactId>\r
<version>1.1.10.Final</version> \r
- </dependency>\r
+ </dependency> \r
+ <dependency>\r
+ <groupId>org.apache.httpcomponents</groupId>\r
+ <artifactId>httpclient</artifactId>\r
+ <version>4.0-beta1</version>\r
+ </dependency> \r
\r
<dependency>\r
<groupId>com.indexdata</groupId>\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+public interface CommandResponse {\r
+ public int getStatusCode();\r
+ public String getContentType();\r
+ public String getResponseString();\r
+}\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.masterkey.pazpar2.client.ClientCommand;\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
\r
\r
private static Logger logger = Logger.getLogger(CommandThread.class);\r
Pazpar2Command command;\r
- Pazpar2Client client;\r
+ SearchClient client;\r
private ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
private StringBuilder response = new StringBuilder(""); \r
\r
- public CommandThread (Pazpar2Command command, Pazpar2Client client) {\r
+ public CommandThread (Pazpar2Command command, SearchClient client) {\r
this.command = command;\r
this.client = client;\r
}\r
* \r
*/\r
public void run() {\r
- ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+ \r
if (command.getName().equals("search")) {\r
- client.setSearchCommand(clientCommand);\r
+ client.setSearchCommand(command);\r
}\r
try {\r
long start = System.currentTimeMillis();\r
- Pazpar2HttpResponse httpResponse = client.executeCommand(clientCommand, baos);\r
- if (httpResponse.getStatusCode()==200) {\r
- response.append(baos.toString("UTF-8")); \r
- } else if (httpResponse.getStatusCode()==417) {\r
+ CommandResponse commandResponse = client.executeCommand(command, baos);\r
+ if (commandResponse.getStatusCode()==200) {\r
+ response.append(commandResponse.getResponseString()); \r
+ } else if (commandResponse.getStatusCode()==417) {\r
logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8"));\r
response.append(CommandError.insertPazpar2ErrorXml(command.getName(), "Expectation failed (417)", baos.toString("UTF-8"))); \r
} else {\r
String resp = baos.toString("UTF-8");\r
- logger.error("Pazpar2 status code was " + httpResponse.getStatusCode() + ": " + resp);\r
- throw new Pazpar2ErrorException(resp,httpResponse.getStatusCode(),resp,null);\r
+ logger.error("Pazpar2 status code was " + commandResponse.getStatusCode() + ": " + resp);\r
+ throw new Pazpar2ErrorException(resp,commandResponse.getStatusCode(),resp,null);\r
} \r
long end = System.currentTimeMillis(); \r
logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." );\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
+import javax.inject.Named;\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.impl.client.DefaultHttpClient;\r
+import org.apache.http.util.EntityUtils;\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
+import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+@Named @SessionScoped @Alternative\r
+public class ProxyPz2Client implements SearchClient {\r
+\r
+ private static final long serialVersionUID = -4031644009579840277L;\r
+ private static Logger logger = Logger.getLogger(ProxyPz2Client.class);\r
+ \r
+ ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();\r
+ HttpClient client = new DefaultHttpClient(); \r
+\r
+ public ProxyPz2Client(HttpClient client) {\r
+ }\r
+ \r
+ @Override\r
+ public void configure (Pz2Configurator configurator) {\r
+ logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator));\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 String send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
+ String url = command.getEncodedQueryString(); \r
+ logger.debug("Sending request "+url); \r
+ HttpGet httpget = new HttpGet(url); \r
+ byte[] response = client.execute(httpget, handler); \r
+ return new String(response);\r
+ }\r
+\r
+ \r
+ public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {\r
+ private StatusLine statusLine = null;\r
+ public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {\r
+ HttpEntity entity = response.getEntity(); \r
+ statusLine = response.getStatusLine();\r
+ if (entity != null) { \r
+ return EntityUtils.toByteArray(entity);\r
+ } else {\r
+ return null;\r
+ }\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
+ String response = send(command);\r
+ return new ProxyPz2ClientCommandResponse(getStatusCode(), response); \r
+ }\r
+\r
+ public ProxyPz2Client cloneMe() {\r
+ return this;\r
+ }\r
+\r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+public class ProxyPz2ClientCommandResponse implements CommandResponse {\r
+\r
+ private int statusCode = 0;\r
+ private String content = null;\r
+ \r
+ public ProxyPz2ClientCommandResponse(int statusCode, String content) {\r
+ this.statusCode = statusCode;\r
+ this.content = content;\r
+ }\r
+\r
+ @Override\r
+ public int getStatusCode() {\r
+ return statusCode;\r
+ }\r
+\r
+ @Override\r
+ public String getContentType() {\r
+ return "text/xml;charset=UTF-8"; \r
+ }\r
+\r
+ @Override\r
+ public String getResponseString() {\r
+ return content;\r
+ }\r
+\r
+}\r
private static Logger logger = Logger.getLogger(Pz2Bean.class);\r
\r
Pz2Session pz2; \r
- @Inject Pz2Configurator pz2conf;\r
- \r
+ @Inject Pz2Configurator configurator;\r
+ @Inject SearchClient searchClient;\r
+ \r
public Pz2Bean () {\r
logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]");\r
}\r
public void initiatePz2Session() {\r
logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next.");\r
pz2 = new Pz2Session();\r
- logger.debug(Utils.objectId(this) + " will forward configuration to the new Pz2Session [" + Utils.objectId(pz2) + "]");\r
- pz2.init(pz2conf);\r
+ logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
+ + Utils.objectId(configurator) + "] on session [" \r
+ + Utils.objectId(pz2) + "]" );\r
+ pz2.init(searchClient,configurator);\r
}\r
\r
/* (non-Javadoc)\r
package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
import java.util.ArrayList;\r
import java.util.List;\r
import java.util.Map;\r
\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.masterkey.pazpar2.client.ClientCommand;\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
-import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;\r
-import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
-import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
-import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\r
import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
\r
@Named @SessionScoped \r
public class Pz2Session implements Pz2Interface {\r
- \r
+ \r
+ private static final long serialVersionUID = 3947514708343320514L;\r
private static Logger logger = Logger.getLogger(Pz2Session.class);\r
\r
private Map<String,Pazpar2ResponseData> dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
private QueryStates queryStates = new QueryStates();\r
+ private ErrorHelper errorHelper = null;\r
\r
- private static final long serialVersionUID = 3947514708343320514L; \r
- private Pazpar2ClientConfiguration cfg = null;\r
- private Pazpar2Client client = null; \r
+ private List<ErrorInterface> configurationErrors = null;\r
+ private SearchClient searchClient = null; \r
private TargetFilter targetFilter = null; \r
private ResultsPager pager = null; \r
- private ErrorHelper errorHelper = null;\r
- private List<ErrorInterface> configurationErrors = null;\r
- \r
+ \r
public Pz2Session () {\r
logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); \r
}\r
\r
- public void init(Pz2Configurator pz2conf) {\r
- if (client==null) {\r
- configurationErrors = new ArrayList<ErrorInterface>();\r
- errorHelper = new ErrorHelper(pz2conf);\r
- logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf));\r
- try {\r
- cfg = new Pazpar2ClientConfiguration(pz2conf.getConfig());\r
- } catch (ProxyErrorException pe) {\r
- logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
- configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper));\r
- } catch (ConfigurationException io) {\r
- logger.error("Could not configure Pazpar2 client: " + io.getMessage());\r
- configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper));\r
- }\r
- if (cfg != null) {\r
- try {\r
- client = new Pazpar2ClientGeneric(cfg); \r
- } catch (ProxyErrorException pe) {\r
- logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage());\r
- configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); \r
- } \r
- if (hasConfigurationErrors()) {\r
- logger.info("Found " + configurationErrors.size() + " configuration errors");\r
- }\r
- }\r
- resetDataObjects();\r
- } else {\r
- logger.warn("Attempt to configure session but it already has a configured client");\r
- }\r
+ public void init(SearchClient searchClient, Pz2Configurator configurator) {\r
+ configurationErrors = new ArrayList<ErrorInterface>();\r
+ errorHelper = new ErrorHelper(configurator); \r
+ logger.debug(Utils.objectId(this) + " will configure search client for the session");\r
+ try {\r
+ searchClient.configure(configurator);\r
+ \r
+ // The cloning is a hack: \r
+ // At the time of writing this search client is injected using Weld. \r
+ // However, the client is used for asynchronously sending off requests\r
+ // to the server AND propagation of context to threads is not supported.\r
+ // Trying so will throw a WELD-001303 error. To avoid that, a context\r
+ // free client is spawned from the context dependent one. \r
+ this.searchClient = searchClient.cloneMe();\r
+ \r
+ } catch (ConfigurationException e) {\r
+ logger.info("Found " + configurationErrors.size() + " configuration errors"); \r
+ } \r
+ resetDataObjects();\r
}\r
\r
public void doSearch(String query) {\r
List<CommandThread> threadList = new ArrayList<CommandThread>();\r
StringTokenizer tokens = new StringTokenizer(commands,",");\r
while (tokens.hasMoreElements()) {\r
- threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); \r
+ threadList.add(new CommandThread(getCommand(tokens.nextToken()),searchClient)); \r
}\r
for (CommandThread thread : threadList) {\r
thread.start();\r
}\r
for (CommandThread thread : threadList) {\r
String commandName = thread.getCommand().getName();\r
- Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
+ String response = thread.getResponse();\r
+ logger.debug("Response was: " + response);\r
+ Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
dataObjects.put(commandName, responseObject); \r
}\r
return getActiveClients();\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
+import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
+import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+\r
+\r
+public interface SearchClient extends Serializable {\r
+\r
+ public void configure(Pz2Configurator configurator) throws ConfigurationException;\r
+ public void setSearchCommand(Pazpar2Command command);\r
+ public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException;\r
+ public SearchClient cloneMe();\r
+ \r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
+import javax.inject.Named;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.masterkey.pazpar2.client.ClientCommand;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
+import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
+import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
+import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
+\r
+@Named @SessionScoped @Alternative\r
+public class StraightPz2Client implements SearchClient {\r
+\r
+ private static final long serialVersionUID = 5414266730169982028L;\r
+ private static Logger logger = Logger.getLogger(StraightPz2Client.class);\r
+ private Pazpar2Client client = null;\r
+ private Pazpar2ClientConfiguration cfg = null; \r
+ \r
+ public StraightPz2Client() {}\r
+ \r
+ public void configure(Pz2Configurator configurator) throws ConfigurationException {\r
+ logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(configurator));\r
+ try {\r
+ cfg = new Pazpar2ClientConfiguration(configurator.getConfig());\r
+ } catch (ProxyErrorException pe) {\r
+ logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
+ throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe);\r
+ } \r
+ if (cfg != null) {\r
+ try {\r
+ client = new Pazpar2ClientGeneric(cfg); \r
+ } catch (ProxyErrorException pe) {\r
+ logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
+ throw new ConfigurationException("Could not configure StraightPz2Client: "+ pe.getMessage(),pe);\r
+ }\r
+ } else {\r
+ logger.error("There was a problem creating StraightPz2Client. Client is null after configuration.");\r
+ throw new ConfigurationException("Pazpar2Client is null after configuration");\r
+ } \r
+ }\r
+\r
+ @Override\r
+ public void setSearchCommand(Pazpar2Command command) {\r
+ ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+ client.setSearchCommand(clientCommand);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
+ throws Pazpar2ErrorException, IOException {\r
+ ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
+ Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
+ return new StraightPz2CommandResponse(pz2HttpResponse,baos);\r
+ }\r
+\r
+ public StraightPz2Client cloneMe() {\r
+ logger.debug("Cloning StraightPz2Client");\r
+ StraightPz2Client clone = new StraightPz2Client();\r
+ clone.client = this.client;\r
+ clone.cfg = this.cfg;\r
+ return clone;\r
+ }\r
+}\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.UnsupportedEncodingException;\r
+\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse;\r
+\r
+public class StraightPz2CommandResponse implements CommandResponse {\r
+ \r
+ private Pazpar2HttpResponse pz2httpResponse = null;\r
+ private ByteArrayOutputStream content = null;\r
+ \r
+ public StraightPz2CommandResponse(Pazpar2HttpResponse pz2response, ByteArrayOutputStream content) {\r
+ pz2httpResponse = pz2response;\r
+ this.content = content;\r
+ }\r
+\r
+ @Override\r
+ public int getStatusCode() { \r
+ return pz2httpResponse.getStatusCode();\r
+ }\r
+\r
+ @Override\r
+ public String getContentType() {\r
+ return pz2httpResponse.getContentType();\r
+ }\r
+\r
+ @Override\r
+ public String getResponseString() {\r
+ try {\r
+ return content.toString("UTF-8");\r
+ } catch (UnsupportedEncodingException e) { \r
+ e.printStackTrace();\r
+ return null;\r
+ }\r
+ }\r
+\r
+\r
+}\r