@ -17,21 +17,34 @@
@@ -17,21 +17,34 @@
package org.springframework.cloud.netflix.eureka.server ;
import java.util.Collections ;
import java.util.Map ;
import java.util.Set ;
import javax.servlet.Filter ;
import javax.ws.rs.Path ;
import javax.ws.rs.ext.Provider ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty ;
import org.springframework.boot.context.embedded.FilterRegistrationBean ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient ;
import org.springframework.cloud.netflix.eureka.EurekaServerConfigBean ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Import ;
import org.springframework.core.Ordered ;
import org.springframework.core.env.Environment ;
import org.springframework.core.io.ResourceLoader ;
import org.springframework.core.type.filter.AnnotationTypeFilter ;
import org.springframework.util.ClassUtils ;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter ;
import com.google.common.collect.Maps ;
import com.google.common.collect.Sets ;
import com.sun.jersey.api.core.DefaultResourceConfig ;
import com.sun.jersey.spi.container.servlet.ServletContainer ;
/ * *
@ -42,27 +55,76 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
@@ -42,27 +55,76 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
@EnableDiscoveryClient
@EnableConfigurationProperties ( EurekaDashboardProperties . class )
public class EurekaServerConfiguration extends WebMvcConfigurerAdapter {
/ * *
* List of packages containing Jersey resources required by the Eureka server
* /
private static String [ ] EUREKA_PACKAGES = new String [ ] {
"com.netflix.discovery" ,
"com.netflix.eureka" } ;
@Bean
@ConditionalOnProperty ( prefix = "eureka.dashboard" , name = "enabled" , matchIfMissing = true )
public EurekaController eurekaController ( ) {
return new EurekaController ( ) ;
}
/ * *
* Register the Jersey filter
*
* @param eurekaJerseyApp the jersey application
* @return
* /
@Bean
public FilterRegistrationBean jersey ( ) {
public FilterRegistrationBean jerseyFilterRegistration ( javax . ws . rs . core . Application eurekaJerseyApp ) {
FilterRegistrationBean bean = new FilterRegistrationBean ( ) ;
bean . setFilter ( new ServletContainer ( ) ) ;
bean . setFilter ( new ServletContainer ( eurekaJerseyApp ) ) ;
bean . setOrder ( Ordered . LOWEST_PRECEDENCE ) ;
bean . addInitParameter ( "com.sun.jersey.config.property.WebPageContentRegex" ,
EurekaServerConfigBean . DEFAULT_PREFIX + "/(fonts|images|css|js)/.*" ) ;
bean . addInitParameter ( "com.sun.jersey.config.property.packages" ,
"com.netflix.discovery;com.netflix.eureka" ) ;
bean . setUrlPatterns ( Collections
. singletonList ( EurekaServerConfigBean . DEFAULT_PREFIX + "/*" ) ) ;
return bean ;
}
/ * *
* Construct a Jersey { @link javax . ws . rs . core . Application } with all the resources
* required by the Eureka server .
* /
@Bean
public javax . ws . rs . core . Application jerseyApplication ( Environment environment , ResourceLoader resourceLoader ) {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider ( false , environment ) ;
// Filter to include only classes that have a particular annotation.
//
provider . addIncludeFilter ( new AnnotationTypeFilter ( Path . class ) ) ;
provider . addIncludeFilter ( new AnnotationTypeFilter ( Provider . class ) ) ;
// Find classes in Eureka packages (or subpackages)
//
Set < Class < ? > > classes = Sets . newHashSet ( ) ;
for ( String basePackage : EUREKA_PACKAGES ) {
Set < BeanDefinition > beans = provider . findCandidateComponents ( basePackage ) ;
for ( BeanDefinition bd : beans ) {
Class < ? > cls = ClassUtils . resolveClassName ( bd . getBeanClassName ( ) , resourceLoader . getClassLoader ( ) ) ;
classes . add ( cls ) ;
}
}
// Construct the Jersey ResourceConfig
//
Map < String , Object > propsAndFeatures = Maps . newHashMap ( ) ;
propsAndFeatures . put (
// Skip static content used by the webapp
ServletContainer . PROPERTY_WEB_PAGE_CONTENT_REGEX ,
EurekaServerConfigBean . DEFAULT_PREFIX + "/(fonts|images|css|js)/.*" ) ;
DefaultResourceConfig rc = new DefaultResourceConfig ( classes ) ;
rc . setPropertiesAndFeatures ( propsAndFeatures ) ;
return rc ;
}
@Bean
public FilterRegistrationBean traceFilterRegistration (
@Qualifier ( "webRequestLoggingFilter" ) Filter filter ) {
@ -71,5 +133,4 @@ public class EurekaServerConfiguration extends WebMvcConfigurerAdapter {
@@ -71,5 +133,4 @@ public class EurekaServerConfiguration extends WebMvcConfigurerAdapter {
bean . setOrder ( Ordered . LOWEST_PRECEDENCE - 10 ) ;
return bean ;
}
}