/*
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.control;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.service.ImageService;
import no.dusken.aranea.util.ImageUtils;
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.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* This is used in src tags. <img src=" HERE " /> Or better, use our #viewImage velocity macro
*/
public class ImageController extends AbstractController {
private Logger log = LoggerFactory.getLogger(getClass());
private ImageService imageService;
private ImageUtils imageUtils;
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
// get parameters from request
int maxHeight = ServletRequestUtils.getIntParameter(request, "maxheight", 0);
int maxWidth = ServletRequestUtils.getIntParameter(request, "maxwidth", 0);
Long ID = ServletRequestUtils.getLongParameter(request, "ID", 0L);
// the image file to view
File imageFile = null;
// get the image
log.debug("Asking for image height: {} width: {}, ID: {}", new Object[]{maxHeight, maxWidth, ID});
Image i = imageService.getEntity(ID);
if (i != null) {
imageFile = imageUtils.getImageFile(i);
}
if (imageFile == null || !imageFile.exists()) {
throw new PageNotFoundException("Imagefile with id " + ID + " not found!");
}
if (maxHeight > 0 || maxWidth > 0) {
// do resize
imageFile = imageUtils.resizeImage(imageFile, maxWidth, maxHeight);
}
map.put("file", imageFile);
return new ModelAndView("fileView", map);
}
@Required
public void setImageService(ImageService imageService) {
this.imageService = imageService;
}
@Required
public void setImageUtils(ImageUtils imageUtils) {
this.imageUtils = imageUtils;
}
}