View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.http;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  
19  import org.eclipse.jetty.io.Buffer;
20  import org.eclipse.jetty.io.ByteArrayBuffer;
21  import org.eclipse.jetty.util.resource.Resource;
22  
23  /* ------------------------------------------------------------ */
24  /** HttpContent.
25   * 
26   *
27   */
28  public interface HttpContent
29  {
30      Buffer getContentType();
31      Buffer getLastModified();
32      Buffer getIndirectBuffer();
33      Buffer getDirectBuffer();
34      Resource getResource();
35      long getContentLength();
36      InputStream getInputStream() throws IOException;
37      void release();
38      
39      /* ------------------------------------------------------------ */
40      /* ------------------------------------------------------------ */
41      /* ------------------------------------------------------------ */
42      public class ResourceAsHttpContent implements HttpContent
43      {
44          final Resource _resource;
45          final Buffer _mimeType;
46          final int _maxBuffer;
47  
48          public ResourceAsHttpContent(final Resource resource, final Buffer mimeType)
49          {
50              _resource=resource;
51              _mimeType=mimeType;
52              _maxBuffer=-1;
53          }
54          
55          public ResourceAsHttpContent(final Resource resource, final Buffer mimeType, int maxBuffer)
56          {
57              _resource=resource;
58              _mimeType=mimeType;
59              _maxBuffer=maxBuffer;
60          }
61  
62          /* ------------------------------------------------------------ */
63          public Buffer getContentType()
64          {
65              return _mimeType;
66          }
67  
68          /* ------------------------------------------------------------ */
69          public Buffer getLastModified()
70          {
71              return null;
72          }
73  
74          /* ------------------------------------------------------------ */
75          public Buffer getDirectBuffer()
76          {
77              return null;
78          }
79  
80          /* ------------------------------------------------------------ */
81          public Buffer getIndirectBuffer()
82          {
83              try
84              {
85                  if (_resource.length()<=0 || _maxBuffer<_resource.length())
86                      return null;
87                  ByteArrayBuffer buffer = new ByteArrayBuffer((int)_resource.length());
88                  buffer.readFrom(_resource.getInputStream(),(int)_resource.length());
89                  return buffer;
90              }
91              catch(IOException e)
92              {
93                  throw new RuntimeException(e);
94              }
95          }
96  
97          /* ------------------------------------------------------------ */
98          public long getContentLength()
99          {
100             return _resource.length();
101         }
102 
103         /* ------------------------------------------------------------ */
104         public InputStream getInputStream() throws IOException
105         {
106             return _resource.getInputStream();
107         }
108 
109         /* ------------------------------------------------------------ */
110         public Resource getResource()
111         {
112             return _resource;
113         }
114 
115         /* ------------------------------------------------------------ */
116         public void release()
117         {
118             _resource.release();
119         }
120     }
121 }