private static boolean isResponseCommitted(Response response) { Response.Stream stream = response.stream(); // Request has been aborted by the client or the response was partially streamed, nothing can been done as Tomcat has committed the response return stream instanceof ServletResponse.ServletStream && ((ServletResponse.ServletStream) stream).response().isCommitted(); }
@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) 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 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); } } }
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 { boolean isQueueEmpty = false; try (DbSession dbSession = dbClient.openSession(false)) { isQueueEmpty = dbClient.ceQueueDao().selectAllInAscOrder(dbSession).isEmpty(); } catch (Exception e) { // ignore this FP : https://gist.github.com/simonbrandhof/3d98f854d427519ef5b858a73b59585b Loggers.get(getClass()).error("Cannot select rows from ce_queue", e); } IOUtils.write(String.valueOf(isQueueEmpty), response.stream().output(), UTF_8); } }
private static Response mockServletResponse(boolean committed) { Response response = mock(Response.class, Mockito.RETURNS_DEEP_STUBS); ServletResponse.ServletStream servletStream = mock(ServletResponse.ServletStream.class, Mockito.RETURNS_DEEP_STUBS); when(response.stream()).thenReturn(servletStream); HttpServletResponse httpServletResponse = mock(HttpServletResponse.class, Mockito.RETURNS_DEEP_STUBS); when(httpServletResponse.isCommitted()).thenReturn(committed); when(servletStream.response()).thenReturn(httpServletResponse); return response; }
private static WebService newPingWs(Consumer<WebService.NewAction> consumer) { return newWs("api/ping", a -> { a.setHandler((request, response) -> response.stream().output().write("pong".getBytes(UTF_8))); consumer.accept(a); }); }
@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 (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(); } } } }
@Test public void use_default_value_of_optional_parameter() { Request request = new TestRequest().setPath("api/print"); DumbResponse response = run(request, newWs("api/print", a -> { a.createParam("message").setDefaultValue("hello"); a.setHandler((req, resp) -> resp.stream().output().write(req.param("message").getBytes(UTF_8))); })); assertThat(response.stream().outputAsString()).isEqualTo("hello"); assertThat(response.stream().status()).isEqualTo(200); }
@Test public void use_request_parameter_on_parameter_with_default_value() { Request request = new TestRequest().setPath("api/print").setParam("message", "bar"); DumbResponse response = run(request, newWs("api/print", a -> { a.createParam("message").setDefaultValue("default_value"); a.setHandler((req, resp) -> resp.stream().output().write(req.param("message").getBytes(UTF_8))); })); assertThat(response.stream().outputAsString()).isEqualTo("bar"); assertThat(response.stream().status()).isEqualTo(200); }
@Test public void support_aborted_request_when_response_is_already_committed() { Request request = new TestRequest().setPath("api/foo"); Response response = mockServletResponse(true); run(request, response, newClientAbortWs()); // response is committed (status is already sent), so status can't be changed verify(response.stream(), never()).setStatus(anyInt()); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Request api/foo has been aborted by client"); }
@Test public void accept_parameter_value_within_defined_possible_values() { Request request = new TestRequest().setPath("api/foo").setParam("format", "json"); DumbResponse response = run(request, newWs("api/foo", a -> { a.createParam("format").setPossibleValues("json", "xml"); a.setHandler((req, resp) -> resp.stream().output().write(req.mandatoryParam("format").getBytes(UTF_8))); })); assertThat(response.stream().outputAsString()).isEqualTo("json"); assertThat(response.stream().status()).isEqualTo(200); }
@Test public void fail_if_parameter_value_is_not_in_defined_possible_values() { Request request = new TestRequest().setPath("api/foo").setParam("format", "yml"); DumbResponse response = run(request, newWs("api/foo", a -> { a.createParam("format").setPossibleValues("json", "xml"); a.setHandler((req, resp) -> resp.stream().output().write(req.mandatoryParam("format").getBytes(UTF_8))); })); assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"Value of parameter 'format' (yml) must be one of: [json, xml]\"}]}"); assertThat(response.stream().status()).isEqualTo(400); }
@Test public void internal_error_when_response_is_already_committed() { Request request = new TestRequest().setPath("api/foo"); Response response = mockServletResponse(true); run(request, response, newFailWs()); // response is committed (status is already sent), so status can't be changed verify(response.stream(), never()).setStatus(anyInt()); assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Fail to process request api/foo"); }
@Test public void support_aborted_request_when_response_is_not_committed() { Request request = new TestRequest().setPath("api/foo"); Response response = mockServletResponse(false); run(request, response, newClientAbortWs()); verify(response.stream()).setStatus(299); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Request api/foo has been aborted by client"); }
@Test public void internal_error_when_response_is_not_committed() { Request request = new TestRequest().setPath("api/foo"); Response response = mockServletResponse(false); run(request, response, newFailWs()); verify(response.stream()).setStatus(500); assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Fail to process request api/foo"); }