head	1.7;
access;
symbols
	TIGRIS_1_1_0RC2:1.4.2.2
	TIGRIS_1_1_0RC1:1.4.2.2
	TIGRIS_1_1:1.4.2.2.0.2
	TIGRIS_1_0_8:1.4.2.2
	TIGRIS_1_0_8RC3:1.4.2.2
	TIGRIS_1_0_8RC2:1.4.2.2
	TIGRIS_1_0_8RC1:1.4.2.2
	TIGRIS_1_0_7:1.4.2.2
	TIGRIS_1_0_7RC3:1.4.2.2
	TIGRIS_1_0_7RC2:1.4.2.2
	TIGRIS_1_0_7RC1:1.4.2.2
	TIGRIS_1_0_6:1.4.2.2
	TIGRIS_1_0_6RC5:1.4.2.2
	TIGRIS_1_0_6RC4:1.4.2.2
	TIGRIS_1_0_6RC3:1.4.2.2
	TIGRIS_1_0_6RC2:1.4.2.2
	TIGRIS_1_0_6RC1:1.4.2.2
	TIGRIS_1_0:1.4.0.2;
locks; strict;
comment	@# @;


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

1.6
date	2001.06.18.22.12.21;	author leonardr;	state Exp;
branches;
next	1.5;

1.5
date	2001.06.15.20.23.08;	author leonardr;	state Exp;
branches;
next	1.4;

1.4
date	2001.06.01.23.23.53;	author dlr;	state Exp;
branches
	1.4.2.1;
next	1.3;

1.3
date	2001.06.01.23.21.05;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.05.31.21.12.25;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.05.31.20.54.04;	author dlr;	state Exp;
branches;
next	;

1.4.2.1
date	2001.06.04.05.37.06;	author dlr;	state Exp;
branches;
next	1.4.2.2;

1.4.2.2
date	2001.06.04.05.46.56;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.7
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
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 CollabNet (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 CollabNet.
 * 
 * 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.File;
import java.io.IOException;

import org.apache.commons.util.FileUtils;

import org.apache.turbine.util.Log;
import org.apache.commons.util.StringUtils;
import org.apache.commons.util.exception.NestableException;

import org.tigris.helm.security.*;
import org.tigris.helm.om.Attribute;
import org.tigris.helm.om.AttributeValue;
import org.tigris.helm.om.AttributeValuePeer;
import org.tigris.helm.om.Host;
import org.tigris.helm.om.ProjectPeer;

/**
 * Support for project file artifact addition.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
abstract class FileArtifactManipulate extends HelmServlet
{
    /**
     * The field name of the file field which contains the uploaded files.
     */
    protected static final String FILES_FIELD = "file";

    private static final char[] INVALID_FILE_NAME_CHARS =
    {
        '[',
        '?',
        '*'
    };

    /**
     * Returns the name of the servlet used to list artifacts of the
     * implementor's type.
     */
    protected abstract String getArtifactListServletName();

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

    /**
     * Determines whether the supplied file name is valid.
     *
     * @@param name The name of the file.
     * @@return     The file name's validity.
     */
    protected final boolean isValidFileName(String name)
    {
        if (StringUtils.isValid(name))
        {
            for (int i = 0; i < INVALID_FILE_NAME_CHARS.length; i++)
            {
                if (name.indexOf(INVALID_FILE_NAME_CHARS[i]) != -1)
                {
                    return false;
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Returns the maximum size in bytes for a HTTP POST which
     * includes a file upload.  Calculated using the host attribute
     * <code>maxUploadSize</code> plus a little extra for the
     * remaining HTTP request overhead.
     *
     * @@return A byte count.
     */
    protected int getMaxPostSize()
        throws Exception
    {
        AttributeValue attribVal = AttributeValuePeer.getInstanceByName
            (Attribute.HOST_TYPE, ProjectPeer.getHost().getID(),
             Attribute.MAX_UPLOAD_SIZE);
        return (Integer.parseInt(attribVal.getValue()) * FileUtils.ONE_MB)
            + FileUtils.ONE_KB;
    }

    /**
     * Returns the path where the file upload should be saved
     * [baseSaveDirectory] / [documentID]
     */
    protected String getSaveDir(String id)
    {
        return (getBaseSaveDir() + File.separatorChar + id);
    }

    /**
     * Sets the file system permissions for uploaded, saved files.
     */
    public void setFilesystemPermissions (String perms, String path)
        throws InterruptedException, IOException, Exception
    {
        String[] cmd = { "chmod", perms, path };

        // Execute the command.
        try
        {
            Process p = Runtime.getRuntime().exec(cmd);
            if (p.waitFor() != 0)
            {
                // Command did not execute successfully.
                throw new Exception
                    ("Could not change permissions for path '" + path + '\'');
            }
        }
        catch (InterruptedException e)
        {
            Log.error(e);
            throw new Exception(e.getMessage());
        }
        catch (IOException e)
        {
            Log.error(e);
            throw new Exception(e.getMessage());
        }
    }
}
@


1.6
log
@Huge patch of the day: Got rid of redundant HELM_DOMAIN table. The project representation of the domain and the host is now the canonical one. Host and Domain now subclass ProjectGroup. The canonical representation of a project's membership in a domain group is now the corresponding mapping in HELM_PROJECT_PROJECTGROUP. Schema changes: No more HELM_DOMAIN table (obviously); no more DOMAIN_ID in HELM_PROJECT; HELM_USER and HELM_VC_VIEW now have foreign keys in HELM_PROJECT instead of HELM_DOMAIN.
@
text
@d53 1
a53 1
import org.apache.turbine.util.FileUtils;
d56 2
a57 2
import org.apache.turbine.util.StringUtils;
import org.apache.turbine.util.TurbineException;
@


1.5
log
@Rearranged attribute constants and added generic host attribute-editing interface to AdminUtils.
@
text
@d64 1
d136 1
a136 1
            (Attribute.HOST_TYPE, new Host().getSpecialProject().getID(),
@


1.4
log
@Corrected variable name.
@
text
@d137 2
a138 1
        return Integer.parseInt(attribVal.getValue()) + FileUtils.ONE_KB;
@


1.4.2.1
log
@Better named and more functional replacement for ProjectFileArtifactAdd.
@
text
@@


1.4.2.2
log
@Entity attributes don't yet exist in the 1.0 branch, and max post size
is set by editting a properites file.
@
text
@d53 2
d60 3
d124 17
d147 31
@


1.3
log
@Refactored isValidFileName() class method to check for validity of
proferred file name before interrogating it further.
@
text
@d106 1
a106 1
        if (StringUtils.isValid(fileName))
@


1.2
log
@Corrected servlet class name.
@
text
@d56 1
d101 1
a101 1
     * @@param name The non-<code>null</code> name of the file.
d106 1
a106 1
        for (int i = 0; i < INVALID_FILE_NAME_CHARS.length; i++)
d108 1
a108 1
            if (name.indexOf(INVALID_FILE_NAME_CHARS[i]) != -1)
d110 4
a113 1
                return false;
d115 5
a120 1
        return true;
@


1.1
log
@This will replace ProjectFileArtifactAdd, since ProjectFileArtifactAdd
also needs to be used by Edit servlet(s).
@
text
@d69 1
a69 1
abstract class FileArtifactAdd extends HelmServlet
@

