18 package org.turro.elephant.servlet.context;
20 import java.io.IOException;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.RequestDispatcher;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.annotation.WebFilter;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.turro.config.Configurator;
34 import org.turro.string.Strings;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.direct.DirectContents;
37 import org.turro.elephant.servlet.crawler.CrawlerOptions;
38 import org.turro.file.Document;
39 import org.turro.http.HttpUtil;
40 import org.turro.http.activity.IActivityOptions;
41 import org.turro.http.activity.IPActivity;
42 import org.turro.util.Cached;
48 @WebFilter(filterName =
"ContextFilter", urlPatterns = {
"/*"})
51 private FilterConfig filterConfig;
52 private IPActivity ipActivity;
55 public void doFilter(ServletRequest request, ServletResponse response,
57 throws IOException, ServletException {
58 if(request instanceof HttpServletRequest httpRequest) {
60 if(ipActivity.isBlocked(request.getRemoteAddr(), httpRequest.getServletPath(), httpRequest.getHeader(
"User-Agent"))) {
61 if(response instanceof HttpServletResponse httpResponse) {
63 httpResponse.setContentType(
"text/html");
65 httpResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
66 httpResponse.getWriter().write(crawlers.content());
70 }
else if(!pass(httpRequest.getServletPath())) {
71 if(resource(httpRequest.getServletPath())) {
72 RequestDispatcher rd = getServletContext().getNamedDispatcher(
"default");
73 if(rd!=
null) rd.forward(request, response);
76 if(!checkSchemas(httpRequest, (HttpServletResponse) response)) {
81 forwardMobile(httpRequest, response);
84 ipActivity.check(request.getRemoteAddr(), httpRequest.getServletPath(), httpRequest.getHeader(
"User-Agent"));
88 chain.doFilter(request, response);
92 public void init(FilterConfig filterConfig)
throws ServletException {
93 this.filterConfig = filterConfig;
101 private ServletContext getServletContext() {
102 return filterConfig.getServletContext();
105 private static final Set<String> PASS = Set.of(
"/zkau",
"/tag_",
"/ws_",
"/up_");
107 private boolean pass(String reqUri) {
108 return PASS.stream().anyMatch(pass -> reqUri.equals(pass) || reqUri.startsWith(pass +
"/"));
111 private boolean resource(String reqUri) {
112 String req = HttpUtil.removeSession(reqUri);
113 return !req.endsWith(
".jsp") && req.matches(
".*\\.[a-zA-Z0-9]{2,5}");
117 private boolean checkSchemas(HttpServletRequest request, HttpServletResponse response) {
118 String forcedDomain = ElephantContext.getSiteForcedDomain(),
119 serverName = request.getServerName(),
120 scheme = request.getScheme();
121 boolean changesDone =
false;
122 String url = request.getRequestURL().toString();
123 if(forcedDomain !=
null && !forcedDomain.equals(serverName)) {
124 url = url.replaceFirst(serverName, forcedDomain);
127 if(ElephantContext.getUseSSL() && !
"https".equals(scheme)) {
128 url = url.replaceFirst(
"http\\:",
"https:");
132 String query = request.getQueryString();
133 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
134 response.setHeader(
"Location", url + (Strings.isBlank(query) ?
"" :
"?" + query));
135 response.setHeader(
"Connection",
"close" );
140 private boolean checkMobile(HttpServletRequest request) {
141 String mobilePath = Configurator.instance().asString(
"Context.Mobile.Path");
142 return !Strings.isBlank(mobilePath) &&
143 request.getHeader(
"User-Agent").contains(
"Mobi") &&
144 !request.getServletPath().startsWith(mobilePath);
147 private void forwardMobile(HttpServletRequest request, ServletResponse response)
throws ServletException, IOException {
148 String mobilePath = Configurator.instance().asString(
"Context.Mobile.Path");
149 ((HttpServletResponse) response).sendRedirect(request.getContextPath() +
150 mobilePath + HttpUtil.getServletQuery(request));
static String getRealPath(String path)
static void initServerSchemas(HttpServletRequest request)
static String getEncoding()
static boolean isDirectContent(HttpServletRequest request)
void init(FilterConfig filterConfig)
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
static final transient String CRAWLERS_RESPONSE
static CrawlerOptions instance()