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

com.formkiq.server.config.ApplicationConfig Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * 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 com.formkiq.server.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

/**
 * ApplicationConfig.
 *
 */
@Configuration
@EnableAsync
@EnableScheduling
@EnableGlobalMethodSecurity(securedEnabled = true)
public class ApplicationConfig {

    /** URL to handle 404. */
	public static final String API_ERROR_404 = "/api/error/404";
	/** URL to handle 405. */
	public static final String API_ERROR_405 = "/api/error/405";
	/** URL to handle 500. */
	public static final String API_ERROR_500 = "/api/error/500";

    /** DataSource. */
    @Autowired
    private DataSource dataSource;

	/** Embedded Servlet Container. */
	private static final EmbeddedServletContainerCustomizer CONTAINER =
	        new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(
                final ConfigurableEmbeddedServletContainer container) {

            ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN,
                    "/403");

            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
                    API_ERROR_404);
            ErrorPage error405Page = new ErrorPage(
                    HttpStatus.METHOD_NOT_ALLOWED, API_ERROR_405);
            ErrorPage error500Page = new ErrorPage(
                    HttpStatus.INTERNAL_SERVER_ERROR, API_ERROR_500);

            container.addErrorPages(error403Page, error404Page, error405Page,
                    error500Page);
        }
    };

	/**
	 * Create Customizer container.
	 * @return EmbeddedServletContainerCustomizer
	 */
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
        return CONTAINER;
	}

	/**
	 * Set Dates to use ISO-8601 format.
	 * @return Jackson2ObjectMapperBuilder
	 */
	@Bean
	public Jackson2ObjectMapperBuilder jacksonBuilder() {
	    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
	    builder.simpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
	    builder.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE);
	    builder.serializationInclusion(Include.NON_NULL);
	    return builder;
	}

	/**
     * @return JdbcClientDetailsService
     */
    @Bean
    public JdbcClientDetailsService jdbcClientDetailsService() {

        JdbcClientDetailsService service = new JdbcClientDetailsService(
                this.dataSource);

        service.setPasswordEncoder(passwordEncoder());
        return service;
    }

    /**
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy