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 import java.nio.ByteBuffer;
24
25 import org.eclipse.jetty.util.resource.Resource;
26
27 /* ------------------------------------------------------------ */
28 /** HttpContent.
29 *
30 *
31 */
32 public interface HttpContent
33 {
34 String getContentType();
35 String getLastModified();
36 ByteBuffer getIndirectBuffer();
37 ByteBuffer getDirectBuffer();
38 Resource getResource();
39 long getContentLength();
40 InputStream getInputStream() throws IOException;
41 void release();
42
43 /* ------------------------------------------------------------ */
44 /* ------------------------------------------------------------ */
45 /* ------------------------------------------------------------ */
46 public class ResourceAsHttpContent implements HttpContent
47 {
48 final Resource _resource;
49 final String _mimeType;
50 final int _maxBuffer;
51
52 public ResourceAsHttpContent(final Resource resource, final String mimeType)
53 {
54 _resource=resource;
55 _mimeType=mimeType;
56 _maxBuffer=-1;
57 }
58
59 public ResourceAsHttpContent(final Resource resource, final String mimeType, int maxBuffer)
60 {
61 _resource=resource;
62 _mimeType=mimeType;
63 _maxBuffer=maxBuffer;
64 }
65
66 /* ------------------------------------------------------------ */
67 @Override
68 public String getContentType()
69 {
70 return _mimeType;
71 }
72
73 /* ------------------------------------------------------------ */
74 @Override
75 public String getLastModified()
76 {
77 return null;
78 }
79
80 /* ------------------------------------------------------------ */
81 @Override
82 public ByteBuffer getDirectBuffer()
83 {
84 return null;
85 }
86
87 /* ------------------------------------------------------------ */
88 @Override
89 public ByteBuffer getIndirectBuffer()
90 {
91 try
92 {
93 if (_resource.length()<=0 || _maxBuffer<_resource.length())
94 return null;
95 int length=(int)_resource.length();
96 byte[] array = new byte[length];
97
98 int offset=0;
99 InputStream in=_resource.getInputStream();
100
101 do
102 {
103 int filled=in.read(array,offset,length);
104 if (filled<0)
105 break;
106 length-=filled;
107 offset+=filled;
108 }
109 while(length>0);
110
111 ByteBuffer buffer = ByteBuffer.wrap(array);
112 return buffer;
113 }
114 catch(IOException e)
115 {
116 throw new RuntimeException(e);
117 }
118 }
119
120 /* ------------------------------------------------------------ */
121 @Override
122 public long getContentLength()
123 {
124 return _resource.length();
125 }
126
127 /* ------------------------------------------------------------ */
128 @Override
129 public InputStream getInputStream() throws IOException
130 {
131 return _resource.getInputStream();
132 }
133
134 /* ------------------------------------------------------------ */
135 @Override
136 public Resource getResource()
137 {
138 return _resource;
139 }
140
141 /* ------------------------------------------------------------ */
142 @Override
143 public void release()
144 {
145 _resource.release();
146 }
147 }
148 }