001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: CachedFilePresentation.java,v 1.3 2007-10-19 10:05:39 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.ByteArrayInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.io.InputStream;
031:
032: /**
033: * Presentation for file data that is cached in memory.
034: */
035: class CachedFilePresentation implements HttpPresentation {
036: private String mimeType;
037: private int contentLen = 0;
038:
039: private static final int READ_BUFFER_SIZE = 4096;
040: private byte[] fileByteArray;
041:
042: /**
043: * Construct a new static file presentation, given an input stream.
044: */
045: protected CachedFilePresentation(ClassLoader classLoader,
046: String urlPath, String fileMimeType)
047: throws FilePresentationException {
048: mimeType = fileMimeType;
049: InputStream input = classLoader.getResourceAsStream(urlPath);
050: if (input == null) {
051: throw new FilePresentationException(
052: new FileNotFoundException("File \"" + urlPath
053: + "\" not found on application class path"));
054: }
055:
056: ByteArrayOutputStream inData = new ByteArrayOutputStream();
057: try {
058: // Read in file.
059: try {
060: byte[] buffer = new byte[READ_BUFFER_SIZE];
061: while (true) {
062: int readLen = input.read(buffer, 0,
063: READ_BUFFER_SIZE);
064: if (readLen < 0)
065: break;
066: inData.write(buffer, 0, readLen);
067: contentLen += readLen;
068: }
069: } finally {
070: fileByteArray = inData.toByteArray();
071: input.close();
072: }
073: } catch (Exception except) {
074: throw new FilePresentationException(except);
075: }
076: }
077:
078: /**
079: * Entry point for static file presenation.
080: */
081: public void run(HttpPresentationComms comms)
082: throws FilePresentationException {
083: // ByteArrayInputStream fileData = null;
084: try {
085: comms.response.setContentLength(contentLen);
086: comms.response.setContentType(mimeType);
087:
088: // fileData = new ByteArrayInputStream(fileByteArray);
089:
090: if (!comms.request.getMethod().equals("HEAD")) {
091: // byte [] buffer = new byte[READ_BUFFER_SIZE];
092: HttpPresentationOutputStream output = comms.response
093: .getOutputStream();
094:
095: // while (true) {
096: // int readLen = fileData.read(buffer, 0, READ_BUFFER_SIZE);
097: // if (readLen < 0) {
098: // break;
099: // }
100: // output.write(buffer, 0, readLen);
101: // }
102:
103: output.write(fileByteArray);
104: }
105: } catch (Exception except) {
106: throw new FilePresentationException(except);
107: } /*finally {
108: if (fileData!=null){
109: try {
110: fileData.close();
111: } catch (IOException ex){}
112: }
113: }*/
114: }
115: }
|