use of org.ambraproject.wombat.service.EntityNotFoundException in project wombat by PLOS.
the class AbstractRemoteService method getResponse.
@Override
public CloseableHttpResponse getResponse(HttpUriRequest target) throws IOException {
// Don't close the client, as this shuts down the connection pool. Do close every response or its entity stream.
CloseableHttpClient client = createClient();
// We want to return an unclosed response, so close the response only if we throw an exception.
boolean returningResponse = false;
CloseableHttpResponse response = null;
try {
try {
response = client.execute(target);
} catch (HttpHostConnectException e) {
throw new ServiceConnectionException(e);
}
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (isErrorStatus(statusCode)) {
URI targetUri = target.getURI();
String address = targetUri.getPath();
if (!Strings.isNullOrEmpty(targetUri.getQuery())) {
address += "?" + targetUri.getQuery();
}
if (statusCode == HttpStatus.NOT_FOUND.value()) {
throw new EntityNotFoundException(address);
} else {
String responseBody = IOUtils.toString(response.getEntity().getContent());
String message = String.format("Request to \"%s\" failed (%d): %s.", address, statusCode, statusLine.getReasonPhrase());
throw new ServiceRequestException(statusCode, message, responseBody);
}
}
returningResponse = true;
return response;
} finally {
if (!returningResponse && response != null) {
response.close();
}
}
}
use of org.ambraproject.wombat.service.EntityNotFoundException in project wombat by PLOS.
the class ExternalContentController method renderExternalContent.
@RequestMapping(name = "externalContent", value = "/external/{pageName}")
public String renderExternalContent(Model model, @SiteParam Site site, @PathVariable String pageName) throws IOException {
String repoKey = REPO_KEY_PREFIX.concat(".").concat(pageName);
// may not need, but kept for prototype
model.addAttribute("externalServiceName", "ember");
try {
model.addAttribute("externalData", editorialContentApi.getJson("externalContent", repoKey));
} catch (EntityNotFoundException e) {
// Return a 404 if no object found.
throw new NotFoundException(e);
}
return site + "/ftl/externalContent/container";
}
use of org.ambraproject.wombat.service.EntityNotFoundException 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);
}
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.service.EntityNotFoundException 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.service.EntityNotFoundException in project wombat by PLOS.
the class FetchHtmlDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
Object typeObj = params.get("type");
if (typeObj == null) {
throw new TemplateModelException("type parameter required");
}
Object pathObj = params.get("path");
if (pathObj == null) {
throw new TemplateModelException("path parameter required");
}
String pageType = typeObj.toString();
Set<HtmlElementTransformation> transformations = ELEMENT_TRANSFORMS.get(pageType);
if (transformations == null) {
throw new TemplateModelException(String.format("type parameter '%s' is invalid.", pageType));
}
ImmutableList<HtmlElementSubstitution> substitutions = HtmlElementSubstitution.buildList(body, SUBST_ATTR_NAME);
SitePageContext sitePageContext = new SitePageContext(siteResolver, env);
try (Reader html = editorialContentApi.readHtml(sitePageContext, pageType, pathObj.toString(), transformations, substitutions)) {
IOUtils.copy(html, env.getOut());
} catch (EntityNotFoundException e) {
// TODO: Allow themes to provide custom, user-visible error blocks
log.error("Could not retrieve HTML of type \"{}\" at path \"{}\"", typeObj, pathObj);
}
}
Aggregations