1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.http;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18
19 import org.eclipse.jetty.io.Buffer;
20 import org.eclipse.jetty.io.ByteArrayBuffer;
21 import org.eclipse.jetty.util.log.Log;
22 import org.eclipse.jetty.util.log.Logger;
23 import org.eclipse.jetty.util.resource.Resource;
24
25 /* ------------------------------------------------------------ */
26 /** HttpContent.
27 *
28 *
29 */
30 public interface HttpContent
31 {
32 Buffer getContentType();
33 Buffer getLastModified();
34 Buffer getIndirectBuffer();
35 Buffer getDirectBuffer();
36 Resource getResource();
37 long getContentLength();
38 InputStream getInputStream() throws IOException;
39 void release();
40
41 /* ------------------------------------------------------------ */
42 /* ------------------------------------------------------------ */
43 /* ------------------------------------------------------------ */
44 public class ResourceAsHttpContent implements HttpContent
45 {
46 private static final Logger LOG = Log.getLogger(ResourceAsHttpContent.class);
47
48 final Resource _resource;
49 final Buffer _mimeType;
50 final int _maxBuffer;
51
52 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType)
53 {
54 _resource=resource;
55 _mimeType=mimeType;
56 _maxBuffer=-1;
57 }
58
59 public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, int maxBuffer)
60 {
61 _resource=resource;
62 _mimeType=mimeType;
63 _maxBuffer=maxBuffer;
64 }
65
66 /* ------------------------------------------------------------ */
67 public Buffer getContentType()
68 {
69 return _mimeType;
70 }
71
72 /* ------------------------------------------------------------ */
73 public Buffer getLastModified()
74 {
75 return null;
76 }
77
78 /* ------------------------------------------------------------ */
79 public Buffer getDirectBuffer()
80 {
81 return null;
82 }
83
84 /* ------------------------------------------------------------ */
85 public Buffer getIndirectBuffer()
86 {
87 InputStream inputStream = null;
88 try
89 {
90 if (_resource.length() <= 0 || _maxBuffer < _resource.length())
91 return null;
92 ByteArrayBuffer buffer = new ByteArrayBuffer((int)_resource.length());
93 inputStream = _resource.getInputStream();
94 buffer.readFrom(inputStream,(int)_resource.length());
95 return buffer;
96 }
97 catch (IOException e)
98 {
99 throw new RuntimeException(e);
100 }
101 finally
102 {
103 if (inputStream != null)
104 {
105 try
106 {
107 inputStream.close();
108 }
109 catch (IOException e)
110 {
111 LOG.warn("Couldn't close inputStream. Possible file handle leak",e);
112 }
113 }
114 }
115 }
116
117 /* ------------------------------------------------------------ */
118 public long getContentLength()
119 {
120 return _resource.length();
121 }
122
123 /* ------------------------------------------------------------ */
124 public InputStream getInputStream() throws IOException
125 {
126 return _resource.getInputStream();
127 }
128
129 /* ------------------------------------------------------------ */
130 public Resource getResource()
131 {
132 return _resource;
133 }
134
135 /* ------------------------------------------------------------ */
136 public void release()
137 {
138 _resource.release();
139 }
140 }
141 }