001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * Description: Utility Class for uploading the FileObject
004: * Copyright: Copyright (c) B. Lofi Dewanto
005: * Company: University of Muenster
006: * @author B. Lofi Dewanto
007: * @version 1.0
008: */package org.openuss.utility;
009:
010: import com.lutris.appserver.server.httpPresentation.*;
011:
012: import com.lutris.mime.*;
013:
014: //import com.lutris.xml.xmlc.*;
015:
016: import java.io.*;
017:
018: import java.rmi.*;
019:
020: import java.text.*;
021:
022: import java.util.*;
023:
024: import org.w3c.dom.*;
025: import org.w3c.dom.html.*;
026:
027: /**
028: * Helper for uploading a file.
029: * This class gets the data for uploading the file with its
030: * date, filename, title and the file object itself.
031: *
032: * @author B. Lofi Dewanto
033: * @version 1.0
034: */
035: public class UploadFileHelper {
036: // The contents of the update form
037: protected static String FILE_DATE = "Date";
038: protected static String FILE_NAME = "Filename";
039: protected static String FILE_TITLE = "Name";
040: protected static String FILE_FILE = "File";
041:
042: // Data
043: protected String title;
044: protected String filename;
045: protected String date;
046: protected FileObjectWrapper fileObject;
047:
048: /**
049: * Constructor.
050: */
051: public UploadFileHelper() throws Exception {
052: // Nothing to do
053: }
054:
055: /**
056: * Constructor.
057: */
058: public UploadFileHelper(HttpPresentationComms comms)
059: throws Exception {
060: this ();
061:
062: // Calculate...
063: calculateData(comms);
064: }
065:
066: /**
067: * Get the title.
068: */
069: public String getTitle() {
070: return title;
071: }
072:
073: /**
074: * Set the title.
075: */
076: public void setTitle(String title) {
077: this .title = title;
078: }
079:
080: /**
081: * Get the date.
082: */
083: public String getDate() {
084: return date;
085: }
086:
087: /**
088: * Set the date.
089: */
090: public void setDate(String date) {
091: this .date = date;
092: }
093:
094: /**
095: * Get the filename.
096: */
097: public String getFilename() {
098: return filename;
099: }
100:
101: /**
102: * Set the filename.
103: */
104: public void setFilename(String filename) {
105: this .filename = filename;
106: }
107:
108: /**
109: * Get the size.
110: */
111: public long getSize() {
112: if (fileObject != null) {
113: // Calculate the size of the FileObject
114: byte[] data = fileObject.getData();
115: long fileSize = data.length;
116:
117: // Put this in kilobyte, don't forget to
118: // add one on it
119: long result = (fileSize / 1024) + 1;
120:
121: return result;
122: } else {
123: // No file
124: return 0;
125: }
126: }
127:
128: /**
129: * Get the file.
130: */
131: public FileObjectWrapper getFileObject() {
132: return fileObject;
133: }
134:
135: /**
136: * Set the file.
137: */
138: public void setFileObject(FileObjectWrapper fileObject) {
139: this .fileObject = fileObject;
140: }
141:
142: /**
143: * Calculate the posted data.
144: */
145: protected void calculateData(HttpPresentationComms comms)
146: throws Exception {
147: // Get the content type first
148: String contentType = comms.request.getContentType();
149:
150: if (contentType == null) {
151: contentType = "";
152: }
153:
154: // Create the content header
155: ContentHeader contentHdr = new ContentHeader("Content-Type: "
156: + contentType);
157:
158: // Get the input stream
159: HttpPresentationInputStream input = comms.request
160: .getInputStream();
161:
162: // Create the multiple mime
163: MultipartMimeInput mime = new MultipartMimeInput(input,
164: contentHdr);
165: MultipartMimeInputStream in;
166:
167: // Go through the multiple content...
168: while ((in = mime.nextPart()) != null) {
169: MimeHeader hdr = in.getHeader("Content-Disposition");
170:
171: // Check the header
172: if (hdr != null) {
173: String contentName = null;
174:
175: ContentHeader chdr = new ContentHeader(hdr);
176: contentName = chdr.getParameter("name");
177:
178: // --- Get the file ---
179: if (contentName.equals(FILE_FILE)) {
180: /* !!!For testing purpose: Directly writing the file to the
181: file system!!!
182: int n;
183: byte[] buffer = new byte[1024];
184: String outFileName = "c:/temp/" + contentName +
185: System.currentTimeMillis();
186:
187: FileOutputStream out = new FileOutputStream(outFileName);
188:
189: while ((n = in.read(buffer, 0, buffer.length)) > 0) {
190: out.write(buffer, 0, n);
191: }
192:
193: out.close();
194: */
195:
196: // Read the stream
197: int n;
198: byte[] buffer = new byte[1024];
199:
200: ByteArrayOutputStream outByte = new ByteArrayOutputStream();
201:
202: while ((n = in.read(buffer, 0, buffer.length)) > 0) {
203: outByte.write(buffer, 0, n);
204: }
205:
206: // Save into the FileObject
207: outByte.flush();
208:
209: byte[] data = outByte.toByteArray();
210:
211: // Save into the FileObject, if the size bigger than zero
212: if (outByte.size() > 0) {
213: fileObject = new FileObjectWrapper(data);
214: }
215:
216: outByte.close();
217: }
218:
219: // --- Get the title ---
220: if (contentName.equals(FILE_TITLE)) {
221: int n;
222: byte[] buffer = new byte[1024];
223:
224: StringBuffer sb = new StringBuffer();
225:
226: while ((n = in.read(buffer, 0, buffer.length)) > 0) {
227: for (int i = 0; i < n; i++) {
228: sb
229: .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
230: }
231: }
232:
233: // Save in the private variable
234: title = new String(sb);
235: }
236:
237: // --- Get the date ---
238: if (contentName.equals(FILE_DATE)) {
239: int n;
240: byte[] buffer = new byte[1024];
241:
242: StringBuffer sb = new StringBuffer();
243:
244: while ((n = in.read(buffer, 0, buffer.length)) > 0) {
245: for (int i = 0; i < n; i++) {
246: sb
247: .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
248: }
249: }
250:
251: // Save in the private variable
252: date = new String(sb);
253: }
254:
255: // --- Get the filename ---
256: if (contentName.equals(FILE_NAME)) {
257: int n;
258: byte[] buffer = new byte[1024];
259:
260: StringBuffer sb = new StringBuffer();
261:
262: while ((n = in.read(buffer, 0, buffer.length)) > 0) {
263: for (int i = 0; i < n; i++) {
264: sb
265: .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
266: }
267: }
268:
269: // Save in the private variable
270: filename = new String(sb);
271: }
272: }
273:
274: // Close the input but not the whole upload
275: in.close();
276: }
277: }
278: }
|