r1054 - in xwiki/trunk/src/main: java/com/xpn/xwiki/web resources

Nguyen Viet Chung chungnv at users.forge.objectweb.org
Mon Apr 24 03:25:02 CEST 2006


Author: chungnv
Date: 2006-04-24 03:25:01 +0200 (Mon, 24 Apr 2006)
New Revision: 1054

Modified:
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiAction.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java
   xwiki/trunk/src/main/resources/ApplicationResources.properties
Log:
Fixed bug XWiki-224 . Create links to older version of attachment when viewing a revision

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java	2006-04-22 21:19:49 UTC (rev 1053)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java	2006-04-24 01:25:01 UTC (rev 1054)
@@ -25,11 +25,17 @@
         else {
             attachment = doc.getAttachment(filename);
         }
+        if (attachment==null) {
+            Object[] args = { filename };
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND,
+                    "Attachment {0} not found", null, args);
+        }
 
         synchronized (attachment) {
             try {
                 attachment = attachment.getAttachmentRevision(rev, context);
-            } catch(Exception e){
+            } catch(XWikiException e){
                 String url = context.getDoc().getURL("viewattachrev", true, context);
                 try {
                     context.getResponse().sendRedirect(url+"/" + filename);
@@ -39,16 +45,9 @@
             }
         }
 
-        if (attachment==null) {
-            Object[] args = { filename };
-            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
-                    XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND,
-                    "Attachment {0} not found", null, args);
-        }
+        XWikiPluginManager plugins = context.getWiki().getPluginManager();
+        attachment = plugins.downloadAttachment(attachment, context);
 
-	XWikiPluginManager plugins = context.getWiki().getPluginManager();
-	attachment = plugins.downloadAttachment(attachment, context);
-
         // Choose the right content type
         String mimetype = attachment.getMimeType(context);
         response.setContentType(mimetype);

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiAction.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiAction.java	2006-04-22 21:19:49 UTC (rev 1053)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiAction.java	2006-04-24 01:25:01 UTC (rev 1054)
@@ -152,11 +152,10 @@
                 return null;
             } catch (Throwable e) {
                 if (!(e instanceof XWikiException)) {
-                    e = new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN,
+                   e = new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN,
                             "Uncaught exception", e);
                 }
 
-                vcontext.put("exp", e);
                 try {
                     XWikiException xex = (XWikiException) e;
                     if (xex.getCode() == XWikiException.ERROR_XWIKI_ACCESS_DENIED) {
@@ -167,8 +166,13 @@
                         String page = Utils.getPage(request, "userinactive");
                         Utils.parseTemplate(page, context);
                         return null;
+                    }else if(xex.getCode() == XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND){
+                        String page = Utils.getPage(request, "exception");
+                        context.put("message","attachmentdoesnotexist");
+                        Utils.parseTemplate(page, context);
+                        return null;
                     }
-
+                    vcontext.put("exp", e);
                     Log log = LogFactory.getLog(XWikiAction.class);
                     if (log.isWarnEnabled()) {
                         log.warn("Uncaught exception: " + e.getMessage(), e);
@@ -201,7 +205,7 @@
                     xwiki.getNotificationManager().verify(context.getDoc(), mapping.getName(), context);
                 } catch (Throwable e) {
                     e.printStackTrace();
-                }
+                    }
 
                 if (monitor != null)
                     monitor.endTimer("notify");

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java	2006-04-22 21:19:49 UTC (rev 1053)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java	2006-04-24 01:25:01 UTC (rev 1054)
@@ -28,9 +28,18 @@
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.io.IOException;
+import java.util.List;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.plugin.XWikiPluginManager;
+import com.xpn.xwiki.doc.XWikiAttachment;
+import com.xpn.xwiki.doc.XWikiDocument;
+import org.apache.velocity.VelocityContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 public class XWikiServletURLFactory extends XWikiDefaultURLFactory {
     protected URL serverURL;
@@ -96,11 +105,11 @@
     private URL getServerURL(String xwikidb, XWikiContext context) throws MalformedURLException {
         if (context.getRequest()!=null){      // necessary to the tests
             final String host = context.getRequest().getHeader("x-forwarded-host"); // apache modproxy host
-            if (host!=null) {            	
-            	int comaind = host.indexOf(',');
-            	final String host1 = comaind>0 ? host.substring(0, comaind) : host;
-            	if (!host1.equals(""))
-            		return new URL("http://"+host1);
+            if (host!=null) {
+                int comaind = host.indexOf(',');
+                final String host1 = comaind>0 ? host.substring(0, comaind) : host;
+                if (!host1.equals(""))
+                    return new URL("http://"+host1);
             }
         }
         if (xwikidb==null)
@@ -113,7 +122,7 @@
             if (url==null)
              return serverURL;
             else
-             return url; 
+             return url;
         }
     }
 
@@ -237,15 +246,42 @@
         addSpace(newpath, web, action, context);
         addName(newpath, name, action, context);
         addFileName(newpath, filename, context);
+        XWikiAttachment attachment = null;
+        XWikiAttachment attLastVer = null;
+        String revdoc =null;
+        String lastver = null;
+
         if ((querystring!=null)&&(!querystring.equals(""))) {
             newpath.append("?");
             newpath.append(querystring);
         }
 
-        try {
+        if(context.getAction().equals("viewrev")){
+            revdoc = context.get("rev").toString();
+            Log log = LogFactory.getLog(XWikiServletURLFactory.class);
+            try{
+                lastver = context.getWiki().getDocument(context.getDoc().getFullName(),context).getVersion();
+                attLastVer = context.getWiki().getDocument(context.getDoc().getFullName(),context).getAttachment(filename) ;
+                attachment = findAttachmentForDocRevision(context.getDoc(),revdoc,filename,context);
+            }catch(XWikiException e){
+                if(log.isErrorEnabled()) log.error("Exception while trying to get attachment version !",e);
+            }
+        }
+
+        try{
+           if(attLastVer != null){
+                if(revdoc != null && !revdoc.equals(lastver)){
+                    String veratt = attachment.getVersion();
+                    String lastveratt = attLastVer.getVersion();
+                    if( !veratt.equals(lastveratt) ){
+                        return createAttachmentRevisionURL(filename,web,name,veratt,querystring,xwikidb,context);
+                    }
+                }
+            }
+
             return new URL(getServerURL(xwikidb, context), newpath.toString());
-        } catch (MalformedURLException e) {
-            // This should not happen
+        }
+        catch(Exception e){
             return null;
         }
     }
@@ -305,15 +341,26 @@
             return "";
         }
     }
-    public URL getRequestURL(XWikiContext context) {    	
-    	final URL url = super.getRequestURL(context);
-		try {
-			final URL servurl = getServerURL(context); // if use apache mod_proxy we needed to know external host address
-			return new URL(url.getProtocol(), servurl.getHost(), servurl.getPort(), url.getFile());
-		} catch (MalformedURLException e) {
-			// This should not happen
-			e.printStackTrace();
-			return url;
-		}
+    public URL getRequestURL(XWikiContext context) {
+        final URL url = super.getRequestURL(context);
+        try {
+            final URL servurl = getServerURL(context); // if use apache mod_proxy we needed to know external host address
+            return new URL(url.getProtocol(), servurl.getHost(), servurl.getPort(), url.getFile());
+        } catch (MalformedURLException e) {
+            // This should not happen
+            e.printStackTrace();
+            return url;
+        }
     }
+    public XWikiAttachment findAttachmentForDocRevision(XWikiDocument doc, String revdoc ,String filename,XWikiContext context ) throws XWikiException{
+        XWikiAttachment attachment = null;
+        XWikiDocument rdoc = context.getWiki().getDocument(doc,revdoc,context);
+        if(filename != null){
+            attachment = rdoc.getAttachment(filename);
+        }else{
+            return  null;
+        }
+
+        return attachment ;
+    }
 }

Modified: xwiki/trunk/src/main/resources/ApplicationResources.properties
===================================================================
--- xwiki/trunk/src/main/resources/ApplicationResources.properties	2006-04-22 21:19:49 UTC (rev 1053)
+++ xwiki/trunk/src/main/resources/ApplicationResources.properties	2006-04-24 01:25:01 UTC (rev 1054)
@@ -207,6 +207,7 @@
 notification_pages=Notification Pages
 auth_active_check=Check Active fields for user authentication
 advanced=Advance
+errornotdefine=Error not define in XWikiException !
 
 chartwizard=Chart Wizard
 chwhide=hide





More information about the Xwiki-notifications mailing list