1 16 package com.blandware.atleap.webapp.servlet; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.ContentImage; 20 import com.blandware.atleap.model.core.Role; 21 import com.blandware.atleap.service.core.ContentResourceManager; 22 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm; 23 import com.blandware.atleap.webapp.util.core.GlobalProperties; 24 import com.blandware.atleap.webapp.util.core.ImageUtil; 25 import com.blandware.atleap.webapp.util.core.ResourceTypesManager; 26 import com.blandware.atleap.webapp.util.core.WebappConstants; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.upload.FormFile; 30 import org.springframework.context.ApplicationContext; 31 import org.springframework.web.context.support.WebApplicationContextUtils; 32 33 import javax.imageio.IIOImage ; 34 import javax.servlet.ServletConfig ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.io.ByteArrayOutputStream ; 41 import java.io.IOException ; 42 import java.io.OutputStream ; 43 import java.util.List ; 44 45 57 public class ThumbnailServlet extends HttpServlet { 58 59 62 protected transient final Log log = LogFactory.getLog(ThumbnailServlet.class); 63 64 public static final String THUMBNAIL_DEFAULT_WIDTH_KEY = "thumbnail.default.width"; 65 public static final String THUMBNAIL_DEFAULT_HEIGHT_KEY = "thumbnail.default.height"; 66 67 private ResourceTypesManager resourceTypesManager = null; 68 private ApplicationContext ctx = null; 69 70 73 public void init(ServletConfig config) throws ServletException { 74 super.init(config); 75 resourceTypesManager = ResourceTypesManager.getInstance(getServletContext()); 76 ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 77 } 78 79 82 public void destroy() { 83 84 } 85 86 92 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 93 throws ServletException , IOException { 94 95 Long contentResourceId = null; 96 if ( request.getParameter("contentResourceId") != null ) { 97 contentResourceId = Long.valueOf(request.getParameter("contentResourceId")); 98 } else if ( request.getAttribute("contentResourceId") != null ) { 99 contentResourceId = (Long ) request.getAttribute("contentResourceId"); 100 } 101 102 ServletContext servletContext = request.getSession().getServletContext(); 103 104 int maxWidth = GlobalProperties.getInstance(servletContext).getInteger(THUMBNAIL_DEFAULT_WIDTH_KEY).intValue(); 105 int maxHeight = GlobalProperties.getInstance(servletContext).getInteger(THUMBNAIL_DEFAULT_HEIGHT_KEY).intValue(); 106 try { 107 if ( request.getParameter("width") != null ) { 108 maxWidth = Integer.valueOf(request.getParameter("width")).intValue(); 109 } 110 if ( request.getParameter("height") != null ) { 111 maxHeight = Integer.valueOf(request.getParameter("height")).intValue(); 112 } 113 } catch ( Exception ex ) { 114 } 116 117 UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 118 FormFile file = null; 119 if ( uploadContentResourceForm != null ) { 120 file = uploadContentResourceForm.getFile(); 121 } 122 123 String mimeType = null; 124 String resourceType = null; 125 byte[] imageData; 126 127 if ( contentResourceId != null ) { 128 ContentResourceManager contentResourceManager = (ContentResourceManager) ctx.getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 129 ContentImage contentImage = contentResourceManager.retrieveContentImage(contentResourceId); 130 131 if ( contentImage == null ) { 132 String message = "No content resource with ID=" + contentResourceId + " could be found"; 133 if ( log.isErrorEnabled() ) { 134 log.error(message); 135 } 136 throw new ServletException (message); 137 } 138 139 List roles = contentImage.getRoles(); 141 boolean granted = false; 142 if ( roles != null && roles.size() > 0 ) { 143 for ( int i = 0; i < roles.size(); i++ ) { 144 Role role = (Role) roles.get(i); 145 if ( request.isUserInRole(role.getName()) ) { 146 if ( log.isDebugEnabled() ) { 147 log.debug(" User '" + request.getRemoteUser() + 148 "' has role '" + role.getName() + "', granting access"); 149 } 150 granted = true; 151 break; 152 } 153 } 154 } else { 155 granted = true; 156 if ( log.isDebugEnabled() ) { 157 log.debug("Anonymous user granting access"); 158 } 159 } 160 161 if ( !granted ) { 162 if ( log.isDebugEnabled() ) { 164 log.debug(" User '" + request.getRemoteUser() + 165 "' does not have any required role, denying access"); 166 } 167 response.sendError(HttpServletResponse.SC_FORBIDDEN); 168 return; 169 } 170 171 imageData = contentImage.getResourceData().getData(); 172 mimeType = contentImage.getMimeType(); 173 resourceType = contentImage.getType(); 174 175 if ( log.isDebugEnabled() ) { 176 log.debug("Showing thumbnail from DB with mimeType=" + mimeType); 177 } 178 179 } else if ( file != null && file.getFileName() != null && file.getFileName().trim().length() != 0 ) { 180 181 mimeType = resourceTypesManager.getMimeTypeByFileName(file.getFileName()); 182 resourceType = resourceTypesManager.getResourceTypeByFileName(file.getFileName()); 183 imageData = file.getFileData(); 184 185 if ( log.isDebugEnabled() ) { 186 log.debug("Showing thumbnail from session with mimeType=" + mimeType); 187 } 188 189 } else { 190 String message = "There are neither contentResourceId parameter in request nor " + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + " attribute in session"; 191 if ( log.isWarnEnabled() ) { 192 log.warn(message); 193 } 194 response.sendError(HttpServletResponse.SC_NOT_FOUND); 195 return; 196 } 197 198 if ( !resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_IMAGE) ) { 199 String message = "Resource type must be image"; 200 if ( log.isErrorEnabled() ) { 201 log.error(message); 202 } 203 throw new ServletException (message); 204 } 205 206 OutputStream out = response.getOutputStream(); 207 208 try { 209 IIOImage source = ImageUtil.decodeImage(imageData, mimeType); 210 IIOImage scaled = ImageUtil.scaleToRectangle(source, maxWidth, maxHeight); 211 if ( source != scaled ) { 212 ByteArrayOutputStream scaledStream = new ByteArrayOutputStream (); 213 try { 214 ImageUtil.encodeImage(scaled, scaledStream, mimeType); 215 } catch ( IllegalArgumentException ex ) { 216 mimeType = resourceTypesManager.getDefaultMimeType(resourceType); 218 response.setContentType(mimeType); 219 ImageUtil.encodeImage(scaled, scaledStream, mimeType); 220 } 221 imageData = scaledStream.toByteArray(); 222 scaledStream.close(); 223 } 224 } catch ( IOException ex ) { 225 if ( log.isErrorEnabled() ) { 226 log.error("Cannot read image, maybe data corrupted", ex); 227 } 228 } 229 230 response.addHeader("Cache-Control", "no-cache,no-store,max-age=0"); 231 response.addHeader("Pragma", "No-cache"); 232 response.setDateHeader("Expires", 1); 233 response.setContentType(mimeType); 234 response.setContentLength(imageData.length); 235 out.write(imageData); 236 237 out.close(); 238 } 239 240 246 protected void doGet(HttpServletRequest request, HttpServletResponse response) 247 throws ServletException , IOException { 248 processRequest(request, response); 249 } 250 251 257 protected void doPost(HttpServletRequest request, HttpServletResponse response) 258 throws ServletException , IOException { 259 processRequest(request, response); 260 } 261 262 265 public String getServletInfo() { 266 return "Servlet to view thumbnail of image content resource"; 267 } 268 269 } | Popular Tags |