KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > servlets > image > ThumbnailImage


1 package com.dotmarketing.servlets.image;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Graphics2D JavaDoc;
5 import java.awt.image.BufferedImage JavaDoc;
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.OutputStream JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
11
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServlet JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 import org.apache.commons.logging.LogFactory;
18
19 import sun.awt.image.codec.JPEGImageEncoderImpl;
20
21 import com.dotmarketing.beans.Host;
22 import com.dotmarketing.cache.LiveCache;
23 import com.dotmarketing.factories.HostFactory;
24 import com.dotmarketing.factories.InodeFactory;
25 import com.dotmarketing.portlets.files.factories.FileFactory;
26 import com.dotmarketing.portlets.files.model.File;
27 import com.dotmarketing.util.Logger;
28 import com.dotmarketing.util.UtilMethods;
29 import com.sun.image.codec.jpeg.JPEGEncodeParam;
30
31 public class ThumbnailImage extends HttpServlet JavaDoc {
32
33     private static final long serialVersionUID = 1L;
34     int heightThumbnail = 100;
35     int widthThumbnail = 100;
36
37     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
38
39         long time = System.currentTimeMillis();
40
41         String JavaDoc inode = request.getParameter("inode");
42         if (!UtilMethods.isSet(inode)) {
43             String JavaDoc url = request.getParameter("url");
44             Host currentHost = HostFactory.getCurrentHost(request);
45             String JavaDoc path = LiveCache.getPathFromCache(url, currentHost);
46             inode = UtilMethods.getFileName(path);
47         }
48         
49         //Set the expiration time
50
GregorianCalendar JavaDoc expiration = new GregorianCalendar JavaDoc();
51         expiration.add(java.util.Calendar.MONTH, 1);
52         java.text.SimpleDateFormat JavaDoc httpDate = new java.text.SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss z");
53         response.setHeader("Expires", httpDate.format(expiration.getTime()));
54         response.setHeader("Cache-Control", "max-age=2592000");
55         //END Set the expiration time
56

57         String JavaDoc h = request.getParameter("h");
58         String JavaDoc w = request.getParameter("w");
59         String JavaDoc r = request.getParameter("r");
60         String JavaDoc g = request.getParameter("g");
61         String JavaDoc b = request.getParameter("b");
62         
63         int rInt = 255;
64         int gInt = 255;
65         int bInt = 255;
66         
67         try {
68             rInt = Integer.parseInt(r);
69             gInt = Integer.parseInt(g);
70             bInt = Integer.parseInt(b);
71         }
72         catch (Exception JavaDoc e) {
73             Logger.debug(ThumbnailImage.class,"Error with RGB number");
74         }
75
76         String JavaDoc fileBgColorName = rInt + "_" + gInt + "_" + bInt;
77             
78         try {
79                         
80             int height = (UtilMethods.isInt(h) ? Integer.parseInt(h) : heightThumbnail);
81             int width = (UtilMethods.isInt(w) ? Integer.parseInt(w) : widthThumbnail);
82
83             if (inode != null && inode.length() > 0) {
84
85                 File file = (File) InodeFactory.getInode(inode, File.class);
86                 
87                 if (file.getInode() > 0)
88                 {
89                     // gets file extension
90
String JavaDoc suffix = UtilMethods.getFileExtension(file.getFileName());
91
92                     // gets the real path to the assets directory
93
String JavaDoc filePath = FileFactory.getRealAssetsRootPath();
94
95                     // creates the path where to save the working file based on
96
// the inode
97
String JavaDoc workingFileInodePath = String.valueOf(inode);
98                     if (workingFileInodePath.length() == 1) {
99                         workingFileInodePath = workingFileInodePath + "0";
100                     }
101                     // creates the path with inode{1} + inode{2}
102
workingFileInodePath = workingFileInodePath.substring(0, 1) + java.io.File.separator
103                             + workingFileInodePath.substring(1, 2);
104
105                     String JavaDoc thumbExtension = "_thumb_" + height + "_" + width + "_"+ fileBgColorName;
106
107                     String JavaDoc thumbnailFilePath = filePath + java.io.File.separator + workingFileInodePath
108                             + java.io.File.separator + inode + thumbExtension + "." + suffix;
109
110                     java.io.File JavaDoc thumbFile = new java.io.File JavaDoc(thumbnailFilePath);
111                     Color JavaDoc bgColor = new Color JavaDoc(rInt,gInt,bInt);
112                     
113                     if (!thumbFile.exists() || (request.getParameter("nocache") != null)) {
114                         com.dotmarketing.util.Thumbnail.resizeImage(filePath + java.io.File.separator
115                                 + workingFileInodePath + java.io.File.separator, String.valueOf(inode), suffix,
116                                 thumbExtension, height, width, bgColor);
117                         thumbFile = new java.io.File JavaDoc(thumbnailFilePath);
118                     }
119
120                     // set the content type and get the output stream
121
response.setContentType("image/png");
122
123                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(thumbFile));
124                     OutputStream JavaDoc os = response.getOutputStream();
125                     byte[] buf = new byte[4096];
126                     int i = 0;
127
128                     while ((i = bis.read(buf)) != -1) {
129                         os.write(buf, 0, i);
130                     }
131
132                     os.close();
133                     bis.close();
134                     LogFactory.getLog(this.getClass()).debug(
135                             "time to build thumbnail: " + (System.currentTimeMillis() - time) + "ms");
136                 }
137                 else
138                 {
139                     //set the content type and get the output stream
140
response.setContentType("image/jpeg");
141                     //Construct the image
142
OutputStream JavaDoc os = response.getOutputStream();
143                     JPEGImageEncoderImpl jpegEncode = new JPEGImageEncoderImpl(os);
144                     BufferedImage JavaDoc bufferedImage = new BufferedImage JavaDoc(1,1,BufferedImage.TYPE_INT_RGB);
145                     Graphics2D JavaDoc graphics = bufferedImage.createGraphics();
146                     Color JavaDoc bgColor = new Color JavaDoc(rInt,gInt,bInt);
147                     graphics.setBackground(bgColor);
148                     JPEGEncodeParam encodeParam = jpegEncode.getDefaultJPEGEncodeParam(bufferedImage);
149                     encodeParam.setQuality(1,true);
150                     jpegEncode.setJPEGEncodeParam(encodeParam);
151                     jpegEncode.encode(bufferedImage);
152                 }
153             }
154         } catch (Exception JavaDoc e) {
155             Logger.debug(ThumbnailImage.class,"Error creating thumbnail from servlet");
156             e.printStackTrace(System.out);
157         }
158         return;
159     }
160 }
161
Popular Tags