DownloadServlet.java 3.48 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.AccessDeniedException
 *  javax.jcr.Binary
 *  javax.jcr.Item
 *  javax.jcr.ItemNotFoundException
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.servlet.ServletException
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.apache.jackrabbit.util.Text
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 */
package com.day.crx.delite.impl.servlets;

import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.jcr.AccessDeniedException;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.util.Text;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;

public class DownloadServlet
extends AbstractServlet {
    public DownloadServlet(BundleContext bc) {
        super(bc);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doService(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
        RequestData req = new RequestData(request);
        String path = req.getParameter("path");
        try {
            Item item = session.getItem(path);
            if (item.isNode()) {
                res.sendError(501);
                return;
            }
            InputStream in = null;
            try {
                Binary bin;
                Property p = (Property)item;
                if (p.isMultiple()) {
                    int index = Integer.parseInt(req.getParameter("index"));
                    bin = p.getValues()[index].getBinary();
                } else {
                    bin = p.getBinary();
                }
                in = bin.getStream();
                res.setContentType("application/octet-stream");
                res.addHeader("Content-Disposition", "attachment; filename=\"" + Text.escape((String)this.getDownloadName(p)) + "\"");
                res.addHeader("Content-Length", String.valueOf(bin.getSize()));
                IOUtils.copy((InputStream)in, (OutputStream)res.getOutputStream());
            }
            finally {
                if (in != null) {
                    IOUtils.closeQuietly((InputStream)in);
                }
            }
        }
        catch (PathNotFoundException e) {
            res.sendError(404);
        }
        catch (Exception e) {
            this.logger.error("Error downloading: " + path, (Throwable)e);
            res.sendError(500);
        }
    }

    private String getDownloadName(Property p) throws AccessDeniedException, ItemNotFoundException, RepositoryException {
        if (p.getName().equals("jcr:data")) {
            return p.getParent().getName();
        }
        return p.getName();
    }
}