use of org.springframework.extensions.webscripts.Cache in project records-management by Alfresco.
the class BaseTransferWebScript method execute.
/**
* @see org.alfresco.web.scripts.WebScript#execute(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
*/
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
File tempFile = null;
try {
// retrieve requested format
String format = req.getFormat();
// construct model for template
Status status = new Status();
Cache cache = new Cache(getDescription().getRequiredCache());
Map<String, Object> model = new HashMap<String, Object>();
model.put("status", status);
model.put("cache", cache);
// get the parameters that represent the NodeRef, we know they are present
// otherwise this webscript would not have matched
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String storeType = templateVars.get("store_type");
String storeId = templateVars.get("store_id");
String nodeId = templateVars.get("id");
String transferId = templateVars.get("transfer_id");
// create and return the file plan NodeRef
NodeRef filePlan = new NodeRef(new StoreRef(storeType, storeId), nodeId);
if (logger.isDebugEnabled()) {
logger.debug("Retrieving transfer '" + transferId + "' from file plan: " + filePlan);
}
// ensure the file plan exists
if (!this.nodeService.exists(filePlan)) {
status.setCode(HttpServletResponse.SC_NOT_FOUND, "Node " + filePlan.toString() + " does not exist");
Map<String, Object> templateModel = createTemplateParameters(req, res, model);
sendStatus(req, res, status, cache, format, templateModel);
return;
}
// ensure the node is a filePlan object
if (!filePlanService.isFilePlan(filePlan)) {
status.setCode(HttpServletResponse.SC_BAD_REQUEST, "Node " + filePlan.toString() + " is not a file plan");
Map<String, Object> templateModel = createTemplateParameters(req, res, model);
sendStatus(req, res, status, cache, format, templateModel);
return;
}
// attempt to find the transfer node
NodeRef transferNode = findTransferNode(filePlan, transferId);
// send 404 if the transfer is not found
if (transferNode == null) {
status.setCode(HttpServletResponse.SC_NOT_FOUND, "Could not locate transfer with id: " + transferId);
Map<String, Object> templateModel = createTemplateParameters(req, res, model);
sendStatus(req, res, status, cache, format, templateModel);
return;
}
// execute the transfer operation
tempFile = executeTransfer(transferNode, req, res, status, cache);
} catch (Exception e) {
throw createStatusException(e, req, res);
} finally {
// try and delete the temporary file (if not in debug mode)
if (tempFile != null) {
if (logger.isDebugEnabled()) {
logger.debug("Transfer report saved to temporary file: " + tempFile.getAbsolutePath());
} else {
tempFile.delete();
}
}
}
}
use of org.springframework.extensions.webscripts.Cache in project records-management by Alfresco.
the class DynamicAuthoritiesGet method execute.
/*
* (non-Javadoc)
* @see org.alfresco.repo.web.scripts.content.StreamContent#execute(org.springframework.extensions.webscripts.
* WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
*/
@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
// retrieve requested format
String format = req.getFormat();
try {
String mimetype = getContainer().getFormatRegistry().getMimeType(req.getAgent(), format);
if (mimetype == null) {
throw new WebScriptException("Web Script format '" + format + "' is not registered");
}
// construct model for script / template
Status status = new Status();
Cache cache = new Cache(getDescription().getRequiredCache());
Map<String, Object> model = buildModel(req, res);
if (model == null) {
return;
}
model.put("status", status);
model.put("cache", cache);
Map<String, Object> templateModel = createTemplateParameters(req, res, model);
// render output
int statusCode = status.getCode();
if (statusCode != HttpServletResponse.SC_OK && !req.forceSuccessStatus()) {
if (logger.isDebugEnabled()) {
logger.debug("Force success status header in response: " + req.forceSuccessStatus());
logger.debug("Setting status " + statusCode);
}
res.setStatus(statusCode);
}
// apply location
String location = status.getLocation();
if (location != null && location.length() > 0) {
if (logger.isDebugEnabled())
logger.debug("Setting location to " + location);
res.setHeader(WebScriptResponse.HEADER_LOCATION, location);
}
// apply cache
res.setCache(cache);
String callback = null;
if (getContainer().allowCallbacks()) {
callback = req.getJSONCallback();
}
if (format.equals(WebScriptResponse.JSON_FORMAT) && callback != null) {
if (logger.isDebugEnabled())
logger.debug("Rendering JSON callback response: content type=" + Format.JAVASCRIPT.mimetype() + ", status=" + statusCode + ", callback=" + callback);
// NOTE: special case for wrapping JSON results in a javascript function callback
res.setContentType(Format.JAVASCRIPT.mimetype() + ";charset=UTF-8");
res.getWriter().write((callback + "("));
} else {
if (logger.isDebugEnabled())
logger.debug("Rendering response: content type=" + mimetype + ", status=" + statusCode);
res.setContentType(mimetype + ";charset=UTF-8");
}
// render response according to requested format
renderFormatTemplate(format, templateModel, res.getWriter());
if (format.equals(WebScriptResponse.JSON_FORMAT) && callback != null) {
// NOTE: special case for wrapping JSON results in a javascript function callback
res.getWriter().write(")");
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
StringWriter stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());
}
throw createStatusException(e, req, res);
}
}
Aggregations