Skip to content

Commit

Permalink
chore: fix broken content negotiation manager config (dhis2#6119)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroms authored Sep 14, 2020
1 parent d050b4e commit 4756c0b
Showing 1 changed file with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import org.hisp.dhis.webapi.mvc.messageconverter.PdfMessageConverter;
import org.hisp.dhis.webapi.mvc.messageconverter.RenderServiceMessageConverter;
import org.hisp.dhis.webapi.mvc.messageconverter.XmlMessageConverter;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.view.CustomPathExtensionContentNegotiationStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
Expand All @@ -64,8 +66,10 @@
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Arrays;
import java.util.List;
Expand All @@ -80,24 +84,30 @@
@Configuration
@Order( 1000 )
@ComponentScan( basePackages = { "org.hisp.dhis" } )
@EnableWebMvc
@EnableGlobalMethodSecurity( prePostEnabled = true )
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer
public class WebMvcConfig extends DelegatingWebMvcConfiguration
{
@Bean
public DhisApiVersionHandlerMethodArgumentResolver dhisApiVersionHandlerMethodArgumentResolver()
{
return new DhisApiVersionHandlerMethodArgumentResolver();
}

@Autowired
public CurrentUserHandlerMethodArgumentResolver currentUserHandlerMethodArgumentResolver;

@Autowired
public CurrentUserInfoHandlerMethodArgumentResolver currentUserInfoHandlerMethodArgumentResolver;

@Autowired
private ContextService contextService;

@Bean( "multipartResolver" )
public MultipartResolver multipartResolver()
{
return new CommonsMultipartResolver();
}

@Bean
public DhisApiVersionHandlerMethodArgumentResolver dhisApiVersionHandlerMethodArgumentResolver()
{
return new DhisApiVersionHandlerMethodArgumentResolver();
}

@Override
public void addArgumentResolvers( List<HandlerMethodArgumentResolver> resolvers )
Expand All @@ -121,6 +131,12 @@ public NodeService nodeService()
return new DefaultNodeService();
}

@Bean
public RenderServiceMessageConverter renderServiceMessageConverter()
{
return new RenderServiceMessageConverter();
}

@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters )
Expand All @@ -132,7 +148,7 @@ public void configureMessageConverters(
Arrays.stream( Compression.values() )
.forEach( compression -> converters.add( new CsvMessageConverter( nodeService(), compression ) ) );

converters.add( new JsonPMessageConverter( nodeService(), null ) );
converters.add( new JsonPMessageConverter( nodeService(), contextService ) );
converters.add( new PdfMessageConverter( nodeService() ) );
converters.add( new ExcelMessageConverter( nodeService() ) );

Expand All @@ -143,14 +159,10 @@ public void configureMessageConverters(
converters.add( renderServiceMessageConverter() );
}

@Primary
@Bean
public RenderServiceMessageConverter renderServiceMessageConverter()
{
return new RenderServiceMessageConverter();
}

@Bean
public CustomRequestMappingHandlerMapping requestMappingHandlerMapping()
@Override
public ContentNegotiationManager mvcContentNegotiationManager()
{
CustomPathExtensionContentNegotiationStrategy pathExtensionNegotiationStrategy =
new CustomPathExtensionContentNegotiationStrategy( mediaTypeMap );
Expand All @@ -167,17 +179,20 @@ public CustomRequestMappingHandlerMapping requestMappingHandlerMapping()
FixedContentNegotiationStrategy fixedContentNegotiationStrategy = new FixedContentNegotiationStrategy(
MediaType.APPLICATION_JSON );

ContentNegotiationManager manager = new ContentNegotiationManager(
return new ContentNegotiationManager(
Arrays.asList(
pathExtensionNegotiationStrategy,
parameterContentNegotiationStrategy,
headerContentNegotiationStrategy,
fixedContentNegotiationStrategy ) );
}

@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping()
{
CustomRequestMappingHandlerMapping mapping = new CustomRequestMappingHandlerMapping();
mapping.setOrder( 0 );
mapping.setContentNegotiationManager( manager );

mapping.setContentNegotiationManager( mvcContentNegotiationManager() );
return mapping;
}

Expand Down

0 comments on commit 4756c0b

Please sign in to comment.