Skip to content

Support multipart uploads with @RestControllerEndpoint when the management server is running on a separate port #31554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,10 @@

package org.springframework.boot.actuate.autoconfigure.web.servlet;

import jakarta.servlet.MultipartConfigElement;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand Down Expand Up @@ -83,8 +86,11 @@ DispatcherServlet dispatcherServlet() {
}

@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet) {
return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet,
ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, "/");
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}

@Bean(name = DispatcherServlet.HANDLER_MAPPING_BEAN_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,40 @@

package smoketest.actuator;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartFile;

/**
* The Sample rest controller endpoint with exception.
* The Sample rest controller endpoint.
*
* @author Guirong Hu
*/
@Component
@RestControllerEndpoint(id = "exception")
public class SampleRestControllerEndpointWithException {
@RestControllerEndpoint(id = "rest")
public class SampleRestControllerEndpoint {

@GetMapping("/")
@GetMapping("/exception")
public String exception() {
throw new CustomException();
}

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
return StreamUtils.copyToString(file.getInputStream(), StandardCharsets.UTF_8);
}

@RestControllerAdvice
static class CustomExceptionHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class ManagementDifferentPortAndEndpointWithExceptionHandlerSampleActuatorApplic
private int managementPort;

@Test
void testExceptionHandlerRestControllerEndpoint() {
void endpointCanUseExceptionHandler() {
ResponseEntity<String> entity = new TestRestTemplate("user", "password")
.getForEntity("http://localhost:" + this.managementPort + "/actuator/exception", String.class);
.getForEntity("http://localhost:" + this.managementPort + "/actuator/rest/exception", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.I_AM_A_TEAPOT);
assertThat(entity.getBody()).isEqualTo("this is a custom exception body");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smoketest.actuator;

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalManagementPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for separate management and main service ports with Actuator's MVC
* {@link RestControllerEndpoint rest controller endpoints} and multipart uploads.
*
* @author Guirong Hu
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
ManagementDifferentPortAndEndpointWithMultipartUploadSampleActuatorApplicationTests.SecurityConfiguration.class,
SampleActuatorApplication.class },
properties = { "management.endpoints.web.exposure.include=*", "management.server.port=0" })
class ManagementDifferentPortAndEndpointWithMultipartUploadSampleActuatorApplicationTests {

@LocalManagementPort
private int managementPort;

@Test
void endpointShouldBeSupportMultipartUpload() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
requestBody.add("file", new ClassPathResource("test-file.txt"));

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeaders);

ResponseEntity<String> responseEntity = new TestRestTemplate("user", "password").postForEntity(
"http://localhost:" + this.managementPort + "/actuator/rest/upload", requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isEqualTo("this is a test file");
}

@Configuration(proxyBeanMethods = false)
static class SecurityConfiguration {

@Bean
SecurityFilterChain configure(HttpSecurity http) throws Exception {
http.csrf().disable();
return http.build();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test file