From: Niels Erik G. Nielsen Date: Thu, 9 May 2013 17:52:56 +0000 (-0400) Subject: Tweaks init command and init doc submission. X-Git-Tag: v0.0.7~117 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=641505fae22dcf83a1794707dcc39e821dbdd0ea;p=mkjsf-moved-to-github.git Tweaks init command and init doc submission. Makes submitting SP init docs available directly on "pz2x" (SP extension bean) and puts the init response into the regular flow of processing for display as any other response. Makes it optional to reset the init response when resetting search/record/show data. As before, search data are reset on renewed search or when the server or authentication changes. Init response is now only reset on changed init (whether it happens implicit or explicit). --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java index 6185dae..902a497 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java @@ -96,13 +96,12 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria errors.addConfigurationError(new ConfigurationError("Search Client","Configuration",e.getMessage())); } logger.info(configReader.document()); - pzresp.reset(); + pzresp.resetAllSessionData(); } - public void resetSearchAndResults () { + public void resetSearchAndRecordCommands () { pzreq.getRecord().removeParametersInState(); - pzreq.getSearch().removeParametersInState(); - pzresp.reset(); + pzreq.getSearch().removeParametersInState(); } @@ -113,7 +112,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria public void doSearch() { stateMgr.hasPendingStateChange("search",false); - pzresp.reset(); + pzresp.resetSearchResponses(); // resets some record and show command parameters without // changing state or creating state change feedback pzreq.getRecord().removeParametersInState(); @@ -186,7 +185,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria } } else { logger.debug("Skipped requests for " + commands + " as there's not yet a query."); - pzresp.reset(); + pzresp.resetSearchResponses(); return "0"; } } else { @@ -327,7 +326,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria if (url!=null && searchClient != null && !url.equals(searchClient.getServiceUrl())) { pzreq.getRecord().removeParametersInState(); pzreq.getSearch().removeParametersInState(); - pzresp.reset(); + pzresp.resetAllSessionData(); user.clear(); searchClient.setServiceUrl(url); } @@ -451,7 +450,8 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria private void setServiceType(String type) { if (!serviceType.equals(type) && !serviceType.equals(SERVICE_TYPE_TBD)) { - resetSearchAndResults(); + resetSearchAndRecordCommands(); + pzresp.resetAllSessionData(); } serviceType = type; if (serviceType.equals(SERVICE_TYPE_PZ2)) { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java index c5b93ea..87a7322 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java @@ -9,15 +9,20 @@ import javax.inject.Inject; import javax.inject.Named; import org.apache.log4j.Logger; +import org.apache.myfaces.custom.fileupload.UploadedFile; import com.indexdata.mkjsf.config.ConfigurationReader; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands; import com.indexdata.mkjsf.pazpar2.commands.sp.InitDocUpload; +import com.indexdata.mkjsf.pazpar2.data.InitResponse; +import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; +import com.indexdata.mkjsf.pazpar2.data.ResponseParser; import com.indexdata.mkjsf.pazpar2.data.Responses; import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; +import com.indexdata.mkjsf.utils.Utils; @Named("pz2x") @SessionScoped -public class ServiceProxyExtensions implements ServiceProxyInterface, Serializable { +public class ServiceProxyExtensions implements ServiceProxyInterface, Serializable { private static final long serialVersionUID = 4221824985678758225L; private static Logger logger = Logger.getLogger(ServiceProxyExtensions.class); @@ -52,8 +57,9 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializa @Override public String login(String navigateTo) { logger.info("doing login by " + user + " using " + pz2 + " and client " + pz2.getSpClient()); - pz2.getSpClient().authenticate(user); - pz2.resetSearchAndResults(); + pz2.resetSearchAndRecordCommands(); + pzresp.resetAllSessionData(); + pz2.getSpClient().authenticate(user); return navigateTo; } @@ -62,7 +68,8 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializa if (user.isAuthenticated()) { user.clear(); } - pz2.resetSearchAndResults(); + pz2.resetSearchAndRecordCommands(); + pzresp.resetAllSessionData(); pz2.getSpClient().ipAuthenticate(user); } } @@ -87,14 +94,16 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializa String initDocPath = pz2.getSpClient().getInitDocPaths().get(0); logger.info("Paths: " + pz2.getSpClient().getInitDocPaths()); logger.info("Path: " + initDocPath); - pz2.resetSearchAndResults(); + pz2.resetSearchAndRecordCommands(); + pzresp.resetAllSessionData(); ClientCommandResponse response = pz2.getSpClient().postInitDoc(initDocPath + getInitFileName()); return response; } @Override public ClientCommandResponse postInit(byte[] initDoc, boolean includeDebug) throws UnsupportedEncodingException, IOException { - pz2.resetSearchAndResults(); + pz2.resetSearchAndRecordCommands(); + pzresp.resetAllSessionData(); ClientCommandResponse response = pz2.getSpClient().postInitDoc(initDoc,includeDebug); return response; } @@ -104,8 +113,36 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializa return initDocResponse; } + public void setUploadedInitDoc (UploadedFile uploadedFile) { + initDocUpload.setUploadedFile(uploadedFile); + } + + public UploadedFile getUploadedInitDoc () { + return initDocUpload.getUploadedFile(); + } + + public void submitInitDoc () throws IOException { + HttpResponseWrapper response = initDocUpload.submit(); + ResponseDataObject responseObject = ResponseParser.getParser().getDataObject(response.getResponseString()); + logger.info("Putting init response to : " + Utils.objectId(pzresp)); + pzresp.put("init", responseObject); + } + + public void setIncludeInitDocDebug(boolean bool) { + logger.info(Utils.objectId(this) + " setting debug to " + bool); + initDocUpload.setIncludeDebug(bool); + } + + public boolean getIncludeInitDocDebug() { + return initDocUpload.getIncludeDebug(); + } + + // TODO: Remove when possible public InitDocUpload getInitDocUpload () { return initDocUpload; } + + + } 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 index 7a2fb46..2770618 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/InitDocUpload.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/InitDocUpload.java @@ -6,7 +6,10 @@ import javax.enterprise.context.SessionScoped; import javax.inject.Named; import org.apache.log4j.Logger; +import org.apache.myfaces.custom.fileupload.UploadedFile; +import com.indexdata.mkjsf.pazpar2.ClientCommandResponse; +import com.indexdata.mkjsf.pazpar2.HttpResponseWrapper; import com.indexdata.mkjsf.pazpar2.ServiceProxyExtensions; import com.indexdata.mkjsf.utils.FileUpload; import com.indexdata.mkjsf.utils.Utils; @@ -15,24 +18,34 @@ import com.indexdata.mkjsf.utils.Utils; @SessionScoped public class InitDocUpload extends FileUpload { + + private UploadedFile uploadedFile = null; private static Logger logger = Logger.getLogger(InitDocUpload.class); private static final long serialVersionUID = 1846749236304941323L; private boolean includeDebug = false; - private ServiceProxyExtensions spBean; + private ServiceProxyExtensions spBean; public InitDocUpload(ServiceProxyExtensions spBean) { this.spBean = spBean; } - public String submit() throws IOException { + public HttpResponseWrapper 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 ""; + return response; + } + + 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 setIncludeDebug(boolean bool) { logger.info(Utils.objectId(this) + " setting debug to " + bool); includeDebug = bool; diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java index 6438f9a..6599082 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java @@ -4,8 +4,8 @@ public class InitResponse extends ResponseDataObject { private static final long serialVersionUID = -1479775157276901600L; - public InitResponse() { - // TODO Auto-generated constructor stub + public String getStatus() { + return getOneElementValue("status"); } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java index 9466473..4af0ec2 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java @@ -18,19 +18,6 @@ import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; -import com.indexdata.mkjsf.pazpar2.data.ByTarget; -import com.indexdata.mkjsf.pazpar2.data.Hit; -import com.indexdata.mkjsf.pazpar2.data.Location; -import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; -import com.indexdata.mkjsf.pazpar2.data.RecordResponse; -import com.indexdata.mkjsf.pazpar2.data.ShowResponse; -import com.indexdata.mkjsf.pazpar2.data.StatResponse; -import com.indexdata.mkjsf.pazpar2.data.Target; -import com.indexdata.mkjsf.pazpar2.data.TermListResponse; -import com.indexdata.mkjsf.pazpar2.data.TermListsResponse; -import com.indexdata.mkjsf.pazpar2.data.TermResponse; -import com.indexdata.mkjsf.pazpar2.data.TermXTargetResponse; - public class ResponseParser extends DefaultHandler { private XMLReader xmlReader = null; @@ -41,7 +28,7 @@ public class ResponseParser extends DefaultHandler { private static Logger logger = Logger.getLogger(ResponseParser.class); public static final List docTypes = - Arrays.asList("bytarget","termlist","show","stat","record","search"); + Arrays.asList("bytarget","termlist","show","stat","record","search","init"); public ResponseParser() { try { @@ -97,12 +84,18 @@ public class ResponseParser extends DefaultHandler { */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { - if (localName.equals("show")) { + if (localName.equals("init")) { + currentElement = new InitResponse(); + } else if (localName.equals("search")) { + currentElement = new SearchResponse(); + } else if (localName.equals("show")) { currentElement = new ShowResponse(); } else if (localName.equals("hit")) { currentElement = new Hit(); } else if (localName.equals("location")) { currentElement = new Location(); + } else if (localName.equals("record")) { + currentElement = new RecordResponse(); } else if (localName.equals("bytarget")) { currentElement = new ByTarget(); } else if (localName.equals("target")) { @@ -122,10 +115,6 @@ public class ResponseParser extends DefaultHandler { currentElement = new TermResponse(); } ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement); - } else if (localName.equals("record")) { - currentElement = new RecordResponse(); - } else if (localName.equals("search")) { - currentElement = new SearchResponse(); } else if (localName.equals("applicationerror")) { currentElement = new CommandError(); } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java index 6f16348..80d9ce3 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java @@ -13,6 +13,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; +import com.indexdata.mkjsf.utils.Utils; @Named("pzresp") @SessionScoped public class Responses implements Serializable { @@ -69,15 +70,26 @@ public class Responses implements Serializable { return error; } - public void reset() { - logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); - dataObjects = new ConcurrentHashMap(); + public void resetSearchResponses() { + logger.debug("Resetting show,stat,termlist,bytarget,record,search response objects."); dataObjects.put("show", new ShowResponse()); dataObjects.put("stat", new StatResponse()); dataObjects.put("termlist", new TermListsResponse()); dataObjects.put("bytarget", new ByTarget()); dataObjects.put("record", new RecordResponse()); - dataObjects.put("search", new SearchResponse()); + dataObjects.put("search", new SearchResponse()); + } + + public void resetAllSessionData () { + logger.debug("Resetting all response objects"); + dataObjects = new ConcurrentHashMap(); + resetSearchResponses(); + dataObjects.put("init", new InitResponse()); + } + + public InitResponse getInit () { + logger.info("Request to show init response from " + Utils.objectId(this)); + return ((InitResponse) dataObjects.get("init")); } public ShowResponse getShow () { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java index 84b7e0c..909e05b 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java @@ -4,4 +4,8 @@ public class SearchResponse extends ResponseDataObject { private static final long serialVersionUID = -3320013021497018972L; + public String getStatus() { + return getOneElementValue("status"); + } + } diff --git a/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java b/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java index 71598f2..ba8c242 100644 --- a/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java +++ b/src/main/java/com/indexdata/mkjsf/utils/FileUpload.java @@ -14,7 +14,7 @@ import com.indexdata.mkjsf.pazpar2.HttpResponseWrapper; public abstract class FileUpload implements Serializable { - protected UploadedFile uploadedFile; + protected HttpResponseWrapper response; private static Logger logger = Logger.getLogger(FileUpload.class); private static final long serialVersionUID = 748784638056392862L; @@ -22,25 +22,20 @@ public abstract class FileUpload implements Serializable { public FileUpload() { } - public abstract String submit() throws IOException; + public abstract HttpResponseWrapper 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 abstract UploadedFile getUploadedFile(); + + public abstract void setUploadedFile(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()); + externalContext.setResponseHeader("Content-Type", getUploadedFile().getContentType()); + externalContext.setResponseHeader("Content-Length", String.valueOf((getUploadedFile().getBytes().length))); + externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + FilenameUtils.getBaseName(getUploadedFile().getName()) + "\""); + externalContext.getResponseOutputStream().write(getUploadedFile().getBytes()); facesContext.responseComplete(); }