View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.http.MimeTypes.Type;
27  import org.eclipse.jetty.util.BufferUtil;
28  import org.eclipse.jetty.util.resource.Resource;
29  
30  
31  /* ------------------------------------------------------------ */
32  /** HttpContent created from a {@link Resource}.
33   * <p>The HttpContent is used to server static content that is not
34   * cached. So fields and values are only generated as need be an not 
35   * kept for reuse</p>
36   */
37  public class ResourceHttpContent implements HttpContent
38  {
39      final Resource _resource;
40      final String _contentType;
41      final int _maxBuffer;
42      HttpContent _gzip;
43      String _etag;
44  
45      /* ------------------------------------------------------------ */
46      public ResourceHttpContent(final Resource resource, final String contentType)
47      {
48          this(resource,contentType,-1,null);
49      }
50  
51      /* ------------------------------------------------------------ */
52      public ResourceHttpContent(final Resource resource, final String contentType, int maxBuffer)
53      {
54          this(resource,contentType,maxBuffer,null);
55      }
56      
57      /* ------------------------------------------------------------ */
58      public ResourceHttpContent(final Resource resource, final String contentType, int maxBuffer, HttpContent gzip)
59      {
60          _resource=resource;
61          _contentType=contentType;
62          _maxBuffer=maxBuffer;
63          _gzip=gzip;
64      }
65  
66      /* ------------------------------------------------------------ */
67      @Override
68      public String getContentTypeValue()
69      {
70          return _contentType;
71      }
72      
73      /* ------------------------------------------------------------ */
74      @Override
75      public HttpField getContentType()
76      {
77          return _contentType==null?null:new HttpField(HttpHeader.CONTENT_TYPE,_contentType);
78      }
79  
80      /* ------------------------------------------------------------ */
81      @Override
82      public HttpField getContentEncoding()
83      {
84          return null;
85      }
86  
87      /* ------------------------------------------------------------ */
88      @Override
89      public String getContentEncodingValue()
90      {
91          return null;
92      }
93  
94      /* ------------------------------------------------------------ */
95      @Override
96      public String getCharacterEncoding()
97      {
98          return _contentType==null?null:MimeTypes.getCharsetFromContentType(_contentType);
99      }
100 
101     /* ------------------------------------------------------------ */
102     @Override
103     public Type getMimeType()
104     {
105         return _contentType==null?null:MimeTypes.CACHE.get(MimeTypes.getContentTypeWithoutCharset(_contentType));
106     }
107 
108     /* ------------------------------------------------------------ */
109     @Override
110     public HttpField getLastModified()
111     {
112         long lm = _resource.lastModified();
113         return lm>=0?new HttpField(HttpHeader.LAST_MODIFIED,DateGenerator.formatDate(lm)):null;
114     }
115 
116     /* ------------------------------------------------------------ */
117     @Override
118     public String getLastModifiedValue()
119     {
120         long lm = _resource.lastModified();
121         return lm>=0?DateGenerator.formatDate(lm):null;
122     }
123 
124     /* ------------------------------------------------------------ */
125     @Override
126     public ByteBuffer getDirectBuffer()
127     {
128         if (_resource.length()<=0 || _maxBuffer<_resource.length())
129             return null;
130         try
131         {
132             return BufferUtil.toBuffer(_resource,true);
133         }
134         catch(IOException e)
135         {
136             throw new RuntimeException(e);
137         }
138     }
139     
140     /* ------------------------------------------------------------ */
141     @Override
142     public HttpField getETag()
143     {
144         return new HttpField(HttpHeader.ETAG,getETagValue());
145     }
146     
147     /* ------------------------------------------------------------ */
148     @Override
149     public String getETagValue()
150     {
151         return _resource.getWeakETag();
152     }
153 
154     /* ------------------------------------------------------------ */
155     @Override
156     public ByteBuffer getIndirectBuffer()
157     {
158         if (_resource.length()<=0 || _maxBuffer<_resource.length())
159             return null;
160         try
161         {
162             return BufferUtil.toBuffer(_resource,false);
163         }
164         catch(IOException e)
165         {
166             throw new RuntimeException(e);
167         }
168     }
169 
170     /* ------------------------------------------------------------ */
171     @Override
172     public HttpField getContentLength()
173     {
174         long l=_resource.length();
175         return l==-1?null:new HttpField.LongValueHttpField(HttpHeader.CONTENT_LENGTH,_resource.length());
176     }
177 
178     /* ------------------------------------------------------------ */
179     @Override
180     public long getContentLengthValue()
181     {
182         return _resource.length();
183     }
184 
185     /* ------------------------------------------------------------ */
186     @Override
187     public InputStream getInputStream() throws IOException
188     {
189         return _resource.getInputStream();
190     }
191     
192     /* ------------------------------------------------------------ */
193     @Override
194     public ReadableByteChannel getReadableByteChannel() throws IOException
195     {
196         return _resource.getReadableByteChannel();
197     }
198 
199     /* ------------------------------------------------------------ */
200     @Override
201     public Resource getResource()
202     {
203         return _resource;
204     }
205 
206     /* ------------------------------------------------------------ */
207     @Override
208     public void release()
209     {
210         _resource.close();
211     }
212     
213     /* ------------------------------------------------------------ */
214     @Override
215     public String toString()
216     {
217         return String.format("%s@%x{r=%s,gz=%b}",this.getClass().getSimpleName(),hashCode(),_resource,_gzip!=null);
218     }
219 
220     /* ------------------------------------------------------------ */
221     @Override
222     public HttpContent getGzipContent()
223     {
224         return _gzip==null?null:new GzipHttpContent(this,_gzip);
225     }
226 
227 }