View Javadoc

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