<commit_before>package org.ambraproject.wombat.controller;

import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;
import org.ambraproject.wombat.config.site.Site;
import org.ambraproject.wombat.config.theme.Theme;
import org.ambraproject.wombat.service.EntityNotFoundException;
import org.ambraproject.wombat.service.PowerPointService;
import org.ambraproject.wombat.service.remote.ServiceRequestException;
import org.ambraproject.wombat.service.remote.SoaService;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Endpoint for downloading PowerPoint slides of figures.
 */
@Controller
public class PowerPointController extends WombatController {
  private static final Logger log = LoggerFactory.getLogger(PowerPointController.class);

  private static final String LOGO_PATH = "resource/img/logo.png";

  @Autowired
  private SoaService soaService;
  @Autowired
  private PowerPointService powerPointService;

  @RequestMapping({"/article/figure/powerpoint", "/{site}/article/figure/powerpoint"})
  public void download(HttpServletRequest request, HttpServletResponse response,
                       @SiteParam Site site,
                       @RequestParam(value = "id", required = true) String figureId)
      throws IOException {

    Map<String, Object> figureMetadata;
    try {
      figureMetadata = soaService.requestObject("assets/" + figureId + "?figure", Map.class);
    } catch (ServiceRequestException e) {
      if (e.getStatusCode() == HttpStatus.BAD_REQUEST.value()) {
        /*
         * Probably a request for a non-figure asset. Could easily be caused by an application bug, but technically
         * could just be a bogus URL from the user, so give them a 404.
         *
         * We might need a more explicit way for the service API to say, "that asset exists but isn't a figure", rather
         * than just assuming that's the only thing that causes a 400 status.
         */
        throw new EntityNotFoundException(figureId);
      } else throw e;
    }

    Map<String, Object> parentArticleMetadata = (Map<String, Object>) figureMetadata.get("parentArticle");
    validateArticleVisibility(site, parentArticleMetadata);

    final Theme theme = site.getTheme();
    PowerPointService.JournalLogoCallback logoCallback = new PowerPointService.JournalLogoCallback() {
      @Override
      public InputStream openLogoStream() throws IOException {
        InputStream stream = theme.getStaticResource(LOGO_PATH);
        if (stream == null) {
          log.warn("Logo file not found at {} for theme: {}", LOGO_PATH, theme.getKey());
        }
        return stream;
      }
    };
<commit_msg>DPRO-464: Fix error-handling in controller<commit_after>package org.ambraproject.wombat.controller;

import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;
import org.ambraproject.wombat.config.site.Site;
import org.ambraproject.wombat.config.theme.Theme;
import org.ambraproject.wombat.service.PowerPointService;
import org.ambraproject.wombat.service.remote.ServiceRequestException;
import org.ambraproject.wombat.service.remote.SoaService;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Endpoint for downloading PowerPoint slides of figures.
 */
@Controller
public class PowerPointController extends WombatController {
  private static final Logger log = LoggerFactory.getLogger(PowerPointController.class);

  private static final String LOGO_PATH = "resource/img/logo.png";

  @Autowired
  private SoaService soaService;
  @Autowired
  private PowerPointService powerPointService;

  @RequestMapping({"/article/figure/powerpoint", "/{site}/article/figure/powerpoint"})
  public void download(HttpServletRequest request, HttpServletResponse response,
                       @SiteParam Site site,
                       @RequestParam(value = "id", required = true) String figureId)
      throws IOException {

    Map<String, Object> figureMetadata;
    try {
      figureMetadata = soaService.requestObject("assets/" + figureId + "?figure", Map.class);
    } catch (ServiceRequestException e) {
      if (e.getStatusCode() == HttpStatus.BAD_REQUEST.value()) {
        /*
         * Probably a request for a non-figure asset. Could easily be caused by an application bug, but technically
         * could just be a bogus URL from the user, so give them a 404.
         *
         * We might need a more explicit way for the service API to say, "that asset exists but isn't a figure", rather
         * than just assuming that's the only thing that causes a 400 status.
         */
        throw new NotFoundException(figureId);
      } else throw e;
    }

    Map<String, Object> parentArticleMetadata = (Map<String, Object>) figureMetadata.get("parentArticle");
    validateArticleVisibility(site, parentArticleMetadata);

    final Theme theme = site.getTheme();
    PowerPointService.JournalLogoCallback logoCallback = new PowerPointService.JournalLogoCallback() {
      @Override
      public InputStream openLogoStream() throws IOException {
        InputStream stream = theme.getStaticResource(LOGO_PATH);
        if (stream == null) {
          log.warn("Logo file not found at {} for theme: {}", LOGO_PATH, theme.getKey());
        }
        return stream;
      }
    };

{'fraction_non_alphanumeric': 0.09383986680793098, 'fraction_numerical': 0.002875737853791433, 'mean_word_length': 4.009855951478393, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649650', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1786, 'n_words': 462}