Quadcap Image Database

com/quadcap/app/dbimage/DbImageServlet.java

Go to the documentation of this file.
00001 package com.quadcap.app.dbimage; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.ByteArrayOutputStream; 00042 import java.io.IOException; 00043 import java.io.InputStream; 00044 import java.io.OutputStream; 00045 00046 import java.util.Enumeration; 00047 import java.util.Properties; 00048 00049 import java.sql.Connection; 00050 import java.sql.DatabaseMetaData; 00051 import java.sql.DriverManager; 00052 import java.sql.PreparedStatement; 00053 import java.sql.ResultSet; 00054 import java.sql.Statement; 00055 import java.sql.SQLException; 00056 00057 import javax.servlet.ServletConfig; 00058 import javax.servlet.ServletException; 00059 00060 import javax.servlet.http.HttpServlet; 00061 import javax.servlet.http.HttpServletRequest; 00062 import javax.servlet.http.HttpServletResponse; 00063 00064 // ---- image libraries 00065 import java.awt.Image; 00066 import java.awt.Graphics2D; 00067 import java.awt.geom.AffineTransform; 00068 import java.awt.image.BufferedImage; 00069 import java.io.IOException; 00070 import java.io.OutputStream; 00071 import java.io.FileOutputStream; 00072 import javax.swing.ImageIcon; 00073 import com.sun.image.codec.jpeg.JPEGCodec; 00074 import com.sun.image.codec.jpeg.JPEGImageEncoder; 00075 00076 /** 00077 * This servlet fetches and returns images from the database. 00078 * 00079 * @author Stan Bailes 00080 */ 00081 public class DbImageServlet extends HttpServlet { 00082 String url = null; 00083 00084 public void init(ServletConfig config) throws ServletException { 00085 super.init(config); 00086 try { 00087 url = config.getInitParameter("jdbc.url"); 00088 if (url == null) { 00089 throw new ServletException("No jdbc.url property"); 00090 } else { 00091 String driver = "com.quadcap.jdbc.JdbcDriver"; 00092 String d = config.getInitParameter("jdbc.driver"); 00093 if (d != null) driver = d; 00094 Class.forName(driver); 00095 } 00096 config.getServletContext().setAttribute("servlet", this); 00097 00098 Connection conn = getConnection(); 00099 Statement stmt = conn.createStatement(); 00100 try { 00101 DatabaseMetaData dbmeta = conn.getMetaData(); 00102 ResultSet rs = dbmeta.getTables(null, null, "images", null); 00103 if (!rs.next()) { 00104 stmt.execute( 00105 "create table images(" + 00106 " name varchar(99999), " + 00107 " maxDim int, " + 00108 " data blob," + 00109 " size int)"); 00110 stmt.execute( 00111 "create unique index images1 on images(name, maxDim)"); 00112 } 00113 rs.close(); 00114 } finally { 00115 stmt.close(); 00116 conn.close(); 00117 } 00118 00119 } catch (Exception e) { 00120 e.printStackTrace(System.err); 00121 throw new ServletException(e); 00122 } 00123 } 00124 00125 public Connection getConnection() throws SQLException { 00126 Properties p = new Properties(); 00127 p.put("create", "true"); 00128 return DriverManager.getConnection(url, p); 00129 } 00130 00131 byte[] resizeImage(byte[] imageBytes, int maxDim) throws IOException { 00132 ImageIcon inImage = new ImageIcon(imageBytes); 00133 00134 // Determine the scale. 00135 double scale = (double)maxDim/(double)inImage.getIconHeight(); 00136 00137 if (inImage.getIconWidth() > inImage.getIconHeight()) { 00138 scale = (double)maxDim/(double)inImage.getIconWidth(); 00139 } 00140 00141 // Determine size of new image. 00142 // One of them hould equal maxDim. 00143 int scaledW = (int)(scale*inImage.getIconWidth()); 00144 int scaledH = (int)(scale*inImage.getIconHeight()); 00145 00146 // Create an image buffer in which to paint on. 00147 BufferedImage outImage = new BufferedImage(scaledW, scaledH, 00148 BufferedImage.TYPE_INT_RGB); 00149 00150 // Set the scale. 00151 AffineTransform tx = new AffineTransform(); 00152 00153 // If the image is smaller than the desired image size, 00154 // don't bother scaling. 00155 if (scale < 1.0d) { 00156 tx.scale(scale, scale); 00157 } 00158 00159 // Paint image. 00160 Graphics2D g2d = outImage.createGraphics(); 00161 g2d.drawImage(inImage.getImage(), tx, null); 00162 g2d.dispose(); 00163 00164 // JPEG-encode the image and write the response 00165 ByteArrayOutputStream os = new ByteArrayOutputStream(); 00166 JPEGImageEncoder encoder = 00167 JPEGCodec.createJPEGEncoder(os); 00168 encoder.encode(outImage); 00169 00170 return os.toByteArray(); 00171 } 00172 00173 void sendImage(byte[] image, HttpServletResponse response) 00174 throws ServletException, IOException 00175 { 00176 OutputStream os = response.getOutputStream(); 00177 response.setContentLength(image.length); 00178 os.write(image); 00179 } 00180 00181 void storeImage(Connection conn, byte[] image, String name, int dim) 00182 throws SQLException 00183 { 00184 PreparedStatement p = conn.prepareStatement( 00185 "insert into images(?,?,?,?)"); 00186 p.clearParameters(); 00187 p.setString(1, name); 00188 p.setInt(2, image.length); 00189 p.setBytes(3, image); 00190 p.setInt(4, dim); 00191 p.executeUpdate(); 00192 p.close(); 00193 } 00194 00195 public void doGet(HttpServletRequest request, 00196 HttpServletResponse response) 00197 throws ServletException 00198 { 00199 String s = request.getServletPath(); 00200 response.setContentType(getServletContext().getMimeType(s)); 00201 Connection conn; 00202 try { 00203 conn = getConnection(); 00204 } catch (SQLException ex) { 00205 throw new ServletException(ex); 00206 } 00207 int dim = -1; 00208 String mdim = request.getParameter("maxDim"); 00209 if (mdim != null) { 00210 dim = Integer.parseInt(mdim); 00211 } 00212 try { 00213 Statement stmt = conn.createStatement(); 00214 if (dim <= 0) { 00215 ResultSet rs = stmt.executeQuery( 00216 "select data from images where name = '" + s + 00217 "' and maxDim <= 0"); 00218 if (rs.next()) { 00219 byte[] image = rs.getBytes(1); 00220 rs.close(); 00221 sendImage(image, response); 00222 } else { 00223 rs.close(); 00224 // not found 00225 } 00226 } else { 00227 ResultSet rs = stmt.executeQuery( 00228 "select data from images where name ='" + s + 00229 "' and maxDim = " + dim); 00230 if (rs.next()) { 00231 byte[] image = rs.getBytes(1); 00232 rs.close(); 00233 sendImage(image, response); 00234 } else { 00235 rs.close(); 00236 rs = stmt.executeQuery( 00237 "select data, size from images where name = '" + s + 00238 "' and maxDim <= 0"); 00239 if (rs.next()) { 00240 byte[] image = rs.getBytes(1); 00241 rs.close(); 00242 byte[] resized = resizeImage(image, dim); 00243 storeImage(conn, resized, s, dim); 00244 sendImage(resized, response); 00245 } else { 00246 // not found 00247 rs.close(); 00248 } 00249 } 00250 } 00251 } catch (Throwable t) { 00252 throw new ServletException(t); 00253 } finally { 00254 try { 00255 conn.close(); 00256 } catch (Throwable t) { 00257 throw new ServletException(t); 00258 } 00259 } 00260 } 00261 00262 }