no.dusken.aranea.admin.control.ImageDimensionController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.control.ImageDimensionController.java

Source

/*
 Copyright 2006 - 2010 Under Dusken
    
 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 no.dusken.aranea.admin.control;

import no.dusken.aranea.model.Image;
import no.dusken.aranea.service.ImageService;
import no.dusken.aranea.util.ImageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageDimensionController implements Controller {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    private ImageService imageService;

    private ImageUtils imageUtils;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

        // ignore the request, this is plain work
        for (Image image : imageService.getImages()) {
            File file = imageUtils.getImageFile(image);
            image.setFileSize(file.length());
            BufferedImage rendImage;
            try {
                rendImage = ImageIO.read(file);
            } catch (IOException e) {
                log.error("Could not read image {} {}", image, e);
                continue;
            }
            image.setHeight(rendImage.getHeight());
            image.setWidth(rendImage.getWidth());

            imageService.saveOrUpdate(image);
        }
        return new ModelAndView("redirect:/");
    }

    @Required
    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    @Required
    public void setImageUtils(ImageUtils imageUtils) {
        this.imageUtils = imageUtils;
    }
}