Search in sources :

Example 11 with Site

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

the class GeneralDoiController method getRedirectFor.

private Link getRedirectFor(Site site, RequestedDoiVersion id) throws IOException {
    DoiTypeInfo typeInfo = getTypeOf(id);
    RedirectFunction redirectFunction = redirectHandlers.get(typeInfo.typeKey);
    if (redirectFunction == null) {
        throw new RuntimeException(String.format("Unrecognized type (%s) for: %s", typeInfo.typeKey, id));
    }
    Site targetSite = site.getTheme().resolveForeignJournalKey(siteSet, typeInfo.journalKey);
    Link.Factory factory = Link.toAbsoluteAddress(targetSite);
    return redirectFunction.getLink(factory, id);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) Link(org.ambraproject.wombat.config.site.url.Link)

Example 12 with Site

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

the class RenderAssetsDirective method renderAssets.

/**
 * Renders queued asset links as HTML. If in dev mode, the rendered output will be a sequence of plain links to the
 * asset resources. Else, the queued assets will be compiled into a minified form, and the rendered output will be a
 * single link to the result.
 * <p/>
 * Either way, assets will be ordered according to their dependencies, defaulting to the order in which they were
 * enqueued. (That is, in dev mode the links appear in that order, and in production mode the assets are concatenated
 * in that order before they are minified.)
 * <p/>
 * This method pulls asset nodes from the named environment variable. Executing the method clears the queue.
 *
 * @param assetType           defines the type of asset (.js or .css)
 * @param requestVariableName the name of the request variable that uncompiled assets have been stored in by calls to
 *                            a subclass of {@link AssetDirective}
 * @param environment         freemarker execution environment
 * @throws TemplateException
 * @throws IOException
 */
protected void renderAssets(AssetService.AssetType assetType, String requestVariableName, Environment environment) throws TemplateException, IOException {
    HttpServletRequest request = ((HttpRequestHashModel) environment.getDataModel().get("Request")).getRequest();
    Map<String, AssetNode> assetNodes = (Map<String, AssetNode>) request.getAttribute(requestVariableName);
    if (assetNodes == null)
        return;
    List<String> assetPaths = sortNodes(assetNodes.values());
    // Reset in case new assets get put in for a second render
    assetNodes.clear();
    if (assetPaths != null && !assetPaths.isEmpty()) {
        SitePageContext sitePageContext = new SitePageContext(siteResolver, environment);
        if (runtimeConfiguration.getCompiledAssetDir() == null) {
            for (String assetPath : assetPaths) {
                String assetAddress = Link.toLocalSite(sitePageContext.getSite()).toPath(assetPath).get(sitePageContext.getRequest());
                environment.getOut().write(getHtml(assetAddress));
            }
        } else {
            Site site = sitePageContext.getSite();
            String assetLink = assetService.getCompiledAssetLink(assetType, assetPaths, site);
            String path = PathUtil.JOINER.join(AssetService.AssetUrls.RESOURCE_NAMESPACE, assetLink);
            String assetAddress = Link.toLocalSite(site).toPath(path).get(request);
            environment.getOut().write(getHtml(assetAddress));
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Site(org.ambraproject.wombat.config.site.Site) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) SitePageContext(org.ambraproject.wombat.freemarker.SitePageContext) Map(java.util.Map)

Example 13 with Site

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

the class ExceptionHandlerAdvisor method handleException.

/**
 * Handler invoked for all uncaught exceptions.  Renders a "nice" 500 page.
 *
 * @param exception uncaught exception
 * @param request   HttpServletRequest
 * @param response  HttpServletResponse
 * @return ModelAndView specifying the view
 * @throws java.io.IOException
 */
@ExceptionHandler(Exception.class)
protected ModelAndView handleException(Exception exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
    log.error("handleException", exception);
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    Site site = siteResolver.resolveSite(request);
    // For some reason, methods decorated with @ExceptionHandler cannot accept Model parameters,
    // unlike @RequestMapping methods.  So this is a little different...
    String viewName = chooseExceptionView(site, exception);
    ModelAndView mav = new ModelAndView(viewName);
    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    mav.addObject("stackTrace", stackTrace.toString());
    mav.addObject("showDebug", runtimeConfiguration.showDebug());
    return mav;
}
Also used : Site(org.ambraproject.wombat.config.site.Site) StringWriter(java.io.StringWriter) ModelAndView(org.springframework.web.servlet.ModelAndView) PrintWriter(java.io.PrintWriter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 14 with Site

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

the class ExceptionHandlerAdvisor method handleNotFound.

/**
 * Directs unhandled exceptions that indicate an invalid URL to a 404 page.
 *
 * @param request  HttpServletRequest
 * @param response HttpServletResponse
 * @return ModelAndView specifying the view
 */
@ExceptionHandler({ MissingServletRequestParameterException.class, NotFoundException.class, NotVisibleException.class, NoHandlerFoundException.class })
protected ModelAndView handleNotFound(HttpServletRequest request, HttpServletResponse response) {
    Site site = siteResolver.resolveSite(request);
    if (site == null && request.getServletPath().equals("/")) {
        response.setHeader("Content-Type", MediaType.TEXT_HTML.toString());
        return appRootPage.serveAppRoot();
    }
    response.setStatus(HttpStatus.NOT_FOUND.value());
    String viewName = (site == null) ? "/NULLSITE/notFound" : (site.getKey() + "/ftl/error/notFound");
    return new ModelAndView(viewName);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 15 with Site

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

the class Theme method resolveForeignJournalKey.

/**
 * Resolve a journal key for another site into that site. Uses theme-specific config hooks to define links between
 * sites (for example, mobile themes link to mobile sites) before defaulting to searching the site set for a site with
 * that journal key.
 *
 * @param siteSet    the system's set of all sites
 * @param journalKey a journal key belonging to another site
 * @return a site for the journal key, using preferences defined for this theme if any
 */
public Site resolveForeignJournalKey(SiteSet siteSet, String journalKey) throws UnmatchedSiteException {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(journalKey));
    Map<?, ?> otherJournals = (Map<?, ?>) getConfigMap("journal").get("otherJournals");
    if (otherJournals != null) {
        String otherSiteKey = (String) otherJournals.get(journalKey);
        if (otherSiteKey != null) {
            return siteSet.getSite(otherSiteKey);
        }
    // else, fall through and try the other way
    }
    // No site name was explicitly given for the journal key, so just search siteSet for it.
    for (Site candidateSite : siteSet.getSites()) {
        if (candidateSite.getJournalKey().equals(journalKey)) {
            return candidateSite;
        }
    }
    throw new UnmatchedSiteException("Journal key not matched to site: " + journalKey);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) UnmatchedSiteException(org.ambraproject.wombat.service.UnmatchedSiteException) Map(java.util.Map)

Aggregations

Site (org.ambraproject.wombat.config.site.Site)27 HashMap (java.util.HashMap)11 Map (java.util.Map)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 SiteRequestScheme (org.ambraproject.wombat.config.site.url.SiteRequestScheme)7 Test (org.junit.Test)7 Link (org.ambraproject.wombat.config.site.url.Link)6 ImmutableList (com.google.common.collect.ImmutableList)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 SiteSet (org.ambraproject.wombat.config.site.SiteSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 StringWriter (java.io.StringWriter)4 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)4 RequestedDoiVersion (org.ambraproject.wombat.identity.RequestedDoiVersion)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 RequestMappingContextDictionary (org.ambraproject.wombat.config.site.RequestMappingContextDictionary)3 Theme (org.ambraproject.wombat.config.theme.Theme)3 Logger (org.slf4j.Logger)3