com.github.spring.example.SpringExampleApp.java Source code

Java tutorial

Introduction

Here is the source code for com.github.spring.example.SpringExampleApp.java

Source

/*
 * Copyright 2015 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.github.spring.example;

import net.rossillo.spring.web.mvc.CacheControl;
import net.rossillo.spring.web.mvc.CacheControlHandlerInterceptor;
import net.rossillo.spring.web.mvc.CachePolicy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.*;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableAsync
@EnableCaching
@RestController
@SpringBootApplication
public class SpringExampleApp extends WebMvcConfigurerAdapter {

    public static final String QRCODE_ENDPOINT = "/qrcode";

    @Autowired
    ImageService imageService;

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

    @RequestMapping(value = QRCODE_ENDPOINT, method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @CacheControl(maxAge = 3600, policy = { CachePolicy.PUBLIC })
    public ListenableFuture<byte[]> getQRCode(@RequestParam(value = "text", required = true) String text) {
        try {
            return imageService.generateQRCodeAsync(text, 256, 256);
        } catch (Exception ex) {
            throw new InternalServerError("Error while generating QR code image.", ex);
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CacheControlHandlerInterceptor());
    }

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public class InternalServerError extends RuntimeException {

        private static final long serialVersionUID = 1L;

        public InternalServerError(final String message, final Throwable cause) {
            super(message);
        }

    }

}