package com.indexdata.mkjsf.pazpar2.data;\r
\r
+import java.io.IOException;\r
import java.io.Serializable;\r
+import java.io.UnsupportedEncodingException;\r
import java.util.List;\r
import java.util.Map;\r
import java.util.concurrent.ConcurrentHashMap;\r
\r
import javax.enterprise.context.SessionScoped;\r
+import javax.faces.context.ExternalContext;\r
+import javax.faces.context.FacesContext;\r
import javax.inject.Named;\r
\r
import org.apache.log4j.Logger;\r
\r
import com.indexdata.mkjsf.errors.ErrorHelper;\r
import com.indexdata.mkjsf.errors.ErrorInterface;\r
-import com.indexdata.mkjsf.pazpar2.data.sp.SpResponses;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse;\r
+import com.indexdata.mkjsf.utils.Utils;\r
\r
@Named("pzresp") @SessionScoped\r
public class Responses implements Serializable {\r
return error; \r
}\r
\r
- public void reset() {\r
- logger.debug("Resetting show,stat,termlist,bytarget,search response objects.");\r
- dataObjects = new ConcurrentHashMap<String,ResponseDataObject>();\r
+ public void resetSearchResponses() {\r
+ logger.debug("Resetting show,stat,termlist,bytarget,record,search response objects.");\r
dataObjects.put("show", new ShowResponse());\r
dataObjects.put("stat", new StatResponse());\r
dataObjects.put("termlist", new TermListsResponse());\r
dataObjects.put("bytarget", new ByTarget());\r
dataObjects.put("record", new RecordResponse());\r
- dataObjects.put("search", new SearchResponse());\r
+ dataObjects.put("search", new SearchResponse()); \r
}\r
-\r
+ \r
+ public void resetAllSessionData () {\r
+ logger.debug("Resetting all response objects");\r
+ dataObjects = new ConcurrentHashMap<String,ResponseDataObject>(); \r
+ resetSearchResponses();\r
+ dataObjects.put("init", new InitResponse());\r
+ dataObjects.put("auth", new AuthResponse());\r
+ dataObjects.put("categories", new CategoriesResponse());\r
+ }\r
+ \r
+ public InitResponse getInit () { \r
+ return ((InitResponse) dataObjects.get("init"));\r
+ }\r
+ \r
public ShowResponse getShow () {\r
return ((ShowResponse) dataObjects.get("show"));\r
}\r
public TermListsResponse getTermLists () {\r
return ((TermListsResponse) dataObjects.get("termlist"));\r
}\r
- \r
+ \r
public List<TermResponse> getFacetTerms (String facet, int count) {\r
return (getTermLists().getTermList(facet).getTerms(count));\r
}\r
return ((ByTarget) dataObjects.get("bytarget"));\r
}\r
\r
+ // Service Proxy extras \r
+ public AuthResponse getAuth () {\r
+ return ((AuthResponse) dataObjects.get("auth"));\r
+ }\r
+\r
+ public CategoriesResponse getCategories() {\r
+ return ((CategoriesResponse) dataObjects.get("categories"));\r
+ }\r
+ // Service Proxy extras\r
+ \r
+\r
public ResponseDataObject getResponseObject (String name) {\r
return dataObjects.get(name);\r
}\r
}\r
\r
public String getActiveClients() { \r
- if (getShow()!=null) {\r
+ if (getShow()!=null && getShow().getActiveClients().length()>0) {\r
logger.debug("Active clients: "+getShow().getActiveClients());\r
- return getShow().getActiveClients();\r
+ return String.valueOf(\r
+ Math.max(Integer.parseInt(getShow().getActiveClients()),\r
+ getStat().getActiveClients()));\r
} else {\r
return "";\r
}\r
}\r
\r
- public SpResponses getSp() {\r
- // TODO:\r
- return null;\r
+ public void download(String commandName) throws UnsupportedEncodingException, IOException {\r
+ logger.info(Utils.objectId(this) + " got a download request for "\r
+ + commandName);\r
+ ResponseDataObject object = dataObjects.get(commandName); \r
+ FacesContext facesContext = FacesContext.getCurrentInstance();\r
+ ExternalContext externalContext = facesContext.getExternalContext();\r
+ if (object.getIsBinary()) {\r
+ externalContext.setResponseHeader("Content-Type","application/octet-stream");\r
+ externalContext.setResponseHeader("Content-Length",String.valueOf(object.getBinary().length));\r
+ externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + commandName + ".data\"");\r
+ externalContext.getResponseOutputStream().write(object.getBinary()); \r
+ } else {\r
+ externalContext.setResponseHeader("Content-Type","application/xml; charset=\"utf-8\"");\r
+ externalContext.setResponseHeader("Content-Length",String.valueOf(dataObjects.get(commandName).getXml().getBytes("UTF-8").length));\r
+ externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + commandName + ".xml\"");\r
+ externalContext.getResponseOutputStream().write(dataObjects.get(commandName).getXml().getBytes("UTF-8"));\r
+ }\r
+ facesContext.responseComplete();\r
}\r
-\r
-}\r
+ \r
+}
\ No newline at end of file