Index: src/main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- src/main/java/com/xpn/xwiki/XWiki.java (revision 8758)
+++ src/main/java/com/xpn/xwiki/XWiki.java Fri Mar 28 19:31:00 CET 2008
@@ -823,7 +823,11 @@
getSkinClass(context);
getGlobalRightsClass(context);
getStatsService(context);
+ if (context.getDatabase().equals(context.getMainXWiki())
+ && "1".equals(context.getWiki().Param("xwiki.preferences.redirect"))) {
+ getRedirectClass(context);
- }
+ }
+ }
// Add a notification for notifications
getNotificationManager().addGeneralRule(new XWikiActionRule(new XWikiPageNotification()));
@@ -2517,6 +2539,41 @@
}
/**
+ * Verify if the XWiki.GlobalRedirect
page exists and that it contains all the
+ * required configuration properties to make the redirection feature work properly. If some properties are missing
+ * they are created and saved in the database.
+ *
+ * @param context the XWiki Context
+ * @return the Redirect Base Class object containing the properties
+ * @throws XWikiException if an error happens during the save to the datavase
+ */
+ public BaseClass getRedirectClass(XWikiContext context) throws XWikiException {
+ XWikiDocument doc;
+ boolean needsUpdate = false;
+
+ doc = getDocument("XWiki.GlobalRedirect", context);
+
+ BaseClass bclass = doc.getxWikiClass();
+ if (context.get("initdone") != null) {
+ return bclass;
+ }
+
+ bclass.setName("XWiki.GlobalRedirect");
+ needsUpdate |= bclass.addTextField("pattern", "Pattern", 30);
+ needsUpdate |= bclass.addTextField("destination", "Destination", 30);
+ String content = doc.getContent();
+ if ((content == null) || (content.equals(""))) {
+ needsUpdate = true;
+ doc.setContent("1 XWiki Global Redirect Class");
+ }
+
+ if (needsUpdate) {
+ saveDocument(doc, context);
+ }
+ return bclass;
+ }
+
+ /**
* Verify if the XWiki.XWikiPreferences
page exists and that it contains all the
* required configuration properties to make XWiki work properly. If some properties are missing
* they are created and saved in the database.
Index: src/main/java/com/xpn/xwiki/web/XWikiAction.java
===================================================================
--- src/main/java/com/xpn/xwiki/web/XWikiAction.java (revision 8758)
+++ src/main/java/com/xpn/xwiki/web/XWikiAction.java Fri Mar 28 19:30:16 CET 2008
@@ -22,6 +22,8 @@
package com.xpn.xwiki.web;
import java.io.IOException;
+import java.util.Vector;
+import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -41,6 +43,7 @@
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.monitor.api.MonitorPlugin;
import com.xpn.xwiki.plugin.fileupload.FileUploadPlugin;
@@ -117,19 +120,25 @@
// Add the form to the context
context.setForm((XWikiForm) form);
XWiki xwiki = null;
+
+ // Verify that the wiki exists
try {
xwiki = XWiki.getXWiki(context);
} catch (XWikiException e) {
if (e.getCode() == XWikiException.ERROR_XWIKI_DOES_NOT_EXIST) {
- // redirect
- String redirect = context.getWiki().Param("xwiki.virtual.redirect");
- response.sendRedirect(redirect);
+ if (!sendGlobalRedirect(response, context.getURL().toString(), context))
+ response.sendRedirect(context.getWiki().Param("xwiki.virtual.redirect"));
return null;
} else {
throw e;
}
}
+ // Send global redirection (if any)
+ if (sendGlobalRedirect(response, context.getURL().toString(), context)) {
+ return null;
+ }
+
// Start monitoring timer
monitor = (MonitorPlugin) xwiki.getPlugin("monitor", context);
if (monitor != null) {
@@ -365,6 +374,39 @@
}
}
+ /**
+ * Send redirection based on a regexp pattern (if any) set at the main wiki level.
+ * To enable this feature you must add xwiki.preferences.redirect=1 to your xwiki.cfg
+ *
+ * @param response the servlet response
+ * @param url url of the request
+ * @param context the XWiki context
+ * @return true if a redirection has been sent
+ * @throws Exception
+ */
+ protected boolean sendGlobalRedirect(XWikiResponse response, String url, XWikiContext context)
+ throws Exception
+ {
+ if ("1".equals(context.getWiki().Param("xwiki.preferences.redirect"))) {
+ XWikiDocument globalPreferences = context.getWiki().getDocument("xwiki:XWiki.XWikiPreferences", context);
+ Vector redirects = globalPreferences.getObjects("XWiki.GlobalRedirect");
+
+ if (redirects != null) {
+ Iterator it = redirects.iterator();
+ while (it.hasNext()) {
+ BaseObject redir = (BaseObject) it.next();
+ String p = redir.getStringValue("pattern");
+ if (url.matches(p)) {
+ String dest = redir.getStringValue("destination");
+ response.sendRedirect(url.replaceAll(p, dest));
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+ }
+
protected void sendRedirect(XWikiResponse response, String page) throws XWikiException
{
try {