Package org.internna.iwebmvc.spring.mvc.controllers

Source Code of org.internna.iwebmvc.spring.mvc.controllers.RemoteRepositoryAccessController

/*
* Copyright 2002-2007 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 org.internna.iwebmvc.spring.mvc.controllers;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.internna.iwebmvc.model.DocumentHolder;
import org.internna.iwebmvc.repository.DocumentRepository;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
* Return the document contents
*
* @author Jose Noheda
* @since 1.0
*/
@Controller
@RequestMapping({"/repository.iwebmvc", "/repository.mp3"})
public class RemoteRepositoryAccessController {

    private static Log log = LogFactory.getLog(RemoteRepositoryAccessController.class);

    @Autowired protected Decipherer decipherer;
    @Autowired protected DocumentRepository documentRepository;

    protected BufferedImage getScaledInstance(BufferedImage original, int width, int height) {
        int w = original.getWidth();
        int h = original.getHeight();
        BufferedImage ret = original;
        int type = original.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        while ((w > width) & (h > height)) {
            if (w > width) {
                w /= 2;
                if (w < width) w = width;
            }
            if (h > height) {
                h /= 2;
                if (h < height) h = height;
            }
            BufferedImage tmp = new BufferedImage(w, h, type);
            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.drawImage(ret, 0, 0, w, h, null);
            g2.dispose();
            ret = tmp;
        }
        return ret;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView serve(@RequestParam("file") String file, @RequestParam(required = false, value = "width") String width, @RequestParam(required = false, value = "height") String height, HttpServletResponse response) throws Exception {
        try {
            Assert.isEncrypted(decipherer, file);
            String uri = decipherer.decrypt(file);
            if (log.isDebugEnabled()) log.debug("Retrieving file from URI [" + uri + "]");
            DocumentHolder doc = documentRepository.retrieve(uri);
            if (StringUtils.hasText(doc.getMimeType())) response.setContentType(doc.getMimeType());
            if (width != null) {
                Assert.notNull(height);
                BufferedImage im = getScaledInstance(ImageIO.read(doc.getStream()), Integer.parseInt(width), Integer.parseInt(height));
                ImageIO.write(im, "PNG", response.getOutputStream());
            } else {
                FileCopyUtils.copy(doc.getStream(), response.getOutputStream());
            }
        } catch (RuntimeException r) {
            if (log.isWarnEnabled()) log.warn("Error processing document download: " + r.getMessage());
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
        response.getOutputStream().flush();
        response.getOutputStream().close();
        return null;
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.mvc.controllers.RemoteRepositoryAccessController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.