Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concord-server: support priority annotation when binding jetty components #841

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Priority;
import javax.inject.Inject;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.lang.management.ManagementFactory;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Set;

Expand Down Expand Up @@ -101,76 +103,76 @@ public HttpServer(ServerConfiguration cfg,
}

// init all @WebListeners
for (ServletContextListener listener : contextListeners) {
contextListeners.stream().sorted(byPriority()).forEachOrdered(listener -> {
WebListener annotation = listener.getClass().getAnnotation(WebListener.class);
if (annotation == null) {
continue;
return;
}

log.info("Event listener -> {}", listener.getClass());
contextHandler.addEventListener(listener);
}
});

// init all @WebServlets
for (HttpServlet servlet : servlets) {
servlets.stream().sorted(byPriority()).forEachOrdered(servlet -> {
WebServlet annotation = servlet.getClass().getAnnotation(WebServlet.class);
if (annotation == null) {
continue;
return;
}

ServletHolder holder = new ServletHolder(servlet);
for (String pathSpec : annotation.value()) {
log.info("Servlet -> {} @ {}", servlet.getClass(), pathSpec);
contextHandler.addServlet(holder, pathSpec);
}
}
});

for (ServletHolder holder : servletHolders) {
servletHolders.stream().sorted(byPriority()).forEachOrdered(holder -> {
WebServlet annotation = holder.getClass().getAnnotation(WebServlet.class);
if (annotation == null) {
continue;
return;
}

for (String pathSpec : annotation.value()) {
log.info("Servlet -> {} @ {}", holder.getClass(), pathSpec);
contextHandler.addServlet(holder, pathSpec);
}
}
});

// init all @WebFilters
for (Filter filter : filters) {
filters.stream().sorted(byPriority()).forEachOrdered(filter -> {
WebFilter annotation = filter.getClass().getAnnotation(WebFilter.class);
if (annotation == null) {
continue;
return;
}

FilterHolder holder = new FilterHolder(filter);
for (String pathSpec : annotation.value()) {
log.info("Servlet -> {} @ {}", filter.getClass(), pathSpec);
log.info("Filter -> {} @ {}", filter.getClass(), pathSpec);
contextHandler.addFilter(holder, pathSpec, EnumSet.allOf(DispatcherType.class));
}
}
});

for (FilterHolder holder : filterHolders) {
filterHolders.stream().sorted(byPriority()).forEachOrdered(holder -> {
WebFilter annotation = holder.getClass().getAnnotation(WebFilter.class);
if (annotation == null) {
continue;
return;
}

for (String pathSpec : annotation.value()) {
log.info("Filter -> {} @ {}", holder.getClass(), pathSpec);
contextHandler.addFilter(holder, pathSpec, EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR));
contextHandler.addFilter(holder, pathSpec, EnumSet.allOf(DispatcherType.class));
}
}
});

ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
contextHandlerCollection.addHandler(contextHandler);

// additional handlers
for (ContextHandlerConfigurator configurator : contextHandlerConfigurators) {
contextHandlerConfigurators.stream().sorted(byPriority()).forEachOrdered(configurator -> {
log.info("Configuring additional context handlers {}...", configurator.getClass());
configurator.configure(contextHandlerCollection);
}
});

StatisticsHandler statisticsHandler = new StatisticsHandler();
statisticsHandler.setHandler(contextHandlerCollection);
Expand Down Expand Up @@ -202,4 +204,11 @@ private static RequestLog createRequestLog(ServerConfiguration cfg) {

return new CustomRequestLog(writer, ServerConfiguration.ACCESS_LOG_FORMAT);
}

private static Comparator<Object> byPriority() {
return Comparator.comparingInt(o -> {
var a = o.getClass().getAnnotation(Priority.class);
return a != null ? a.value() : 0;
});
}
}