/
FastFileServer.java
203 lines (181 loc) · 7.49 KB
/
FastFileServer.java
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
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.embedded;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.StandardOpenOption;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
/**
* Fast FileServer.
* <p>
* This example shows how to use the Jetty APIs for sending static as fast as
* possible using various strategies for small, medium and large content.
* </p>
* <p>
* The Jetty {@link DefaultServlet} does all this and more, and to a lesser
* extent so does the {@link ResourceHandler}, so unless you have exceptional
* circumstances it is best to use those classes for static content
* </p>
*/
public class FastFileServer
{
public static Server createServer(int port, File resourceBase)
{
Server server = new Server(port);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{
new FastFileHandler(resourceBase),
new DefaultHandler()
});
server.setHandler(handlers);
return server;
}
public static void main(String[] args) throws Exception
{
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
File directory = new File(System.getProperty("user.dir"));
Server server = createServer(port, directory);
server.start();
server.join();
}
static class FastFileHandler extends AbstractHandler
{
private final MimeTypes mimeTypes = new MimeTypes();
private final File dir;
private FastFileHandler(File dir)
{
this.dir = dir;
}
@Override
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
// define small medium and large.
// This should be turned for your content, JVM and OS, but we will
// huge HTTP response buffer size as a measure
final int SMALL = response.getBufferSize();
final int MEDIUM = 8 * SMALL;
// What file to serve?
final File file = new File(this.dir, request.getPathInfo());
// Only handle existing files
if (!file.exists())
return;
// we will handle this request
baseRequest.setHandled(true);
// Handle directories
if (file.isDirectory())
{
if (!request.getPathInfo().endsWith(URIUtil.SLASH))
{
response.sendRedirect(response.encodeRedirectURL(request.getRequestURI() + URIUtil.SLASH));
return;
}
String listing = Resource.newResource(file).getListHTML(
request.getRequestURI(),
request.getPathInfo().lastIndexOf("/") > 0,
request.getQueryString());
response.setContentType("text/html; charset=utf-8");
response.getWriter().println(listing);
return;
}
// Set some content headers.
// Jetty DefaultServlet will cache formatted date strings, but we
// will reformat for each request here
response.setDateHeader("Last-Modified", file.lastModified());
response.setContentLengthLong(file.length());
response.setContentType(mimeTypes.getMimeByExtension(file.getName()));
// send "small" files blocking directly from an input stream
if (file.length() < SMALL)
{
// need to caste to Jetty output stream for best API
((HttpOutput)response.getOutputStream())
.sendContent(FileChannel.open(file.toPath(),
StandardOpenOption.READ));
return;
}
// send not "small" files asynchronously so we don't hold threads if
// the client is slow
final AsyncContext async = request.startAsync();
Callback completionCB = new Callback()
{
@Override
public void succeeded()
{
// Async content write succeeded, so complete async response
async.complete();
}
@Override
public void failed(Throwable x)
{
// log error and complete async response;
x.printStackTrace();
async.complete();
}
};
// send "medium" files from an input stream
if (file.length() < MEDIUM)
{
// the file channel is closed by the async send
((HttpOutput)response.getOutputStream())
.sendContent(FileChannel.open(file.toPath(),
StandardOpenOption.READ), completionCB);
return;
}
// for "large" files get the file mapped buffer to send Typically
// the resulting buffer should be cached as allocating kernel memory
// can be hard to GC on some JVMs. But for this example we will
// create a new buffer per file
ByteBuffer buffer;
try (RandomAccessFile raf = new RandomAccessFile(file, "r");)
{
buffer = raf.getChannel().map(MapMode.READ_ONLY, 0,
raf.length());
}
// Assuming the file buffer might be shared cached version, so lets
// take our own view of it
buffer = buffer.asReadOnlyBuffer();
// send the content as a buffer with a callback to complete the
// async request need to caste to Jetty output stream for best API
((HttpOutput)response.getOutputStream()).sendContent(buffer,
completionCB);
}
}
}