Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7416c2c4ab1b1e5df72e43a3a42ae3c9984154cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.http;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

import org.eclipse.jetty.http.MimeTypes.Type;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.resource.Resource;


/* ------------------------------------------------------------ */
/** HttpContent created from a {@link Resource}.
 * <p>The HttpContent is used to server static content that is not
 * cached. So fields and values are only generated as need be an not 
 * kept for reuse</p>
 */
public class ResourceHttpContent implements HttpContent
{
    final Resource _resource;
    final String _contentType;
    final int _maxBuffer;
    HttpContent _gzip;
    String _etag;

    /* ------------------------------------------------------------ */
    public ResourceHttpContent(final Resource resource, final String contentType)
    {
        this(resource,contentType,-1,null);
    }

    /* ------------------------------------------------------------ */
    public ResourceHttpContent(final Resource resource, final String contentType, int maxBuffer)
    {
        this(resource,contentType,maxBuffer,null);
    }
    
    /* ------------------------------------------------------------ */
    public ResourceHttpContent(final Resource resource, final String contentType, int maxBuffer, HttpContent gzip)
    {
        _resource=resource;
        _contentType=contentType;
        _maxBuffer=maxBuffer;
        _gzip=gzip;
    }

    /* ------------------------------------------------------------ */
    @Override
    public String getContentTypeValue()
    {
        return _contentType;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public HttpField getContentType()
    {
        return _contentType==null?null:new HttpField(HttpHeader.CONTENT_TYPE,_contentType);
    }

    /* ------------------------------------------------------------ */
    @Override
    public HttpField getContentEncoding()
    {
        return null;
    }

    /* ------------------------------------------------------------ */
    @Override
    public String getContentEncodingValue()
    {
        return null;
    }

    /* ------------------------------------------------------------ */
    @Override
    public String getCharacterEncoding()
    {
        return _contentType==null?null:MimeTypes.getCharsetFromContentType(_contentType);
    }

    /* ------------------------------------------------------------ */
    @Override
    public Type getMimeType()
    {
        return _contentType==null?null:MimeTypes.CACHE.get(MimeTypes.getContentTypeWithoutCharset(_contentType));
    }

    /* ------------------------------------------------------------ */
    @Override
    public HttpField getLastModified()
    {
        long lm = _resource.lastModified();
        return lm>=0?new HttpField(HttpHeader.LAST_MODIFIED,DateGenerator.formatDate(lm)):null;
    }

    /* ------------------------------------------------------------ */
    @Override
    public String getLastModifiedValue()
    {
        long lm = _resource.lastModified();
        return lm>=0?DateGenerator.formatDate(lm):null;
    }

    /* ------------------------------------------------------------ */
    @Override
    public ByteBuffer getDirectBuffer()
    {
        if (_resource.length()<=0 || _maxBuffer>0 && _maxBuffer<_resource.length())
            return null;
        try
        {
            return BufferUtil.toBuffer(_resource,true);
        }
        catch(IOException e)
        {
            throw new RuntimeException(e);
        }
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public HttpField getETag()
    {
        return new HttpField(HttpHeader.ETAG,getETagValue());
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public String getETagValue()
    {
        return _resource.getWeakETag();
    }

    /* ------------------------------------------------------------ */
    @Override
    public ByteBuffer getIndirectBuffer()
    {
        if (_resource.length()<=0 || _maxBuffer>0 && _maxBuffer<_resource.length())
            return null;
        try
        {
            return BufferUtil.toBuffer(_resource,false);
        }
        catch(IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    /* ------------------------------------------------------------ */
    @Override
    public HttpField getContentLength()
    {
        long l=_resource.length();
        return l==-1?null:new HttpField.LongValueHttpField(HttpHeader.CONTENT_LENGTH,_resource.length());
    }

    /* ------------------------------------------------------------ */
    @Override
    public long getContentLengthValue()
    {
        return _resource.length();
    }

    /* ------------------------------------------------------------ */
    @Override
    public InputStream getInputStream() throws IOException
    {
        return _resource.getInputStream();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public ReadableByteChannel getReadableByteChannel() throws IOException
    {
        return _resource.getReadableByteChannel();
    }

    /* ------------------------------------------------------------ */
    @Override
    public Resource getResource()
    {
        return _resource;
    }

    /* ------------------------------------------------------------ */
    @Override
    public void release()
    {
        _resource.close();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        return String.format("%s@%x{r=%s,gz=%b}",this.getClass().getSimpleName(),hashCode(),_resource,_gzip!=null);
    }

    /* ------------------------------------------------------------ */
    @Override
    public HttpContent getGzipContent()
    {
        return _gzip==null?null:new GzipHttpContent(this,_gzip);
    }

}

Back to the top