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