All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.micro.server.rest.jersey.SpringBootJerseyRestApplication Maven / Gradle / Ivy

There is a newer version: 0.91.11
Show newest version
package com.aol.micro.server.rest.jersey;

import java.util.List;
import java.util.Map;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.aol.micro.server.GlobalState;
import com.aol.micro.server.auto.discovery.Rest;
import com.aol.micro.server.auto.discovery.RestResource;
import com.aol.micro.server.module.JaxRsProvider;
import com.aol.micro.server.module.Module;
import com.aol.micro.server.module.ModuleDataExtractor;

public class SpringBootJerseyRestApplication extends ResourceConfig {

	@Autowired(required=false)
	public SpringBootJerseyRestApplication(ApplicationContext context){
		this(context, GlobalState.state.getModules().firstValue());
	}
	
	@Autowired(required=false)
	public SpringBootJerseyRestApplication(ApplicationContext context,Module module){
		ModuleDataExtractor extractor = new ModuleDataExtractor(module);
		
		List allResources = extractor.getRestResources(context);
		
		System.out.println("Resources " + allResources);
		Map serverProperties = module.getServerProperties();
		if (allResources != null) {
			for (Object next : allResources) {
				if(isSingleton(next))
					register(next);
				else
					register(next.getClass());
			}
		}
				
		if (serverProperties.isEmpty()) {
			property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
	        //http://stackoverflow.com/questions/25755773/bean-validation-400-errors-are-returning-default-error-page-html-instead-of-re
	        property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
		} else {
			for (Map.Entry entry : serverProperties.entrySet()) {
				property(entry.getKey(), entry.getValue());
			}
		}
		
		context.getBeansOfType(AbstractBinder.class).forEach((n,e)->register(e));
		
		
		module.getDefaultJaxRsPackages().stream().forEach( e -> packages(e));
		module.getDefaultResources().stream().forEach( e -> register(e));
		
		
		module.getResourceConfigManager().accept(new JaxRsProvider<>(this));
	}
	

	private boolean isSingleton(Object next) {
		if(next instanceof RestResource)
			return ((RestResource)next).isSingleton();
		Rest rest = next.getClass().getAnnotation(Rest.class);
		if(rest == null)
			return !(next instanceof Class);
		return rest.isSingleton();
	}


	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy