com.beetle.framework.web.controller.UploadController.java Source code

Java tutorial

Introduction

Here is the source code for com.beetle.framework.web.controller.UploadController.java

Source

/*
 * BJAF - Beetle J2EE Application Framework
 * J2EE??
 * ?2003-2015  (www.beetlesoft.net)
 * 
 * ??
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *??????
 *
 * ??
 *  <yuhaodong@gmail.com/>.
 */
package com.beetle.framework.web.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;

import com.beetle.framework.log.AppLogger;
import com.beetle.framework.resource.dic.def.DaoField;
import com.beetle.framework.resource.dic.def.ServiceField;
import com.beetle.framework.util.ObjectUtil;
import com.beetle.framework.web.common.CommonUtil;
import com.beetle.framework.web.common.WebConst;
import com.beetle.framework.web.controller.upload.FileObj;
import com.beetle.framework.web.controller.upload.IUpload;
import com.beetle.framework.web.controller.upload.UploadFactory;
import com.beetle.framework.web.controller.upload.UploadForm;
import com.beetle.framework.web.view.ModelData;
import com.beetle.framework.web.view.View;

/**
 * <p>
 * Title: BeetleWeb
 * </p>
 * 
 * <p>
 * Description: 
 * 
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * 
 * <p>
 * Company: 
 * 
 * </p>
 * 
 * @author (hdyu@beetlesoft.net)
 * @version 1.0
 */
public class UploadController extends ControllerImp {
    private static final Logger logger = AppLogger.getLogger(UploadController.class);

    public UploadController() {
        super();
        this.setCacheSeconds(0);
        this.setInstanceCacheFlag(false);
    }

    /**
     * execute
     * 
     * @param webInput WebInput
     * @return Model
     * @throws ServletException
     * @todo Implement this com.beetle.framework.web.controller.ControllerImp method
     */
    public View perform(WebInput webInput) throws ControllerException {
        HttpServletRequest request = webInput.getRequest();
        return doupload(webInput, request);
    }

    private boolean injectFlag = false;

    private void serviceInject(IUpload upload) throws ControllerException {
        // ?
        if (!this.injectFlag) {
            this.injectFlag = true;// ??
            Field[] fields = upload.getClass().getDeclaredFields();
            if (fields != null && fields.length > 0) {
                for (Field f : fields) {
                    if (f.isAnnotationPresent(ServiceField.class)) {
                        try {
                            ObjectUtil.setFieldValue(upload, f.getName(), this.serviceLookup(f.getType()));
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw new ControllerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                    AppLogger.getErrStackTraceInfo(e));
                        }
                    } else if (f.isAnnotationPresent(DaoField.class)) {
                        throw new ControllerException("The [" + upload.getClass().getName()
                                + "] cannot use daoField annotation, do not conform to the programming paradigm!");
                    }
                }
            }
        }
        //
    }

    /**
     * use the overdue upload package
     * 
     * @param webInput
     * @param request
     * @return
     * @throws ControllerException
     */
    private View doupload(WebInput webInput, HttpServletRequest request) throws ControllerException {
        UploadForm fp = null;
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(factory);
        List<?> fileItems = null;
        boolean openApiCase = false;
        try {
            IUpload upload = (IUpload) webInput.getRequest().getAttribute("UPLOAD_CTRL_IOBJ");
            if (upload == null) {
                logger.debug("get upload from :{}", webInput.getControllerName());
                String ctrlimpName = (String) webInput.getRequest().getAttribute(CommonUtil.controllerimpclassname);
                if (ctrlimpName != null)
                    upload = UploadFactory.getUploadInstance(webInput.getControllerName(), ctrlimpName); // 2007-03-21
                serviceInject(upload);
            }
            if (upload == null) {
                String uploadclass = webInput.getParameter("$upload");
                if (uploadclass == null || uploadclass.trim().length() == 0) {
                    throw new ControllerException("upload dealer can't not found!");
                }
                openApiCase = true;
                String uploadclass_ = ControllerFactory.composeClassImpName(webInput.getRequest(), uploadclass);
                logger.debug("uploadclass:{}", uploadclass);
                logger.debug("uploadclass_:{}", uploadclass_);
                upload = UploadFactory.getUploadInstance(uploadclass, uploadclass_);
                serviceInject(upload);
            }
            logger.debug("IUpload:{}", upload);
            long sizeMax = webInput.getParameterAsLong("sizeMax", 0);
            if (sizeMax == 0) {
                sfu.setSizeMax(IUpload.sizeMax);
            } else {
                sfu.setSizeMax(sizeMax);
            }
            int sizeThreshold = webInput.getParameterAsInteger("sizeThreshold", 0);
            if (sizeThreshold == 0) {
                factory.setSizeThreshold(IUpload.sizeThreshold);
            } else {
                factory.setSizeThreshold(sizeThreshold);
            }
            Map<String, String> fieldMap = new HashMap<String, String>();
            List<FileObj> fileList = new ArrayList<FileObj>();
            fileItems = sfu.parseRequest(request);
            Iterator<?> i = fileItems.iterator();
            while (i.hasNext()) {
                FileItem fi = (FileItem) i.next();
                if (fi.isFormField()) {
                    fieldMap.put(fi.getFieldName(), fi.getString());
                } else {
                    fileList.add(new FileObj(fi));
                }
            }
            fp = new UploadForm(fileList, fieldMap, request, webInput.getResponse());
            View view = upload.processUpload(fp);
            if (view.getViewname() == null || view.getViewname().trim().equals("")) {
                // view.setViewName(AbnormalViewControlerImp.abnormalViewName);
                //
                if (openApiCase) {
                    return view;
                }
                UpService us = new UpService(view);
                return us.perform(webInput);
            }
            return view;
        } catch (Exception ex) {
            logger.error("upload", ex);
            throw new ControllerException(WebConst.WEB_EXCEPTION_CODE_UPLOAD, ex);
        } finally {
            if (fileItems != null) {
                fileItems.clear();
            }
            if (fp != null) {
                fp.clear();
            }
            sfu = null;
        }
    }

    private static class UpService extends WebServiceController {
        private View vw;

        public UpService(View vw) {
            super();
            this.vw = vw;
        }

        @Override
        public ModelData defaultAction(WebInput webInput) throws ControllerException {
            return vw.getMd();
        }

    }
}