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.resource.Resource;
27  
28  /* ------------------------------------------------------------ */
29  /** HttpContent.
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             try
116             {
117                 if (_resource.length()<=0 || _maxBuffer<_resource.length())
118                     return null;
119                 int length=(int)_resource.length();
120                 byte[] array = new byte[length];
121 
122                 int offset=0;
123                 InputStream in=_resource.getInputStream();
124 
125                 do
126                 {
127                     int filled=in.read(array,offset,length);
128                     if (filled<0)
129                         break;
130                     length-=filled;
131                     offset+=filled;
132                 }
133                 while(length>0);
134 
135                 ByteBuffer buffer = ByteBuffer.wrap(array);
136                 return buffer;
137             }
138             catch(IOException e)
139             {
140                 throw new RuntimeException(e);
141             }
142         }
143 
144         /* ------------------------------------------------------------ */
145         @Override
146         public long getContentLength()
147         {
148             return _resource.length();
149         }
150 
151         /* ------------------------------------------------------------ */
152         @Override
153         public InputStream getInputStream() throws IOException
154         {
155             return _resource.getInputStream();
156         }
157         
158         /* ------------------------------------------------------------ */
159         @Override
160         public ReadableByteChannel getReadableByteChannel() throws IOException
161         {
162             return _resource.getReadableByteChannel();
163         }
164 
165         /* ------------------------------------------------------------ */
166         @Override
167         public Resource getResource()
168         {
169             return _resource;
170         }
171 
172         /* ------------------------------------------------------------ */
173         @Override
174         public void release()
175         {
176             _resource.release();
177         }
178     }
179 }