public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String uri = request.getRequestURI();
// Strip jsessionid from URI.
int jsessionidIndex = uri.indexOf(";jsessionid=");
if (jsessionidIndex != -1) {
uri = uri.substring(0, jsessionidIndex);
}
// Process internal context prefix
int tilde = uri.indexOf("~");
if (tilde != -1) {
String path = uri.substring(tilde + 1);
response.sendRedirect(response.encodeRedirectURL(buildURI(
request.getContextPath() + path, request.getQueryString())));
return;
}
// Try to resolve node from URI (without context path).
String nodePath = uri.substring(request.getContextPath().length());
Node node = Nodes.getByPath(nodePath);
// Process redirect rules
if (node == null && !servlets.matcher(nodePath).matches()) {
String sourceURI = buildURI(nodePath, request.getQueryString());
String targetURI = rewriteURI(sourceURI);
if (!targetURI.equals(sourceURI)) {
if (targetURI.contains("://")) {
response.sendRedirect(response.encodeRedirectURL(targetURI));
} else {
response.sendRedirect(response.encodeRedirectURL(request
.getContextPath() + targetURI));
}
return;
}
}
// Save "virtual" root node. Per default it is the absolute root of the
// instance.
// If a node with the server name exists, this node is used as virtual
// root.
if (request.getAttribute("Root") == null) {
String server = Servlets.getServerName(request).replaceFirst(
"www.", "");
Node root = Nodes.getRoot() != null ? Nodes.getRoot().getChild(
server) : null;
if (root == null) {
root = Nodes.getRoot();
}
request.setAttribute("Root", root);
if (node != null && root.getParent() == node) {
response.sendRedirect(response.encodeRedirectURL(request
.getContextPath() + root.getPath()));
return;
}
}
// If no node is found, process filter chain.
if (node == null) {
chain.doFilter(request, response);
return;
}
// Redirect if trailing slash is missing for containers.
if (node.isContainer() && !uri.endsWith("/")) {
response.sendRedirect(response.encodeRedirectURL(buildURI(
uri + "/", request.getQueryString())));
} else {
// Set node into request scope and forward to dispatcher
request.setAttribute(Node.class.getSimpleName(), node);
request.setAttribute(Names.JEASE_SITE_DISPATCHER, dispatcher);