2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.ReadableByteChannel;
26 import org.eclipse.jetty.util.BufferUtil;
27 import org.eclipse.jetty.util.resource.Resource;
29 /* ------------------------------------------------------------ */
34 public interface HttpContent
36 String getContentType();
37 String getLastModified();
38 ByteBuffer getIndirectBuffer();
39 ByteBuffer getDirectBuffer();
41 Resource getResource();
42 long getContentLength();
43 InputStream getInputStream() throws IOException;
44 ReadableByteChannel getReadableByteChannel() throws IOException;
47 /* ------------------------------------------------------------ */
48 /* ------------------------------------------------------------ */
49 /* ------------------------------------------------------------ */
50 public class ResourceAsHttpContent implements HttpContent
52 final Resource _resource;
53 final String _mimeType;
57 /* ------------------------------------------------------------ */
58 public ResourceAsHttpContent(final Resource resource, final String mimeType)
60 this(resource,mimeType,-1,false);
63 /* ------------------------------------------------------------ */
64 public ResourceAsHttpContent(final Resource resource, final String mimeType, int maxBuffer)
66 this(resource,mimeType,maxBuffer,false);
69 /* ------------------------------------------------------------ */
70 public ResourceAsHttpContent(final Resource resource, final String mimeType, boolean etag)
72 this(resource,mimeType,-1,etag);
75 /* ------------------------------------------------------------ */
76 public ResourceAsHttpContent(final Resource resource, final String mimeType, int maxBuffer, boolean etag)
81 _etag=etag?resource.getWeakETag():null;
84 /* ------------------------------------------------------------ */
86 public String getContentType()
91 /* ------------------------------------------------------------ */
93 public String getLastModified()
98 /* ------------------------------------------------------------ */
100 public ByteBuffer getDirectBuffer()
102 if (_resource.length()<=0 || _maxBuffer<_resource.length())
106 return BufferUtil.toBuffer(_resource,true);
110 throw new RuntimeException(e);
114 /* ------------------------------------------------------------ */
116 public String getETag()
121 /* ------------------------------------------------------------ */
123 public ByteBuffer getIndirectBuffer()
125 if (_resource.length()<=0 || _maxBuffer<_resource.length())
129 return BufferUtil.toBuffer(_resource,false);
133 throw new RuntimeException(e);
137 /* ------------------------------------------------------------ */
139 public long getContentLength()
141 return _resource.length();
144 /* ------------------------------------------------------------ */
146 public InputStream getInputStream() throws IOException
148 return _resource.getInputStream();
151 /* ------------------------------------------------------------ */
153 public ReadableByteChannel getReadableByteChannel() throws IOException
155 return _resource.getReadableByteChannel();
158 /* ------------------------------------------------------------ */
160 public Resource getResource()
165 /* ------------------------------------------------------------ */
167 public void release()