KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > common > ResourceServlet


1 package org.nextime.ion.common;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Graphics JavaDoc;
5 import java.awt.Image JavaDoc;
6 import java.awt.image.BufferedImage JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import javax.activation.FileTypeMap JavaDoc;
12 import javax.imageio.ImageIO JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14 import javax.servlet.http.HttpServlet JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import org.nextime.ion.frontoffice.bean.ResourceXmlBean;
19 import org.nextime.ion.frontoffice.bean.Resources;
20
21 public class ResourceServlet extends HttpServlet JavaDoc {
22
23     public static String JavaDoc relativePath = null;
24     public static String JavaDoc configPath = null;
25
26     /**
27      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
28      */

29     protected void doGet(
30         HttpServletRequest JavaDoc request,
31         HttpServletResponse JavaDoc response)
32         throws ServletException JavaDoc, IOException JavaDoc {
33
34         String JavaDoc qs = request.getPathInfo().substring(1);
35
36         String JavaDoc resourcesId = qs.substring(0, qs.indexOf("/"));
37         String JavaDoc resourceId = qs.substring(qs.indexOf("/") + 1);
38
39         // retrieve resources selected
40
String JavaDoc path = null;
41         try {
42             ResourceXmlBean bean =
43                 Resources.getResourceXmlBean(this, resourcesId);
44             path = bean.getDirectory();
45         } catch (Exception JavaDoc e) {
46             throw new ServletException JavaDoc(e);
47         }
48         String JavaDoc realPath = getServletContext().getRealPath("/");
49         File JavaDoc resources = new File JavaDoc(realPath, ResourceServlet.relativePath);
50         File JavaDoc tresources = new File JavaDoc(resources, path);
51         File JavaDoc tfile = new File JavaDoc(tresources, resourceId);
52
53         if (request.getParameter("width") != null
54             || request.getParameter("height") != null) {
55
56             int w = Integer.parseInt(request.getParameter("width") + "");
57             int h = Integer.parseInt(request.getParameter("height") + "");
58
59             
60             File JavaDoc cache = new File JavaDoc(resources, "cache");
61             File JavaDoc possibleCached =
62                 new File JavaDoc(cache, w + "_" + h + "_" + tfile.getName() + ".jpg");
63             
64             String JavaDoc mimeType =
65                 FileTypeMap.getDefaultFileTypeMap().getContentType(possibleCached);
66             response.setContentType(mimeType);
67             
68             if (possibleCached.exists()) {
69                 // send content
70
byte[] buffer = new byte[(int) possibleCached.length()];
71                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(possibleCached);
72                 fis.read(buffer);
73                 fis.close();
74                 response.getOutputStream().write(buffer);
75                 response.getOutputStream().flush();
76                 response.getOutputStream().close();
77             } else {
78
79                 BufferedImage JavaDoc image = ImageIO.read(tfile);
80
81                 /*BufferedImage myImage =
82                     new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
83                 Graphics g = myImage.getGraphics();
84                 g.setColor(Color.WHITE);
85                 g.fillRect(0, 0, w, h);*/

86
87                 int width = image.getWidth();
88
89                 /*if (width > height) {
90                     Image newImage =
91                         image.getScaledInstance(w, -1, Image.SCALE_SMOOTH);
92                     g.drawImage(
93                         newImage,
94                         0,
95                         (h - newImage.getHeight(null)) / 2,
96                         null);
97                 } else {
98                     Image newImage =
99                         image.getScaledInstance(-1, h, Image.SCALE_SMOOTH);
100                     g.drawImage(
101                         newImage,
102                         (w - newImage.getWidth(null)) / 2,
103                         0,
104                         null);
105                 }*/

106                 
107                 Image JavaDoc newImage = null;
108                 Image JavaDoc newImage2 = null;
109                 
110                 if( width>w ) {
111                     newImage =
112                         image.getScaledInstance(w, -1, Image.SCALE_SMOOTH);
113                 } else {
114                     newImage = (Image JavaDoc)image;
115                 }
116                 
117                 int height = newImage.getHeight(null);
118                 
119                 if( height>h ) {
120                     newImage2 =
121                         newImage.getScaledInstance(-1, h, Image.SCALE_SMOOTH);
122                 } else {
123                     newImage2 = newImage;
124                 }
125                 
126                 BufferedImage JavaDoc myImage =
127                     new BufferedImage JavaDoc(newImage2.getWidth(null), newImage2.getHeight(null), BufferedImage.TYPE_INT_RGB);
128                 Graphics JavaDoc g = myImage.getGraphics();
129                 g.drawImage(newImage2,0,0,null);
130                 
131
132                 ImageIO.write(myImage, "jpg", possibleCached);
133                 ImageIO.write(myImage, "jpg", response.getOutputStream());
134                 response.getOutputStream().flush();
135                 response.getOutputStream().close();
136             }
137
138         } else {
139
140             // retrieve mime type
141
String JavaDoc mimeType =
142                 FileTypeMap.getDefaultFileTypeMap().getContentType(tfile);
143             response.setContentType(mimeType);
144
145             // send content
146
byte[] buffer = new byte[(int) tfile.length()];
147             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(tfile);
148             fis.read(buffer);
149             fis.close();
150             response.getOutputStream().write(buffer);
151             response.getOutputStream().flush();
152             response.getOutputStream().close();
153
154         }
155     }
156
157     /**
158      * @see javax.servlet.GenericServlet#init()
159      */

160     public void init() throws ServletException JavaDoc {
161         relativePath = getInitParameter("relativePath");
162         configPath = getInitParameter("configPath");
163     }
164
165 }
166
Popular Tags