1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import java.nio.channels.ReadableByteChannel;
25
26 import org.eclipse.jetty.util.resource.Resource;
27
28
29
30
31
32
33 public interface HttpContent
34 {
35 String getContentType();
36 String getLastModified();
37 ByteBuffer getIndirectBuffer();
38 ByteBuffer getDirectBuffer();
39 String getETag();
40 Resource getResource();
41 long getContentLength();
42 InputStream getInputStream() throws IOException;
43 ReadableByteChannel getReadableByteChannel() throws IOException;
44 void release();
45
46
47
48
49 public class ResourceAsHttpContent implements HttpContent
50 {
51 final Resource _resource;
52 final String _mimeType;
53 final int _maxBuffer;
54 final String _etag;
55
56
57 public ResourceAsHttpContent(final Resource resource, final String mimeType)
58 {
59 this(resource,mimeType,-1,false);
60 }
61
62
63 public ResourceAsHttpContent(final Resource resource, final String mimeType, int maxBuffer)
64 {
65 this(resource,mimeType,maxBuffer,false);
66 }
67
68
69 public ResourceAsHttpContent(final Resource resource, final String mimeType, boolean etag)
70 {
71 this(resource,mimeType,-1,etag);
72 }
73
74
75 public ResourceAsHttpContent(final Resource resource, final String mimeType, int maxBuffer, boolean etag)
76 {
77 _resource=resource;
78 _mimeType=mimeType;
79 _maxBuffer=maxBuffer;
80 _etag=etag?resource.getWeakETag():null;
81 }
82
83
84 @Override
85 public String getContentType()
86 {
87 return _mimeType;
88 }
89
90
91 @Override
92 public String getLastModified()
93 {
94 return null;
95 }
96
97
98 @Override
99 public ByteBuffer getDirectBuffer()
100 {
101 return null;
102 }
103
104
105 @Override
106 public String getETag()
107 {
108 return _etag;
109 }
110
111
112 @Override
113 public ByteBuffer getIndirectBuffer()
114 {
115 if (_resource.length()<=0 || _maxBuffer<_resource.length())
116 return null;
117 int length=(int)_resource.length();
118 byte[] array = new byte[length];
119
120 int offset=0;
121 try (InputStream in=_resource.getInputStream())
122 {
123 do
124 {
125 int filled=in.read(array,offset,length);
126 if (filled<0)
127 break;
128 length-=filled;
129 offset+=filled;
130 }
131 while(length>0);
132
133 ByteBuffer buffer = ByteBuffer.wrap(array);
134 return buffer;
135 }
136 catch(IOException e)
137 {
138 throw new RuntimeException(e);
139 }
140 }
141
142
143 @Override
144 public long getContentLength()
145 {
146 return _resource.length();
147 }
148
149
150 @Override
151 public InputStream getInputStream() throws IOException
152 {
153 return _resource.getInputStream();
154 }
155
156
157 @Override
158 public ReadableByteChannel getReadableByteChannel() throws IOException
159 {
160 return _resource.getReadableByteChannel();
161 }
162
163
164 @Override
165 public Resource getResource()
166 {
167 return _resource;
168 }
169
170
171 @Override
172 public void release()
173 {
174 _resource.close();
175 }
176 }
177 }