head	1.15;
access;
symbols
	TIGRIS_1_1_0RC2:1.4.6.4
	TIGRIS_1_1_0RC1:1.4.6.4
	TIGRIS_1_1:1.4.6.4.0.2
	TIGRIS_1_0_8:1.4.6.4
	TIGRIS_1_0_8RC3:1.4.6.4
	TIGRIS_1_0_8RC2:1.4.6.4
	TIGRIS_1_0_8RC1:1.4.6.4
	TIGRIS_1_0_7:1.4.6.4
	TIGRIS_1_0_7RC3:1.4.6.4
	TIGRIS_1_0_7RC2:1.4.6.4
	TIGRIS_1_0_7RC1:1.4.6.4
	TIGRIS_1_0_6:1.4.6.4
	TIGRIS_1_0_6RC5:1.4.6.4
	TIGRIS_1_0_6RC4:1.4.6.4
	TIGRIS_1_0_6RC3:1.4.6.4
	TIGRIS_1_0_6RC2:1.4.6.4
	TIGRIS_1_0_6RC1:1.4.6.4
	TIGRIS_1_0_5:1.4.6.2
	TIGRIS_1_0_5RC6:1.4.6.2
	TIGRIS_1_0_5RC5:1.4.6.2
	TIGRIS_1_0_5RC4:1.4.6.2
	TIGRIS_1_0_5RC3:1.4.6.2
	TIGRIS_1_0_5RC2:1.4.6.2
	TIGRIS_1_0_5RC1:1.4.6.2
	TIGRIS_1_0_4:1.4.6.2
	TIGRIS_1_0_3:1.4.6.1
	TIGRIS_1_0_2:1.4.6.1
	TIGRIS_1_0_1:1.4
	TIGRIS_1_0:1.4.0.6
	TIGRIS_1_0_0:1.4
	TIGRIS_1_0_0_RC1:1.4.0.2
	dlr:1.1.1
	HELM_PEER_PORT_BRANCH:1.1.1.1;
locks; strict;
comment	@# @;


1.15
date	2001.06.13.19.55.10;	author elicia;	state dead;
branches;
next	1.14;

1.14
date	2001.06.04.06.33.03;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.06.04.06.26.23;	author dlr;	state Exp;
branches;
next	1.12;

1.12
date	2001.06.04.06.25.50;	author dlr;	state Exp;
branches;
next	1.11;

1.11
date	2001.06.04.06.16.57;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.06.04.06.16.32;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.05.23.01.50.15;	author dlr;	state Exp;
branches;
next	1.8;

1.8
date	2001.05.04.23.35.43;	author ms;	state Exp;
branches;
next	1.7;

1.7
date	2001.04.21.15.47.21;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.04.19.19.12.25;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.04.19.02.38.06;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.03.07.01.53.24;	author lucas;	state Exp;
branches
	1.4.6.1;
next	1.3;

1.3
date	2001.03.06.23.27.10;	author elicia;	state Exp;
branches;
next	1.2;

1.2
date	2001.02.21.03.02.29;	author edk;	state Exp;
branches;
next	1.1;

1.1
date	2001.02.14.00.50.54;	author elicia;	state dead;
branches
	1.1.1.1
	1.1.2.1;
next	;

1.1.1.1
date	2001.02.21.00.35.00;	author dlr;	state Exp;
branches;
next	;

1.1.2.1
date	2001.02.14.00.50.54;	author elicia;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.15.20.50.42;	author leonardr;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2001.02.15.21.29.04;	author elicia;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2001.02.16.03.01.34;	author dlr;	state Exp;
branches;
next	1.1.2.5;

1.1.2.5
date	2001.02.20.01.53.25;	author dlr;	state Exp;
branches;
next	1.1.2.6;

1.1.2.6
date	2001.02.20.23.34.11;	author elicia;	state Exp;
branches;
next	;

1.4.6.1
date	2001.04.20.21.46.30;	author edk;	state Exp;
branches;
next	1.4.6.2;

1.4.6.2
date	2001.05.04.23.44.00;	author ms;	state Exp;
branches;
next	1.4.6.3;

1.4.6.3
date	2001.06.04.06.27.38;	author dlr;	state Exp;
branches;
next	1.4.6.4;

1.4.6.4
date	2001.06.04.06.40.13;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.15
log
@Deleting the deprecated ProjectDownload servlets. Pointing them at their ProjectDocument counterparts
@
text
@/* ================================================================
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.project;

import java.io.*;
import java.util.*;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.context.Context;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.TurbineException;

import com.oreilly.servlet.MultipartRequest;

import com.workingdogs.village.*;

import org.joist.util.*;

import org.joist.security.*;
import org.tigris.helm.security.*;
import org.tigris.helm.om.*;
import org.tigris.helm.tool.MDACookie;
import org.tigris.helm.tool.DBFlag;

/**
 * Handles project uploads and downloads.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:elicia@@collab.net">Elicia David</a>
 * @@version $Id: ProjectDownloadList.java,v 1.14 2001/06/04 06:33:03 dlr Exp $
 */
public class ProjectDownloadList extends HelmServlet
{
    /**
     * The action key for download an existing file.
     */
    private static final String DO_DOWNLOAD = "download";

    /**
     * The query string parameter name of a download's database key.
     */
    private static final String DOWNLOAD_ID = "dlID";

    /**
     * Key for whether the download is private.
     */
    private static final String IS_PUBLIC = "isPublic";

    /**
     * Whether to log debugging info.
     */
    private static final boolean DEBUG = false;

    /**
     * Display and process project downloads screen.
     */
    protected String processRequest(Context ctx)
        throws TurbineException, RedirectException
    {
        String templateName = super.processRequest(ctx);

        String action = getAction(ctx);
        if (DEBUG) Log.debug("action=" + action);
        HttpServletRequest req = getRequest(ctx);
        // TODO: figure out better way to do this!
        //String msg = req.getParameter(Project.MESSAGE);
        //if (haveValue(msg)) appendToMessage(ctx, msg);

        try
        {
            String descrip = null;
            String isPublic = null;
            Project project = getCurrentProject(ctx);
            User user = getLoginUser(ctx);
            AccessControlList acl = user.getACL();

            if (action != null && action.equals(DO_DOWNLOAD))
            {
                // Authorize the download and redirect to the file server.
                String dlID = req.getParameter(DOWNLOAD_ID);
                if (DEBUG) Log.debug("dlID=" + dlID);
                ProjectDownload dl = (ProjectDownload)ProjectDownloadPeer
                   .getInstance(dlID);

                if (dl.isViewableBy(acl))
                {
                    // Authorize the download.
                    MDACookie.write(myProperties.getProperty("fileServer"),
                                    project, user, dl.isPublic(), req,
                                    getResponse(ctx));

                    StringBuffer dlURL = new StringBuffer(req.getScheme());
                    dlURL.append("://")
                        .append(myProperties.getProperty("fileServer"));
                    if (req.getServerPort() != URL.HTTP_PORT &&
                        req.getServerPort() != URL.HTTPS_PORT)
                    {
                        dlURL.append(':').append(req.getServerPort());
                    }
                    dlURL.append("/files/").append(project.getID())
                        .append('/').append(dl.getProposedBy()).append('/')
                        .append(URL.encode(dl.getFileName()));
                    redirectToURL(ctx, dlURL.toString());
                }
                else
                {
                    appendToMessage(ctx, "No permission to access request " +
                                    "file '" + dl.getFileName() + '\'');
                }
            }

            String msg = getParameter(ctx, Project.MESSAGE);
            if (haveValue(msg)) appendToMessage(ctx, msg);
            ctx.put("description", descrip != null ? descrip : "");
            if (isPublic != null) ctx.put(IS_PUBLIC, DBFlag.ON);
        }
        catch (ACLException e)
        {
            throw e;
        }
        catch (RedirectURLException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new TurbineException(e);
        }

        return templateName;
    }
}
@


1.14
log
@Corrected method name to fix compile error introduced last commit.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.13 2001/06/04 06:26:23 dlr Exp $
@


1.13
log
@Corrected formatting.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.12 2001/06/04 06:25:50 dlr Exp $
d158 1
a158 1
                                    "file '" + dl.getName() + '\'');
@


1.12
log
@Present MDA cryptographic token only to users with sufficient
permission.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.11 2001/06/04 06:16:57 dlr Exp $
d108 1
a108 1
	throws TurbineException, RedirectException
@


1.11
log
@Removed worthless DEBUG.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.10 2001/06/04 06:16:32 dlr Exp $
d135 1
a135 10
                // Authorize the download.
                MDACookie.write(myProperties.getProperty("fileServer"),
                                project, user, dl.isPublic(), req,
                                getResponse(ctx));

                StringBuffer dlURL = new StringBuffer(req.getScheme());
                dlURL.append("://")
                    .append(myProperties.getProperty("fileServer"));
                if (req.getServerPort() != URL.HTTP_PORT &&
                    req.getServerPort() != URL.HTTPS_PORT)
d137 22
a158 1
                    dlURL.append(':').append(req.getServerPort());
a159 4
                dlURL.append("/files/").append(project.getID())
                     .append('/').append(dl.getProposedBy()).append('/')
		             .append(URL.encode(dl.getFileName()));
                redirectToURL(ctx, dlURL.toString());
@


1.10
log
@Removed low-value method.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.9 2001/05/23 01:50:15 dlr Exp $
a171 1
	if (DEBUG) Log.debug("done");
@


1.9
log
@Ported Helm from WebMacro to Velocity.
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.8 2001/05/04 23:35:43 ms Exp $
d134 6
a139 2
                authDownload(project, user, dl.isPublic(), req,
                             getResponse(ctx));
a174 24


    /**
     * Cookies the user with a ticket that authorizes download of a specific
     * file.  The authorization must happen regardless of whether the file is 
     * public--otherwise, mod_auth_mda will reject access as a result of no
     * MDACookie existing for the client.
     */
    private void authDownload (Project project, User user, boolean isPublic,
                               HttpServletRequest req, HttpServletResponse res)
        throws TurbineException
    {
        try
        {
            MDACookie.write(myProperties.getProperty("fileServer"), project, 
                            user, isPublic, req, res);
        }
        catch (Exception e)
        {
            Log.error(e);
            throw new TurbineException(e);
        }
    }

@


1.8
log
@redirect users to the proper filename
@
text
@d58 1
a58 1
import org.webmacro.servlet.*;
d60 2
d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.7 2001/04/21 15:47:21 dlr Exp $
d107 2
a108 2
    protected String processRequest (Connection conn, WebContext ctx)
	throws HandlerException, RedirectException
d110 1
a110 1
        String templateName = super.processRequest(conn, ctx);
d114 1
a114 1
        HttpServletRequest req = ctx.getRequest();
d135 1
a135 1
                             ctx.getResponse());
d150 1
a150 1
            String msg = ctx.getForm(Project.MESSAGE);
a162 4
        catch (HandlerException e)
        {
            throw e;
        }
d165 1
a165 2
            Log.error(e);
            throw new HandlerException(e.getMessage());
d181 1
a181 1
        throws HandlerException
d191 1
a191 1
            throw new HandlerException(e.getMessage());
@


1.7
log
@Shouldn't be checked in with DEBUG on.
@
text
@d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.6 2001/04/19 19:12:25 dlr Exp $
d144 1
a144 1
		             .append(URLEncoder.encode(dl.getFileName()));
d197 1
@


1.6
log
@Someone forgot to include the mod_auth_mda message when refactoring.
@
text
@d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.5 2001/04/19 02:38:06 dlr Exp $
d100 1
a100 1
    private static final boolean DEBUG = true;
@


1.5
log
@Formatting fixes (no functional change).
@
text
@d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.4 2001/03/07 01:53:24 lucas Exp $
d148 2
a151 1

@


1.4
log
@Corrected regex madness for Torque upgrade.  -dlr
@
text
@d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.3 2001/03/06 23:27:10 elicia Exp $
d132 2
a133 1
                authDownload(project, user, dl.isPublic(), req, ctx.getResponse());
@


1.4.6.1
log
@Port some of dlr's bug fixes back to the branch.
@
text
@d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.6 2001/04/19 19:12:25 dlr Exp $
d132 1
a132 2
                authDownload(project, user, dl.isPublic(), req,
                             ctx.getResponse());
a146 2
            String msg = ctx.getForm(Project.MESSAGE);
            if (haveValue(msg)) appendToMessage(ctx, msg);
d149 1
@


1.4.6.2
log
@redirect users to the proper filename
@
text
@d53 1
d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.4.6.1 2001/04/20 21:46:30 edk Exp $
d100 1
a100 1
    private static final boolean DEBUG = false;
d144 1
a144 1
		             .append(URL.encode(dl.getFileName()));
@


1.4.6.3
log
@* Removed low value authDownload() method in favor of inling the
coding directly.

* Removed low value debug trace.

* Present MDA cryptographic token only to users with sufficient
permission.
@
text
@d77 1
a77 1
 * @@version $Id: ProjectDownloadList.java,v 1.4.6.2 2001/05/04 23:44:00 ms Exp $
d128 10
a137 25
                if (DEBUG) Log.debug("ProjectDownloadList: id=" + dlID);
                ProjectDownload dl =
                    (ProjectDownload) ProjectDownloadPeer.getInstance(dlID);

                if (dl.isViewableBy(acl))
                {
                    // Authorize the download.
                    MDACookie.write(myProperties.getProperty("fileServer"),
                                    project, user, dl.isPublic(), req,
                                    ctx.getResponse());

                    StringBuffer dlURL = new StringBuffer(req.getScheme());
                    dlURL.append("://")
                        .append(myProperties.getProperty("fileServer"));
                    if (req.getServerPort() != URL.HTTP_PORT &&
                        req.getServerPort() != URL.HTTPS_PORT)
                    {
                        dlURL.append(':').append(req.getServerPort());
                    }
                    dlURL.append("/files/").append(project.getID())
                        .append('/').append(dl.getProposedBy()).append('/')
                        .append(URL.encode(dl.getFileName()));
                    redirectToURL(ctx, dlURL.toString());
                }
                else
d139 1
a139 2
                    appendToMessage(ctx, "No permission to access request " +
                                    "file '" + dl.getFileName() + '\'');
d141 4
d170 1
d172 23
@


1.4.6.4
log
@redirect -> redirected
@
text
@d77 1
a77 1
 * @@version $Id: ProjectDownloadList.java,v 1.4.6.3 2001/06/04 06:27:38 dlr Exp $
d154 1
a154 1
                    appendToMessage(ctx, "No permission to access requested " +
@


1.3
log
@removed add functionality (moved to separate add page)
@
text
@a69 2
import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.base.*;
d78 1
a78 1
 * @@version $Id: ProjectDownloadList.java,v 1.2 2001/02/21 03:02:29 edk Exp $
@


1.2
log
@adding files from the branch to the head
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.1.1 2001/02/21 00:35:00 dlr Exp $
a84 5
     * The action key for adding a new download.
     */
    private static final String ADD_DOWNLOAD = "Upload File";

    /**
d127 1
a127 49
            if (action != null && action.equals(ADD_DOWNLOAD))
            {
                // Do not allow overwrites of existing downloads.
                if (FileUpload.isValidContentType(ctx))
                {
                    File saveDir = new File
                        (project.getUploadDirectory(getBaseSaveDir(), user));
                    if (!saveDir.exists()) saveDir.mkdirs();
                    int maxPostSize = Integer.parseInt
                        (myProperties.getProperty("maxPostSize"));
                    String fsPerms = 
                        myProperties.getProperty("filesystemPermissions");
                    FileUpload upload = 
                        new FileUpload(conn, ctx, maxPostSize, 
                                       saveDir.getAbsolutePath(), fsPerms);
                    List files = upload.getFiles();
                    MultipartRequest mreq = upload.getMultipartRequest();
                    descrip = mreq.getParameter("description");

                    isPublic = mreq.getParameter(IS_PUBLIC);
                    if (DEBUG) Log.debug("isPublic=" + isPublic);

                    // HELP: The user actually may have uploaded many files, 
                    // but the current UI doesn't support this.  Punting, 
                    // handling only the first upload for now.
                    switch (files.size())
                    {
                    case 1:
                        processUpload(conn, ctx, project, (File)files.get(0), 
                                      descrip, isPublic);

                        // No need to redisplay the user input.
                        descrip = "";
                        isPublic = null;
                        break;
                    case 0:
                        appendToMessage(ctx, "No file uploaded");
                        break;
                    default:
                        appendToError
                            (ctx, "Please upload only one file at a time");
                    }
                }
                else
                {
                    Log.warn("Incorrect content type for file upload");
                }
            }
            else if (action != null && action.equals(DO_DOWNLOAD))
a151 6
            // If we already have a list, it needs to be refreshed.
            List downloads = project.getProjectDownloads();
            if (downloads.size() > 0)
            {
                ctx.put("downloads", downloads);
            }
a174 46
    /**
     * Processes a single uploaded file.
     */
    private void processUpload (Connection conn, WebContext ctx,
                                Project project, File fileToAdd,
                                String descrip, String isPublic)
        throws ACLException, Exception
    {
        User user = getLoginUser(ctx);
        AccessControlList acl = user.getACL();
        String url = URL.schemeAndServer(ctx) + "/servlets/ProjectDownloadList";

        // Check for overwritten files.
        String fileName = fileToAdd.getName();
        String filePath = ProjectDownload.extractLocation(fileToAdd);
        if (DEBUG) Log.debug("fileName=" + fileName);
        List downloads = project.getProjectDownloads();
        int nbrDownloads = downloads.size();
        for (int i = 0; i < nbrDownloads; i++)
        {
            ProjectDownload dl = (ProjectDownload)downloads.get(i);
            if (DEBUG) Log.debug("dl.getFileName()=" + dl.getFileName());
            if (filePath.equals(dl.getLocation()) &&
                fileName.equals(dl.getFileName()))
            {
                // The file has already been overwritten--no need to delete it
                // from disk.                
                dl.delete(acl, true, url);
                appendToMessage(ctx, fileName + 
                                " was overwritten by your upload");
                break;
            }
        }

        // Add the download to the project.
        ProjectDownload.add(acl, project, descrip, fileToAdd, isPublic, url);
        redirectToURL(ctx, "ProjectDownloadList");
    }

    /**
     * Macro to return the upload directory.
     */
    private final String getBaseSaveDir ()
    {
        return myProperties.getProperty("baseSaveDir");
    }
@


1.1
log
@file ProjectDownloadList.java was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@d1 302
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@a0 302
/* ================================================================
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.project;

import java.io.*;
import java.util.*;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.webmacro.servlet.*;
import org.apache.turbine.util.Log;

import com.oreilly.servlet.MultipartRequest;

import com.workingdogs.village.*;

import org.joist.util.*;

import org.joist.security.*;
import org.tigris.helm.security.*;
import org.tigris.helm.om.*;
import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.base.*;
import org.tigris.helm.tool.MDACookie;
import org.tigris.helm.tool.DBFlag;

/**
 * Handles project uploads and downloads.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:elicia@@collab.net">Elicia David</a>
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.6 2001/02/20 23:34:11 elicia Exp $
 */
public class ProjectDownloadList extends HelmServlet
{
    /**
     * The action key for adding a new download.
     */
    private static final String ADD_DOWNLOAD = "Upload File";

    /**
     * The action key for download an existing file.
     */
    private static final String DO_DOWNLOAD = "download";

    /**
     * The query string parameter name of a download's database key.
     */
    private static final String DOWNLOAD_ID = "dlID";

    /**
     * Key for whether the download is private.
     */
    private static final String IS_PUBLIC = "isPublic";

    /**
     * Whether to log debugging info.
     */
    private static final boolean DEBUG = true;

    /**
     * Display and process project downloads screen.
     */
    protected String processRequest (Connection conn, WebContext ctx)
	throws HandlerException, RedirectException
    {
        String templateName = super.processRequest(conn, ctx);

        String action = getAction(ctx);
        if (DEBUG) Log.debug("action=" + action);
        HttpServletRequest req = ctx.getRequest();
        // TODO: figure out better way to do this!
        //String msg = req.getParameter(Project.MESSAGE);
        //if (haveValue(msg)) appendToMessage(ctx, msg);

        try
        {
            String descrip = null;
            String isPublic = null;
            Project project = getCurrentProject(ctx);
            User user = getLoginUser(ctx);
            AccessControlList acl = user.getACL();

            if (action != null && action.equals(ADD_DOWNLOAD))
            {
                // Do not allow overwrites of existing downloads.
                if (FileUpload.isValidContentType(ctx))
                {
                    File saveDir = new File
                        (project.getUploadDirectory(getBaseSaveDir(), user));
                    if (!saveDir.exists()) saveDir.mkdirs();
                    int maxPostSize = Integer.parseInt
                        (myProperties.getProperty("maxPostSize"));
                    String fsPerms = 
                        myProperties.getProperty("filesystemPermissions");
                    FileUpload upload = 
                        new FileUpload(conn, ctx, maxPostSize, 
                                       saveDir.getAbsolutePath(), fsPerms);
                    List files = upload.getFiles();
                    MultipartRequest mreq = upload.getMultipartRequest();
                    descrip = mreq.getParameter("description");

                    isPublic = mreq.getParameter(IS_PUBLIC);
                    if (DEBUG) Log.debug("isPublic=" + isPublic);

                    // HELP: The user actually may have uploaded many files, 
                    // but the current UI doesn't support this.  Punting, 
                    // handling only the first upload for now.
                    switch (files.size())
                    {
                    case 1:
                        processUpload(conn, ctx, project, (File)files.get(0), 
                                      descrip, isPublic);

                        // No need to redisplay the user input.
                        descrip = "";
                        isPublic = null;
                        break;
                    case 0:
                        appendToMessage(ctx, "No file uploaded");
                        break;
                    default:
                        appendToError
                            (ctx, "Please upload only one file at a time");
                    }
                }
                else
                {
                    Log.warn("Incorrect content type for file upload");
                }
            }
            else if (action != null && action.equals(DO_DOWNLOAD))
            {
                // Authorize the download and redirect to the file server.
                String dlID = req.getParameter(DOWNLOAD_ID);
                if (DEBUG) Log.debug("dlID=" + dlID);
                ProjectDownload dl = (ProjectDownload)ProjectDownloadPeer
                   .getInstance(dlID);
                authDownload(project, user, dl.isPublic(), req, ctx.getResponse());
                StringBuffer dlURL = new StringBuffer(req.getScheme());
                dlURL.append("://")
                    .append(myProperties.getProperty("fileServer"));
                if (req.getServerPort() != URL.HTTP_PORT &&
                    req.getServerPort() != URL.HTTPS_PORT)
                {
                    dlURL.append(':').append(req.getServerPort());
                }
                dlURL.append("/files/").append(project.getID())
                     .append('/').append(dl.getProposedBy()).append('/')
		             .append(URLEncoder.encode(dl.getFileName()));
                redirectToURL(ctx, dlURL.toString());
            }

            ctx.put("description", descrip != null ? descrip : "");
            if (isPublic != null) ctx.put(IS_PUBLIC, DBFlag.ON);

            // If we already have a list, it needs to be refreshed.
            List downloads = project.getProjectDownloads();
            if (downloads.size() > 0)
            {
                ctx.put("downloads", downloads);
            }
        }
        catch (ACLException e)
        {
            throw e;
        }
        catch (RedirectURLException e)
        {
            throw e;
        }
        catch (HandlerException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            Log.error(e);
            throw new HandlerException(e.getMessage());
        }

	if (DEBUG) Log.debug("done");
        return templateName;
    }

    /**
     * Processes a single uploaded file.
     */
    private void processUpload (Connection conn, WebContext ctx,
                                Project project, File fileToAdd,
                                String descrip, String isPublic)
        throws ACLException, Exception
    {
        User user = getLoginUser(ctx);
        AccessControlList acl = user.getACL();
        String url = URL.schemeAndServer(ctx) + "/servlets/ProjectDownloadList";

        // Check for overwritten files.
        String fileName = fileToAdd.getName();
        String filePath = ProjectDownload.extractLocation(fileToAdd);
        if (DEBUG) Log.debug("fileName=" + fileName);
        List downloads = project.getProjectDownloads();
        int nbrDownloads = downloads.size();
        for (int i = 0; i < nbrDownloads; i++)
        {
            ProjectDownload dl = (ProjectDownload)downloads.get(i);
            if (DEBUG) Log.debug("dl.getFileName()=" + dl.getFileName());
            if (filePath.equals(dl.getLocation()) &&
                fileName.equals(dl.getFileName()))
            {
                // The file has already been overwritten--no need to delete it
                // from disk.                
                dl.delete(acl, true, url);
                appendToMessage(ctx, fileName + 
                                " was overwritten by your upload");
                break;
            }
        }

        // Add the download to the project.
        ProjectDownload.add(acl, project, descrip, fileToAdd, isPublic, url);
        redirectToURL(ctx, "ProjectDownloadList");
    }

    /**
     * Macro to return the upload directory.
     */
    private final String getBaseSaveDir ()
    {
        return myProperties.getProperty("baseSaveDir");
    }

    /**
     * Cookies the user with a ticket that authorizes download of a specific
     * file.  The authorization must happen regardless of whether the file is 
     * public--otherwise, mod_auth_mda will reject access as a result of no
     * MDACookie existing for the client.
     */
    private void authDownload (Project project, User user, boolean isPublic,
                               HttpServletRequest req, HttpServletResponse res)
        throws HandlerException
    {
        try
        {
            MDACookie.write(myProperties.getProperty("fileServer"), project, 
                            user, isPublic, req, res);
        }
        catch (Exception e)
        {
            Log.error(e);
            throw new HandlerException(e.getMessage());
        }
    }
}
@


1.1.2.1
log
@ported to turbine
@
text
@a0 301
/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.project;

import java.io.*;
import java.util.*;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.webmacro.servlet.*;
import org.apache.turbine.util.Log;

import com.oreilly.servlet.MultipartRequest;

import com.workingdogs.village.*;

import org.joist.util.*;

import org.joist.security.*;
import org.tigris.helm.security.*;
import org.tigris.helm.om.*;
import org.tigris.helm.om.peer.*;
import org.tigris.helm.tool.MDACookie;
import org.tigris.helm.tool.DBFlag;

/**
 * Handles project uploads and downloads.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:elicia@@collab.net">Elicia David</a>
 * @@version $Id: ProjectDownload.java,v 1.57 2001/02/08 02:38:38 elicia Exp $
 */
public class ProjectDownloadList extends HelmServlet
{
    /**
     * The action key for adding a new download.
     */
    private static final String ADD_DOWNLOAD = "Upload File";

    /**
     * The action key for download an existing file.
     */
    private static final String DO_DOWNLOAD = "download";

    /**
     * The query string parameter name of a download's database key.
     */
    private static final String DOWNLOAD_ID = "dlID";

    /**
     * Key for whether the download is private.
     */
    private static final String IS_PUBLIC = "isPublic";

    /**
     * Whether to log debugging info.
     */
    private static final boolean debug = true;

    /**
     * Display and process project downloads screen.
     */
    protected String processRequest (Connection conn, WebContext ctx)
	throws HandlerException, RedirectException
    {
        if (debug) Log.debug("started");
        String templateName = super.processRequest(conn, ctx);

        String action = getAction(ctx);
        if (debug) Log.debug("action=" + action);
        HttpServletRequest req = ctx.getRequest();
        // TODO: figure out better way to do this!
        //String msg = req.getParameter(Project.MESSAGE);
        //if (haveValue(msg)) appendToMessage(ctx, msg);

        try
        {
            String descrip = null;
            String isPublic = null;
            Project project = getCurrentProject(ctx);
            User user = getLoginUser(ctx);
            AccessControlList acl = user.getACL();

            if (action != null && action.equals(ADD_DOWNLOAD))
            {
                // Do not allow overwrites of existing downloads.
                if (FileUpload.isValidContentType(ctx))
                {
                    File saveDir = new File
                        (project.getUploadDirectory(getBaseSaveDir(), user));
                    if (!saveDir.exists()) saveDir.mkdirs();
                    int maxPostSize = Integer.parseInt
                        (myProperties.getProperty("maxPostSize"));
                    String fsPerms = 
                        myProperties.getProperty("filesystemPermissions");
                    FileUpload upload = 
                        new FileUpload(conn, ctx, maxPostSize, 
                                       saveDir.getAbsolutePath(), fsPerms);
                    List files = upload.getFiles();
                    MultipartRequest mreq = upload.getMultipartRequest();
                    descrip = mreq.getParameter("description");

                    isPublic = mreq.getParameter(IS_PUBLIC);
                    if (debug) Log.debug("isPublic=" + isPublic);

                    // HELP: The user actually may have uploaded many files, 
                    // but the current UI doesn't support this.  Punting, 
                    // handling only the first upload for now.
                    switch (files.size())
                    {
                    case 1:
                        processUpload(conn, ctx, project, (File)files.get(0), 
                                      descrip, isPublic);

                        // No need to redisplay the user input.
                        descrip = "";
                        isPublic = null;
                        break;
                    case 0:
                        appendToMessage(ctx, "No file uploaded");
                        break;
                    default:
                        appendToError
                            (ctx, "Please upload only one file at a time");
                    }
                }
                else
                {
                    Log.warn("Incorrect content type for file upload");
                }
            }
            else if (action != null && action.equals(DO_DOWNLOAD))
            {
                // Authorize the download and redirect to the file server.
                String dlID = req.getParameter(DOWNLOAD_ID);
                if (debug) Log.debug("dlID=" + dlID);
                ProjectDownload dl = (ProjectDownload)ProjectDownloadPeer
                   .getInstance(dlID);
                authDownload(project, user, dl.isPublic(), req, ctx.getResponse());
                StringBuffer dlURL = new StringBuffer(req.getScheme());
                dlURL.append("://")
                    .append(myProperties.getProperty("fileServer"));
                if (req.getServerPort() != URL.HTTP_PORT &&
                    req.getServerPort() != URL.HTTPS_PORT)
                {
                    dlURL.append(':').append(req.getServerPort());
                }
                dlURL.append("/files/").append(project.getID())
                     .append('/').append(dl.getProposedBy()).append('/')
		             .append(URLEncoder.encode(dl.getFileName()));
                redirectToURL(ctx, dlURL.toString());
            }

            ctx.put("description", descrip != null ? descrip : "");
            if (isPublic != null) ctx.put(IS_PUBLIC, DBFlag.ON);

            // If we already have a list, it needs to be refreshed.
            List downloads = project.getProjectDownloads();
            if (downloads.size() > 0)
            {
                ctx.put("downloads", downloads);
            }
        }
        catch (ACLException e)
        {
            throw e;
        }
        catch (RedirectURLException e)
        {
            throw e;
        }
        catch (HandlerException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            Log.error(e);
            throw new HandlerException(e.getMessage());
        }

	if (debug) Log.debug("done");
        return templateName;
    }

    /**
     * Processes a single uploaded file.
     */
    private void processUpload (Connection conn, WebContext ctx,
                                Project project, File fileToAdd,
                                String descrip, String isPublic)
        throws ACLException, Exception
    {
        User user = getLoginUser(ctx);
        AccessControlList acl = user.getACL();
        String url = URL.schemeAndServer(ctx) + "/servlets/ProjectDownload";

        // Check for overwritten files.
        String fileName = fileToAdd.getName();
        String filePath = ProjectDownload.extractLocation(fileToAdd);
        if (debug) Log.debug("fileName=" + fileName);
        List downloads = project.getProjectDownloads();
        int nbrDownloads = downloads.size();
        for (int i = 0; i < nbrDownloads; i++)
        {
            ProjectDownload dl = (ProjectDownload)downloads.get(i);
            if (debug) Log.debug("dl.getFileName()=" + dl.getFileName());
            if (filePath.equals(dl.getLocation()) &&
                fileName.equals(dl.getFileName()))
            {
                // The file has already been overwritten--no need to delete it
                // from disk.                
                dl.delete(acl, true, ctx);
                appendToMessage(ctx, fileName + 
                                " was overwritten by your upload");
                break;
            }
        }

        // Add the download to the project.
        ProjectDownload.add(acl, project, descrip, fileToAdd, isPublic, url);
    }

    /**
     * Macro to return the upload directory.
     */
    private final String getBaseSaveDir ()
    {
        return myProperties.getProperty("baseSaveDir");
    }

    /**
     * Cookies the user with a ticket that authorizes download of a specific
     * file.  The authorization must happen regardless of whether the file is 
     * public--otherwise, mod_auth_mda will reject access as a result of no
     * MDACookie existing for the client.
     */
    private void authDownload (Project project, User user, boolean isPublic,
                               HttpServletRequest req, HttpServletResponse res)
        throws HandlerException
    {
        try
        {
            MDACookie.write(myProperties.getProperty("fileServer"), project, 
                            user, isPublic, req, res);
        }
        catch (Exception e)
        {
            Log.error(e);
            throw new HandlerException(e.getMessage());
        }
    }
}
@


1.1.2.2
log
@Normalized copyright dates.
@
text
@d2 1
a2 1
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
d79 1
a79 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.1 2001/02/14 00:50:54 elicia Exp $
@


1.1.2.3
log
@changed file name in redirect URL from ProjectDownload to ProjectDownloadList
@
text
@d79 1
a79 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.2 2001/02/15 20:50:42 leonardr Exp $
d244 1
a244 1
        String url = URL.schemeAndServer(ctx) + "/servlets/ProjectDownloadList";
@


1.1.2.4
log
@Added new imports necessitated by Torque.
@
text
@a70 1
import org.tigris.helm.om.base.*;
d79 1
a79 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.3 2001/02/15 21:29:04 elicia Exp $
@


1.1.2.5
log
@* Fixed copyright.
* debug --> DEBUG
@
text
@d32 1
a32 1
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.4 2001/02/16 03:01:34 dlr Exp $
d107 1
a107 1
    private static final boolean DEBUG = true;
d115 1
d119 1
a119 1
        if (DEBUG) Log.debug("action=" + action);
d153 1
a153 1
                    if (DEBUG) Log.debug("isPublic=" + isPublic);
d185 1
a185 1
                if (DEBUG) Log.debug("dlID=" + dlID);
d231 1
a231 1
	if (DEBUG) Log.debug("done");
d250 1
a250 1
        if (DEBUG) Log.debug("fileName=" + fileName);
d256 1
a256 1
            if (DEBUG) Log.debug("dl.getFileName()=" + dl.getFileName());
@


1.1.2.6
log
@updated call to delete method
@
text
@d80 1
a80 1
 * @@version $Id: ProjectDownloadList.java,v 1.1.2.5 2001/02/20 01:53:25 dlr Exp $
d261 1
a261 1
                dl.delete(acl, true, url);
a269 1
        redirectToURL(ctx, "ProjectDownloadList");
@


