Skip to content

Commit

Permalink
SONAR-17786 Update Jetty to 11.X
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-poreda-sonarsource authored and sonartech committed Dec 18, 2024
1 parent d9bf1b1 commit 4e41444
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ subprojects {
exclude 'org.slf4j:slf4j-api'
}
dependency 'com.fasterxml.staxmate:staxmate:2.4.1'
dependencySet(group: 'org.eclipse.jetty', version: '9.4.6.v20170531') {
dependencySet(group: 'org.eclipse.jetty', version: '11.0.24') {
entry 'jetty-proxy'
entry 'jetty-server'
entry 'jetty-servlet'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,11 @@ public RequestDispatcher getNamedDispatcher(String s) {
throw new UnsupportedOperationException();
}

@Override
public Servlet getServlet(String s) {
throw new UnsupportedOperationException();
}

@Override
public Enumeration<Servlet> getServlets() {
throw new UnsupportedOperationException();
}

@Override
public Enumeration<String> getServletNames() {
throw new UnsupportedOperationException();
}

@Override
public void log(String s) {
throw new UnsupportedOperationException();
}

@Override
public void log(Exception e, String s) {
throw new UnsupportedOperationException();
}

@Override
public void log(String s, Throwable throwable) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,16 @@ public void getResourceAsStream_is_not_supported() {
@Test(expected = UnsupportedOperationException.class)
public void getRequestDispatcher_is_not_supported() {
servletContext.getRequestDispatcher(SOME_STRING);

}

@Test(expected = UnsupportedOperationException.class)
public void getNamedDispatcher_is_not_supported() {
servletContext.getNamedDispatcher(SOME_STRING);

}

@Test(expected = UnsupportedOperationException.class)
public void getServlet_is_not_supported() {
servletContext.getServlet(SOME_STRING);

}

@Test(expected = UnsupportedOperationException.class)
public void getServlets_is_not_supported() {
servletContext.getServlets();

}

@Test(expected = UnsupportedOperationException.class)
public void getServletNames_is_not_supported() {
servletContext.getServletNames();

}

@Test(expected = UnsupportedOperationException.class)
public void log_is_not_supported() {
servletContext.log(SOME_STRING);

}

@Test(expected = UnsupportedOperationException.class)
public void log1_is_not_supported() {
servletContext.log(SOME_EXCEPTION, SOME_STRING);
}

@Test(expected = UnsupportedOperationException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@
*/
package org.sonar.server.platform.web;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import jakarta.servlet.http.HttpServletResponse;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogAndArguments;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.core.extension.CoreExtensionRepository;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
Expand All @@ -49,18 +53,19 @@

public class StaticResourcesServletTest {

@Rule
public LogTester logTester = new LogTester();
@RegisterExtension
LogTesterJUnit5 logTester = new LogTesterJUnit5();

private Server jetty;

private PluginRepository pluginRepository = mock(PluginRepository.class);
private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
private TestSystem system = new TestSystem(pluginRepository, coreExtensionRepository);
private final PluginRepository pluginRepository = mock(PluginRepository.class);
private final CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
private final TestSystem system = new TestSystem(pluginRepository, coreExtensionRepository);

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
logTester.setLevel(Level.TRACE);
jetty = new Server(0);
jetty = new Server(InetSocketAddress.createUnresolved("localhost", 0));
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
ServletHolder servletHolder = new ServletHolder(new StaticResourcesServlet(system));
Expand All @@ -69,93 +74,96 @@ public void setUp() throws Exception {
jetty.start();
}

@After
@AfterEach
public void tearDown() throws Exception {
if (jetty != null) {
jetty.stop();
}
}

private Response callAndStop(String path) throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(jetty.getURI().resolve(path).toString())
private HttpResponse<String> callAndStop(String path) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(jetty.getURI().resolve(URI.create(path)))
.build();
Response response = client.newCall(request).execute();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

jetty.stop();
return response;
}

@Test
public void return_content_if_exists_in_installed_plugin() throws Exception {
system.pluginStream = IOUtils.toInputStream("bar");
system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.txt");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.txt");

assertThat(response.isSuccessful()).isTrue();
assertThat(response.body().string()).isEqualTo("bar");
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("bar");
assertThat(system.pluginResource).isEqualTo("static/foo.txt");
}

@Test
public void return_content_of_folder_of_installed_plugin() throws Exception {
system.pluginStream = IOUtils.toInputStream("bar");
system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo/bar.txt");
HttpResponse<String> response = callAndStop("/static/myplugin/foo/bar.txt");

assertThat(response.isSuccessful()).isTrue();
assertThat(response.body().string()).isEqualTo("bar");
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("bar");
assertThat(system.pluginResource).isEqualTo("static/foo/bar.txt");
}

@Test
public void return_content_of_folder_of_installed_core_extension() throws Exception {
system.coreExtensionStream = IOUtils.toInputStream("bar");
system.coreExtensionStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("coreext")).thenReturn(true);

Response response = callAndStop("/static/coreext/foo/bar.txt");
HttpResponse<String> response = callAndStop("/static/coreext/foo/bar.txt");

assertThat(response.isSuccessful()).isTrue();
assertThat(response.body().string()).isEqualTo("bar");
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("bar");
assertThat(system.coreExtensionResource).isEqualTo("static/foo/bar.txt");
}

@Test
public void return_content_of_folder_of_installed_core_extension_over_installed_plugin_in_case_of_key_conflict() throws Exception {
system.coreExtensionStream = IOUtils.toInputStream("bar of plugin");
system.coreExtensionStream = IOUtils.toInputStream("bar of plugin", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true);
system.coreExtensionStream = IOUtils.toInputStream("bar of core extension");
system.coreExtensionStream = IOUtils.toInputStream("bar of core extension", Charset.defaultCharset());
when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true);

Response response = callAndStop("/static/samekey/foo/bar.txt");
HttpResponse<String> response = callAndStop("/static/samekey/foo/bar.txt");

assertThat(response.isSuccessful()).isTrue();
assertThat(response.body().string()).isEqualTo("bar of core extension");
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("bar of core extension");
assertThat(system.pluginResource).isNull();
assertThat(system.coreExtensionResource).isEqualTo("static/foo/bar.txt");
}

@Test
public void mime_type_is_set_on_response() throws Exception {
system.pluginStream = IOUtils.toInputStream("bar");
system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.header("Content-Type")).isEqualTo("text/css");
assertThat(response.body().string()).isEqualTo("bar");
Optional<String> header = response.headers().firstValue("Content-Type");
assertThat(header).hasValue("text/css");
assertThat(response.body()).isEqualTo("bar");
}

@Test
public void return_404_if_resource_not_found_in_installed_plugin() throws Exception {
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(404);
assertThat(response.statusCode()).isEqualTo(404);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}

Expand All @@ -164,22 +172,22 @@ public void return_404_if_plugin_does_not_exist() throws Exception {
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(404);
assertThat(response.statusCode()).isEqualTo(404);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}

@Test
public void return_resource_if_exists_in_requested_plugin() throws Exception {
system.pluginStream = IOUtils.toInputStream("bar");
system.pluginStream = IOUtils.toInputStream("bar", Charset.defaultCharset());
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);
when(pluginRepository.getPluginInfo("myplugin")).thenReturn(new PluginInfo("myplugin"));

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.isSuccessful()).isTrue();
assertThat(response.body().string()).isEqualTo("bar");
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("bar");
assertThat(logTester.logs(Level.ERROR)).isEmpty();
}

Expand All @@ -189,9 +197,9 @@ public void do_not_fail_nor_log_ERROR_when_response_is_already_committed_and_plu
system.isCommitted = true;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Response is committed. Cannot send error response code 404");
}
Expand All @@ -201,9 +209,9 @@ public void do_not_fail_nor_log_ERROR_when_sendError_throws_IOException_and_plug
system.sendErrorException = new IOException("Simulating sendError throwing IOException");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(false);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Failed to send error code 404: {}");
}
Expand All @@ -214,9 +222,9 @@ public void do_not_fail_nor_log_ERROR_when_response_is_already_committed_and_res
system.pluginStream = null;
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.logs(Level.TRACE)).contains("Response is committed. Cannot send error response code 404");
}
Expand All @@ -226,9 +234,9 @@ public void do_not_fail_nor_log_not_attempt_to_send_error_if_ClientAbortExceptio
system.pluginStreamException = new ClientAbortException("Simulating ClientAbortException");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(logTester.getLogs(Level.TRACE)).extracting(LogAndArguments::getFormattedMsg).contains(
"Client canceled loading resource [static/foo.css] from plugin [myplugin]: {}");
Expand All @@ -240,9 +248,9 @@ public void do_not_fail_when_response_is_committed_after_other_error() throws Ex
system.pluginStreamException = new RuntimeException("Simulating a error");
when(pluginRepository.hasPlugin("myplugin")).thenReturn(true);

Response response = callAndStop("/static/myplugin/foo.css");
HttpResponse<String> response = callAndStop("/static/myplugin/foo.css");

assertThat(response.code()).isEqualTo(200);
assertThat(response.statusCode()).isEqualTo(200);
assertThat(logTester.logs(Level.ERROR)).contains("Unable to load resource [static/foo.css] from plugin [myplugin]");
}

Expand Down

0 comments on commit 4e41444

Please sign in to comment.