KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > CopyFilePresentation


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: CopyFilePresentation.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.appserver.server.httpPresentation;
30
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34
35 import com.lutris.classloader.MultiClassLoader;
36 import com.lutris.classloader.Resource;
37
38 /**
39  * Presentation for file data. This should not be cached, as it copies
40  * the input stream and closes it.
41  */

42 class CopyFilePresentation implements HttpPresentation {
43     private InputStream JavaDoc input;
44     private int contentLength;
45     private String JavaDoc mimeType;
46
47     private static final int FILE_BUFFER_SIZE = 4096;
48
49     /*
50      * Copy the input stream to the HTTP output stream.
51      */

52     private void copyFile(HttpPresentationComms comms)
53         throws FilePresentationException {
54
55         try {
56             byte [] buffer = new byte[FILE_BUFFER_SIZE];
57             HttpPresentationOutputStream output = comms.response.getOutputStream();
58             while (true) {
59                 int readLen = input.read(buffer, 0, FILE_BUFFER_SIZE);
60                 if (readLen < 0) {
61                     break;
62                 }
63                 output.write(buffer, 0, readLen);
64             }
65         } catch (Exception JavaDoc except) {
66             throw new FilePresentationException(except);
67         }
68     }
69
70     /**
71      * Construct a new copy file presentation, given an input stream.
72      */

73     protected CopyFilePresentation(ClassLoader JavaDoc classLoader,
74                                    String JavaDoc urlPath,
75                                    String JavaDoc fileMimeType)
76             throws FilePresentationException {
77         mimeType = fileMimeType;
78
79         /*
80          * Get the stream from the classloader. If we are using the
81          * Lutris classloader, we maybe able to get the file size from
82          * the classloader.
83          */

84         try {
85             if (classLoader instanceof MultiClassLoader) {
86                 Resource resource = ((MultiClassLoader)classLoader).getResourceAsIs(urlPath);
87                 if (resource != null) {
88                     input = resource.getInputStream();
89                     contentLength = (int)resource.getSize();
90                 }
91             } else {
92                 input = classLoader.getResourceAsStream(urlPath);
93                 contentLength = -1;
94             }
95         } catch (IOException JavaDoc except) {
96             throw new FilePresentationException("Error accessing \"" + urlPath + "\"", except);
97         }
98         if (input == null) {
99             throw new FilePresentationException
100                 (new FileNotFoundException JavaDoc ("File \"" + urlPath
101                                             + "\" not found on application class path"));
102         }
103     }
104     
105     /**
106      * Finalizer to close input file if we haven't done it already.
107      */

108     protected void finalize() throws Throwable JavaDoc {
109         if (input != null) {
110             input.close();
111         }
112         super.finalize();
113     }
114
115     /**
116      * Entry point for copy file presenation.
117      */

118     public void run(HttpPresentationComms comms)
119             throws FilePresentationException {
120         try {
121             comms.response.setContentType(mimeType);
122             comms.response.setContentLength(contentLength);
123             try {
124                 if (!comms.request.getMethod().equals("HEAD")) {
125                     copyFile(comms);
126                 }
127             } finally {
128                 input.close();
129                 input = null;
130             }
131         } catch (FilePresentationException except) {
132             throw except;
133         } catch (Exception JavaDoc except) {
134             throw new FilePresentationException(except);
135         }
136     }
137 }
138
Popular Tags