use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class ContextHandlerCollection method handle.
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Handler[] handlers = getHandlers();
if (handlers == null || handlers.length == 0)
return;
HttpChannelState async = baseRequest.getHttpChannelState();
if (async.isAsync()) {
ContextHandler context = async.getContextHandler();
if (context != null) {
Handler branch = _contextBranches.get(context);
if (branch == null)
context.handle(target, baseRequest, request, response);
else
branch.handle(target, baseRequest, request, response);
return;
}
}
// }
if (target.startsWith("/")) {
int limit = target.length() - 1;
while (limit >= 0) {
// Get best match
Map.Entry<String, Branch[]> branches = _pathBranches.getBest(target, 1, limit);
if (branches == null)
break;
int l = branches.getKey().length();
if (l == 1 || target.length() == l || target.charAt(l) == '/') {
for (Branch branch : branches.getValue()) {
branch.getHandler().handle(target, baseRequest, request, response);
if (baseRequest.isHandled())
return;
}
}
limit = l - 2;
}
} else {
// This may not work in all circumstances... but then I think it should never be called
for (int i = 0; i < handlers.length; i++) {
handlers[i].handle(target, baseRequest, request, response);
if (baseRequest.isHandled())
return;
}
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class ContextHandlerCollection method mapContexts.
/* ------------------------------------------------------------ */
/**
* Remap the context paths.
*/
@ManagedOperation("update the mapping of context path to context")
public void mapContexts() {
_contextBranches.clear();
if (getHandlers() == null) {
_pathBranches = new ArrayTernaryTrie<>(false, 16);
return;
}
// Create map of contextPath to handler Branch
Map<String, Branch[]> map = new HashMap<>();
for (Handler handler : getHandlers()) {
Branch branch = new Branch(handler);
for (String contextPath : branch.getContextPaths()) {
Branch[] branches = map.get(contextPath);
map.put(contextPath, ArrayUtil.addToArray(branches, branch, Branch.class));
}
for (ContextHandler context : branch.getContextHandlers()) _contextBranches.putIfAbsent(context, branch.getHandler());
}
// Sort the branches so those with virtual hosts are considered before those without
for (Map.Entry<String, Branch[]> entry : map.entrySet()) {
Branch[] branches = entry.getValue();
Branch[] sorted = new Branch[branches.length];
int i = 0;
for (Branch branch : branches) if (branch.hasVirtualHost())
sorted[i++] = branch;
for (Branch branch : branches) if (!branch.hasVirtualHost())
sorted[i++] = branch;
entry.setValue(sorted);
}
// Loop until we have a big enough trie to hold all the context paths
int capacity = 512;
Trie<Map.Entry<String, Branch[]>> trie;
loop: while (true) {
trie = new ArrayTernaryTrie<>(false, capacity);
for (Map.Entry<String, Branch[]> entry : map.entrySet()) {
if (!trie.put(entry.getKey().substring(1), entry)) {
capacity += 512;
continue loop;
}
}
break loop;
}
if (LOG.isDebugEnabled()) {
for (String ctx : trie.keySet()) LOG.debug("{}->{}", ctx, Arrays.asList(trie.get(ctx).getValue()));
}
_pathBranches = trie;
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class DefaultHandler method handle.
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (response.isCommitted() || baseRequest.isHandled())
return;
baseRequest.setHandled(true);
String method = request.getMethod();
// little cheat for common request
if (_serveIcon && _favicon != null && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico")) {
if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString()) == _faviconModified)
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
else {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("image/x-icon");
response.setContentLength(_favicon.length);
response.setDateHeader(HttpHeader.LAST_MODIFIED.toString(), _faviconModified);
response.setHeader(HttpHeader.CACHE_CONTROL.toString(), "max-age=360000,public");
response.getOutputStream().write(_favicon);
}
return;
}
if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500)) {
writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
writer.write("No context on this server matched or handled this request.<BR>");
writer.write("Contexts known to this server are: <ul>");
Server server = getServer();
Handler[] handlers = server == null ? null : server.getChildHandlersByClass(ContextHandler.class);
for (int i = 0; handlers != null && i < handlers.length; i++) {
ContextHandler context = (ContextHandler) handlers[i];
if (context.isRunning()) {
writer.write("<li><a href=\"");
if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
writer.write(request.getScheme() + "://" + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
writer.write(context.getContextPath());
if (context.getContextPath().length() > 1 && context.getContextPath().endsWith("/"))
writer.write("/");
writer.write("\">");
writer.write(context.getContextPath());
if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
writer.write(" @ " + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
writer.write(" ---> ");
writer.write(context.toString());
writer.write("</a></li>\n");
} else {
writer.write("<li>");
writer.write(context.getContextPath());
if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
writer.write(" @ " + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
writer.write(" ---> ");
writer.write(context.toString());
if (context.isFailed())
writer.write(" [failed]");
if (context.isStopped())
writer.write(" [stopped]");
writer.write("</li>\n");
}
}
writer.write("</ul><hr>");
baseRequest.getHttpChannel().getHttpConfiguration().writePoweredBy(writer, "<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a> ", "<hr/>\n");
writer.write("\n</BODY>\n</HTML>\n");
writer.flush();
response.setContentLength(writer.size());
try (OutputStream out = response.getOutputStream()) {
writer.writeTo(out);
}
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class HandlerCollection method destroy.
/* ------------------------------------------------------------ */
@Override
public void destroy() {
if (!isStopped())
throw new IllegalStateException("!STOPPED");
Handler[] children = getChildHandlers();
setHandlers(null);
for (Handler child : children) child.destroy();
super.destroy();
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class HandlerWrapper method insertHandler.
/* ------------------------------------------------------------ */
/**
* Replace the current handler with another HandlerWrapper
* linked to the current handler.
* <p>
* This is equivalent to:
* <pre>
* wrapper.setHandler(getHandler());
* setHandler(wrapper);
* </pre>
* @param wrapper the wrapper to insert
*/
public void insertHandler(HandlerWrapper wrapper) {
if (wrapper == null)
throw new IllegalArgumentException();
HandlerWrapper tail = wrapper;
while (tail.getHandler() instanceof HandlerWrapper) tail = (HandlerWrapper) tail.getHandler();
if (tail.getHandler() != null)
throw new IllegalArgumentException("bad tail of inserted wrapper chain");
Handler next = getHandler();
setHandler(wrapper);
tail.setHandler(next);
}
Aggregations