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: CopyFilePresentation.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.io.InputStream;
029:
030: import com.lutris.classloader.MultiClassLoader;
031: import com.lutris.classloader.Resource;
032:
033: /**
034: * Presentation for file data. This should not be cached, as it copies
035: * the input stream and closes it.
036: */
037: class CopyFilePresentation implements HttpPresentation {
038: private InputStream input;
039: private int contentLength;
040: private String mimeType;
041:
042: private static final int FILE_BUFFER_SIZE = 4096;
043:
044: /*
045: * Copy the input stream to the HTTP output stream.
046: */
047: private void copyFile(HttpPresentationComms comms)
048: throws FilePresentationException {
049:
050: try {
051: byte[] buffer = new byte[FILE_BUFFER_SIZE];
052: HttpPresentationOutputStream output = comms.response
053: .getOutputStream();
054: while (true) {
055: int readLen = input.read(buffer, 0, FILE_BUFFER_SIZE);
056: if (readLen < 0) {
057: break;
058: }
059: output.write(buffer, 0, readLen);
060: }
061: } catch (Exception except) {
062: throw new FilePresentationException(except);
063: }
064: }
065:
066: /**
067: * Construct a new copy file presentation, given an input stream.
068: */
069: protected CopyFilePresentation(ClassLoader classLoader,
070: String urlPath, String fileMimeType)
071: throws FilePresentationException {
072: mimeType = fileMimeType;
073:
074: /*
075: * Get the stream from the classloader. If we are using the
076: * Lutris classloader, we maybe able to get the file size from
077: * the classloader.
078: */
079: try {
080: if (classLoader instanceof MultiClassLoader) {
081: Resource resource = ((MultiClassLoader) classLoader)
082: .getResourceAsIs(urlPath);
083: if (resource != null) {
084: input = resource.getInputStream();
085: contentLength = (int) resource.getSize();
086: }
087: } else {
088: input = classLoader.getResourceAsStream(urlPath);
089: contentLength = -1;
090: }
091: } catch (IOException except) {
092: throw new FilePresentationException("Error accessing \""
093: + urlPath + "\"", except);
094: }
095: if (input == null) {
096: throw new FilePresentationException(
097: new FileNotFoundException("File \"" + urlPath
098: + "\" not found on application class path"));
099: }
100: }
101:
102: /**
103: * Finalizer to close input file if we haven't done it already.
104: */
105: protected void finalize() throws Throwable {
106: if (input != null) {
107: input.close();
108: }
109: super .finalize();
110: }
111:
112: /**
113: * Entry point for copy file presenation.
114: */
115: public void run(HttpPresentationComms comms)
116: throws FilePresentationException {
117: try {
118: comms.response.setContentType(mimeType);
119: comms.response.setContentLength(contentLength);
120: try {
121: if (!comms.request.getMethod().equals("HEAD")) {
122: copyFile(comms);
123: }
124: } finally {
125: input.close();
126: input = null;
127: }
128: } catch (FilePresentationException except) {
129: throw except;
130: } catch (Exception except) {
131: throw new FilePresentationException(except);
132: }
133: }
134: }
|