r959 - xwiki/trunk/src/main/java/com/xpn/xwiki/web

Phung Hai Nam namphunghai at users.forge.objectweb.org
Fri Mar 3 03:07:16 CET 2006


Author: namphunghai
Date: 2006-03-03 03:07:14 +0100 (Fri, 03 Mar 2006)
New Revision: 959

Added:
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/web/ViewAttachRevAction.java
Log:
Add DownloadRevAction.java and ViewAttachRevAction.java

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java	2006-03-02 10:57:53 UTC (rev 958)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/DownloadRevAction.java	2006-03-03 02:07:14 UTC (rev 959)
@@ -0,0 +1,69 @@
+package com.xpn.xwiki.web;
+
+import java.io.IOException;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.doc.XWikiAttachment;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.plugin.XWikiPluginManager;
+
+public class DownloadRevAction extends XWikiAction {
+    public String render(XWikiContext context) throws XWikiException {
+        XWikiRequest request = context.getRequest();
+        XWikiResponse response = context.getResponse();
+        XWikiDocument doc = context.getDoc();
+        String rev = request.getParameter("rev");
+        String path = request.getRequestURI();
+        String filename = Utils.decode(path.substring(path.lastIndexOf("/")+1),context);
+        XWikiAttachment attachment = null;
+
+        if (request.getParameter("id")!=null) {
+            int id = Integer.parseInt(request.getParameter("id"));
+            attachment = (XWikiAttachment) doc.getAttachmentList().get(id);
+        }
+        else {
+            attachment = doc.getAttachment(filename);
+        }
+
+        synchronized (attachment) {
+            try {
+                attachment = attachment.getAttachmentRevision(rev, context);
+            } catch(Exception e){
+                String url = context.getDoc().getURL("viewattachrev", true, context);
+                try {
+                    context.getResponse().sendRedirect(url+"/" + filename);
+                } catch (IOException ioe) {
+                    ioe.printStackTrace();
+                }
+            }
+        }
+
+        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);
+
+        // Choose the right content type
+        String mimetype = attachment.getMimeType(context);
+        response.setContentType(mimetype);
+
+        response.setDateHeader("Last-Modified", attachment.getDate().getTime());
+        // Sending the content of the attachment
+        byte[] data = attachment.getContent(context);
+        response.setContentLength(data.length);
+        try {
+            response.getOutputStream().write(data);
+        } catch (IOException e) {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION,
+                    "Exception while sending response", e);
+        }
+        return null;
+	}
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/web/ViewAttachRevAction.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/web/ViewAttachRevAction.java	2006-03-02 10:57:53 UTC (rev 958)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/web/ViewAttachRevAction.java	2006-03-03 02:07:14 UTC (rev 959)
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author namphunghai
+ * @author torcq
+ * @author sdumitriu
+ */
+package com.xpn.xwiki.web;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Attachment;
+import com.xpn.xwiki.api.Document;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.doc.XWikiAttachment;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.velocity.VelocityContext;
+
+public class ViewAttachRevAction extends XWikiAction {
+
+    public String render(XWikiContext context) throws XWikiException {
+        XWikiRequest request = context.getRequest();
+        XWikiDocument doc = context.getDoc();
+        String path = request.getRequestURI();
+        String filename = Utils.decode(path.substring(path.lastIndexOf("/")+1),context);
+        XWikiAttachment attachment = null;
+        if (request.getParameter("id")!=null) {
+            int id = Integer.parseInt(request.getParameter("id"));
+            attachment = (XWikiAttachment) doc.getAttachmentList().get(id);
+        }
+        else {
+            attachment = doc.getAttachment(filename);
+            if (attachment == null ) {
+                context.put("message", "attachmentdoesnotexist");
+                return "exception";
+            }
+        }
+        VelocityContext vcontext = (VelocityContext) context.get("vcontext");
+        vcontext.put("attachment",new Attachment((Document)vcontext.get("doc"), attachment, context));
+
+        return "viewattachrev";
+    }
+
+}





More information about the Xwiki-notifications mailing list