Source Code Cross Referenced for BlobOutputPGSQL.java in  » J2EE » Dinamica » dinamica » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Dinamica » dinamica 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package dinamica;
002:
003:        import java.awt.image.AffineTransformOp;
004:        import java.awt.image.BufferedImage;
005:        import java.io.ByteArrayInputStream;
006:        import java.io.ByteArrayOutputStream;
007:        import java.awt.geom.AffineTransform;
008:        import javax.imageio.ImageIO;
009:
010:        import java.sql.*;
011:        import javax.servlet.ServletOutputStream;
012:        import javax.sql.DataSource;
013:
014:        /**
015:         * Output module to print blob contents stored in PostgreSQL database (v8.0.3), 
016:         * like images, pdfs and other types of documents 
017:         * saved in columns of type "bytea". This class was required
018:         * because PostgreSQL does implement the standard JDBC BLOB API for
019:         * its "bytea" data type, so the default dinamica.BlobOutput cannot be used
020:         * with PostgreSQL databases in the current stable versions (7.4 and 8.0.3).
021:         * <br>
022:         * Creation date: 2-sept-2005<br>
023:         * Last Update: 2-sept-2005<br>
024:         * (c) 2003 Martin Cordova<br>
025:         * This code is released under the LGPL license<br>
026:         * @author Martin Cordova
027:         * */
028:        public class BlobOutputPGSQL extends GenericOutput {
029:
030:            /* (non-Javadoc)
031:             * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
032:             */
033:            public void print(GenericTransaction data) throws Throwable {
034:
035:                //get datasource object
036:                String jndiPrefix = getContext()
037:                        .getInitParameter("jndi-prefix");
038:                String dataSourceName = getContext().getInitParameter(
039:                        "def-datasource");
040:
041:                /* PATCH 2005-03-10 read datasource name from config.xml if available */
042:                if (getConfig().transDataSource != null)
043:                    dataSourceName = getConfig().transDataSource;
044:
045:                if (jndiPrefix == null)
046:                    jndiPrefix = "";
047:
048:                DataSource ds = Jndi.getDataSource(jndiPrefix + dataSourceName);
049:
050:                Connection conn = null;
051:                Statement s = null;
052:                ResultSet rs = null;
053:
054:                ServletOutputStream out = null;
055:
056:                //si se envio el parametro 'scale' en el url entonces se
057:                //asume que es una imagen y se escala, se usa para Thumbnails de imagenes solamente
058:                double scaleFactor = 0;
059:                String scale = getRequest().getParameter("scale");
060:                if (scale != null && !scale.equals("")) {
061:                    scaleFactor = Double.parseDouble(scale);
062:                }
063:
064:                try {
065:                    //connect to database
066:                    conn = ds.getConnection();
067:                    s = conn.createStatement();
068:
069:                    //get recordset with blob metadata
070:                    Recordset info = data.getRecordset("blobinfo");
071:
072:                    //get sql to retrieve blob
073:                    String sql = info.getString("sql");
074:
075:                    //set BLOB content-type
076:                    getResponse().setContentType(info.getString("format"));
077:
078:                    //attach?
079:                    String fileName = info.getString("filename");
080:                    if (fileName != null) {
081:                        getResponse().setHeader("Content-Disposition",
082:                                "attachment; filename=\"" + fileName + "\";");
083:                    }
084:
085:                    //get servlet output stream
086:                    out = getResponse().getOutputStream();
087:
088:                    //execute query and retrieve blob
089:                    rs = s.executeQuery(sql);
090:                    if (rs.next()) {
091:                        //read blob
092:                        byte[] blob = rs.getBytes(1);
093:
094:                        //scale image if requested
095:                        if (blob.length > 20000
096:                                && scaleFactor > 0
097:                                && info.getString("format")
098:                                        .startsWith("image/")) {
099:                            BufferedImage bufimg = ImageIO
100:                                    .read(new ByteArrayInputStream(blob));
101:                            AffineTransform tx = new AffineTransform();
102:                            tx.scale(scaleFactor, scaleFactor);
103:                            AffineTransformOp op = new AffineTransformOp(tx,
104:                                    AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
105:                            BufferedImage newImg = op.filter(bufimg, null);
106:                            ByteArrayOutputStream bout = new ByteArrayOutputStream();
107:                            if (info.getString("format").endsWith("png"))
108:                                ImageIO.write(newImg, "png", bout);
109:                            else
110:                                ImageIO.write(newImg, "jpg", bout);
111:                            blob = bout.toByteArray();
112:                        }
113:
114:                        //set content length
115:                        int size = (int) blob.length;
116:                        getResponse().setContentLength(size);
117:                        out.write(blob);
118:                    }
119:
120:                } catch (Throwable e) {
121:                    throw e;
122:                } finally {
123:                    if (rs != null)
124:                        rs.close();
125:                    if (s != null)
126:                        s.close();
127:                    if (conn != null)
128:                        conn.close();
129:                    if (out != null)
130:                        out.close();
131:                }
132:
133:            }
134:
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.