use of jakarta.servlet.ServletRegistration.Dynamic in project spring-boot by spring-projects.
the class ServletEndpointRegistrar method register.
private void register(ServletContext servletContext, ExposableServletEndpoint endpoint) {
String name = endpoint.getEndpointId().toLowerCaseString() + "-actuator-endpoint";
String path = this.basePath + "/" + endpoint.getRootPath();
String urlMapping = path.endsWith("/") ? path + "*" : path + "/*";
EndpointServlet endpointServlet = endpoint.getEndpointServlet();
Dynamic registration = servletContext.addServlet(name, endpointServlet.getServlet());
registration.addMapping(urlMapping);
registration.setInitParameters(endpointServlet.getInitParameters());
registration.setLoadOnStartup(endpointServlet.getLoadOnStartup());
logger.info("Registered '" + path + "' to " + name);
}
use of jakarta.servlet.ServletRegistration.Dynamic in project spring-boot by spring-projects.
the class JettyServletWebServerFactoryTests method whenServerIsShuttingDownGracefullyThenNewConnectionsCannotBeMade.
@Test
void whenServerIsShuttingDownGracefullyThenNewConnectionsCannotBeMade() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingServlet blockingServlet = new BlockingServlet();
this.webServer = factory.getWebServer((context) -> {
Dynamic registration = context.addServlet("blockingServlet", blockingServlet);
registration.addMapping("/blocking");
registration.setAsyncSupported(true);
});
this.webServer.start();
int port = this.webServer.getPort();
Future<Object> request = initiateGetRequest(port, "/blocking");
blockingServlet.awaitQueue();
this.webServer.shutDownGracefully((result) -> {
});
Future<Object> unconnectableRequest = initiateGetRequest(port, "/");
blockingServlet.admitOne();
Object response = request.get();
assertThat(response).isInstanceOf(HttpResponse.class);
assertThat(unconnectableRequest.get()).isInstanceOf(HttpHostConnectException.class);
this.webServer.stop();
}
use of jakarta.servlet.ServletRegistration.Dynamic in project spring-boot by spring-projects.
the class TomcatServletWebServerFactoryTests method whenServerIsShuttingDownGracefullyThenNewConnectionsCannotBeMade.
@Test
void whenServerIsShuttingDownGracefullyThenNewConnectionsCannotBeMade() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingServlet blockingServlet = new BlockingServlet();
this.webServer = factory.getWebServer((context) -> {
Dynamic registration = context.addServlet("blockingServlet", blockingServlet);
registration.addMapping("/blocking");
registration.setAsyncSupported(true);
});
this.webServer.start();
int port = this.webServer.getPort();
Future<Object> request = initiateGetRequest(port, "/blocking");
blockingServlet.awaitQueue();
this.webServer.shutDownGracefully((result) -> {
});
Object unconnectableRequest = Awaitility.await().until(() -> initiateGetRequest(HttpClients.createDefault(), port, "/").get(), (result) -> result instanceof Exception);
assertThat(unconnectableRequest).isInstanceOf(HttpHostConnectException.class);
blockingServlet.admitOne();
assertThat(request.get()).isInstanceOf(HttpResponse.class);
this.webServer.stop();
}
use of jakarta.servlet.ServletRegistration.Dynamic in project spring-boot by spring-projects.
the class TomcatServletWebServerFactoryTests method nonExistentUploadDirectoryIsCreatedUponMultipartUpload.
@Test
void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() throws IOException, URISyntaxException {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
factory.addInitializers((servletContext) -> {
servletContextReference.set(servletContext);
Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getParts();
}
});
servlet.addMapping("/upload");
servlet.setMultipartConfig(new MultipartConfigElement((String) null));
});
this.webServer = factory.getWebServer();
this.webServer.start();
File temp = (File) servletContextReference.get().getAttribute(ServletContext.TEMPDIR);
FileSystemUtils.deleteRecursively(temp);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
use of jakarta.servlet.ServletRegistration.Dynamic in project spring-boot by spring-projects.
the class TomcatServletWebServerFactoryTests method whenServerIsShuttingDownARequestOnAnIdleConnectionResultsInConnectionReset.
@Test
void whenServerIsShuttingDownARequestOnAnIdleConnectionResultsInConnectionReset() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingServlet blockingServlet = new BlockingServlet();
this.webServer = factory.getWebServer((context) -> {
Dynamic registration = context.addServlet("blockingServlet", blockingServlet);
registration.addMapping("/blocking");
registration.setAsyncSupported(true);
});
HttpClient httpClient = HttpClients.createMinimal();
this.webServer.start();
int port = this.webServer.getPort();
Future<Object> keepAliveRequest = initiateGetRequest(httpClient, port, "/blocking");
blockingServlet.awaitQueue();
blockingServlet.admitOne();
assertThat(keepAliveRequest.get()).isInstanceOf(HttpResponse.class);
Future<Object> request = initiateGetRequest(port, "/blocking");
blockingServlet.awaitQueue();
this.webServer.shutDownGracefully((result) -> {
});
Object idleConnectionRequestResult = Awaitility.await().until(() -> {
Future<Object> idleConnectionRequest = initiateGetRequest(httpClient, port, "/");
Object result = idleConnectionRequest.get();
return result;
}, (result) -> result instanceof Exception);
assertThat(idleConnectionRequestResult).isInstanceOfAny(SocketException.class, NoHttpResponseException.class);
if (idleConnectionRequestResult instanceof SocketException) {
assertThat((SocketException) idleConnectionRequestResult).hasMessage("Connection reset");
}
blockingServlet.admitOne();
Object response = request.get();
assertThat(response).isInstanceOf(HttpResponse.class);
this.webServer.stop();
}
Aggregations