Search in sources :

Example 1 with EntityNotFoundException

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();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) URI(java.net.URI)

Example 2 with EntityNotFoundException

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";
}
Also used : EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with EntityNotFoundException

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

Example 4 with EntityNotFoundException

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";
}
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 EntityNotFoundException

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);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Reader(java.io.Reader) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException)

Aggregations

EntityNotFoundException (org.ambraproject.wombat.service.EntityNotFoundException)5 CacheKey (org.ambraproject.wombat.util.CacheKey)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 TemplateModelException (freemarker.template.TemplateModelException)1 Reader (java.io.Reader)1 URI (java.net.URI)1 RuntimeConfigurationException (org.ambraproject.wombat.config.RuntimeConfigurationException)1 Theme (org.ambraproject.wombat.config.theme.Theme)1 ContentKey (org.ambraproject.wombat.service.remote.ContentKey)1 Header (org.apache.http.Header)1 StatusLine (org.apache.http.StatusLine)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1