Generalizes file upload function and moves it into core
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 7 May 2013 01:48:34 +0000 (21:48 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 7 May 2013 01:48:34 +0000 (21:48 -0400)
pom.xml
src/main/java/com/indexdata/mkjsf/pazpar2/CommandResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2CommandResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/Pz2ProxyBean.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/InitDocUpload.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/data/Pazpar2Responses.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/SpResponses.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java
src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyCommandResponse.java
src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyInterface.java
src/main/java/com/indexdata/mkjsf/utils/FileUpload.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 02e741c..b426c28 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <groupId>org.apache.httpcomponents</groupId>\r
       <artifactId>httpclient</artifactId>\r
       <version>4.2.3</version>\r
-    </dependency>    \r
+    </dependency>\r
+    <!-- File upload -->\r
+    <dependency>\r
+      <groupId>org.apache.myfaces.tomahawk</groupId>\r
+      <artifactId>tomahawk20</artifactId>\r
+      <version>1.1.10</version>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>commons-io</groupId>\r
+      <artifactId>commons-io</artifactId>\r
+      <version>2.1</version>\r
+    </dependency>\r
+        \r
     \r
     <dependency>\r
       <groupId>com.indexdata</groupId>\r
index 59d72ff..573015f 100644 (file)
@@ -4,6 +4,6 @@ public interface CommandResponse {
   public int getStatusCode();\r
   public String getContentType();\r
   public String getResponseString();\r
-  public byte[] getBinaryResponse();\r
+  public byte[] getBytes();\r
   public boolean isBinary();\r
 }\r
index df14da7..27cf441 100644 (file)
@@ -56,7 +56,7 @@ public class Pz2CommandResponse implements CommandResponse {
   }\r
 \r
   @Override\r
-  public byte[] getBinaryResponse() {\r
+  public byte[] getBytes() {\r
     return content;\r
   }\r
 \r
index 87daf80..7bc0145 100644 (file)
@@ -15,6 +15,7 @@ import org.apache.log4j.Logger;
 \r
 import com.indexdata.mkjsf.config.ConfigurationReader;\r
 import com.indexdata.mkjsf.pazpar2.sp.ServiceProxyClient;\r
+import com.indexdata.mkjsf.pazpar2.sp.ServiceProxyCommandResponse;\r
 import com.indexdata.mkjsf.pazpar2.sp.ServiceProxyInterface;\r
 import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
@@ -124,22 +125,20 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface {
   }\r
 \r
   @Override\r
-  public String postInit() throws UnsupportedEncodingException, IOException {    \r
+  public ServiceProxyCommandResponse postInit() throws UnsupportedEncodingException, IOException {    \r
     String initDocPath = ((ServiceProxyClient)searchClient).getInitDocPaths().get(0);\r
     logger.info("Paths: " + ((ServiceProxyClient)searchClient).getInitDocPaths());\r
     logger.info("Path: " + initDocPath);\r
     pzresp.reset();\r
-    byte[] response = ((ServiceProxyClient)searchClient).postInitDoc(initDocPath + getInitFileName());\r
-    initDocResponse = new String(response,"UTF-8");\r
-    return initDocResponse;\r
+    ServiceProxyCommandResponse response = ((ServiceProxyClient)searchClient).postInitDoc(initDocPath + getInitFileName());    \r
+    return response;\r
   }\r
   \r
   @Override\r
-  public String postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException {    \r
+  public ServiceProxyCommandResponse postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException {    \r
     pzresp.reset();\r
-    byte[] response = ((ServiceProxyClient)searchClient).postInitDoc(initDoc,includeDebug);\r
-    initDocResponse = new String(response,"UTF-8");\r
-    return initDocResponse;\r
+    ServiceProxyCommandResponse response = ((ServiceProxyClient)searchClient).postInitDoc(initDoc,includeDebug);    \r
+    return response;\r
   }\r
 \r
   @Override\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/InitDocUpload.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/InitDocUpload.java
new file mode 100644 (file)
index 0000000..02b4c37
--- /dev/null
@@ -0,0 +1,42 @@
+package com.indexdata.mkjsf.pazpar2.commands.sp;\r
+\r
+import java.io.IOException;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.inject.Inject;\r
+import javax.inject.Named;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import com.indexdata.mkjsf.pazpar2.Pz2ProxyBean;\r
+import com.indexdata.mkjsf.utils.FileUpload;\r
+import com.indexdata.mkjsf.utils.Utils;\r
+\r
+@Named\r
+@SessionScoped\r
+public class InitDocUpload extends FileUpload {\r
+\r
+  private static Logger logger = Logger.getLogger(InitDocUpload.class);\r
+  private static final long serialVersionUID = 1846749236304941323L;\r
+  @Inject Pz2ProxyBean spBean;\r
+  private boolean includeDebug = false;\r
+\r
+  public String submit() throws IOException {\r
+    logger.info(Utils.objectId(this) + " submitting");\r
+    //String fileName = FilenameUtils.getName(uploadedFile.getName());\r
+    //String contentType = uploadedFile.getContentType();\r
+    byte[] bytes = uploadedFile.getBytes();\r
+    response = spBean.postInit(bytes, includeDebug);\r
+    return "";\r
+  }\r
+\r
+  public void setIncludeDebug(boolean bool) {\r
+    logger.info(Utils.objectId(this) + " setting debug to " + bool);\r
+    includeDebug = bool;\r
+  }\r
+\r
+  public boolean getIncludeDebug() {\r
+    return includeDebug;\r
+  }\r
+\r
+}\r
index fd0b9f0..600754f 100644 (file)
@@ -12,6 +12,7 @@ import org.apache.log4j.Logger;
 \r
 import com.indexdata.mkjsf.errors.ErrorHelper;\r
 import com.indexdata.mkjsf.errors.ErrorInterface;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses;\r
 \r
 @Named("pzresp") @SessionScoped\r
 public class Pazpar2Responses implements Serializable {\r
@@ -130,5 +131,9 @@ public class Pazpar2Responses implements Serializable {
     }\r
   }\r
 \r
+  public SpResponses getSp() {\r
+    // TODO:\r
+    return null;\r
+  }\r
 \r
 }\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/SpResponses.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/SpResponses.java
new file mode 100644 (file)
index 0000000..db58ca9
--- /dev/null
@@ -0,0 +1,9 @@
+package com.indexdata.mkjsf.pazpar2.data.sp;\r
+\r
+public class SpResponses {\r
+\r
+  public SpResponses() {\r
+    // TODO Auto-generated constructor stub\r
+  }\r
+\r
+}\r
index 12cc734..e52e797 100644 (file)
@@ -73,11 +73,11 @@ public class ServiceProxyClient implements SearchClient {
     logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));\r
     try {\r
       config = configReader.getConfiguration(this);      \r
-      serviceUrls = getMultiProperty(config.get(SERVICE_PROXY_URL));\r
+      serviceUrls = getMultiProperty(config.get(SERVICE_PROXY_URL),",");\r
       if (serviceUrls.size()==1) {\r
         selectedServiceUrl = serviceUrls.get(0);\r
       }\r
-      this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));\r
+      this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS),",");\r
       checkAuth = new AuthCommand(null);\r
       checkAuth.setParameterInState(new CommandParameter("action","=","check"));\r
       ipAuth = new AuthCommand(null);\r
@@ -88,10 +88,10 @@ public class ServiceProxyClient implements SearchClient {
     }    \r
   }\r
   \r
-  private List<String> getMultiProperty(String prop) {\r
+  private List<String> getMultiProperty(String prop, String separator) {\r
     List<String> props = new ArrayList<String>();\r
     if (prop != null) {      \r
-      StringTokenizer tokenizer = new StringTokenizer(prop,",");\r
+      StringTokenizer tokenizer = new StringTokenizer(prop,separator);\r
       while (tokenizer.hasMoreElements()) {\r
         props.add(tokenizer.nextToken());\r
       }     \r
@@ -252,7 +252,7 @@ public class ServiceProxyClient implements SearchClient {
     return null;\r
   }\r
   \r
-  public byte[] postInitDoc (String filePath) throws IOException {\r
+  public ServiceProxyCommandResponse postInitDoc (String filePath) throws IOException {\r
     logger.info("Looking to post the file in : [" + filePath +"]");\r
     HttpPost post = new HttpPost(selectedServiceUrl+"?command=init&includeDebug=yes");\r
     File initDoc = new File(filePath);\r
@@ -268,7 +268,7 @@ public class ServiceProxyClient implements SearchClient {
     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
+    return new ServiceProxyCommandResponse(handler.getStatusCode(),response,handler.getContentType());    \r
   }\r
   \r
   public List<String> getInitDocPaths () {\r
@@ -277,12 +277,12 @@ public class ServiceProxyClient implements SearchClient {
     return initDocPaths;\r
   }\r
   \r
-  public byte[] postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException {\r
+  public ServiceProxyCommandResponse postInitDoc(byte[] initDoc, boolean includeDebug) throws IOException {\r
     HttpPost post = new HttpPost(selectedServiceUrl+"?command=init" + (includeDebug? "&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
+    return new ServiceProxyCommandResponse(handler.getStatusCode(),response,handler.getContentType());    \r
   }\r
   \r
   public void setServiceProxyUrl (String url) {\r
index 6e8c683..90d4f2e 100644 (file)
@@ -48,7 +48,7 @@ public class ServiceProxyCommandResponse implements CommandResponse {
   }\r
 \r
   @Override\r
-  public byte[] getBinaryResponse() {    \r
+  public byte[] getBytes() {    \r
     return content;\r
   }\r
 \r
index abbb1ec..74d5f75 100644 (file)
@@ -9,8 +9,8 @@ public interface ServiceProxyInterface extends Pz2Interface {
   public String login(String navigateTo);  \r
   public void setInitFileName (String fileName);  \r
   public String getInitFileName();\r
-  public String postInit() throws UnsupportedEncodingException, IOException;\r
-  public String postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException;\r
+  public ServiceProxyCommandResponse postInit() throws UnsupportedEncodingException, IOException;\r
+  public ServiceProxyCommandResponse postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException;\r
   public String getInitResponse();\r
   public void setServiceProxyUrl(String url);\r
   public String getServiceProxyUrl();\r
diff --git a/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java b/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java
new file mode 100644 (file)
index 0000000..493102e
--- /dev/null
@@ -0,0 +1,60 @@
+package com.indexdata.mkjsf.utils;\r
+\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+\r
+import javax.faces.context.ExternalContext;\r
+import javax.faces.context.FacesContext;\r
+\r
+import org.apache.commons.io.FilenameUtils;\r
+import org.apache.log4j.Logger;\r
+import org.apache.myfaces.custom.fileupload.UploadedFile;\r
+\r
+import com.indexdata.mkjsf.pazpar2.CommandResponse;\r
+\r
+public abstract class FileUpload implements Serializable {\r
+\r
+  protected UploadedFile uploadedFile;\r
+  protected CommandResponse response;\r
+  private static Logger logger = Logger.getLogger(FileUpload.class);\r
+  private static final long serialVersionUID = 748784638056392862L;\r
+\r
+  public FileUpload() {\r
+  }\r
+\r
+  public abstract String submit() throws IOException;\r
+  \r
+  public UploadedFile getUploadedFile() {\r
+    return uploadedFile;\r
+  }\r
+\r
+  public void setUploadedFile(UploadedFile uploadedFile) {\r
+    logger.info(Utils.objectId(this) + " received an uploaded file [" + Utils.objectId(uploadedFile) + "]");\r
+    this.uploadedFile = uploadedFile;\r
+  }\r
+  \r
+  public void downloadDoc() throws IOException {\r
+    logger.info(Utils.objectId(this) + " got a download request");\r
+    FacesContext facesContext = FacesContext.getCurrentInstance();\r
+    ExternalContext externalContext = facesContext.getExternalContext();\r
+    externalContext.setResponseHeader("Content-Type", uploadedFile.getContentType());\r
+    externalContext.setResponseHeader("Content-Length", String.valueOf((uploadedFile.getBytes().length)));\r
+    externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + FilenameUtils.getBaseName(uploadedFile.getName()) + "\"");\r
+    externalContext.getResponseOutputStream().write(uploadedFile.getBytes());\r
+    facesContext.responseComplete();\r
+  }\r
+  \r
+  public void downloadResponse () throws IOException {\r
+    logger.info(Utils.objectId(this) + " got a download request");\r
+    FacesContext facesContext = FacesContext.getCurrentInstance();\r
+    ExternalContext externalContext = facesContext.getExternalContext();\r
+    externalContext.setResponseHeader("Content-Type", response.getContentType());\r
+    externalContext.setResponseHeader("Content-Length", String.valueOf((response.getBytes().length)));\r
+    externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"initresponse.xml\"");\r
+    externalContext.getResponseOutputStream().write(response.getBytes());\r
+    facesContext.responseComplete();\r
+  }\r
+\r
+\r
+\r
+}\r