public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
VirtualHost vHost = findMatchingHost(_core.getWgaConfiguration(), request);
if (vHost != null) {
// handle vhost
// determine default database key
String defaultDBKey = getDefaultDBKey(_core, vHost);
String uri = httpRequest.getRequestURI();
String[] pathElements = uri.split("/");
if (pathElements == null || pathElements.length < 1) {
// root url request - redirect to default database or hide db
if (defaultDBKey != null) {
if (vHost.isHideDefaultDatabaseInURL()) {
httpRequest = new DefaultDBRequestWrapper(_core, httpRequest, defaultDBKey);
} else {
httpResponse.sendRedirect(uri + defaultDBKey);
return;
}
}
} else if (pathElements.length == 2 && isRootResource(vHost, pathElements[1])) {
// handle root resource request
VirtualResource resource = findVirtualResource(vHost, pathElements[1]);
httpRequest.getRequestDispatcher(resource.getPath()).forward(request, response);
return;
} else {
// normal db request
String requestedDBKey = pathElements[1];
if (vHost.isHideDefaultDatabaseInURL() && defaultDBKey != null) {
if (requestedDBKey.equalsIgnoreCase(defaultDBKey)) {
// if default db requested redirect to url without dbkey
URLBuilder builder = new URLBuilder(httpRequest, _core.getCharacterEncoding());
String path = builder.getPath().substring(httpRequest.getContextPath().length());
builder.setPath(httpRequest.getContextPath() + path.substring(defaultDBKey.length() + 1));
httpResponse.sendRedirect(builder.rebuild().toString());
return;
}
// we have to check if requestedDBKey is a valid content database - if not we use defaultDatabase
if (!_core.getContentdbs().containsKey(requestedDBKey.toLowerCase())) {
requestedDBKey = defaultDBKey;
httpRequest = new DefaultDBRequestWrapper(_core, httpRequest, defaultDBKey);
}
}
if (!isDBKeyAllowed(_core.getWgaConfiguration(), vHost, requestedDBKey)) {
httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource '" + requestedDBKey + "' is unknown for the requested host.");
_core.getLog().warn("Request from '" + request.getRemoteAddr() + "' to resource '" + uri + "' is not allowed by virtual host definition '" + vHost.getServername() + "'.");
return;
}
}
}