Search in sources :

Example 1 with Status

use of org.springframework.extensions.webscripts.Status in project alfresco-remote-api by Alfresco.

the class StreamContent method execute.

/**
 * @see org.springframework.extensions.webscripts.WebScript#execute(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
 */
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    // retrieve requested format
    String format = req.getFormat();
    try {
        // construct model for script / template
        Status status = new Status();
        Cache cache = new Cache(getDescription().getRequiredCache());
        Map<String, Object> model = executeImpl(req, status, cache);
        if (model == null) {
            model = new HashMap<String, Object>(8, 1.0f);
        }
        model.put("status", status);
        model.put("cache", cache);
        // execute script if it exists
        ScriptDetails executeScript = getExecuteScript(req.getContentType());
        if (executeScript != null) {
            if (logger.isDebugEnabled())
                logger.debug("Executing script " + executeScript.getContent().getPathDescription());
            Map<String, Object> scriptModel = createScriptParameters(req, res, executeScript, model);
            // add return model allowing script to add items to template model
            Map<String, Object> returnModel = new HashMap<String, Object>(8, 1.0f);
            scriptModel.put("model", returnModel);
            executeScript(executeScript.getContent(), scriptModel);
            mergeScriptModelIntoTemplateModel(executeScript.getContent().getPath(), returnModel, model);
        }
        // is a redirect to a status specific template required?
        if (status.getRedirect()) {
            // create model for template rendering
            Map<String, Object> templateModel = createTemplateParameters(req, res, model);
            sendStatus(req, res, status, cache, format, templateModel);
        } else {
            // Get the attachement property value
            Boolean attachBoolean = (Boolean) model.get("attach");
            boolean attach = false;
            if (attachBoolean != null) {
                attach = attachBoolean.booleanValue();
            }
            String contentPath = (String) model.get("contentPath");
            if (contentPath == null) {
                // Get the content parameters from the model
                NodeRef nodeRef = (NodeRef) model.get("contentNode");
                if (nodeRef == null) {
                    throw new WebScriptException("The content node was not specified so the content cannot be streamed to the client: " + executeScript.getContent().getPathDescription());
                }
                QName propertyQName = null;
                String contentProperty = (String) model.get("contentProperty");
                if (contentProperty == null) {
                    // default to the standard content property
                    propertyQName = ContentModel.PROP_CONTENT;
                } else {
                    propertyQName = QName.createQName(contentProperty);
                }
                // Stream the content
                delegate.streamContent(req, res, nodeRef, propertyQName, attach, null, model);
            } else {
                // Stream the content
                delegate.streamContent(req, res, contentPath, attach, model);
            }
        }
    } catch (Throwable e) {
        throw createStatusException(e, req, res);
    }
}
Also used : WebScriptStatus(org.springframework.extensions.webscripts.WebScriptStatus) Status(org.springframework.extensions.webscripts.Status) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Cache(org.springframework.extensions.webscripts.Cache)

Example 2 with Status

use of org.springframework.extensions.webscripts.Status 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();
            }
        }
    }
}
Also used : Status(org.springframework.extensions.webscripts.Status) NodeRef(org.alfresco.service.cmr.repository.NodeRef) StoreRef(org.alfresco.service.cmr.repository.StoreRef) HashMap(java.util.HashMap) File(java.io.File) IOException(java.io.IOException) Cache(org.springframework.extensions.webscripts.Cache)

Example 3 with Status

use of org.springframework.extensions.webscripts.Status 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);
    }
}
Also used : Status(org.springframework.extensions.webscripts.Status) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Cache(org.springframework.extensions.webscripts.Cache) PrintWriter(java.io.PrintWriter)

Aggregations

Cache (org.springframework.extensions.webscripts.Cache)3 Status (org.springframework.extensions.webscripts.Status)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 QName (org.alfresco.service.namespace.QName)1 WebScriptStatus (org.springframework.extensions.webscripts.WebScriptStatus)1