org.meruvian.yama.webapi.config.JaxrsConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.meruvian.yama.webapi.config.JaxrsConfig.java

Source

/*
 * Copyright 2012-2014 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
 *
 *      http://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 org.meruvian.yama.webapi.config;

import java.util.Properties;

import javax.inject.Named;

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.plugins.providers.jackson.Jackson2JsonpInterceptor;
import org.jboss.resteasy.plugins.spring.OptionalValueBeanFactory;
import org.jboss.resteasy.plugins.spring.SpringBeanProcessor;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.springmvc.ResteasyHandlerAdapter;
import org.jboss.resteasy.springmvc.ResteasyHandlerMapping;
import org.jboss.resteasy.springmvc.ResteasyNoResourceFoundView;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.BeanNameViewResolver;

@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(ResteasyHandlerMapping.class)
public class JaxrsConfig {
    @Bean(initMethod = "start", destroyMethod = "stop")
    @ConditionalOnMissingBean(ResteasyDeployment.class)
    @ConfigurationProperties(prefix = "resteasy.deployment")
    public ResteasyDeployment resteasyDeployment() {
        return new ResteasyDeployment();
    }

    @Bean
    @ConditionalOnMissingBean(Registry.class)
    public Registry restEasyRegistry(ResteasyDeployment deployment) {
        return deployment.getRegistry();
    }

    @Bean
    @ConditionalOnMissingBean(Dispatcher.class)
    public Dispatcher resteasyDispatcher(ResteasyDeployment deployment) {
        return deployment.getDispatcher();
    }

    @Bean
    @ConditionalOnMissingBean(ResteasyProviderFactory.class)
    public ResteasyProviderFactory resteasyProviderFactory(ResteasyDeployment deployment) {
        return deployment.getProviderFactory();
    }

    @Bean
    @DependsOn("resteasyDeployment")
    @ConditionalOnMissingBean(SpringBeanProcessor.class)
    public SpringBeanProcessor resteasySpringBeanProcessor(ResteasyDeployment resteasyDeployment) {
        return new SpringBeanProcessor(resteasyDeployment);
    }

    @Bean
    @DependsOn("resteasyDeployment")
    @ConditionalOnMissingBean(ResteasyHandlerMapping.class)
    public ResteasyHandlerMapping resteasyHandlerMapper(ResteasyDeployment deployment) {
        ResteasyHandlerMapping handlerMapping = new ResteasyHandlerMapping(deployment);
        handlerMapping.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
        handlerMapping.setThrowNotFound(false);
        handlerMapping.setPrefix("");

        return handlerMapping;
    }

    @Bean
    @ConditionalOnMissingBean(ResteasyHandlerAdapter.class)
    public ResteasyHandlerAdapter resteasyHandlerAdapter(ResteasyDeployment deployment) {
        return new ResteasyHandlerAdapter(deployment);
    }

    @Bean
    @ConditionalOnMissingBean(SimpleMappingExceptionResolver.class)
    public SimpleMappingExceptionResolver resteasyExceptionHandler() {
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        resolver.setExceptionMappings(new Properties() {
            {
                setProperty("org.jboss.resteasy.spi.NoResourceFoundFailure", "resteasyNoResourceFoundView");
            }
        });
        resolver.setExceptionAttribute("exception");

        return resolver;
    }

    @Bean
    @ConditionalOnMissingBean(ResteasyNoResourceFoundView.class)
    public ResteasyNoResourceFoundView resteasyNoResourceFoundView(ResteasyDeployment deployment) {
        ResteasyNoResourceFoundView view = new ResteasyNoResourceFoundView();
        view.setDeployment(deployment);

        return view;
    }

    @Bean
    @ConditionalOnMissingBean(BeanNameViewResolver.class)
    public BeanNameViewResolver resteasyErrorViewResolver() {
        return new BeanNameViewResolver();
    }

    @Bean
    @ConditionalOnMissingBean(OptionalValueBeanFactory.class)
    public OptionalValueBeanFactory resteasyDispatcherInterceptors() {
        return new OptionalValueBeanFactory();
    }

    @Bean
    public Jackson2JsonpInterceptor jsonpInterceptor() {
        return new Jackson2JsonpInterceptor();
    }
}