diff --git a/app/src/main/java/org/apache/roller/planet/business/MultiPlanetURLStrategy.java b/app/src/main/java/org/apache/roller/planet/business/MultiPlanetURLStrategy.java
index 7aa2064758..2139d1ecfc 100644
--- a/app/src/main/java/org/apache/roller/planet/business/MultiPlanetURLStrategy.java
+++ b/app/src/main/java/org/apache/roller/planet/business/MultiPlanetURLStrategy.java
@@ -41,7 +41,7 @@ public String getPlanetURL(String planet) {
}
StringBuilder url = new StringBuilder();
- url.append(WebloggerRuntimeConfig.getProperty("site.absoluteurl"));
+ url.append(WebloggerRuntimeConfig.getAbsoluteContextURL());
url.append('/').append(planet).append('/');
return url.toString();
diff --git a/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java b/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java
index 2b4acd29c7..6f9630881a 100644
--- a/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java
+++ b/app/src/main/java/org/apache/roller/weblogger/business/plugins/entry/SmileysPlugin.java
@@ -39,6 +39,8 @@ public class SmileysPlugin implements WeblogEntryPlugin {
// public for tests
public static Pattern[] smileyPatterns = new Pattern[0];
+ // each img tag after its src's site URL, which comes from the request
+ // being rendered
static String[] imageTags = new String[0];
private static final Properties smileyDefs = new Properties();
@@ -75,15 +77,14 @@ public String getDescription() {
/*
* Convert the SmileyDefs into RegEx patterns and img tags for
- * later use. Need an HttpServletRequest though so that we can
- * get the ServletContext Path. But only do it once.
+ * later use. But only do it once. The site URL in each tag is added
+ * when rendering, since while site.absoluteurl is unset it depends on
+ * the request.
*/
@Override
public synchronized void init(Weblog website) throws WebloggerException {
// don't do this work if Smileys already loaded
if (SmileysPlugin.smileyPatterns.length < 1) {
- String baseURL = WebloggerRuntimeConfig.getAbsoluteContextURL();
-
Pattern[] tempP = new Pattern[SmileysPlugin.smileyDefs.size()];
String[] tempS = new String[SmileysPlugin.smileyDefs.size()];
log.debug("# smileys: " + smileyDefs.size());
@@ -93,8 +94,7 @@ public synchronized void init(Weblog website) throws WebloggerException {
String smiley = (String)enum1.nextElement();
String smileyAlt = htmlEscape(smiley);
tempP[count] = Pattern.compile(regexEscape(smiley));
- tempS[count] = "
requestURLs =
+ new ThreadLocal<>();
+ private static final AtomicBoolean absoluteURLWarningLogged =
+ new AtomicBoolean(false);
+ private static final AtomicBoolean localURLWarningLogged =
+ new AtomicBoolean(false);
// prevent instantiations
@@ -168,34 +190,240 @@ public static String getRuntimeConfigDefsAsString() {
/**
- * Special method which sets the non-persisted absolute url to this site.
+ * The urls seen by the request being handled on this thread. The value of
+ * site.absoluteurl is read once per request, the first time it's needed,
+ * so that all links built for a request and the cache key its content is
+ * stored under agree, even if an administrator sets the property while
+ * the request is being handled.
+ */
+ private static final class RequestURLs {
+ private final String requestURL;
+ private String siteURL;
+ private boolean siteURLRead;
+
+ private RequestURLs(String requestURL) {
+ this.requestURL = requestURL;
+ }
+ }
+
+
+ /**
+ * Set the absolute url of the request being handled on this thread. The
+ * InitFilter sets this at the start of each request and clears it with
+ * clearRequestAbsoluteContextURL() when the request is done.
*
- * This property is *not* persisted in any way.
+ * This value is *not* persisted or shared with other threads.
*/
- public static void setAbsoluteContextURL(String url) {
- absoluteContextURL = url;
+ public static void setRequestAbsoluteContextURL(String url) {
+ requestURLs.set(new RequestURLs(url));
}
-
-
+
+
+ /**
+ * Forget the urls of the request handled on this thread.
+ */
+ public static void clearRequestAbsoluteContextURL() {
+ requestURLs.remove();
+ }
+
+
+ /**
+ * The value of site.absoluteurl, or null if it is unset. Within a
+ * request this is the value the property had when the request first
+ * needed it.
+ */
+ private static String configuredAbsoluteContextURL() {
+ RequestURLs request = requestURLs.get();
+ if (request == null) {
+ return liveAbsoluteContextURL();
+ }
+ if (!request.siteURLRead) {
+ request.siteURL = liveAbsoluteContextURL();
+ request.siteURLRead = true;
+ }
+ return request.siteURL;
+ }
+
+
+ private static String liveAbsoluteContextURL() {
+ String absURL = getProperty("site.absoluteurl");
+ return absURL == null || absURL.isBlank() ? null : absURL;
+ }
+
+
+ /**
+ * True if site.absoluteurl is set, so links can be built without a
+ * request. Background tasks check this before building links.
+ */
+ public static boolean isAbsoluteContextURLSet() {
+ return configuredAbsoluteContextURL() != null;
+ }
+
+
+ /**
+ * The cache key to use for content rendered for the current request, or
+ * null if the content must not be cached. While site.absoluteurl is
+ * unset, links are built from the request's own URL, so the key includes
+ * it: content cached for one host name is only served to requests for
+ * that host name. Content rendered outside a request while the property
+ * is unset is not cached, because its links are incomplete.
+ */
+ public static String cacheKey(String key) {
+ if (key == null || configuredAbsoluteContextURL() != null) {
+ return key;
+ }
+ RequestURLs request = requestURLs.get();
+ if (request == null || request.requestURL == null) {
+ return null;
+ }
+ return key + "|" + request.requestURL;
+ }
+
+ /**
+ * Saves site.absoluteurl from the current request when it is unset and the
+ * request comes from a global administrator, which is the case the first
+ * time an administrator opens the Roller UI after an upgrade.
+ */
+ public static void saveAbsoluteContextURLForAdmin(User user) {
+ if (liveAbsoluteContextURL() != null || user == null
+ || !user.hasGlobalPermission("admin")) {
+ return;
+ }
+ RequestURLs request = requestURLs.get();
+ try {
+ if (request != null && saveAbsoluteContextURLIfUnset(request.requestURL)) {
+ WebloggerFactory.getWeblogger().flush();
+ }
+ } catch (Exception e) {
+ // a lock timeout, for one, is unchecked; the request goes on
+ log.warn("Could not save site.absoluteurl", e);
+ }
+ }
+
+
/**
* Get the absolute url to this site.
*
* This method will just return the value of the "site.absoluteurl"
- * property if it is set, otherwise it will return the non-persisted
- * value which is set by the InitFilter.
+ * property if it is set. Otherwise it returns the url of the request
+ * being handled on this thread, or null when there is no such request
+ * (for example in background tasks).
*/
public static String getAbsoluteContextURL() {
// db prop takes priority if it exists
- String absURL = getProperty("site.absoluteurl");
- if(absURL != null && !absURL.isBlank()) {
+ String absURL = configuredAbsoluteContextURL();
+ if (absURL != null) {
return absURL;
}
-
- return absoluteContextURL;
+
+ if (absoluteURLWarningLogged.compareAndSet(false, true)) {
+ log.warn("The site's absolute URL (site.absoluteurl) is not set, so "
+ + "links in emails and feeds will use the host name of each "
+ + "request. Set \"Absolute URL to site\" under Server "
+ + "Admin > Configuration.");
+ }
+
+ RequestURLs request = requestURLs.get();
+ return request == null ? null : request.requestURL;
+ }
+
+
+ /**
+ * Save the given url as site.absoluteurl if that property is empty.
+ * Called when the site is being set up, by the installer and by the
+ * registration of the first administrator.
+ *
+ * The property is only replaced if it is still empty when it is written,
+ * so of several requests trying at once, only one saves its url. The
+ * rendering caches are cleared when the url is saved, since they hold
+ * content cached per host name while the property was unset.
+ *
+ * @return true if the url was saved; the caller must flush
+ */
+ public static boolean saveAbsoluteContextURLIfUnset(String url)
+ throws WebloggerException {
+
+ if (url == null || url.isBlank()) {
+ return false;
+ }
+ if (!isPublicSiteURL(url)) {
+ if (localURLWarningLogged.compareAndSet(false, true)) {
+ log.warn("Not saving " + url + " as the site's absolute URL "
+ + "(site.absoluteurl) because it isn't a public host name. "
+ + "Set \"Absolute URL to site\" under Server "
+ + "Admin > Configuration.");
+ }
+ return false;
+ }
+ PropertiesManager pmgr = WebloggerFactory.getWeblogger().getPropertiesManager();
+ RuntimeConfigProperty prop = pmgr.getProperty("site.absoluteurl");
+ String current = prop == null ? null : prop.getValue();
+ if (current != null && !current.isBlank()) {
+ return false;
+ }
+ if (prop == null) {
+ // the properties table is filled in at startup, so this only
+ // happens if that failed; a second insert would fail on flush
+ pmgr.saveProperty(new RuntimeConfigProperty("site.absoluteurl", url));
+ } else if (!pmgr.compareAndSetProperty("site.absoluteurl", current, url)) {
+ return false;
+ }
+ clearRenderingCaches();
+ log.info("Set site.absoluteurl to " + url);
+ return true;
}
+ /**
+ * True if the url's host looks like a public name for the site, and so
+ * may be saved automatically. Loopback and private addresses, and host
+ * names without a dot (such as "localhost" or the name of a proxy's
+ * backend), are left for an administrator to set.
+ */
+ static boolean isPublicSiteURL(String url) {
+ String host;
+ try {
+ host = new URI(url).getHost();
+ } catch (URISyntaxException e) {
+ return false;
+ }
+ if (host == null) {
+ return false;
+ }
+ host = host.toLowerCase(Locale.ROOT);
+ if (host.startsWith("[") && host.endsWith("]")) {
+ host = host.substring(1, host.length() - 1);
+ }
+ if (InetAddressValidator.getInstance().isValid(host)) {
+ try {
+ // an address literal, so this doesn't look anything up
+ InetAddress address = InetAddress.getByName(host);
+ byte[] bytes = address.getAddress();
+ return !(address.isLoopbackAddress() || address.isAnyLocalAddress()
+ || address.isLinkLocalAddress() || address.isSiteLocalAddress()
+ || (address instanceof Inet6Address && (bytes[0] & 0xfe) == 0xfc));
+ } catch (UnknownHostException e) {
+ return false;
+ }
+ }
+ return host.indexOf('.') > 0 && !host.endsWith(".")
+ && !host.endsWith(".localhost") && !host.endsWith(".local");
+ }
+
+
+ /**
+ * Clear the caches of rendered content, whose links may have been built
+ * from the url of a request.
+ */
+ private static void clearRenderingCaches() {
+ WeblogPageCache.getInstance().clear();
+ WeblogFeedCache.getInstance().clear();
+ SiteWideCache.getInstance().clear();
+ PlanetCache.getInstance().clear();
+ }
+
+
/**
* Special method which sets the non-persisted relative url to this site.
*
diff --git a/app/src/main/java/org/apache/roller/weblogger/planet/business/PlanetURLStrategy.java b/app/src/main/java/org/apache/roller/weblogger/planet/business/PlanetURLStrategy.java
index 2645badf21..cd3dd48a2f 100644
--- a/app/src/main/java/org/apache/roller/weblogger/planet/business/PlanetURLStrategy.java
+++ b/app/src/main/java/org/apache/roller/weblogger/planet/business/PlanetURLStrategy.java
@@ -35,7 +35,7 @@ public class PlanetURLStrategy extends MultiPlanetURLStrategy {
@Override
public String getPlanetURL(String planet) {
StringBuilder url = new StringBuilder();
- url.append(WebloggerRuntimeConfig.getProperty("site.absoluteurl"));
+ url.append(WebloggerRuntimeConfig.getAbsoluteContextURL());
return url.toString();
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java b/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java
index 076ad46352..2d627743f2 100644
--- a/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java
+++ b/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java
@@ -59,6 +59,23 @@ public WebloggerRomeFeedFetcher() {
}
+ /**
+ * Fetch a feed for a new subscription. Unlike the updater's fetch, which
+ * quietly skips local weblogs until site.absoluteurl is set, this says
+ * why a local weblog can't be added yet.
+ */
+ @Override
+ public Subscription fetchSubscription(String feedURL) throws FetcherException {
+ if (feedURL != null && feedURL.startsWith("weblogger:")
+ && !WebloggerRuntimeConfig.isAbsoluteContextURLSet()) {
+ throw new FetcherException("This site's weblogs can't be added "
+ + "until the site's absolute URL is set. Set it under "
+ + "Server Admin > Configuration.");
+ }
+ return super.fetchSubscription(feedURL);
+ }
+
+
@Override
public Subscription fetchSubscription(String feedURL, Date lastModified)
throws FetcherException {
@@ -74,6 +91,13 @@ public Subscription fetchSubscription(String feedURL, Date lastModified)
return super.fetchSubscription(feedURL, lastModified);
}
+ // the subscription and its entries store absolute links to the
+ // weblog, which can't be built until site.absoluteurl is set
+ if (!WebloggerRuntimeConfig.isAbsoluteContextURLSet()) {
+ log.debug("Skipping LOCAL feed until site.absoluteurl is set - " + feedURL);
+ return null;
+ }
+
// extract blog handle from our special feed url
String weblogHandle = null;
String[] items = feedURL.split(":", 2);
diff --git a/app/src/main/java/org/apache/roller/weblogger/planet/tasks/SyncWebsitesTask.java b/app/src/main/java/org/apache/roller/weblogger/planet/tasks/SyncWebsitesTask.java
index 80f4b23789..373e4dcce4 100644
--- a/app/src/main/java/org/apache/roller/weblogger/planet/tasks/SyncWebsitesTask.java
+++ b/app/src/main/java/org/apache/roller/weblogger/planet/tasks/SyncWebsitesTask.java
@@ -39,6 +39,7 @@
import org.apache.roller.weblogger.business.runnable.RollerTaskWithLeasing;
import org.apache.roller.weblogger.business.startup.WebloggerStartup;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.pojos.Weblog;
@@ -146,6 +147,15 @@ public void runTask() {
return;
}
+ // subscriptions store each weblog's absolute URL, which can't be
+ // built until site.absoluteurl is set
+ if (!WebloggerRuntimeConfig.isAbsoluteContextURLSet()) {
+ log.warn("Not syncing local weblogs with planet subscriptions because "
+ + "the site's absolute URL is not set. Set it under Server "
+ + "Admin > Configuration.");
+ return;
+ }
+
log.info("Syncing local weblogs with planet subscriptions list");
try {
diff --git a/app/src/main/java/org/apache/roller/weblogger/planet/ui/PlanetGroupSubs.java b/app/src/main/java/org/apache/roller/weblogger/planet/ui/PlanetGroupSubs.java
index b4959f5471..b6c3462834 100644
--- a/app/src/main/java/org/apache/roller/weblogger/planet/ui/PlanetGroupSubs.java
+++ b/app/src/main/java/org/apache/roller/weblogger/planet/ui/PlanetGroupSubs.java
@@ -213,7 +213,9 @@ public String saveSubscription() {
addMessage("planetSubscription.success.saved");
} catch (FetcherException ex) {
- addError("planetGroupSubs.error.fetchingFeed", ex.getRootCauseMessage());
+ // not every fetcher error wraps another exception
+ addError("planetGroupSubs.error.fetchingFeed", ex.getRootCause() != null
+ ? ex.getRootCauseMessage() : ex.getMessage());
} catch (RollerException ex) {
log.error("Unexpected error saving subscription", ex);
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java b/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
index 554ccc6beb..2087f0a00b 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
@@ -19,6 +19,7 @@
package org.apache.roller.weblogger.ui.core.filters;
import java.io.IOException;
+import java.util.Locale;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -34,9 +35,14 @@
import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
/**
- * A special initialization filter which ensures that we have an opportunity to
- * extract a few pieces of information about the environment we are running in
- * when the first request is sent.
+ * A special initialization filter which records a few pieces of information
+ * about the environment we are running in.
+ *
+ * The relative context url is taken from the first request. The absolute url
+ * of each request is made available to WebloggerRuntimeConfig for the length
+ * of that request only, so it can stand in for site.absoluteurl while that
+ * property is unset. It is never stored beyond the request, because it comes
+ * from the client's Host header.
*
* @web.filter name="InitFilter"
*/
@@ -44,48 +50,38 @@ public class InitFilter implements Filter {
private static final Log log = LogFactory.getLog(InitFilter.class);
- private boolean initialized = false;
+ private static final UrlValidator URL_VALIDATOR = new UrlValidator(
+ new String[]{"http", "https"},
+ UrlValidator.ALLOW_LOCAL_URLS); // for integration tests
+
+ private volatile boolean initialized = false;
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
- if (!initialized) {
+ HttpServletRequest request = (HttpServletRequest) req;
+ String absPath = getAbsoluteUrl(request);
+
+ if (!initialized && absPath != null) {
// first request, lets do our initialization
- HttpServletRequest request = (HttpServletRequest) req;
-
- UrlValidator validator = new UrlValidator(
- new String[]{"http", "https"},
- UrlValidator.ALLOW_LOCAL_URLS); // for integration tests
-
- if(validator.isValid(request.getRequestURL().toString())) {
-
- // determine absolute and relative url paths to the app
- String relPath = request.getContextPath();
- String absPath = this.getAbsoluteUrl(request);
-
- // set them in our config
- WebloggerRuntimeConfig.setAbsoluteContextURL(absPath);
- WebloggerRuntimeConfig.setRelativeContextURL(relPath);
-
- if (log.isDebugEnabled()) {
- log.debug("relPath = " + relPath);
- log.debug("absPath = " + absPath);
- }
-
- this.initialized = true;
+ String relPath = request.getContextPath();
+ WebloggerRuntimeConfig.setRelativeContextURL(relPath);
+
+ if (log.isDebugEnabled()) {
+ log.debug("relPath = " + relPath);
}
+ this.initialized = true;
}
- chain.doFilter(req, res);
- }
-
- private String getAbsoluteUrl(HttpServletRequest request) {
- return getAbsoluteUrl(request.isSecure(),
- request.getServerName(), request.getContextPath(),
- request.getRequestURI(), request.getRequestURL().toString());
+ WebloggerRuntimeConfig.setRequestAbsoluteContextURL(absPath);
+ try {
+ chain.doFilter(req, res);
+ } finally {
+ WebloggerRuntimeConfig.clearRequestAbsoluteContextURL();
+ }
}
@Override
@@ -96,42 +92,31 @@ public void init(FilterConfig filterConfig) throws ServletException {
public void destroy() {
}
- protected static String getAbsoluteUrl(boolean secure, String serverName, String contextPath, String requestURI, String requestURL){
+ /**
+ * The absolute url of the application as seen by this request: scheme,
+ * host, port (when not the scheme's default) and context path. Returns
+ * null if the request url is not a valid http or https url.
+ */
+ public static String getAbsoluteUrl(HttpServletRequest request) {
- String url;
-
- String fullUrl;
-
- if (!secure) {
- fullUrl = requestURL;
- } else {
- fullUrl = "http://" + serverName + contextPath;
+ if (!URL_VALIDATOR.isValid(request.getRequestURL().toString())) {
+ return null;
}
- // if the uri is only "/" then we are basically done
- if ("/".equals(requestURI)) {
- if (log.isDebugEnabled()) {
- log.debug("requestURI is only '/'. fullUrl: " + fullUrl);
- }
- return removeTrailingSlash(fullUrl);
- }
-
- // find first "/" starting after hostname is specified
- int index = fullUrl.indexOf('/',
- fullUrl.indexOf(serverName));
+ String scheme = request.getScheme().toLowerCase(Locale.ROOT);
+ int port = request.getServerPort();
- if (index != -1) {
- // extract just the part leading up to uri
- url = fullUrl.substring(0, index);
- } else {
- url = fullUrl.trim();
+ StringBuilder url = new StringBuilder(scheme)
+ .append("://").append(request.getServerName());
+ boolean defaultPort = ("http".equals(scheme) && port == 80)
+ || ("https".equals(scheme) && port == 443);
+ if (port > 0 && !defaultPort) {
+ url.append(':').append(port);
}
-
- // then just add on the context path
- url += contextPath;
+ url.append(request.getContextPath());
// make certain that we don't end with a /
- return removeTrailingSlash(url);
+ return removeTrailingSlash(url.toString());
}
protected static String removeTrailingSlash(String url) {
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/PlanetCache.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/PlanetCache.java
index 9ea6214619..2672e8b225 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/PlanetCache.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/PlanetCache.java
@@ -26,6 +26,7 @@
import org.apache.commons.logging.LogFactory;
import org.apache.roller.util.RollerConstants;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.ui.rendering.util.PlanetRequest;
import org.apache.roller.weblogger.util.cache.Cache;
import org.apache.roller.weblogger.util.cache.CacheManager;
@@ -97,8 +98,9 @@ public static PlanetCache getInstance() {
public Object get(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return null;
}
@@ -115,8 +117,9 @@ public Object get(String key) {
public void put(String key, Object value) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
@@ -126,8 +129,9 @@ public void put(String key, Object value) {
public void remove(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/SiteWideCache.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/SiteWideCache.java
index 74e01893b7..9c29c52137 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/SiteWideCache.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/SiteWideCache.java
@@ -103,8 +103,9 @@ public static SiteWideCache getInstance() {
public Object get(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return null;
}
@@ -121,8 +122,9 @@ public Object get(String key) {
public void put(String key, Object value) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
@@ -132,8 +134,9 @@ public void put(String key, Object value) {
public void remove(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogCacheWarmupJob.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogCacheWarmupJob.java
index 98baa89b4e..e020b85ba4 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogCacheWarmupJob.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogCacheWarmupJob.java
@@ -26,6 +26,7 @@
import org.apache.roller.util.RollerConstants;
import org.apache.roller.weblogger.business.runnable.Job;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.pojos.StaticTemplate;
import org.apache.roller.weblogger.pojos.Template;
import org.apache.roller.weblogger.pojos.TemplateRendition.TemplateLanguage;
@@ -102,6 +103,13 @@ private void warmupFeedCache(List weblogs, String type, String format) {
return;
}
+ // without site.absoluteurl, feeds rendered outside a request have no
+ // absolute links, so there is nothing worth caching yet
+ if (!WebloggerRuntimeConfig.isAbsoluteContextURLSet()) {
+ log.info("Skipping cache warmup because site.absoluteurl is not set");
+ return;
+ }
+
// we are working on the feed cache
WeblogFeedCache feedCache = WeblogFeedCache.getInstance();
long start = System.currentTimeMillis();
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogFeedCache.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogFeedCache.java
index 14087b57ba..a6fe2c1c64 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogFeedCache.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogFeedCache.java
@@ -27,6 +27,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest;
import org.apache.roller.weblogger.util.Utilities;
import org.apache.roller.weblogger.util.cache.Cache;
@@ -88,8 +89,9 @@ public static WeblogFeedCache getInstance() {
public Object get(String key, long lastModified) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return null;
}
@@ -115,8 +117,9 @@ public Object get(String key, long lastModified) {
public void put(String key, Object value) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
@@ -126,8 +129,9 @@ public void put(String key, Object value) {
public void remove(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogPageCache.java b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogPageCache.java
index 9edadf97c8..ee4b6191f9 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogPageCache.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/rendering/util/cache/WeblogPageCache.java
@@ -27,6 +27,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
import org.apache.roller.weblogger.util.Utilities;
import org.apache.roller.weblogger.util.cache.Cache;
@@ -87,8 +88,9 @@ public static WeblogPageCache getInstance() {
public Object get(String key, long lastModified) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return null;
}
@@ -113,8 +115,9 @@ public Object get(String key, long lastModified) {
public void put(String key, Object value) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
@@ -124,8 +127,9 @@ public void put(String key, Object value) {
public void remove(String key) {
+ key = WebloggerRuntimeConfig.cacheKey(key);
- if (!cacheEnabled) {
+ if (!cacheEnabled || key == null) {
return;
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Install.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Install.java
index 4ccc4d5105..0c7bd70f03 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Install.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Install.java
@@ -23,6 +23,8 @@
import java.sql.Connection;
import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
@@ -30,9 +32,11 @@
import org.apache.roller.weblogger.business.startup.StartupException;
import org.apache.roller.weblogger.business.startup.WebloggerStartup;
import org.apache.roller.weblogger.config.WebloggerConfig;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
import org.springframework.beans.FatalBeanException;
-import org.apache.struts2.ServletActionContext;
+import org.apache.roller.weblogger.ui.core.filters.InitFilter;
+import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity;
@@ -40,7 +44,7 @@
* Walk user through install process.
*/
// TODO: make this work @AllowedMethods({"execute","create","upgrade","bootstrap"})
-public class Install extends UIAction {
+public class Install extends UIAction implements ServletRequestAware {
private static Log log = LogFactory.getLog(Install.class);
@@ -54,7 +58,17 @@ public class Install extends UIAction {
private boolean success = false;
private List messages = null;
private String databaseName = "Unknown";
+ private HttpServletRequest servletRequest = null;
+
+ protected HttpServletRequest getServletRequest() {
+ return servletRequest;
+ }
+
+ @Override
+ public void setServletRequest(HttpServletRequest servletRequest) {
+ this.servletRequest = servletRequest;
+ }
@Override
public void setPageTitle(String pageTitle) {
@@ -115,7 +129,7 @@ public String execute() {
public String create() {
- if (!BootstrapSecurity.isValid(ServletActionContext.getRequest())) return BOOTSTRAP;
+ if (!BootstrapSecurity.isValid(getServletRequest())) return BOOTSTRAP;
if (WebloggerFactory.isBootstrapped()) {
return SUCCESS;
@@ -136,7 +150,7 @@ public String create() {
public String upgrade() {
- if (!BootstrapSecurity.isValid(ServletActionContext.getRequest())) return BOOTSTRAP;
+ if (!BootstrapSecurity.isValid(getServletRequest())) return BOOTSTRAP;
if (WebloggerFactory.isBootstrapped()) {
return SUCCESS;
@@ -157,7 +171,7 @@ public String upgrade() {
public String bootstrap() {
- if (!BootstrapSecurity.isValid(ServletActionContext.getRequest())) return BOOTSTRAP;
+ if (!BootstrapSecurity.isValid(getServletRequest())) return BOOTSTRAP;
log.info("ENTERING");
if (WebloggerFactory.isBootstrapped()) {
@@ -178,6 +192,10 @@ public String bootstrap() {
// installer reachable until the next restart.
BootstrapSecurity.completeIfInstalled(WebloggerFactory.getWeblogger());
+ // whoever runs the installer is setting up the site, so take the
+ // site's url from this request if none is configured
+ saveSiteUrlIfUnset();
+
// also need to do planet if it's configured
if (WebloggerConfig.getBooleanProperty("planet.aggregator.enabled")) {
@@ -208,6 +226,19 @@ public String bootstrap() {
}
+ private void saveSiteUrlIfUnset() {
+ try {
+ if (WebloggerRuntimeConfig.saveAbsoluteContextURLIfUnset(
+ InitFilter.getAbsoluteUrl(getServletRequest()))) {
+ WebloggerFactory.getWeblogger().flush();
+ }
+ } catch (Exception e) {
+ log.warn("Could not save site.absoluteurl; set it under Server "
+ + "Admin > Configuration", e);
+ }
+ }
+
+
public String getDatabaseProductName() {
String name = "unknown";
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
index 8d9c85050c..0263f348c9 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
@@ -35,6 +35,7 @@
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
import org.apache.roller.weblogger.ui.core.RollerSession;
+import org.apache.roller.weblogger.ui.core.filters.InitFilter;
import org.apache.roller.weblogger.ui.core.security.CustomUserRegistry;
import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
@@ -197,7 +198,8 @@ public String save() {
if (!hasActionErrors()) {
try {
- if (!BootstrapSecurity.isCompleted()) BootstrapSecurity.beginInitialAdmin();
+ boolean initialAdmin = !BootstrapSecurity.isCompleted();
+ if (initialAdmin) BootstrapSecurity.beginInitialAdmin();
UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
@@ -218,6 +220,21 @@ public String save() {
// are we using email activation?
boolean activationEnabled = WebloggerRuntimeConfig.getBooleanProperty(
"user.account.email.activation");
+
+ // the activation email links to the site, so without a site
+ // url it can't be sent and the account could never be
+ // activated. The first administrator is let through, as
+ // nobody else can set the url.
+ if (activationEnabled && !initialAdmin
+ && !WebloggerRuntimeConfig.isAbsoluteContextURLSet()) {
+ log.warn("A registration was refused because activation "
+ + "emails can't be sent until the site's absolute "
+ + "URL is set. Set it under Server Admin > "
+ + "Configuration.");
+ addError("Register.error.activationUnavailable");
+ return INPUT;
+ }
+
if (activationEnabled) {
// User account will be enabled after the activation process
ud.setEnabled(Boolean.FALSE);
@@ -242,6 +259,12 @@ public String save() {
WebloggerFactory.getWeblogger().getPropertiesManager().saveProperty(
new RuntimeConfigProperty(BootstrapSecurity.COMPLETION_PROPERTY, "true"));
+ // the first administrator is the one setting up the site, so
+ // take the site's url from this request if none is configured
+ if (initialAdmin) {
+ saveSiteUrlIfUnset();
+ }
+
WebloggerFactory.getWeblogger().flush();
if (!BootstrapSecurity.isCompleted()) BootstrapSecurity.complete();
@@ -270,6 +293,20 @@ public String save() {
return INPUT;
}
+ /**
+ * Failing to save the site's url doesn't stop the registration; an
+ * administrator can set it later.
+ */
+ private void saveSiteUrlIfUnset() {
+ try {
+ WebloggerRuntimeConfig.saveAbsoluteContextURLIfUnset(
+ InitFilter.getAbsoluteUrl(getServletRequest()));
+ } catch (Exception e) {
+ log.warn("Could not save site.absoluteurl; set it under Server "
+ + "Admin > Configuration", e);
+ }
+ }
+
private String retryActivationCode(UserManager mgr, String inActivationCode) throws WebloggerException {
if (mgr.getUserByActivationCode(inActivationCode) != null) {
// In the *extremely* unlikely event that we generate an
@@ -299,11 +336,13 @@ private void sendActivationMailIfNeeded(User ud, boolean activationEnabled) {
try {
// send activation mail to the user
MailUtil.sendUserActivationEmail(ud);
+ setActivationStatus("pending");
} catch (WebloggerException ex) {
log.error("Error sending activation email to - " + ud.getEmailAddress(), ex);
+ // the account exists but can't be activated without the email
+ addError("Register.error.activationNotSent");
+ setActivationStatus("error");
}
-
- setActivationStatus("pending");
}
}
diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIActionInterceptor.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIActionInterceptor.java
index 9eb5009573..689d5387b6 100644
--- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIActionInterceptor.java
+++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/UIActionInterceptor.java
@@ -24,6 +24,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
import org.apache.roller.weblogger.pojos.Weblog;
import org.apache.roller.weblogger.ui.core.RollerSession;
import org.apache.struts2.StrutsStatics;
@@ -68,6 +69,11 @@ public String doIntercept(ActionInvocation invocation) throws Exception {
RollerSession rses = RollerSession.getRollerSession(request);
theAction.setAuthenticatedUser(rses != null ? rses.getAuthenticatedUser() : null);
+ // the first administrator request after an upgrade fills in an
+ // unset site.absoluteurl, so links and caching don't depend on
+ // the host of each request from then on
+ WebloggerRuntimeConfig.saveAbsoluteContextURLForAdmin(theAction.getAuthenticatedUser());
+
// Set the action weblog by looking up the weblog handle. Always
// overwrite (to null when no weblog resolves), so a value bound
// from request parameters cannot survive.
diff --git a/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java b/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
index eeedc9e322..afb8c60917 100644
--- a/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
+++ b/app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java
@@ -71,6 +71,16 @@ public static boolean isMailConfigured() {
return WebloggerStartup.getMailProvider() != null;
}
+ /**
+ * True if site.absoluteurl is set. Links in emails are only built from
+ * that setting, never from the request that caused the email, because
+ * the request's host name is chosen by whoever sent it.
+ */
+ private static boolean canLinkToSite() {
+ return WebloggerRuntimeConfig.isAbsoluteContextURLSet();
+ }
+
+
/**
* Send an email notice that a new pending entry has been submitted.
*/
@@ -82,6 +92,13 @@ public static void sendPendingEntryNotice(WeblogEntry entry) throws WebloggerExc
if (mailSession == null) {
throw new WebloggerException("Couldn't get mail Session");
}
+
+ if (!canLinkToSite()) {
+ log.warn("No email was sent about the pending entry \"" + entry.getTitle()
+ + "\" because the site's absolute URL is not set. Set it under "
+ + "Server Admin > Configuration.");
+ return;
+ }
try {
WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
@@ -151,6 +168,13 @@ public static void sendWeblogInvitation(Weblog website, User user)
throw new WebloggerException("ERROR: Notification email(s) not sent, "
+ "Roller's mail session not properly configured");
}
+
+ if (!canLinkToSite()) {
+ throw new WebloggerException("The invitation was saved, but no email "
+ + "was sent because the site's absolute URL is not set. An "
+ + "administrator can set it under Server Admin > "
+ + "Configuration.");
+ }
try {
String from = website.getEmailAddress();
@@ -207,6 +231,12 @@ public static void sendUserActivationEmail(User user)
throw new WebloggerException("ERROR: Notification email(s) not sent, "
+ "Roller's mail session not properly configured");
}
+
+ if (!canLinkToSite()) {
+ throw new WebloggerException("No activation email was sent because "
+ + "the site's absolute URL is not set. Set it under Server "
+ + "Admin > Configuration.");
+ }
try {
ResourceBundle resources = ResourceBundle.getBundle(
@@ -334,19 +364,27 @@ public static void sendEmailNotification(WeblogEntryComment commentObject,
msg.append((isPlainText) ? commentObject.getContent()
: Utilities.transformToHTMLSubset(Utilities.escapeHTML(commentObject.getContent())));
- msg.append((isPlainText) ? "\n\n----\n"
- : "
");
- msg.append(resources.getString("email.comment.respond") + ": ");
- msg.append((isPlainText) ? "\n" : "
");
-
- // Build link back to comment
- String commentURL = WebloggerFactory.getWeblogger()
- .getUrlStrategy().getWeblogCommentsURL(weblog, null, entry.getAnchor(), true);
-
- if (isPlainText) {
- msg.append(commentURL);
- } else {
- msg.append(""+commentURL+"");
+ // links are left out while the site's absolute URL is unset
+ boolean linkToSite = canLinkToSite();
+ if (!linkToSite) {
+ log.debug("Leaving links out of comment email because site.absoluteurl is not set");
+ }
+
+ if (linkToSite) {
+ msg.append((isPlainText) ? "\n\n----\n"
+ : "
");
+ msg.append(resources.getString("email.comment.respond") + ": ");
+ msg.append((isPlainText) ? "\n" : "
");
+
+ // Build link back to comment
+ String commentURL = WebloggerFactory.getWeblogger()
+ .getUrlStrategy().getWeblogCommentsURL(weblog, null, entry.getAnchor(), true);
+
+ if (isPlainText) {
+ msg.append(commentURL);
+ } else {
+ msg.append(""+commentURL+"");
+ }
}
// next the additional information that is sent to the blog owner
@@ -399,19 +437,24 @@ public static void sendEmailNotification(WeblogEntryComment commentObject,
ownermsg.append((isPlainText) ? "\n\n" : "
");
}
// add link to weblog edit page so user can login to manage comments
- ownermsg.append(resources.getString("email.comment.management.link") + ": ");
- ownermsg.append((isPlainText) ? "\n" : "
");
+ if (linkToSite) {
+ ownermsg.append(resources.getString("email.comment.management.link") + ": ");
+ ownermsg.append((isPlainText) ? "\n" : "
");
- Map parameters = new HashMap<>();
- parameters.put("bean.entryId", entry.getId());
- String deleteURL = WebloggerFactory.getWeblogger().getUrlStrategy().getActionURL(
- "comments", "/roller-ui/authoring", weblog.getHandle(), parameters, true);
+ Map parameters = new HashMap<>();
+ parameters.put("bean.entryId", entry.getId());
+ String deleteURL = WebloggerFactory.getWeblogger().getUrlStrategy().getActionURL(
+ "comments", "/roller-ui/authoring", weblog.getHandle(), parameters, true);
- if (isPlainText) {
- ownermsg.append(deleteURL);
- } else {
- ownermsg.append(
- "" + deleteURL + "");
+ if (isPlainText) {
+ ownermsg.append(deleteURL);
+ } else {
+ ownermsg.append("" + deleteURL + "");
+ }
+ }
+
+ if (!isPlainText) {
+ ownermsg.append("");
msg.append("