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

org.n3r.quartz.glass.web.controller.FilesController Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package org.n3r.quartz.glass.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Serves all static files : css, images, js files...
 *
 * @author damien bourdette
 */
@Controller
public class FilesController {

    public static final int BUFFER_SIZE = 1024;

    @RequestMapping("/css/style.css")
    public void ccs(HttpServletResponse response) throws IOException {
        response.setContentType("text/css");

        serveResource("/org/n3r/quartz/glass/css/style.css", response);
    }

    @RequestMapping("/image/{name}.png")
    public void image(@PathVariable String name, HttpServletResponse response) throws IOException {
        response.setContentType("image/png");

        serveResource("/org/n3r/quartz/glass/image/" + name + ".png", response);
    }

    @RequestMapping("/js/{name}.js")
    public void javascript(@PathVariable String name, HttpServletResponse response) throws IOException {
        response.setContentType("application/javascript");

        serveResource("/org/n3r/quartz/glass/js/" + name + ".js", response);
    }

    private void serveResource(String name, HttpServletResponse response) throws IOException {
        InputStream inputStream = getClass().getResourceAsStream(name);
        OutputStream outputStream = response.getOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];

        int size = 0;
        while ((size = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, size);
        }

        inputStream.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy