Search in sources :

Example 6 with Link

use of org.ambraproject.wombat.config.site.url.Link in project wombat by PLOS.

the class ArticleAssetController method serve.

private void serve(HttpServletRequest requestFromClient, HttpServletResponse responseToClient, AssetUrlStyle requestedStyle, Site site, RequestedDoiVersion id, String fileType, boolean isDownload) throws IOException {
    AssetPointer asset = articleResolutionService.toParentIngestion(id);
    Map<String, ?> itemMetadata = articleService.getItemMetadata(asset);
    String itemType = (String) itemMetadata.get("itemType");
    AssetUrlStyle itemStyle = AssetUrlStyle.findByItemType(itemType);
    if (requestedStyle != itemStyle) {
        Link redirectLink = itemStyle.buildRedirectLink(requestMappingContextDictionary, site, id, fileType, isDownload);
        String location = redirectLink.get(requestFromClient);
        log.warn(String.format("Redirecting %s request for %s to <%s>. Bad link?", requestedStyle, asset.asParameterMap(), location));
        redirectTo(responseToClient, location);
        return;
    }
    Map<String, ?> files = (Map<String, ?>) itemMetadata.get("files");
    Map<String, ?> fileRepoKey = (Map<String, ?>) files.get(fileType);
    if (fileRepoKey == null) {
        fileRepoKey = handleUnmatchedFileType(id, itemType, fileType, files);
    }
    // TODO: Check visibility against site?
    ContentKey contentKey = createKey(fileRepoKey);
    try (CloseableHttpResponse responseFromApi = corpusContentApi.request(contentKey, ImmutableList.of())) {
        forwardAssetResponse(responseFromApi, responseToClient, isDownload);
    }
}
Also used : ContentKey(org.ambraproject.wombat.service.remote.ContentKey) AssetPointer(org.ambraproject.wombat.identity.AssetPointer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Link(org.ambraproject.wombat.config.site.url.Link)

Example 7 with Link

use of org.ambraproject.wombat.config.site.url.Link in project wombat by PLOS.

the class ArticleMetadata method validateVisibility.

/**
 * Validate that an article ought to be visible to the user. If not, throw an exception indicating that the user
 * should see a 404.
 * <p/>
 * An article may be invisible if it is not in a published state, or if it has not been published in a journal
 * corresponding to the site.
 *
 * @throws NotVisibleException if the article is not visible on the site
 */
public ArticleMetadata validateVisibility(String handlerName) {
    Map<String, ?> journal = (Map<String, ?>) ingestionMetadata.get("journal");
    String publishedJournalKey = (String) journal.get("journalKey");
    String siteJournalKey = site.getJournalKey();
    if (!publishedJournalKey.equals(siteJournalKey)) {
        Link link = buildCrossSiteRedirect(publishedJournalKey, handlerName);
        throw new InternalRedirectException(link);
    }
    return this;
}
Also used : Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Link(org.ambraproject.wombat.config.site.url.Link)

Example 8 with Link

use of org.ambraproject.wombat.config.site.url.Link in project wombat by PLOS.

the class LegacyArticleAssetController method serveAsset.

/**
 * Serve the identified asset file.
 *
 * @param rawId    an ID for an asset (if {@code unique} is present) or an asset file (if {@code unique} is absent)
 * @param unique   if present, assume the asset has a single file and serve that file; else, serve an identified file
 * @param download forward Content-Disposition headers with "attachment" value only if {@code true}
 */
@RequestMapping(name = "asset", value = "/article/asset")
public ModelAndView serveAsset(HttpServletRequest request, @SiteParam Site site, @RequestParam(value = "id", required = true) String rawId, @RequestParam(value = "unique", required = false) String unique, @RequestParam(value = "download", required = false) String download) throws IOException {
    requireNonemptyParameter(rawId);
    final String assetDoi;
    final Optional<String> fileExtension;
    if (booleanParameter(unique)) {
        assetDoi = rawId;
        fileExtension = Optional.empty();
    } else {
        int extensionIndex = rawId.lastIndexOf(".");
        fileExtension = (extensionIndex < 0) ? Optional.empty() : Optional.of(rawId.substring(extensionIndex + 1));
        assetDoi = (extensionIndex < 0) ? rawId : rawId.substring(0, extensionIndex);
    }
    Map<String, ?> itemMetadata = getItemMetadata(assetDoi);
    String itemType = (String) itemMetadata.get("itemType");
    final String fileType;
    if (fileExtension.isPresent()) {
        fileType = LegacyFileExtensionRedirectStrategy.resolveToFileType(itemType, fileExtension.get());
    } else {
        Map<String, ?> itemFiles = (Map<String, ?>) itemMetadata.get("files");
        Set<String> fileTypes = itemFiles.keySet();
        if (fileTypes.size() == 1) {
            fileType = Iterables.getOnlyElement(fileTypes);
        } else {
            /*
           * The user queried for the unique file of a non-unique asset. Because they might have manually punched in an
           * invalid URL, show a 404 page. Also log a warning in case it was caused by a buggy link.
           */
            log.warn("Received request for unique asset file with ID=\"{}\". More than one associated file type: {}", assetDoi, fileTypes);
            throw new NotFoundException();
        }
    }
    ArticleAssetController.AssetUrlStyle style = ArticleAssetController.AssetUrlStyle.findByItemType(itemType);
    Link redirectLink = style.buildRedirectLink(requestMappingContextDictionary, site, RequestedDoiVersion.of(assetDoi), fileType, booleanParameter(download));
    return new ModelAndView(redirectLink.getRedirect(request));
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) Link(org.ambraproject.wombat.config.site.url.Link) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Link

use of org.ambraproject.wombat.config.site.url.Link in project wombat by PLOS.

the class SiteLinkDirective method getValue.

@Override
protected String getValue(Environment env, Map params) throws TemplateModelException, IOException {
    String path = getStringValue(params.get("path"));
    String targetJournal = getStringValue(params.get("journalKey"));
    String handlerName = getStringValue(params.get("handlerName"));
    boolean absoluteLink = TemplateModelUtil.getBooleanValue((TemplateModel) params.get("absoluteLink"));
    SitePageContext sitePageContext = new SitePageContext(siteResolver, env);
    Site site = sitePageContext.getSite();
    Link.Factory linkFactory = (targetJournal == null) ? (absoluteLink ? Link.toAbsoluteAddress(site) : Link.toLocalSite(site)) : Link.toForeignSite(site, targetJournal, siteSet);
    final Link link;
    if (handlerName != null) {
        Map<String, ?> variables = TemplateModelUtil.getAsMap((TemplateModel) params.get("pathVariables"));
        ListMultimap<String, ?> queryParameters = TemplateModelUtil.getAsMultimap((TemplateModel) params.get("queryParameters"));
        List<?> wildcardValues = TemplateModelUtil.getAsList((TemplateModel) params.get("wildcardValues"));
        link = linkFactory.toPattern(requestMappingContextDictionary, handlerName, variables, queryParameters, wildcardValues);
    } else if (path != null) {
        link = linkFactory.toPath(path);
    } else {
        throw new RuntimeException("Either a path or handlerName parameter is required");
    }
    return link.get(sitePageContext.getRequest());
}
Also used : Site(org.ambraproject.wombat.config.site.Site) Link(org.ambraproject.wombat.config.site.url.Link)

Aggregations

Link (org.ambraproject.wombat.config.site.url.Link)9 Map (java.util.Map)5 Site (org.ambraproject.wombat.config.site.Site)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ByteStreams (com.google.common.io.ByteStreams)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 URLDecoder (java.net.URLDecoder)1 Charset (java.nio.charset.Charset)1