com.clearprecision.microservices.reference.jetty.BirthdayApplication.java Source code

Java tutorial

Introduction

Here is the source code for com.clearprecision.microservices.reference.jetty.BirthdayApplication.java

Source

/*
 * Copyright 2012-2013 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 com.clearprecision.microservices.reference.jetty;

import com.google.common.base.Predicate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.AuthorizationScopeBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableAutoConfiguration
@EnableSwagger2
@ComponentScan
public class BirthdayApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(BirthdayApplication.class, args);
    }

    @Bean
    public CorrleationInterceptor corrleationInterceptor() {
        return new CorrleationInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(corrleationInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public Docket birthdayApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("birthday-service").apiInfo(apiInfo()).select()
                .paths(birthdayPaths()).build();
    }

    @Bean
    public Docket userApi() {
        AuthorizationScope[] authScopes = new AuthorizationScope[1];
        authScopes[0] = new AuthorizationScopeBuilder().scope("read").description("read access").build();
        SecurityReference securityReference = SecurityReference.builder().reference("test").scopes(authScopes)
                .build();

        ArrayList<SecurityContext> securityContexts = newArrayList(
                SecurityContext.builder().securityReferences(newArrayList(securityReference)).build());
        return new Docket(DocumentationType.SWAGGER_2).securitySchemes(newArrayList(new BasicAuth("test")))
                .securityContexts(securityContexts).groupName("birthday-api").apiInfo(apiInfo()).select()
                .paths(userOnlyEndpoints()).build();
    }

    private Predicate<String> birthdayPaths() {
        return or(regex("/birthday/*"));
    }

    private Predicate<String> userOnlyEndpoints() {
        return new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return input.contains("birthday");
            }
        };
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Birthday Service")
                .description("This service returns teh number of days until the users birthday")
                .termsOfServiceUrl("http://springfox.io").contact("Tony Ayres")
                .license("Apache License Version 2.0")
                .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE").version("2.0").build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration("validatorUrl");
    }

}