⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pmattachmentwebhandler.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/PmAttachmentWebHandler.java,v 1.54 2006/04/14 17:05:27 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.54 $
 * $Date: 2006/04/14 17:05:27 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2006 by MyVietnam.net
 *
 * All copyright notices regarding mvnForum MUST remain 
 * intact in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in 
 * the footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at MyVietnam net
 *
 * @author: Minh Nguyen  
 * @author: Mai  Nguyen  
 */
package com.mvnforum.user;

import java.io.*;
import java.sql.Timestamp;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvnforum.*;
import com.mvnforum.auth.*;
import com.mvnforum.common.PrivateMessageUtil;
import com.mvnforum.db.*;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.interceptor.InterceptorService;
import net.myvietnam.mvncore.service.BinaryStorage;
import net.myvietnam.mvncore.util.*;
import net.myvietnam.mvncore.web.*;
import net.myvietnam.mvncore.web.fileupload.FileItem;
import net.myvietnam.mvncore.web.fileupload.FileUploadException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PmAttachmentWebHandler {

    private static Log log = LogFactory.getLog(PmAttachmentWebHandler.class);

    private OnlineUserManager userManager = OnlineUserManager.getInstance();

    public PmAttachmentWebHandler() {
    }

    public void prepareAdd(GenericRequest request)
        throws BadInputException, DatabaseException, ObjectNotFoundException,
        AuthenticationException, AssertionException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnableMessageAttachment() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.message_attachment_is_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot add Message Attachment because Message Attachment feature is disabled by administrator.");
        }

        OnlineUser onlineUser = userManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();
        permission.ensureCanAddMessageAttachment();

        int messageID  = GenericParamUtil.getParameterInt(request, "message");
        MessageBean messageBean = null;
        try {
            messageBean = DAOFactory.getMessageDAO().getMessage(messageID);
        } catch (ObjectNotFoundException e) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.messageid_not_exists", new Object[] {new Integer(messageID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        if (messageBean.getFolderName().equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT) == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_add_attachment.pm_does_not_in_folder_draft");
            throw new BadInputException(localizedMessage);
            //throw new BadInputException("Cannot add attachment because this Private Message does not in the folder Draft");
        }

        if (messageBean.getMemberID() != onlineUser.getMemberID() ) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.pm_not_belongs_to_you");
            throw new BadInputException(localizedMessage);
            //throw new BadInputException("This Private Message does not belong to you");
        }

        request.setAttribute("MessageBean", messageBean);
    }

    public void processAdd(GenericRequest request, GenericResponse response)
        throws BadInputException, CreateException, DatabaseException, IOException, ForeignKeyNotFoundException,
        AuthenticationException, AssertionException, ObjectNotFoundException, InterceptorException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnableMessageAttachment() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.message_attachment_is_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot add Attachment because Attachment in Private Message feature is disabled by administrator.");  // localization
        }

        OnlineUser onlineUser = userManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();
        permission.ensureCanAddMessageAttachment();

        MyUtil.saveVNTyperMode(request, response);

        final int UNLIMITED = -1;
        int sizeMax = permission.canAdminSystem() ? UNLIMITED : MVNForumConfig.getMaxMessageAttachmentSize() ;
        int sizeThreshold = 100000;
        String tempDir = MVNForumConfig.getTempDir();

        log.debug("PmAttachmentWebHandler : process upload with temp dir = " + tempDir);

        List fileItems;
        try {
            FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
            fileItems = uploadParser.parseRequest(request, sizeMax, sizeThreshold, tempDir, "UTF-8");
        } catch (FileUploadException ex) {
            log.error("Cannot upload", ex);
            String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.cannot_upload", new Object[] {ex.getMessage()});
            throw new IOException(localizedMessage);
            //throw new IOException("Cannot upload. Detailed reason: " + ex.getMessage());
        }

        // values that must get from the form
        int messageID               = 0;
        String attachFilename       = null;
        int attachFileSize          = 0;
        String attachMimeType       = null;
        String attachDesc           = null;
        boolean attachMore          = false;
        boolean markAsQuote         = false;
        boolean addToSentFolder     = false;

        FileItem attachFileItem = null;

        String actionParam = URLResolverFactory.getURLResolver().getActionParam();
        for (int i = 0; i < fileItems.size(); i++ ) {
            FileItem currentFileItem = (FileItem)fileItems.get(i);
            String fieldName = currentFileItem.getFieldName();
            if (fieldName.equals("AddToSentFolder")) {
                String content = currentFileItem.getString("utf-8");
                addToSentFolder = (content.length() > 0);
                log.debug("addToSentFolder = " + addToSentFolder);
            } else if (fieldName.equals("AttachMore")) {
                String content = currentFileItem.getString("utf-8");
                attachMore = (content.length() > 0);
                log.debug("attachMore = " + attachMore);
            } else if (fieldName.equals("MarkAsQuote")) {
                String content = currentFileItem.getString("utf-8");
                markAsQuote = (content.length() > 0);
                log.debug("markAsQuote = " + markAsQuote);
            } else if (fieldName.equals("MessageID")) {
                String content = currentFileItem.getString("utf-8");
                messageID = Integer.parseInt(content);
                log.debug("messageID = " + messageID);
            } else if (fieldName.equals("AttachDesc")) {
                String content = currentFileItem.getString("utf-8");
                attachDesc = DisableHtmlTagFilter.filter(content);
                log.debug("attachDesc = " + attachDesc);
                attachDesc = InterceptorService.getInstance().validateContent(attachDesc);
            } else if (fieldName.equals("vnselector")) {
                //ignore
            } else if (fieldName.equals("AttachFilename")) {
                if (currentFileItem.isFormField() == true) {
                    String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_uploaded_attach_file_with_form_field");
                    throw new AssertionException(localizedMessage);
                    //throw new AssertionException("Cannot process uploaded attach file with a form field.");
                }
                attachMimeType = currentFileItem.getContentType();
                attachMimeType = DisableHtmlTagFilter.filter(attachMimeType);
                attachFileSize = (int)currentFileItem.getSize();
                if (attachFileSize == 0) {
                    String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_process_upload_with_file_size_is_zero");
                    throw new BadInputException(localizedMessage);
                    //throw new BadInputException("Cannot process an attach file with size = 0. Please check the file size or check if your file is missing.");
                }
                String fullFilePath = currentFileItem.getName();
                attachFilename = FileUtil.getFileName(fullFilePath);
                attachFilename = DisableHtmlTagFilter.filter(attachFilename);
                log.debug("attachFilename = " + attachFilename);

                // now save to attachFileItem
                attachFileItem = currentFileItem;
            } else if (fieldName.equals(actionParam)) {
                // ignore
            } else {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_field_name", new Object[] {fieldName});
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Cannot process field name = " + fieldName);
            }
        } // end for
        Timestamp now = DateUtil.getCurrentGMTTimestamp();

        // check constraint
        MessageBean messageBean = null;
        try {
            messageBean = DAOFactory.getMessageDAO().getMessage(messageID);
        } catch (ObjectNotFoundException e) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.messageid_not_exists", new Object[] {new Integer(messageID)});
            throw new ObjectNotFoundException(localizedMessage);
        }

        if (messageBean.getFolderName().equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT) == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_add_attachment.pm_does_not_in_folder_draft");
            throw new BadInputException(localizedMessage);
            //throw new BadInputException("Cannot add attachment because this Private Message does not in the folder Draft");
        }

        int logonMemberID = onlineUser.getMemberID();

        // Check if the message is from this member
        if (messageBean.getMemberID() != logonMemberID ) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.pm_not_belongs_to_you");
            throw new BadInputException(localizedMessage);
            //throw new BadInputException("This Private Message does not belong to you");
        }

        // Check if the message is from this logon member
        if (messageBean.getMessageSenderID() != logonMemberID ) {
            throw new AssertionException("Assertion: The MessageSenderID must equals the current logined user.");
        }
        if (messageBean.getMessageSenderName().equals(onlineUser.getMemberName()) == false) {
            throw new AssertionException("Assertion: The MessageSenderName must equals the current logined user.");
        }
        //String logonMemberName  = onlineUser.getMemberName();

        // now all contraints/permission have been checked
        // values that we can init now
        String creationIP           = request.getRemoteAddr();
        Timestamp attachCreationDate= now;
        Timestamp attachModifiedDate= now;
        int attachDownloadCount     = 0;
        int attachOption            = 0;// check it
        int attachStatus            = 0;// check it
        int attachID = DAOFactory.getPmAttachmentDAO().create(logonMemberID, attachFilename,
                                                     attachFileSize, attachMimeType, attachDesc,
                                                     creationIP, attachCreationDate, attachModifiedDate,
                                                     attachDownloadCount, attachOption, attachStatus);
        try {
            DAOFactory.getPmAttachMessageDAO().create(messageID, attachID, 0/*type*/, 0/*option*/, 0/*status*/);
        } catch (DuplicateKeyException ex) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -