io.microsphere.spring.web.servlet.util.WebUtils Maven / Gradle / Ivy
package io.microsphere.spring.web.servlet.util;
import org.springframework.util.ClassUtils;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.Registration;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Web Utilities Class
*
* @author Mercy
* @see WebUtils
* @since 1.0.0
*/
public abstract class WebUtils {
/**
* Is Running in Servlet 3 or Above
*/
private static final boolean servlet3OrAbove;
/**
* javax.servlet.ServletContainerInitializer @since 3.0
*/
private static final String SERVLET_CONTAINER_INITIALIZER_CLASS_NAME = "javax.servlet.ServletContainerInitializer";
static {
ClassLoader classLoader = WebUtils.class.getClassLoader();
servlet3OrAbove = ClassUtils.isPresent(SERVLET_CONTAINER_INITIALIZER_CLASS_NAME, classLoader);
}
/**
* Is Running below Servlet 3 Container
*
* @return If below , true
*/
public static boolean isRunningBelowServlet3Container() {
return !servlet3OrAbove;
}
/**
* Get {@link ServletContext} from {@link HttpServletRequest}
*
* @param request {@link HttpServletRequest}
* @return non-null
*/
public static ServletContext getServletContext(HttpServletRequest request) {
if (isRunningBelowServlet3Container()) { // below Servlet 3.x
return request.getSession().getServletContext();
}
return request.getServletContext();
}
/**
* Find the {@link Map map} of {@link FilterRegistration} in specified {@link Filter} {@link Class}
*
* @param servletContext {@link ServletContext}
* @param filterClass The {@link Class of class} of {@link Filter}
* @return the {@link Map map} of {@link ServletRegistration}
*/
public static Map findFilterRegistrations(
ServletContext servletContext, Class extends Filter> filterClass) {
Map filterRegistrationsMap = servletContext.getFilterRegistrations();
return findRegistrations(servletContext, filterRegistrationsMap, filterClass);
}
/**
* Find the {@link Map map} of {@link ServletRegistration} in specified {@link Servlet} {@link Class}
*
* @param servletContext {@link ServletContext}
* @param servletClass The {@link Class of class} of {@link Servlet}
* @return the unmodifiable {@link Map map} of {@link ServletRegistration}
*/
public static Map findServletRegistrations(
ServletContext servletContext, Class extends Servlet> servletClass) {
Map servletRegistrationsMap = servletContext.getServletRegistrations();
return findRegistrations(servletContext, servletRegistrationsMap, servletClass);
}
/**
* Find the {@link Map map} of {@link Registration} in specified {@link Class}
*
* @param servletContext {@link ServletContext}
* @param registrationsMap the {@link Map map} of {@link Registration}
* @param targetClass the target {@link Class}
* @param the subtype of {@link Registration}
* @return the unmodifiable {@link Map map} of {@link Registration} i
*/
protected static Map findRegistrations(
ServletContext servletContext, Map registrationsMap, Class> targetClass) {
if (registrationsMap.isEmpty()) {
return Collections.emptyMap();
}
ClassLoader classLoader = servletContext.getClassLoader();
Map foundRegistrationsMap = new LinkedHashMap();
for (Map.Entry entry : registrationsMap.entrySet()) {
R registration = entry.getValue();
String className = registration.getClassName();
Class> registeredRegistrationClass = ClassUtils.resolveClassName(className, classLoader);
if (ClassUtils.isAssignable(targetClass, registeredRegistrationClass)) {
String servletName = entry.getKey();
foundRegistrationsMap.put(servletName, registration);
}
}
return Collections.unmodifiableMap(foundRegistrationsMap);
}
}