Search in sources :

Example 1 with CacheKey

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);
    }
}
Also used : Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) List(java.util.List) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) CacheKey(org.ambraproject.wombat.util.CacheKey)

Example 2 with CacheKey

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);
}
Also used : CacheKey(org.ambraproject.wombat.util.CacheKey)

Example 3 with CacheKey

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);
}
Also used : Reader(java.io.Reader) StringReader(java.io.StringReader) HtmlElementTransformation(org.ambraproject.wombat.freemarker.HtmlElementTransformation) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) StringReader(java.io.StringReader) CacheKey(org.ambraproject.wombat.util.CacheKey) HtmlElementSubstitution(org.ambraproject.wombat.freemarker.HtmlElementSubstitution)

Example 4 with CacheKey

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";
}
Also used : ContentKey(org.ambraproject.wombat.service.remote.ContentKey) RuntimeConfigurationException(org.ambraproject.wombat.config.RuntimeConfigurationException) Theme(org.ambraproject.wombat.config.theme.Theme) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) CacheKey(org.ambraproject.wombat.util.CacheKey) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with CacheKey

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));
}
Also used : CacheKey(org.ambraproject.wombat.util.CacheKey)

Aggregations

CacheKey (org.ambraproject.wombat.util.CacheKey)5 EntityNotFoundException (org.ambraproject.wombat.service.EntityNotFoundException)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 List (java.util.List)1 RuntimeConfigurationException (org.ambraproject.wombat.config.RuntimeConfigurationException)1 Theme (org.ambraproject.wombat.config.theme.Theme)1 HtmlElementSubstitution (org.ambraproject.wombat.freemarker.HtmlElementSubstitution)1 HtmlElementTransformation (org.ambraproject.wombat.freemarker.HtmlElementTransformation)1 ContentKey (org.ambraproject.wombat.service.remote.ContentKey)1 Header (org.apache.http.Header)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 Document (org.jsoup.nodes.Document)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1