VirtualHost vHost = WGAVirtualHostingFilter.findMatchingHost(core.getWgaConfiguration(), request);
if (vHost == null) {
return url;
} else {
URLBuilder builder = parseURL(core, request, url);
if (builder != null) {
String path = builder.getPath().substring(request.getContextPath().length());
if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.indexOf("/") != -1) {
String targetDBKey = path.substring(0, path.indexOf("/"));
// skip vhosts processing for plugins
if (targetDBKey.startsWith("plugin-")) {
return url;
}
// first check if default database is requested and if we have to hide it from url
if (vHost.isHideDefaultDatabaseInURL() && vHost.getDefaultDatabase() != null) {
String defaultDBKey = WGAVirtualHostingFilter.getDefaultDBKey(core, vHost);
if (defaultDBKey != null && targetDBKey.equalsIgnoreCase(defaultDBKey)) {
// default db requested - remove from path
builder.setPath(path.substring(defaultDBKey.length()));
try {
return builder.rebuild(false).toString();
}
catch (UnsupportedEncodingException e) {
core.getLog().error("Unable to rewrite url '" + url + "'.");
}
}
}
if (!WGAVirtualHostingFilter.isDBKeyAllowed(core.getWgaConfiguration(), vHost, targetDBKey)) {
// we have to find the best matching host for this db and create an absolute url here
VirtualHost preferredHost = WGAVirtualHostingFilter.findPreferredHostForDatabase(core.getWgaConfiguration(), targetDBKey);
if (preferredHost != null) {
builder.setHost(preferredHost.getServername());
if (preferredHost.isHideDefaultDatabaseInURL() && preferredHost.getDefaultDatabase() != null) {
String defaultDBKey = WGAVirtualHostingFilter.getDefaultDBKey(core, preferredHost);
if (defaultDBKey != null && targetDBKey.equalsIgnoreCase(defaultDBKey)) {
// default db requested - remove from path
builder.setPath(path.substring(defaultDBKey.length()));
}
}
try {
return builder.rebuild(true).toString();
}
catch (UnsupportedEncodingException e) {
core.getLog().error("Unable to rewrite url '" + url + "'.");
}
}