@Override public void handle(Request request, Response response) throws Exception { response.setHeader("Cache-Control", "no-cache"); response.stream().setMediaType(SVG); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = support.getComponent(dbSession, request); Level qualityGateStatus = getQualityGate(dbSession, project); String result = svgGenerator.generateQualityGate(qualityGateStatus); String eTag = getETag(result); Optional<String> requestedETag = request.header("If-None-Match"); if (requestedETag.filter(eTag::equals).isPresent()) { response.stream().setStatus(304); return; } response.setHeader("ETag", eTag); write(result, response.stream().output(), UTF_8); } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) { // There is an issue, so do not return any ETag but make this response expire now SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US); response.setHeader("Expires", sdf.format(new Date())); write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { String pluginKey = request.mandatoryParam(PLUGIN_PARAM); Optional<InstalledPlugin> file = pluginFileSystem.getInstalledPlugin(pluginKey); if (!file.isPresent()) { throw new NotFoundException("Plugin " + pluginKey + " not found"); } FileAndMd5 downloadedFile; FileAndMd5 compressedJar = file.get().getCompressedJar(); if (compressedJar != null && PACK200.equals(request.param(ACCEPT_COMPRESSIONS_PARAM))) { response.stream().setMediaType("application/octet-stream"); response.setHeader("Sonar-Compression", PACK200); response.setHeader("Sonar-UncompressedMD5", file.get().getLoadedJar().getMd5()); downloadedFile = compressedJar; } else { response.stream().setMediaType("application/java-archive"); downloadedFile = file.get().getLoadedJar(); } response.setHeader("Sonar-MD5", downloadedFile.getMd5()); try (InputStream input = FileUtils.openInputStream(downloadedFile.getFile())) { IOUtils.copyLarge(input, response.stream().output()); } } }
@Override public void handle(Request request, Response response) throws Exception { response.setHeader("Cache-Control", "no-cache"); response.stream().setMediaType(SVG); String metricKey = request.mandatoryParam(PARAM_METRIC); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = support.getComponent(dbSession, request); MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey); checkState(metric != null && metric.isEnabled(), "Metric '%s' hasn't been found", metricKey); LiveMeasureDto measure = getMeasure(dbSession, project, metricKey); String result = generateSvg(metric, measure); String eTag = getETag(result); Optional<String> requestedETag = request.header("If-None-Match"); if (requestedETag.filter(eTag::equals).isPresent()) { response.stream().setStatus(304); return; } response.setHeader("ETag", eTag); write(result, response.stream().output(), UTF_8); } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) { // There is an issue, so do not return any ETag but make this response expire now SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US); response.setHeader("Expires", sdf.format(new Date())); write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { // Allowed to users without admin permission: http://jira.sonarsource.com/browse/SONAR-2039 Stream stream = response.stream(); stream.setMediaType(MediaTypes.XML); try (OutputStreamWriter writer = new OutputStreamWriter(stream.output(), UTF_8); DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request)); response.setHeader("Content-Disposition", String.format("attachment; filename=%s.xml", profile.getKee())); backuper.backup(dbSession, profile, writer); } } }
@Test public void send_response_headers() { Request request = new TestRequest().setPath("api/foo"); DumbResponse response = run(request, newWs("api/foo", a -> a.setHandler((req, resp) -> resp.setHeader("Content-Disposition", "attachment; filename=foo.zip")))); assertThat(response.getHeader("Content-Disposition")).isEqualTo("attachment; filename=foo.zip"); }
@Override public void handle(Request request, Response response) throws Exception { response.setHeader("Cache-Control", "no-cache"); response.stream().setMediaType(SVG); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = support.getComponent(dbSession, request); Level qualityGateStatus = getQualityGate(dbSession, project); String result = svgGenerator.generateQualityGate(qualityGateStatus); String eTag = getETag(result); Optional<String> requestedETag = request.header("If-None-Match"); if (requestedETag.filter(eTag::equals).isPresent()) { response.stream().setStatus(304); return; } response.setHeader("ETag", eTag); write(result, response.stream().output(), UTF_8); } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) { // There is an issue, so do not return any ETag but make this response expire now SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US); response.setHeader("Expires", sdf.format(new Date())); write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { String pluginKey = request.mandatoryParam(PLUGIN_PARAM); Optional<InstalledPlugin> file = pluginFileSystem.getInstalledPlugin(pluginKey); if (!file.isPresent()) { throw new NotFoundException("Plugin " + pluginKey + " not found"); } FileAndMd5 downloadedFile; FileAndMd5 compressedJar = file.get().getCompressedJar(); if (compressedJar != null && PACK200.equals(request.param(ACCEPT_COMPRESSIONS_PARAM))) { response.stream().setMediaType("application/octet-stream"); response.setHeader("Sonar-Compression", PACK200); response.setHeader("Sonar-UncompressedMD5", file.get().getLoadedJar().getMd5()); downloadedFile = compressedJar; } else { response.stream().setMediaType("application/java-archive"); downloadedFile = file.get().getLoadedJar(); } response.setHeader("Sonar-MD5", downloadedFile.getMd5()); try (InputStream input = FileUtils.openInputStream(downloadedFile.getFile())) { IOUtils.copyLarge(input, response.stream().output()); } } }
@Override public void handle(Request request, Response response) throws Exception { response.setHeader("Cache-Control", "no-cache"); response.stream().setMediaType(SVG); String metricKey = request.mandatoryParam(PARAM_METRIC); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = support.getComponent(dbSession, request); MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey); checkState(metric != null && metric.isEnabled(), "Metric '%s' hasn't been found", metricKey); LiveMeasureDto measure = getMeasure(dbSession, project, metricKey); String result = generateSvg(metric, measure); String eTag = getETag(result); Optional<String> requestedETag = request.header("If-None-Match"); if (requestedETag.filter(eTag::equals).isPresent()) { response.stream().setStatus(304); return; } response.setHeader("ETag", eTag); write(result, response.stream().output(), UTF_8); } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) { // There is an issue, so do not return any ETag but make this response expire now SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US); response.setHeader("Expires", sdf.format(new Date())); write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { // Allowed to users without admin permission: http://jira.sonarsource.com/browse/SONAR-2039 Stream stream = response.stream(); stream.setMediaType(MediaTypes.XML); try (OutputStreamWriter writer = new OutputStreamWriter(stream.output(), UTF_8); DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request)); response.setHeader("Content-Disposition", String.format("attachment; filename=%s.xml", profile.getKee())); backuper.backup(dbSession, profile, writer); } } }