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