PageRenderTime 45ms CodeModel.GetById 24ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/google/ie/web/controller/ImageController.java

http://thoughtsite.googlecode.com/
Java | 140 lines | 85 code | 10 blank | 45 comment | 16 complexity | 401a07794032c5b16c904e7fd5f5db83 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.web.controller;
 17
 18import com.google.appengine.api.datastore.Blob;
 19import com.google.ie.business.service.ProjectService;
 20
 21import org.apache.log4j.Logger;
 22import org.springframework.beans.factory.annotation.Autowired;
 23import org.springframework.stereotype.Controller;
 24import org.springframework.web.bind.annotation.PathVariable;
 25import org.springframework.web.bind.annotation.RequestMapping;
 26import org.springframework.web.bind.annotation.RequestMethod;
 27
 28import java.io.BufferedInputStream;
 29import java.io.ByteArrayOutputStream;
 30import java.io.IOException;
 31import java.io.OutputStream;
 32import java.net.URL;
 33
 34import javax.servlet.http.HttpServletRequest;
 35
 36/**
 37 * The controller that handles the request for images used in the system
 38 * 
 39 * @author Sachneet
 40 * 
 41 */
 42@Controller
 43public class ImageController {
 44    private static final Logger LOG = Logger.getLogger(ImageController.class);
 45    public static final String PATH_TO_DEFAULT_IMAGE = "/public/images/img.gif";
 46    private static byte[] defaultImage;
 47    @Autowired
 48    private ProjectService projectService;
 49
 50    /**
 51     * Get the profile image of project
 52     * 
 53     * @param key the key of the project
 54     * @param req the {@link HttpServletRequest} object
 55     * @param outputStream {@link OutputStream} object
 56     */
 57    @RequestMapping(value = "/showImage/{key}", method = RequestMethod.GET)
 58    public void getProjectImageContent(@PathVariable("key") String key, HttpServletRequest req,
 59                    OutputStream outputStream) {
 60        byte[] imageBytes = null;
 61        boolean imageExist = false;
 62        try {
 63            Blob image = projectService.getImageById(key);
 64            if (image != null) {
 65                imageBytes = image.getBytes();
 66                if (imageBytes.length > WebConstants.ZERO) {
 67                    outputStream.write(imageBytes, WebConstants.ZERO, imageBytes.length);
 68                    imageExist = true;
 69                }
 70            }
 71            if (!imageExist) {
 72                /* if there's not image, return default image */
 73                byte img[] = getDefaultImage(req);
 74                if (img != null)
 75                    outputStream.write(img, WebConstants.ZERO, img.length);
 76            }
 77        } catch (IOException e) {
 78            LOG.error("couldn't load defaultImage ", e);
 79        }
 80    }
 81
 82    /**
 83     * Construct the url for the default image
 84     * 
 85     * @param req the {@link HttpServletRequest} object
 86     * @return the url of the default image
 87     */
 88    protected String constructDefaultImageURL(HttpServletRequest req) {
 89        String baseURL = req.getProtocol() + "://" + req.getServerName() + PATH_TO_DEFAULT_IMAGE;
 90        return baseURL;
 91    }
 92
 93    /**
 94     * Lazy load default image for use if SwagItem isn't created with an image
 95     * (ImageIO had a nice way to do it but it is blacklisted on appengine)
 96     * 
 97     * @param req {@link HttpServletRequest } object
 98     * @return the byte array of the image
 99     */
100    private byte[] getDefaultImage(HttpServletRequest req) {
101        if (defaultImage != null && defaultImage.length != WebConstants.ZERO) {
102            return defaultImage;
103        }
104        String defaultImageURLString = constructDefaultImageURL(req);
105        ByteArrayOutputStream bas = null;
106        BufferedInputStream bis = null;
107        /*
108         * create defaultImage byte[] from URL.
109         * Ouch this would have been easier with ImageIO!
110         */
111        try {
112            URL defaultImageURL = new URL(defaultImageURLString);
113            bis = new BufferedInputStream(defaultImageURL.openStream());
114            bas = new ByteArrayOutputStream();
115            int i;
116            while ((i = bis.read()) != -1) {
117                bas.write(i);
118            }
119            defaultImage = bas.toByteArray();
120            return defaultImage;
121        } catch (IOException e) {
122            LOG.warn("couldn't load defaultImage at " + defaultImageURLString, e);
123            return null;
124        } finally {
125            try {
126                if (bas != null)
127                    bas.close();
128            } catch (IOException e) {
129                LOG.warn(e);
130            }
131            try {
132                if (bis != null)
133                    bis.close();
134            } catch (IOException e) {
135                LOG.warn(e);
136            }
137        }
138    }
139}
140