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

📄 actionportlet.java

📁 GridSphere 门户 提供一个基于 portlet 的高级开放源代码门户。GridSphere 是在欧盟提供基金的 GridLab 项目下开发的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id: ActionPortlet.java 5032 2006-08-17 18:15:06Z novotny $ */package org.gridsphere.provider.portlet.jsr;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.gridsphere.portlet.impl.ActionRequestImpl;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portlet.service.PortletService;import org.gridsphere.portlet.service.PortletServiceException;import org.gridsphere.portlet.service.spi.PortletServiceFactory;import org.gridsphere.portletcontainer.DefaultPortletAction;import org.gridsphere.portletcontainer.DefaultPortletRender;import org.gridsphere.provider.event.jsr.ActionFormEvent;import org.gridsphere.provider.event.jsr.FormEvent;import org.gridsphere.provider.event.jsr.RenderFormEvent;import org.gridsphere.provider.event.jsr.impl.ActionFormEventImpl;import org.gridsphere.provider.event.jsr.impl.RenderFormEventImpl;import org.gridsphere.provider.portletui.beans.MessageBoxBean;import org.gridsphere.provider.portletui.beans.MessageStyle;import org.gridsphere.provider.portletui.beans.TagBean;import javax.portlet.*;import javax.servlet.http.HttpServletRequestWrapper;import java.io.File;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.*;/** * An <code>ActionPortlet</code> provides an abstraction on top of * <code>GenericPortlet</code> to develop portlets under the action provider model. */public class ActionPortlet extends GenericPortlet {    public Log log = LogFactory.getLog(ActionPortlet.class);    // Default error page    protected String ERROR_PAGE = "doError";    // Default VIEW mode    protected String DEFAULT_VIEW_PAGE = "view.jsp";    // Default EDIT mode    protected String DEFAULT_EDIT_PAGE = "edit.jsp";    // Default HELP mode    protected String DEFAULT_HELP_PAGE = "help.jsp";    // Default CONFIGURE mode    protected String DEFAULT_CONFIGURE_PAGE = "configure.jsp";    public void init(PortletConfig config) throws PortletException {        super.init(config);    }    protected void setFileDownloadEvent(PortletRequest req, String fileName, String path) {        setFileDownloadEvent(req, fileName, path, false);    }    protected void setFileDownloadEvent(PortletRequest req, String fileName, String path, boolean deleteFile) {        req.setAttribute(SportletProperties.FILE_DOWNLOAD_NAME, fileName);        req.setAttribute(SportletProperties.FILE_DOWNLOAD_PATH, path);        req.setAttribute(SportletProperties.FILE_DELETE, Boolean.valueOf(deleteFile));    }    protected void setFileDownloadEvent(PortletRequest req, File file) {        req.setAttribute(SportletProperties.FILE_DOWNLOAD_NAME, file.getName());        req.setAttribute(SportletProperties.FILE_DOWNLOAD_BINARY, file);    }    /**     * Sets the next display state. The state specified may be either a JSP or it can     * be another method name to invoke.     *     * @param request the <code>Portletrequest</code>     * @param state   the next display state     */    protected void setNextState(PortletRequest request, String state) {        String id = getUniqueId();        request.getPortletSession(true).setAttribute(id + ".state", state, PortletSession.PORTLET_SCOPE);        log.debug("in ActionPortlet in setNextState: setting state to " + state);    }    /**     * Returns the next display state. The state specified may be either a JSP or it can     * be another method name to invoke.     *     * @param request the <code>PortletRequest</code>     * @return the next display state, either the JSP to include or a method to invoke     */    protected String getNextState(PortletRequest request) {        String id = getUniqueId();        return (String) request.getPortletSession(true).getAttribute(id + ".state", PortletSession.PORTLET_SCOPE);    }    protected void removeNextState(PortletRequest request) {        String id = getUniqueId();        request.getPortletSession(true).removeAttribute(id + ".state", PortletSession.PORTLET_SCOPE);    }    /**     * Sets the tag beans obtained from the FormEvent. Used internally and should not     * normally need to be invoked by portlet developers.     *     * @param request  the <code>PortletRequest</code>     * @param tagBeans a <code>Map</code> containing the portlet UI visual beans     */    protected void setTagBeans(PortletRequest request, Map tagBeans) {        String id = getUniqueId();        log.debug("saving tag beans in session " + id + ".beans");        request.getPortletSession(true).setAttribute(id + ".beans", tagBeans, PortletSession.PORTLET_SCOPE);    }    protected void removeTagBeans(PortletRequest request) {        String id = getUniqueId();        log.debug("removing tag beans from session " + id + ".beans");        request.getPortletSession(true).removeAttribute(id + ".beans", PortletSession.PORTLET_SCOPE);    }    /**     * Returns the tag beans obtained from the FormEvent. Used internally and should not     * normally need to be invoked by portlet developers.     *     * @param request the <code>PortletRequest</code>     * @return the visual beans     */    protected Map<String, TagBean> getTagBeans(PortletRequest request) {        String id = getUniqueId();        log.debug("getting tag beans from session " + id + ".beans");        return (Map<String, TagBean>) request.getPortletSession(true).getAttribute(id + ".beans", PortletSession.PORTLET_SCOPE);    }    /**     * Uses the action name obtained from the <code>ActionEvent</code> to invoke the     * appropriate portlet action method.     *     * @param actionRequest  the <code>ActionRequest</code>     * @param actionResponse the <code>ActionResponse</code>     * @throws PortletException if a portlet exception occurs     */    public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException {        log.debug("in ActionPortlet: processAction\t\t\t");        // if cid is null (true in non-GS portlet container) then use the portlet name        String cid = (String) actionRequest.getAttribute(SportletProperties.COMPONENT_ID);        if (cid == null) actionRequest.setAttribute(SportletProperties.COMPONENT_ID, getUniqueId());        DefaultPortletAction action = (DefaultPortletAction) actionRequest.getAttribute(SportletProperties.ACTION_EVENT);        // In non-GS container this will need to be created        // TODO        if (!(actionRequest instanceof ActionRequestImpl)) {            //action = GridSphereEventImpl.createAction(actionRequest);            //System.err.println("action name" + action.getName());        }        ActionFormEvent formEvent = new ActionFormEventImpl(action, actionRequest, actionResponse);        Class[] parameterTypes = new Class[]{ActionFormEvent.class};        Object[] arguments = new Object[]{formEvent};        String methodName = formEvent.getAction().getName();        // reset next state        removeNextState(actionRequest);        doAction(actionRequest, actionResponse, methodName, parameterTypes, arguments);        //System.err.println("in processAction: befoire store cid=" + actionRequest.getAttribute(SportletProperties.COMPONENT_ID));        // If it's a POST then remove state        /*        if (((HttpServletRequest)actionRequest).getMethod().toUpperCase().equals("POST")) {            removeNextState(actionRequest);            removeTagBeans(actionRequest);            removeNextTitle(actionRequest);        }        */        setTagBeans(actionRequest, formEvent.getTagBeans());    }    /**     * Invokes the appropriate portlet action method based on the portlet action received     *     * @param request        the portlet request     * @param response       the portlet response     * @param methodName     the method name to invoke     * @param parameterTypes the method parameters     * @param arguments      the method arguments     * @throws PortletException if a portlet error occurs     */    protected void doAction(PortletRequest request, PortletResponse response,                            String methodName,                            Class[] parameterTypes,                            Object[] arguments) throws PortletException {        // Get object and class references        Class thisClass = this.getClass();        // Call method specified by action name        try {            Method method = thisClass.getMethod(methodName, parameterTypes);            method.invoke(this, arguments);            StringBuffer sb = new StringBuffer();            sb.append("Invoking portlet action ").append(thisClass.getName()).append("#").append(methodName);            if (request.getUserPrincipal() != null) {                sb.append(" user=").append(request.getUserPrincipal().getName());                sb.append(" session id=").append(request.getPortletSession().getId());            }            if (request instanceof HttpServletRequestWrapper) {                sb.append(" remote ip=").append(((HttpServletRequestWrapper) request).getRemoteAddr());                sb.append(" user agent=").append(((HttpServletRequestWrapper) request).getHeader("user-agent"));            }            log.info(sb.toString());        } catch (NoSuchMethodException e) {            String error = "No such method: " + methodName + "\n" + e.getMessage();            log.error(error, e);            throw new PortletException(error, e);        } catch (IllegalAccessException e) {            String error = "Error accessing action method: " + methodName + "\n" + e.getMessage();            log.error(error, e);            throw new PortletException(error, e);        } catch (InvocationTargetException e) {            String error = "Error invoking action method: " + methodName;            log.error(error, e.getTargetException());            throw new PortletException(error, e.getTargetException());        }    }    /**     * Renders the supplied JSP page.     *     * @param request  the portlet request     * @param response the portlet response     * @param jsp      the JSP page to include     * @throws PortletException if a portlet exception occurs     */    public void doViewJSP(RenderRequest request, RenderResponse response, String jsp) throws PortletException {        log.debug("Including JSP page:" + jsp);        response.setContentType("text/html; charset=utf-8");        try {            if (jsp.startsWith("/")) {                getPortletConfig().getPortletContext().getRequestDispatcher(jsp).include(request, response);            } else {                getPortletConfig().getPortletContext().getRequestDispatcher("/jsp/" + jsp).include(request, response);            }        } catch (Exception e) {            throw new PortletException("Unable to include JSP", e);        }    }    /**     * Uses #getNextState to either render a JSP or invoke the specified render action method     *     * @param request  the portlet request     * @param response the portlet response     * @throws PortletException if a portlet exception occurs     * @throws IOException      if an I/O error occurs     */    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {        String next = getNextState(request);        if (next == null) {            log.debug("in ActionPortlet: state is null-- setting to DEFAULT_VIEW_PAGE");            setNextState(request, DEFAULT_VIEW_PAGE);        }        doMode(request, response);    }    protected void doMode(RenderRequest request, RenderResponse response) throws PortletException {        String next = getNextState(request);        log.debug("in ActionPortlet: portlet id= " + getUniqueId() + "  next page is= " + next);        // if cid is null (true in non-GS portlet container) then use the portlet name        String cid = (String) request.getAttribute(SportletProperties.COMPONENT_ID);        if (cid == null) request.setAttribute(SportletProperties.COMPONENT_ID, getUniqueId());        DefaultPortletRender render = (DefaultPortletRender) request.getAttribute(SportletProperties.RENDER_EVENT);        Map<String, TagBean> tagBeans = getTagBeans(request);        RenderFormEvent formEvent = new RenderFormEventImpl(render, request, response, tagBeans);        // check to see if this render event should invoke a render method        if (render != null) {            String methodName = render.getName();            if (!methodName.equals("")) next = methodName;        }        if (next.endsWith(".jsp")) {            // this is necessary in case beans were modified in action method and set next state is a JSP to render which needs the beans            formEvent.store();            doViewJSP(request, response, next);        } else {            Class[] paramTypes = new Class[]{RenderFormEvent.class};            Object[] arguments = new Object[]{formEvent};            doAction(request, response, next, paramTypes, arguments);            //System.err.println("in doMode: before store cid=" + request.getAttribute(SportletProperties.COMPONENT_ID));            formEvent.store();            next = getNextState(request);            if ((next != null) && (next.endsWith(".jsp"))) {                log.debug("in doMode: next page is= " + next);                doViewJSP(request, response, next);            }        }        removeTagBeans(request);        removeNextState(request);    }    protected void doDispatch(RenderRequest request,                              RenderResponse response) throws PortletException, IOException {        // if cid is null (true in non-GS portlet container) then use the portlet name        String cid = (String) request.getAttribute(SportletProperties.COMPONENT_ID);        if (cid == null) request.setAttribute(SportletProperties.COMPONENT_ID, getUniqueId());        WindowState state = request.getWindowState();        try {            super.doDispatch(request, response);        } catch (PortletException e) {            if (!state.equals(WindowState.MINIMIZED)) {                PortletMode mode = request.getPortletMode();                if (mode.toString().equalsIgnoreCase("CONFIG")) {                    doConfigure(request, response);                    return;                }            }            throw e;        }    }    /**     * Simply forwards to #doView     *     * @param request  the portlet request     * @param response the portlet response     * @throws PortletException    if a portlet exception occurs     * @throws java.io.IOException if an I/O error occurs     */    public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {        log.debug("ActionPortlet: in doEdit");        String next = getNextState(request);        if (next == null) {            log.debug("in ActionPortlet: state is null-- setting to DEFAULT_EDIT_PAGE");            setNextState(request, DEFAULT_EDIT_PAGE);        }        doMode(request, response);    }    /**     * Simply forwards to #doView     *     * @param request  the portlet request     * @param response the portlet response     * @throws PortletException if a portlet exception occurs     * @throws IOException      if an I/O error occurs     */    public void doConfigure(RenderRequest request, RenderResponse response) throws PortletException, IOException {        log.debug("ActionPortlet: in doConfigure");        String next = getNextState(request);        if (next == null) {            log.debug("in ActionPortlet: state is null-- setting to DEFAULT_CONFIGURE_PAGE");            setNextState(request, DEFAULT_CONFIGURE_PAGE);        }

⌨️ 快捷键说明

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