/*
 * 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 moghrabix
 * @author sdumitriu
 */
package com.xpn.xwiki.web;

import com.xpn.xwiki.XWiki;
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;

import java.io.IOException;

public class DownloadAction extends XWikiAction {


    public String getFileName(String path, String action) {
        path = path.substring(path.indexOf("/" + action));
        int pos = 0;
        for (int i = 0; i < 3; i++) {
            pos = path.indexOf("/", pos + 1);
        }
        if (path.indexOf("/", pos + 1) > 0)
            return path.substring(pos + 1, path.indexOf("/", pos + 1));
        return path.substring(pos + 1);
    }

    public String render(XWikiContext context) throws XWikiException {
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        String path = request.getRequestURI();
        String filename = Utils.decode(getFileName(path, "download"), 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) {
            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);

        String ofilename = XWiki.getURLEncoded(attachment.getFilename()).replaceAll("\\+", " ");
        response.addHeader("Content-disposition", "inline; filename=\"" +  ofilename + "\"");
        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;
	}
}

@@ -3233,16 +3233,48 @@
   byte%5B%5D
+ data;%0A        try %7B%0A           
  data = 
@@ -3297,32 +3297,341 @@
 ntent(context);%0A
+        %7D%0A        catch(XWikiException e) %7B%0A            Object%5B%5D args = %7B filename %7D;%0A            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,%0A                    XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND,%0A                    %22Attachment content %7B0%7D not found%22, null, args);%0A        %7D%0A%0A
         response

add a friendly message if we can't get the content of an attachment