@Override public void handle(Request request, Response response) throws Exception { try { response.stream().setMediaType("text/plain"); String index = batchIndex.getIndex(); checkState(index != null, "No available files"); IOUtils.write(index, response.stream().output(), UTF_8); } catch (IOException e) { throw new IllegalStateException(e); } } }
@Override public void handle(Request request, Response response) { String fileKey = request.mandatoryParam(PARAM_KEY); String branch = request.param(PARAM_BRANCH); String pullRequest = request.param(PARAM_PULL_REQUEST); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto file = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, fileKey, branch, pullRequest); userSession.checkComponentPermission(UserRole.CODEVIEWER, file); Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(dbSession, file.uuid(), 1, Integer.MAX_VALUE); response.stream().setMediaType("text/plain"); if (lines.isPresent()) { OutputStream output = response.stream().output(); for (String line : lines.get()) { output.write(line.getBytes(StandardCharsets.UTF_8)); output.write("\n".getBytes(StandardCharsets.UTF_8)); } } } catch (IOException e) { throw new IllegalStateException("Fail to write raw source of file " + fileKey, e); } } }
@Override public void handle(Request wsRequest, Response wsResponse) throws Exception { userSession.checkIsSystemAdministrator(); String processKey = wsRequest.mandatoryParam(PROCESS_PROPERTY); ProcessId processId = ProcessId.fromKey(processKey); File logsDir = serverLogging.getLogsDir(); File file = new File(logsDir, processId.getLogFilenamePrefix() + ".log"); // filenames are defined in the enum LogProcess. Still to prevent any vulnerability, // path is double-checked to prevent returning any file present on the file system. if (file.exists() && file.getParentFile().equals(logsDir)) { wsResponse.stream().setMediaType(MediaTypes.TXT); FileUtils.copyFile(file, wsResponse.stream().output()); } else { wsResponse.stream().setStatus(HttpURLConnection.HTTP_NOT_FOUND); } } }
@Override public void handle(Request request, Response response) throws Exception { response.stream().setMediaType("text/plain"); write("pong", response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { String filename = request.mandatoryParam("name"); try { response.stream().setMediaType("application/java-archive"); File file = batchIndex.getFile(filename); FileUtils.copyFile(file, response.stream().output()); } catch (IOException e) { throw new IllegalStateException(e); } } }
@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); } } }
@Override public void handle(Request request, Response response) throws Exception { try { response.stream().setMediaType("text/plain"); String index = batchIndex.getIndex(); checkState(index != null, "No available files"); IOUtils.write(index, response.stream().output(), UTF_8); } catch (IOException e) { throw new IllegalStateException(e); } } }
stream.setMediaType(MediaTypes.JSON); try (JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), StandardCharsets.UTF_8))) { json.beginObject();
response.stream().setMediaType(MediaTypes.PROTOBUF); OutputStream output = 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 wsRequest, Response wsResponse) throws Exception { final ListResponse.Builder listResponseBuilder = ListResponse.newBuilder(); final ListResponse.Rule.Builder ruleBuilder = ListResponse.Rule.newBuilder(); try (DbSession dbSession = dbClient.openSession(false)) { dbClient.ruleDao().selectEnabled(dbSession, resultContext -> { RuleDefinitionDto dto = resultContext.getResultObject(); ruleBuilder .clear() .setRepository(dto.getRepositoryKey()) .setKey(dto.getRuleKey()) .setName(nullToEmpty(dto.getName())) .setInternalKey(nullToEmpty(dto.getConfigKey())); listResponseBuilder.addRules(ruleBuilder.build()); }); } // JSON response is voluntarily not supported. This WS is for internal use. wsResponse.stream().setMediaType(MediaTypes.PROTOBUF); listResponseBuilder.build().writeTo(wsResponse.stream().output()); }
@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); 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); } }
public static void writeProtobuf(Message msg, Request request, Response response) { OutputStream output = response.stream().output(); try { if (request.getMediaType().equals(PROTOBUF)) { response.stream().setMediaType(PROTOBUF); msg.writeTo(output); } else { response.stream().setMediaType(JSON); try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, UTF_8))) { ProtobufJsonFormat.write(msg, writer); } } } catch (Exception e) { throw new IllegalStateException("Error while writing protobuf message", e); } finally { IOUtils.closeQuietly(output); } }
private void writeResponse(DbSession dbSession, QProfileDto profile, @Nullable String exporterKey, Response response) throws IOException { Stream stream = response.stream(); try ( OutputStream output = response.stream().output(); Writer writer = new OutputStreamWriter(output, UTF_8)) { if (exporterKey == null) { stream.setMediaType(MediaTypes.XML); backuper.backup(dbSession, profile, writer); } else { stream.setMediaType(exporters.mimeType(exporterKey)); exporters.export(dbSession, profile, exporterKey, writer); } } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession session = dbClient.openSession(false)) { String componentKey = request.mandatoryParam("key"); ComponentDto component = componentFinder.getByKey(session, componentKey); userSession.checkComponentPermission(UserRole.USER, component); response.stream().setMediaType("text/plain"); try (OutputStreamWriter writer = new OutputStreamWriter(response.stream().output(), StandardCharsets.UTF_8)) { HashConsumer hashFunction = new HashConsumer(writer, componentKey); dbClient.fileSourceDao().readLineHashesStream(session, component.uuid(), hashFunction); if (!hashFunction.hasData()) { response.noContent(); } } } }
@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); } } }
@Override public void handle(Request request, Response response) throws Exception { String filename = request.mandatoryParam("name"); try { response.stream().setMediaType("application/java-archive"); File file = batchIndex.getFile(filename); FileUtils.copyFile(file, response.stream().output()); } catch (IOException e) { throw new IllegalStateException(e); } } }
@Override public void handle(Request request, Response response) throws Exception { response.stream().setMediaType("text/plain"); write("pong", response.stream().output(), UTF_8); } }
@Override public void handle(Request request, Response response) throws Exception { response.stream().setMediaType(MediaTypes.TXT); IOUtils.write(server.getVersion(), response.stream().output(), UTF_8); } }