org.foxbpm.web.controller.AbstWebController.java Source code

Java tutorial

Introduction

Here is the source code for org.foxbpm.web.controller.AbstWebController.java

Source

/**
 * Copyright 1996-2014 FoxBPM ORG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author yangguangftlp
 */
package org.foxbpm.web.controller;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
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.foxbpm.web.common.constant.FoxbpmServiceNameDefinition;
import org.foxbpm.web.common.constant.WebViewName;
import org.foxbpm.web.common.constant.WebContextAttributeName;
import org.foxbpm.web.common.exception.FoxbpmWebException;
import org.foxbpm.web.service.interfaces.IFlowManageService;
import org.foxbpm.web.service.interfaces.IWorkFlowService;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * @author yangguangftlp
 * @date 2014611
 */
public abstract class AbstWebController {

    // ??
    @Resource(name = FoxbpmServiceNameDefinition.WORKFLOW_SERVICENAME)
    protected IWorkFlowService workFlowService;
    // ???
    @Resource(name = FoxbpmServiceNameDefinition.FLOWMANAGE_SERVICENAME)
    protected IFlowManageService flowManageService;

    @ExceptionHandler
    public String exp(HttpServletRequest request, Exception ex) {
        request.setAttribute("errorMsg", ex);
        return WebViewName.RESULT;
    }

    /**
     * ??? modelAndView 
     * 
     * @param viewName
     *            ??
     * @return 
     */
    public ModelAndView createModelAndView(String viewName) {
        return new ModelAndView(getPrefix() + viewName);
    }

    /**
     * ?? :taskCenter/xxxx ????
     * 
     * @return ?
     */
    protected abstract String getPrefix();

    /**
     * http request ??
     * 
     * @param request
     *            http 
     * @return ?http?
     */
    @SuppressWarnings("unchecked")
    protected Map<String, Object> getRequestParams(HttpServletRequest request) {
        // ?
        Map<String, Object> requestParams = new HashMap<String, Object>();
        requestParams.putAll(request.getParameterMap());
        try {
            Enumeration<String> enumeration = null;
            if (ServletFileUpload.isMultipartContent(request)) {
                ServletFileUpload Uploader = new ServletFileUpload(new DiskFileItemFactory());
                Uploader.setHeaderEncoding("utf-8");
                List<FileItem> fileItems = Uploader.parseRequest(request);
                for (FileItem item : fileItems) {
                    requestParams.put(item.getFieldName(), item);
                    if (WebContextAttributeName.ATTRIBUTE_DEPLOYMENTID.equals(item.getFieldName())) {
                        requestParams.put(WebContextAttributeName.ATTRIBUTE_DEPLOYMENTID, item.getString());
                    }
                }
            } else {
                // ?parmeter?
                enumeration = request.getParameterNames();
                if (null != enumeration) {
                    String key = null;
                    String value = null;
                    while (enumeration.hasMoreElements()) {
                        key = enumeration.nextElement();
                        value = request.getParameter(key);
                        requestParams.put(key, new String(value.getBytes("ISO8859-1"), "utf-8"));
                    }
                }
            }
            // ?attribute?
            enumeration = request.getAttributeNames();
            if (null != enumeration) {
                String key = null;
                while (enumeration.hasMoreElements()) {
                    key = enumeration.nextElement();
                    requestParams.put(key, request.getAttribute(key));
                }
            }
            // ??
            requestParams.put(WebContextAttributeName.USERID,
                    request.getSession().getAttribute(WebContextAttributeName.USERID));
        } catch (Exception e) {
            throw new FoxbpmWebException(e);
        }
        return requestParams;
    }

    /**
     * ?
     * 
     * @param response
     *            ?
     * @param in
     *            ??
     */
    public void doResponse(HttpServletResponse response, InputStream in) {
        try {
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/octet-stream;charset=UTF-8");
            byte[] buff = new byte[2048];
            int size = 0;
            while (in != null && (size = in.read(buff)) != -1) {
                out.write(buff, 0, size);
            }
            out.flush();
        } catch (Exception e) {
            throw new FoxbpmWebException(e);
        }
    }

    /**
     * ?
     * 
     * @param response
     *            ?
     * @param in
     *            ??
     */
    public void doResponse(HttpServletResponse response, String content) {
        try {
            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=UTF-8");
            out.print(content);
            out.flush();
        } catch (Exception e) {
            throw new FoxbpmWebException(e);
        }
    }
}