001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.core.stream;
034:
035: import com.flexive.core.Database;
036: import static com.flexive.core.DatabaseConst.TBL_CONTENT_BINARY;
037: import com.flexive.shared.stream.BinaryDownloadPayload;
038: import com.flexive.stream.DataPacket;
039: import com.flexive.stream.StreamException;
040: import com.flexive.stream.StreamProtocol;
041:
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.nio.ByteBuffer;
045: import java.sql.Connection;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049:
050: /**
051: * Stream protocol to download binaries
052: *
053: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
054: */
055: public class BinaryDownloadProtocol extends
056: StreamProtocol<BinaryDownloadPayload> {
057:
058: private Connection con = null;
059: private PreparedStatement ps = null;
060: private InputStream bin = null;
061: private byte[] _buffer;
062:
063: public BinaryDownloadProtocol() {
064: super (BinaryDownloadPayload.class);
065: }
066:
067: @Override
068: public StreamProtocol<BinaryDownloadPayload> getInstance() {
069: return new BinaryDownloadProtocol();
070: }
071:
072: @Override
073: public boolean canHandle(
074: DataPacket<BinaryDownloadPayload> dataPacket)
075: throws StreamException {
076: return true;
077: }
078:
079: @Override
080: public DataPacket<BinaryDownloadPayload> processPacket(
081: DataPacket<BinaryDownloadPayload> dataPacket)
082: throws StreamException {
083: if (dataPacket.isExpectResponse()
084: && dataPacket.isExpectStream()) {
085: String mimeType;
086: int datasize;
087: try {
088: con = Database.getDbConnection(dataPacket.getPayload()
089: .getDivision());
090: String column = "FBLOB";
091: String sizeColumn = "BLOBSIZE";
092: switch (dataPacket.getPayload().getSize()) {
093: case 1:
094: column = "PREV1";
095: sizeColumn = "PREV1SIZE";
096: break;
097: case 2:
098: column = "PREV2";
099: sizeColumn = "PREV2SIZE";
100: break;
101: case 3:
102: column = "PREV3";
103: sizeColumn = "PREV3SIZE";
104: break;
105: }
106: long previewId = 0;
107: ResultSet rs;
108: if (!"FBLOB".equals(column)) {
109: //unless the real content is requested, try to find the correct preview image
110: ps = con
111: .prepareStatement("SELECT PREVIEW_REF FROM "
112: + TBL_CONTENT_BINARY
113: + " WHERE ID=? AND VER=? AND QUALITY=?");
114: ps.setLong(1, dataPacket.getPayload().getId());
115: ps.setInt(2, dataPacket.getPayload().getVersion());
116: ps.setInt(3, dataPacket.getPayload().getQuality());
117: rs = ps.executeQuery();
118: if (rs != null && rs.next()) {
119: previewId = rs.getLong(1);
120: if (rs.wasNull())
121: previewId = 0;
122: }
123: ps.close();
124: }
125: ps = con.prepareStatement("SELECT " + column
126: + ",MIMETYPE," + sizeColumn + " FROM "
127: + TBL_CONTENT_BINARY
128: + " WHERE ID=? AND VER=? AND QUALITY=?");
129: if (previewId != 0)
130: ps.setLong(1, previewId);
131: else
132: ps.setLong(1, dataPacket.getPayload().getId());
133: ps.setInt(2, dataPacket.getPayload().getVersion());
134: ps.setInt(3, dataPacket.getPayload().getQuality());
135: rs = ps.executeQuery();
136: if (rs == null || !rs.next()) {
137: return new DataPacket<BinaryDownloadPayload>(
138: new BinaryDownloadPayload(true,
139: "ex.stream.notFound"), false);
140: }
141: bin = rs.getBinaryStream(1);
142: mimeType = rs.getString(2);
143: datasize = rs.getInt(3);
144: _buffer = new byte[4096];
145: } catch (SQLException e) {
146: return new DataPacket<BinaryDownloadPayload>(
147: new BinaryDownloadPayload(true, "SQL Error: "
148: + e.getMessage()), false);
149: } finally {
150: if (bin == null)
151: Database.closeObjects(BinaryUploadProtocol.class,
152: con, ps);
153: }
154: return new DataPacket<BinaryDownloadPayload>(
155: new BinaryDownloadPayload(mimeType, datasize),
156: false);
157: }
158: return null;
159: }
160:
161: @Override
162: public void cleanup() {
163: if (bin != null) {
164: Database.closeObjects(BinaryUploadProtocol.class, con, ps);
165: }
166: }
167:
168: /**
169: * Callback for sending streamed data
170: *
171: * @param buffer ByteBuffer to write the data into
172: * @return true if more data needs to be sent and this method should be called again
173: * @throws IOException
174: */
175: @Override
176: public boolean sendStream(ByteBuffer buffer) throws IOException {
177: if (buffer.remaining() <= 0 || bin == null)
178: return true; //should not happen but possible ...
179: int max = Math.min(buffer.remaining(), _buffer.length);
180: int read = bin.read(_buffer, 0, max);
181: if (read == -1) {
182: return false;
183: }
184: buffer.put(_buffer, 0, read);
185: return true;
186: }
187: }
|