001: /*
002: *
003: * Copyright 2007 Luca Molino (luca.molino@assetdata.it)
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.romaframework.aspect.view.echo2;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025:
026: import nextapp.echo2.app.filetransfer.AbstractDownloadProvider;
027:
028: import org.romaframework.aspect.session.SessionAspect;
029: import org.romaframework.core.flow.ObjectContext;
030:
031: /**
032: * @author l.molino
033: *
034: */
035: public class InputStreamDownloadProvider extends
036: AbstractDownloadProvider {
037:
038: private InputStream stream;
039:
040: private String fileName;
041:
042: private int bufferSize = DEF_BUFFER_SIZE;
043:
044: private String contentType = DEF_CONTENT_TYPE;
045:
046: private static final String DEF_CONTENT_TYPE = "text/plain";
047:
048: private static final int DEF_BUFFER_SIZE = 4096;
049:
050: public InputStreamDownloadProvider(InputStream iStream,
051: String iContentType, String iFileName) {
052: stream = iStream;
053: contentType = iContentType;
054: fileName = iFileName;
055: }
056:
057: public InputStreamDownloadProvider(InputStream iStream,
058: String iFileName) {
059: stream = iStream;
060: fileName = iFileName;
061: }
062:
063: /**
064: * @see nextapp.echo2.app.filetransfer.ResourceDownloadProvider#writeFile(java.io.OutputStream)
065: */
066: public void writeFile(OutputStream out) throws IOException {
067: byte[] buffer = new byte[bufferSize];
068: int bytesRead = 0;
069: try {
070: if (stream == null) {
071: throw new IllegalArgumentException(
072: "Specified resource does not exist: "
073: + getFileName() + ".");
074: }
075: do {
076: bytesRead = stream.read(buffer);
077: if (bytesRead > 0) {
078: out.write(buffer, 0, bytesRead);
079: }
080: } while (bytesRead > 0);
081: } finally {
082: if (stream != null) {
083: try {
084: stream.close();
085: } catch (IOException ex) {
086: }
087: }
088: }
089: }
090:
091: @Override
092: public int getSize() {
093: String sessionId = ObjectContext.getInstance().getComponent(
094: SessionAspect.class).getActiveSessionInfo().getId()
095: .toString();
096: File file = new File(sessionId + "_downloadInputStream_"
097: + fileName);
098: InputStream in = null;
099: byte[] buffer = new byte[bufferSize];
100: int bytesRead = 0;
101: int size = -1;
102: try {
103: OutputStream out = new FileOutputStream(file);
104: in = new FileInputStream(file);
105: if (in == null) {
106: throw new IllegalArgumentException(
107: "Specified resource does not exist: "
108: + getFileName() + ".");
109: }
110: try {
111: do {
112: bytesRead = in.read(buffer);
113: if (bytesRead > 0) {
114: out.write(buffer, 0, bytesRead);
115: }
116: } while (bytesRead > 0);
117: } finally {
118: out.flush();
119: out.close();
120: }
121: size = (int) file.length();
122: } catch (IOException ioe) {
123: return size;
124: } finally {
125:
126: if (in != null) {
127: try {
128: in.close();
129: } catch (IOException ex) {
130: }
131: }
132: file.delete();
133: }
134: return size;
135: }
136:
137: @Override
138: public String getFileName() {
139: return fileName;
140: }
141:
142: public String getContentType() {
143: return contentType;
144: }
145:
146: public int getBufferSize() {
147: return bufferSize;
148: }
149:
150: public void setBufferSize(int bufferSize) {
151: this .bufferSize = bufferSize;
152: }
153:
154: public void setContentType(String contentType) {
155: this.contentType = contentType;
156: }
157:
158: }
|