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