use of org.ambraproject.wombat.util.CacheKey in project wombat by PLOS.
the class ExternalResourceController method serve.
private void serve(HttpServletResponse responseToClient, HttpServletRequest requestFromClient, ContentKey key) throws IOException {
CacheKey cacheKey = key.asCacheKey("indirect");
Map<String, Object> fileMetadata;
try {
fileMetadata = editorialContentApi.requestMetadata(cacheKey, key);
} catch (EntityNotFoundException e) {
String message = String.format("Not found in repo: %s", key.toString());
throw new NotFoundException(message, e);
}
String contentType = (String) fileMetadata.get("contentType");
if (contentType != null) {
responseToClient.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
}
String downloadName = (String) fileMetadata.get("downloadName");
if (downloadName != null) {
responseToClient.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + downloadName);
}
List<String> reproxyUrls = (List<String>) fileMetadata.get("reproxyURL");
if (ReproxyUtil.applyReproxy(requestFromClient, responseToClient, reproxyUrls, REPROXY_CACHE_FOR)) {
return;
}
Collection<Header> assetHeaders = HttpMessageUtil.getRequestHeaders(requestFromClient, ASSET_REQUEST_HEADER_WHITELIST);
try (CloseableHttpResponse repoResponse = editorialContentApi.request(key, assetHeaders)) {
forwardAssetResponse(repoResponse, responseToClient, false);
} catch (EntityNotFoundException e) {
throw new NotFoundException("File not found in repo: " + key, e);
}
}
use of org.ambraproject.wombat.util.CacheKey in project wombat by PLOS.
the class CorpusContentApi method readManuscript.
/**
* Consume an article manuscript.
*
* @param articleId the article whose manuscript to read
* @param site the site in which the callback will render the manuscript, for caching purposes
* @param cachePrefix the cache space that stores the operation output
* @param htmlCallback the operation to perform on the manuscript
* @param <T> the result type
* @return the result of the operation
* @throws IOException
*/
public <T> T readManuscript(ArticlePointer articleId, Site site, String cachePrefix, CacheDeserializer<InputStream, T> htmlCallback) throws IOException {
CacheKey cacheKey = CacheKey.create(cachePrefix, site.getKey(), articleId.getDoi(), Integer.toString(articleId.getIngestionNumber()));
ContentKey manuscriptKey = articleService.getManuscriptKey(articleId);
return requestCachedStream(cacheKey, manuscriptKey, htmlCallback);
}
use of org.ambraproject.wombat.util.CacheKey in project wombat by PLOS.
the class EditorialContentApiImpl method readHtml.
/**
* {@inheritDoc}
* <p/>
* Applies transforms to HTML attributes and performs substitution of placeholder HTML elements with stored content
*/
@Override
public Reader readHtml(final SitePageContext sitePageContext, String pageType, String key, final Set<HtmlElementTransformation> transformations, final Collection<HtmlElementSubstitution> substitutions) throws IOException {
Map<String, Object> pageConfig = sitePageContext.getSite().getTheme().getConfigMap(pageType);
// TODO May want to support page versioning at some point using fetchHtmlDirective
ContentKey version = ContentKey.createForLatestVersion(key);
CacheKey cacheKey = CacheKey.create(pageType, key);
Number cacheTtl = (Number) pageConfig.get("cacheTtl");
if (cacheTtl != null) {
cacheKey = cacheKey.addTimeToLive(cacheTtl.intValue());
}
String transformedHtml = requestCachedReader(cacheKey, version, new CacheDeserializer<Reader, String>() {
@Override
public String read(Reader htmlReader) throws IOException {
// It would be nice to feed the reader directly into the parser, but Jsoup's API makes this awkward.
// The whole document will be in memory anyway, so buffering it into a string is no great performance loss.
String htmlString = IOUtils.toString(htmlReader);
Document document = Jsoup.parseBodyFragment(htmlString);
for (HtmlElementTransformation transformation : transformations) {
transformation.apply(sitePageContext, siteSet, document);
}
for (HtmlElementSubstitution substitution : substitutions) {
substitution.substitute(document);
}
// We want to return only the transformed snippet, so retrieve it from the body tag.
return document.getElementsByTag("body").html();
}
});
return new StringReader(transformedHtml);
}
use of org.ambraproject.wombat.util.CacheKey in project wombat by PLOS.
the class SiteContentController method renderSiteContent.
@RequestMapping(name = "siteContent", value = "/s/{pageName}")
public String renderSiteContent(Model model, @SiteParam Site site, @PathVariable String pageName) throws IOException {
Theme theme = site.getTheme();
Map<String, Object> pageConfig = theme.getConfigMap("siteContent");
String repoKeyPrefix = (String) pageConfig.get("contentRepoKeyPrefix");
if (repoKeyPrefix == null) {
throw new RuntimeConfigurationException("Content repo prefix not configured for theme " + theme.toString());
}
String repoKey = repoKeyPrefix + "." + pageName;
CacheKey cacheKey = CacheKey.create("siteContent_meta", repoKey);
// versioning is not supported for site content
ContentKey version = ContentKey.createForLatestVersion(repoKey);
try {
// Check for validity of the content repo key prior to rendering page. Return a 404 if no object found.
editorialContentApi.requestMetadata(cacheKey, version);
} catch (EntityNotFoundException e) {
throw new NotFoundException(e);
}
model.addAttribute("siteContentRepoKey", repoKey);
return site + "/ftl/siteContent/container";
}
use of org.ambraproject.wombat.util.CacheKey in project wombat by PLOS.
the class EditorialContentApiImpl method getJson.
/**
* {@inheritDoc}
* <p/>
* Returns a JSON object from a remote service
*/
@Override
public Object getJson(String pageType, String key) throws IOException {
CacheKey cacheKey = CacheKey.create(pageType, key);
ContentKey version = ContentKey.createForLatestVersion(key);
return requestCachedReader(cacheKey, version, jsonReader -> gson.fromJson(jsonReader, Object.class));
}
Aggregations