...
- Allow Roller to use blog handles as subomains (e.g.
http://handle.baseurl.com
) - Allow one Roller instance to specify different URLs for different weblogs, stored in the database
- Ensure that each weblog is only accessed by one root URL
...
- Is this design flexible enough to meet most of the requests we've seen so far?
- How to avoid a repeat of "handle" in the pathInfo of the URL (e.g.
http://handle.baseurl.com/handle
) - Requires adding URL column to weblog table
- How much refactoring is required to achieve DRY in the URL resolution code?
...
Code Block |
---|
/** Absolute URL of Roller, e.g. http://localhost:8080/roller */
public String getAbsoluteSite() {
// return WebloggerRuntimeConfig.getAbsoluteContextURL();
URLStrategy urlStrategy = WebloggerFactory.getWeblogger().getUrlStrategy();
return urlStrategy.getWeblogURL(weblog, null, true);
}
|
Changes to WeblogRequestMapper
WeblogRequestMapper assumes the weblog handle is at the beginning of the pathInfo string in the request. We'll either need to subclass WeblogRequestMapper or inject (or lookup ) a strategy object that can do this. A strategy object would be better than a subclass. Do we create a new strategy object for decoding a URL or do we add methods to the existing URLStrategy objects?
New SubdomainURLStrategy object
Note: For now this still uses properties to override the default subdomain URL. In "phase II" we'll look at moving this property into the Weblog object/table itself.
Code Block |
---|
package org.apache.roller.weblogger.business; import org.apache.roller.weblogger.business.MultiWeblogURLStrategy; import org.apache.roller.weblogger.config.WebloggerConfig; import org.apache.roller.weblogger.config.WebloggerRuntimeConfig; import org.apache.roller.weblogger.pojos.Weblog; /** Multi-domain URL strategy. */ public class SubdomainURLStrategy extends MultiWeblogURLStrategy { /** Get root url for a given weblog. Optionally for a certain locale. */ public String getWeblogURL(Weblog weblog, String locale, boolean absolute) { if(weblog == null) return null; StringBuffer url = new StringBuffer(); if(absolute) { String weblogAbsoluteURL = WebloggerConfig.getProperty("weblog.absoluteurl." + weblog.getHandle()); if (weblogAbsoluteURL != null) { url.append(weblogAbsoluteURL); } else { String subDomainURL = replaceSubdomain(WebloggerRuntimeConfig.getAbsoluteContextURL(), weblog.getHandle()); url.append(subDomainURL); } } else { url.append(WebloggerRuntimeConfig.getRelativeContextURL()); } // TODO: Remove this, the handle shouldn't be in the hostname and in the path url.append("/").append(weblog.getHandle()).append("/"); if(locale != null) { url.append(locale).append("/"); } return url.toString(); } private String replaceSubdomain(String url, String newSub) { String baseURL; // Base URL for subdomains StringBuffer newURL = new StringBuffer(newSub); // URL for this subdomain // For backward compatibility we leave site.absoluteurl property as the full URL of // the main blog, e.g. roller.domain.com, so we'll have to remove the subdomain and re-add it int firstDot = url.indexOf('.'); if ((firstDot == -1) || (firstDot + 2 > url.length())) { // URL without a dot (!?) or dot is last character - Use the full URL // Maybe we should throw an exception here? baseURL = url; } else { // Take everthing after the first dot baseURL = url.substring(firstDot + 1); } newURL.append(newSub).append('.').append(baseURL); return newURL.toString(); } } |
Configuration
Server Administrator
...
- Configurable URL strategy
- Current Strategy (http://blogs.baseurl.com/handle
)
- Subdomain Strategy (http://handle.baseurl.com
)
- Current Strategy (http://blogs.baseurl.com/handle
- Boolean setting to enable/disable per-blog custom URLs (in roller-custom.properties)
...