public void serveFile(final HttpServletRequest request, final HttpServletResponse response) throws DownloadException { for (final DownloadStrategy strategy : strategies.values()) { if (strategy.matches(request.getRequestURI().toLowerCase())) { strategy.serveFile(request, response); return; } } throw new DownloadException( "Found plugin download strategy during matching but not when trying to serve. Enable debug logging for more information."); }
public boolean matches(final String urlPath) { for (final DownloadStrategy strategy : strategies.values()) { if (strategy.matches(urlPath)) { log.debug("Matched plugin download strategy: {}", strategy.getClass().getName()); return true; } } return false; }
@Override protected final void doGet(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws IOException { final DownloadStrategy downloadStrategy = getDownloadStrategy(httpServletRequest); if (downloadStrategy == null) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The file you were looking for was not found"); return; } try { downloadStrategy.serveFile(httpServletRequest, httpServletResponse); } catch (final DownloadException e) { log.debug("Error while serving file for request:" + httpServletRequest.getRequestURI(), e); if (!httpServletResponse.isCommitted()) { httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error while serving file"); } } }
private DownloadStrategy findStrategy(String url) { for (final DownloadStrategy downloadStrategy : getDownloadStrategies()) { if (downloadStrategy.matches(url)) { return downloadStrategy; } } return null; } }