no.dusken.aranea.web.control.PictureController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.web.control.PictureController.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.web.control;

import no.dusken.aranea.model.Image;
import no.dusken.aranea.service.ImageService;
import no.dusken.common.exception.PageNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * This is used to view an information page bout a single image
 */
public class PictureController extends AbstractController {

    private final static Logger log = LoggerFactory.getLogger(PictureController.class);

    private ImageService imageService;

    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws PageNotFoundException {

        // this content does not change that often, allow two hours cache
        response.setHeader("Cache-Control", "max-age=72000");

        Map<String, Object> map = new HashMap<String, Object>();

        if (request.getParameter("ID") != null) {
            Long imageID = ServletRequestUtils.getLongParameter(request, "ID", 0L);
            Image i = imageService.getEntity(imageID);
            if (i != null) {
                map.put("image", i);
            } else {
                //Throwing exception so this can be threated as a 404 error on a higher level
                log.warn("Did not find image with id {}", imageID);
                throw new PageNotFoundException("image with id=" + imageID);
            }
        }

        return new ModelAndView("no/dusken/aranea/base/web/picture/view", map);
    }

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