net.eusashead.hateoas.springhalbuilder.config.WebConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.springhalbuilder.config.WebConfig.java

Source

package net.eusashead.hateoas.springhalbuilder.config;

/*
 * #[license]
 * spring-halbuilder-example
 * %%
 * Copyright (C) 2013 Eusa's Head
 * %%
 * 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.
 * %[license]
 */

import java.util.List;

import net.eusashead.hateoas.conditional.interceptor.ConditionalResponseInterceptor;
import net.eusashead.hateoas.conditional.service.ETagService;
import net.eusashead.hateoas.conditional.service.impl.ConcurrentMapETagService;
import net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter;
import net.eusashead.hateoas.hal.http.converter.module.HalHttpMessageConverterModule;
import net.eusashead.hateoas.hal.http.converter.module.impl.AnnotationAdapterModuleImpl;
import net.eusashead.hateoas.hal.response.impl.HalPageResponseBuilderHandlerMethodArgumentResolver;
import net.eusashead.hateoas.hal.response.impl.HalResponseBuilderHandlerMethodArgumentResolver;
import net.eusashead.hateoas.response.argumentresolver.ResponseBuilderMethodArgumentResolver;
import net.eusashead.hateoas.springhalbuilder.controller.Marker;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory;

@Configuration
@Import(JpaConfig.class)
@ComponentScan(basePackageClasses = Marker.class)
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // Add the HAL converter
        converters.add(halConverter());
        converters.add(jsonConverter());
    }

    @Bean
    public HttpMessageConverter<Object> jsonConverter() {
        return new MappingJackson2HttpMessageConverter();
    }

    @Bean
    public HttpMessageConverter<Object> halConverter() {

        // Create the converter
        RepresentationFactory factory = representationFactory();
        HalHttpMessageConverter converter = new HalHttpMessageConverter(factory);

        // Create and add the module
        HalHttpMessageConverterModule module = new AnnotationAdapterModuleImpl();
        converter.addModule(module);
        return converter;
    }

    @Bean
    public RepresentationFactory representationFactory() {
        return new StandardRepresentationFactory();
    }

    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
        argumentResolvers.add(new ResponseBuilderMethodArgumentResolver());
        argumentResolvers.add(new HalResponseBuilderHandlerMethodArgumentResolver(representationFactory()));
        argumentResolvers
                .add(new HalPageResponseBuilderHandlerMethodArgumentResolver(representationFactory(), "page"));
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/vendor/**").addResourceLocations("/vendor/");
        registry.addResourceHandler("/browser.html").addResourceLocations("/browser.html");
        registry.addResourceHandler("/styles.css").addResourceLocations("/styles.css");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ConditionalResponseInterceptor(eTagService()));
        super.addInterceptors(registry);
    }

    @Bean
    public ETagService eTagService() {
        return new ConcurrentMapETagService();
    }

}