Search in sources :

Example 11 with Theme

use of org.ambraproject.wombat.config.theme.Theme in project wombat by PLOS.

the class StaticResourceController method serveFile.

/**
 * Serves a file provided by a theme.
 *
 * @param filePath the path to the file (relative to the theme)
 * @param response response object
 * @param theme    specifies the theme from which we are loading the file
 * @throws IOException
 */
private void serveFile(String filePath, HttpServletRequest request, HttpServletResponse response, Theme theme) throws IOException {
    try (InputStream inputStream = theme.getStaticResource(filePath)) {
        if (inputStream == null) {
            throw new NotFoundException();
        } else {
            Theme.ResourceAttributes attributes = theme.getResourceAttributes(filePath);
            // We use a "weak" etag, that is, one prepended by "W/".  This means that the resource should be
            // considered semantically-equivalent, but not byte-identical, if the etags match.  It's probably
            // splitting hairs, but this is most appropriate since we don't use a fingerprint of the contents
            // here (instead concatenating length and mtime).  This is what the legacy ambra does for all
            // resources.
            String etag = String.format("W/\"%d-%d\"", attributes.getContentLength(), attributes.getLastModified());
            if (HttpMessageUtil.checkIfModifiedSince(request, attributes.getLastModified(), etag)) {
                response.setHeader("Etag", etag);
                response.setDateHeader(HttpHeaders.LAST_MODIFIED, attributes.getLastModified());
                try (OutputStream outputStream = response.getOutputStream()) {
                    IOUtils.copy(inputStream, outputStream);
                }
            } else {
                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                response.setHeader("Etag", etag);
            }
        }
    } catch (FileNotFoundException e) {
        // In case filePath refers to a directory
        throw new NotFoundException(e);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) Theme(org.ambraproject.wombat.config.theme.Theme)

Example 12 with Theme

use of org.ambraproject.wombat.config.theme.Theme in project wombat by PLOS.

the class StaticResourceController method serveResource.

@RequestMapping(name = "staticResource", value = "/" + AssetUrls.RESOURCE_NAMESPACE + "/**")
public void serveResource(HttpServletRequest request, HttpServletResponse response, HttpSession session, @SiteParam Site site) throws IOException {
    Theme theme = site.getTheme();
    // Kludge to get "resource/**"
    String servletPath = request.getRequestURI();
    String filePath = pathFrom(servletPath, AssetUrls.RESOURCE_NAMESPACE);
    if (filePath.length() <= AssetUrls.RESOURCE_NAMESPACE.length() + 1) {
        // in case of a request to "resource/" root
        throw new NotFoundException();
    }
    if (corsEnabled(site, filePath)) {
        response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
    }
    response.setContentType(session.getServletContext().getMimeType(servletPath));
    if (filePath.startsWith(COMPILED_NAMESPACE)) {
        serveCompiledAsset(filePath, request, response);
    } else {
        serveFile(filePath, request, response, theme);
    }
}
Also used : Theme(org.ambraproject.wombat.config.theme.Theme) FileNotFoundException(java.io.FileNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with Theme

use of org.ambraproject.wombat.config.theme.Theme in project wombat by PLOS.

the class ThemeConfigDirective method getValue.

@Override
protected Object getValue(Environment env, Map params) throws TemplateException, IOException {
    Object mapNameObj = params.get("map");
    if (mapNameObj == null)
        throw new TemplateModelException("map param required");
    Object valueNameObj = params.get("value");
    if (valueNameObj == null)
        throw new TemplateModelException("value param required");
    Object journalKeyObj = params.get("journal");
    Theme theme = new SitePageContext(siteResolver, env).getSite().getTheme();
    if (journalKeyObj != null) {
        theme = theme.resolveForeignJournalKey(siteSet, journalKeyObj.toString()).getTheme();
    }
    Map<String, Object> configMap = theme.getConfigMap(mapNameObj.toString());
    if (configMap == null) {
        throw new TemplateModelException("No config map exists for: " + mapNameObj);
    }
    return configMap.get(valueNameObj.toString());
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Theme(org.ambraproject.wombat.config.theme.Theme)

Aggregations

Theme (org.ambraproject.wombat.config.theme.Theme)13 SiteRequestScheme (org.ambraproject.wombat.config.site.url.SiteRequestScheme)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Site (org.ambraproject.wombat.config.site.Site)3 Test (org.junit.Test)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 FileNotFoundException (java.io.FileNotFoundException)2 Map (java.util.Map)2 RuntimeConfigurationException (org.ambraproject.wombat.config.RuntimeConfigurationException)2 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)2 RequestedDoiVersion (org.ambraproject.wombat.identity.RequestedDoiVersion)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1