/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. package com.google.ie.web.controller;
  16. import com.google.appengine.api.datastore.Blob;
  17. import com.google.ie.business.service.ProjectService;
  18. import org.apache.log4j.Logger;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.web.bind.annotation.PathVariable;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestMethod;
  24. import java.io.BufferedInputStream;
  25. import java.io.ByteArrayOutputStream;
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.net.URL;
  29. import javax.servlet.http.HttpServletRequest;
  30. /**
  31. * The controller that handles the request for images used in the system
  32. *
  33. * @author Sachneet
  34. *
  35. */
  36. @Controller
  37. public class ImageController {
  38. private static final Logger LOG = Logger.getLogger(ImageController.class);
  39. public static final String PATH_TO_DEFAULT_IMAGE = "/public/images/img.gif";
  40. private static byte[] defaultImage;
  41. @Autowired
  42. private ProjectService projectService;
  43. /**
  44. * Get the profile image of project
  45. *
  46. * @param key the key of the project
  47. * @param req the {@link HttpServletRequest} object
  48. * @param outputStream {@link OutputStream} object
  49. */
  50. @RequestMapping(value = "/showImage/{key}", method = RequestMethod.GET)
  51. public void getProjectImageContent(@PathVariable("key") String key, HttpServletRequest req,
  52. OutputStream outputStream) {
  53. byte[] imageBytes = null;
  54. boolean imageExist = false;
  55. try {
  56. Blob image = projectService.getImageById(key);
  57. if (image != null) {
  58. imageBytes = image.getBytes();
  59. if (imageBytes.length > WebConstants.ZERO) {
  60. outputStream.write(imageBytes, WebConstants.ZERO, imageBytes.length);
  61. imageExist = true;
  62. }
  63. }
  64. if (!imageExist) {
  65. /* if there's not image, return default image */
  66. byte img[] = getDefaultImage(req);
  67. if (img != null)
  68. outputStream.write(img, WebConstants.ZERO, img.length);
  69. }
  70. } catch (IOException e) {
  71. LOG.error("couldn't load defaultImage ", e);
  72. }
  73. }
  74. /**
  75. * Construct the url for the default image
  76. *
  77. * @param req the {@link HttpServletRequest} object
  78. * @return the url of the default image
  79. */
  80. protected String constructDefaultImageURL(HttpServletRequest req) {
  81. String baseURL = req.getProtocol() + "://" + req.getServerName() + PATH_TO_DEFAULT_IMAGE;
  82. return baseURL;
  83. }
  84. /**
  85. * Lazy load default image for use if SwagItem isn't created with an image
  86. * (ImageIO had a nice way to do it but it is blacklisted on appengine)
  87. *
  88. * @param req {@link HttpServletRequest } object
  89. * @return the byte array of the image
  90. */
  91. private byte[] getDefaultImage(HttpServletRequest req) {
  92. if (defaultImage != null && defaultImage.length != WebConstants.ZERO) {
  93. return defaultImage;
  94. }
  95. String defaultImageURLString = constructDefaultImageURL(req);
  96. ByteArrayOutputStream bas = null;
  97. BufferedInputStream bis = null;
  98. /*
  99. * create defaultImage byte[] from URL.
  100. * Ouch this would have been easier with ImageIO!
  101. */
  102. try {
  103. URL defaultImageURL = new URL(defaultImageURLString);
  104. bis = new BufferedInputStream(defaultImageURL.openStream());
  105. bas = new ByteArrayOutputStream();
  106. int i;
  107. while ((i = bis.read()) != -1) {
  108. bas.write(i);
  109. }
  110. defaultImage = bas.toByteArray();
  111. return defaultImage;
  112. } catch (IOException e) {
  113. LOG.warn("couldn't load defaultImage at " + defaultImageURLString, e);
  114. return null;
  115. } finally {
  116. try {
  117. if (bas != null)
  118. bas.close();
  119. } catch (IOException e) {
  120. LOG.warn(e);
  121. }
  122. try {
  123. if (bis != null)
  124. bis.close();
  125. } catch (IOException e) {
  126. LOG.warn(e);
  127. }
  128. }
  129. }
  130. }