[xwiki-notifications] r8802 - in xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki: . web

jvdrean (SVN) notifications at xwiki.org
Tue Apr 1 11:37:55 CEST 2008


Author: jvdrean
Date: 2008-04-01 11:37:55 +0200 (Tue, 01 Apr 2008)
New Revision: 8802

Modified:
   xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java
   xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/web/XWikiAction.java
Log:
XWIKI-2204 : Ability to set url redirections at the wiki level

Applied patch submitted by Raffaello Pelagalli with modifications.

Modified: xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java	2008-03-31 23:39:19 UTC (rev 8801)
+++ xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java	2008-04-01 09:37:55 UTC (rev 8802)
@@ -553,7 +553,7 @@
                 context.setOriginalDatabase(appname);
 
                 try {
-                    // Let's make sure the virtaul wikis are upgraded to the latest database version
+                    // Let's make sure the virtual wikis are upgraded to the latest database version
                     xwiki.updateDatabase(appname, false, context);
                 } catch (HibernateException e) {
                     // Just to report it
@@ -802,6 +802,10 @@
             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
@@ -2437,6 +2441,41 @@
     }
 
     /**
+     * Verify if the <code>XWiki.GlobalRedirect</code> 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 GlobalRedirect 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 <code>XWiki.XWikiPreferences</code> 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.

Modified: xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/web/XWikiAction.java
===================================================================
--- xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/web/XWikiAction.java	2008-03-31 23:39:19 UTC (rev 8801)
+++ xwiki-platform/core/branches/xwiki-core-1.3/xwiki-core/src/main/java/com/xpn/xwiki/web/XWikiAction.java	2008-04-01 09:37:55 UTC (rev 8802)
@@ -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;
@@ -39,6 +41,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;
@@ -114,19 +117,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) {
@@ -346,6 +355,38 @@
         }
     }
 
+    /**
+     * 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 {



More information about the notifications mailing list