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);
}
}
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);
}
}
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());
}
Aggregations