Search in sources :

Example 1 with Cache

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

the class ContentInfo method streamContentImpl.

protected void streamContentImpl(WebScriptRequest req, WebScriptResponse res, ContentReader reader, NodeRef nodeRef, QName propertyQName, boolean attach, Date modified, String eTag, String attachFileName) throws IOException {
    delegate.setAttachment(req, res, attach, attachFileName);
    // establish mimetype
    String mimetype = reader.getMimetype();
    String extensionPath = req.getExtensionPath();
    if (mimetype == null || mimetype.length() == 0) {
        mimetype = MimetypeMap.MIMETYPE_BINARY;
        int extIndex = extensionPath.lastIndexOf('.');
        if (extIndex != -1) {
            String ext = extensionPath.substring(extIndex + 1);
            mimetype = mimetypeService.getMimetype(ext);
        }
    }
    // set mimetype for the content and the character encoding + length for the stream
    res.setContentType(mimetype);
    res.setContentEncoding(reader.getEncoding());
    res.setHeader("Content-Length", Long.toString(reader.getSize()));
    // set caching
    Cache cache = new Cache();
    cache.setNeverCache(false);
    cache.setMustRevalidate(true);
    cache.setMaxAge(0L);
    cache.setLastModified(modified);
    cache.setETag(eTag);
    res.setCache(cache);
}
Also used : Cache(org.springframework.extensions.webscripts.Cache)

Example 2 with Cache

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

the class ContentStreamer method setResponseCache.

/**
 * Set the cache settings on the response
 *
 * @param res WebScriptResponse
 * @param modified Date
 * @param eTag String
 */
protected void setResponseCache(WebScriptResponse res, Date modified, String eTag, Map<String, Object> model) {
    Cache cache = new Cache();
    Object obj;
    if (model != null && (obj = model.get(KEY_CACHE_DIRECTIVE)) instanceof CacheDirective) {
        CacheDirective cacheDirective = (CacheDirective) obj;
        cache.setNeverCache(cacheDirective.isNeverCache());
        cache.setMustRevalidate(cacheDirective.isMustRevalidate());
        cache.setMaxAge(cacheDirective.getMaxAge());
        cache.setLastModified(cacheDirective.getLastModified());
        cache.setETag(cacheDirective.getETag());
        cache.setIsPublic(cacheDirective.isPublic());
    } else if (model == null || !getBooleanValue(model.get(KEY_ALLOW_BROWSER_TO_CACHE))) {
        // if 'allowBrowserToCache' is null or false
        cache.setNeverCache(false);
        cache.setMustRevalidate(true);
        cache.setMaxAge(0L);
        cache.setLastModified(modified);
        cache.setETag(eTag);
    } else {
        cache.setNeverCache(false);
        cache.setMustRevalidate(false);
        // one year
        cache.setMaxAge(Long.valueOf(31536000));
        cache.setLastModified(modified);
        cache.setETag(eTag);
    }
    res.setCache(cache);
}
Also used : CacheDirective(org.alfresco.rest.framework.resource.content.CacheDirective) Cache(org.springframework.extensions.webscripts.Cache)

Example 3 with Cache

use of org.springframework.extensions.webscripts.Cache 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 4 with Cache

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

the class StreamJMXDump 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 {
    PrintWriter tempFileOut = null;
    ZipOutputStream zout = null;
    try {
        // content type and caching
        res.setContentType("application/zip");
        Cache cache = new Cache();
        cache.setNeverCache(true);
        cache.setMustRevalidate(true);
        cache.setMaxAge(0L);
        res.setCache(cache);
        Date date = new Date();
        String attachFileName = "jmxdump_" + (date.getYear() + 1900) + '_' + (date.getMonth() + 1) + '_' + (date.getDate());
        String headerValue = "attachment; filename=\"" + attachFileName + ".zip\"";
        // set header based on filename - will force a Save As from the browse if it doesn't recognize it
        // this is better than the default response of the browser trying to display the contents
        res.setHeader("Content-Disposition", headerValue);
        // write JMX data to temp file
        File tempFile = TempFileProvider.createTempFile("jmxdump", ".txt");
        tempFileOut = new PrintWriter(tempFile);
        JmxDumpUtil.dumpConnection(mbeanServer, tempFileOut);
        tempFileOut.flush();
        tempFileOut.close();
        tempFileOut = null;
        // zip output
        zout = new ZipOutputStream(res.getOutputStream());
        ZipEntry zipEntry = new ZipEntry(attachFileName + ".txt");
        zout.putNextEntry(zipEntry);
        FileCopyUtils.copy(new FileInputStream(tempFile), zout);
        zout = null;
    } catch (IOException ioe) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not output JMX dump: " + ioe.getMessage(), ioe);
    } finally {
        if (tempFileOut != null)
            tempFileOut.close();
        try {
            if (zout != null)
                zout.close();
        } catch (IOException e1) {
        }
    }
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter) Cache(org.springframework.extensions.webscripts.Cache)

Example 5 with Cache

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

the class WithResponseTest method testSetters.

@Test
public void testSetters() throws Exception {
    WithResponse callBack = new WithResponse(Status.STATUS_OK, ResponseWriter.DEFAULT_JSON_CONTENT, ResponseWriter.CACHE_NEVER);
    callBack.setStatus(Status.STATUS_GONE);
    Cache myCache = new Cache(new Description.RequiredCache() {

        @Override
        public boolean getNeverCache() {
            return false;
        }

        @Override
        public boolean getIsPublic() {
            return true;
        }

        @Override
        public boolean getMustRevalidate() {
            return true;
        }
    });
    callBack.setCache(myCache);
    ContentInfo myContent = new ContentInfoImpl(Format.HTML.mimetype(), "UTF-16", 12, Locale.FRENCH);
    callBack.setContentInfo(myContent);
    assertEquals(Status.STATUS_GONE, callBack.getStatus());
    assertEquals(myCache, callBack.getCache());
    assertEquals(myContent, callBack.getContentInfo());
}
Also used : Description(org.springframework.extensions.webscripts.Description) WithResponse(org.alfresco.rest.framework.webscripts.WithResponse) ContentInfo(org.alfresco.rest.framework.resource.content.ContentInfo) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) Cache(org.springframework.extensions.webscripts.Cache) Test(org.junit.Test)

Aggregations

Cache (org.springframework.extensions.webscripts.Cache)7 IOException (java.io.IOException)3 Status (org.springframework.extensions.webscripts.Status)3 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 FileInputStream (java.io.FileInputStream)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CacheDirective (org.alfresco.rest.framework.resource.content.CacheDirective)1 ContentInfo (org.alfresco.rest.framework.resource.content.ContentInfo)1 ContentInfoImpl (org.alfresco.rest.framework.resource.content.ContentInfoImpl)1 WithResponse (org.alfresco.rest.framework.webscripts.WithResponse)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 QName (org.alfresco.service.namespace.QName)1 Test (org.junit.Test)1