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

📄 portletservlet.java

📁 GridSphere 门户 提供一个基于 portlet 的高级开放源代码门户。GridSphere 是在欧盟提供基金的 GridLab 项目下开发的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>* @version $Id: PortletServlet.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.*;import org.gridsphere.portlet.service.spi.PortletServiceFactory;import org.gridsphere.portletcontainer.ApplicationPortlet;import org.gridsphere.portletcontainer.PortletPreferencesManager;import org.gridsphere.portletcontainer.PortletStatus;import org.gridsphere.portletcontainer.impl.ApplicationPortletImpl;import org.gridsphere.portletcontainer.impl.PortletWebApplicationImpl;import org.gridsphere.portletcontainer.impl.descriptor.*;import org.gridsphere.portletcontainer.impl.descriptor.types.TransportGuaranteeType;import org.gridsphere.services.core.persistence.PersistenceManagerRdbms;import org.gridsphere.services.core.persistence.PersistenceManagerService;import org.gridsphere.services.core.portal.PortalConfigService;import org.gridsphere.services.core.registry.PortletManagerService;import org.gridsphere.services.core.registry.PortletRegistryService;import org.gridsphere.services.core.security.auth.AuthModuleService;import org.gridsphere.services.core.user.User;import javax.portlet.*;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.*;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.util.*;import java.util.ResourceBundle;public class PortletServlet extends HttpServlet        implements Servlet, ServletConfig,        HttpSessionAttributeListener, HttpSessionListener, HttpSessionActivationListener {    private transient Log log = LogFactory.getLog(PortletServlet.class);    private transient PortletRegistryService registryService = null;    private transient PortalConfigService configService = null;    private PortletWebApplicationImpl portletWebApp = null;    private PortletContext portletContext = null;    private Map<String, Portlet> portlets = null;    private Map<String, String> portletclasses = null;    private Map<String, ApplicationPortlet> portletApps = null;    private Map<String, PortletConfig> portletConfigHash = null;    private Map<String, String> userKeys = new HashMap<String, String>();    private List<String> securePortlets = new ArrayList<String>();    private transient PersistenceManagerService pms = (PersistenceManagerService) PortletServiceFactory.createPortletService(PersistenceManagerService.class, true);    public void init(ServletConfig config) throws ServletException {        super.init(config);        log.info("in init of PortletServlet");        portlets = new Hashtable<String, Portlet>();        portletclasses = new Hashtable<String, String>();        portletApps = new Hashtable<String, ApplicationPortlet>();        portletConfigHash = new Hashtable<String, PortletConfig>();    }    public void initJSRPortletWebapp() {        registryService = (PortletRegistryService) PortletServiceFactory.createPortletService(PortletRegistryService.class, true);        configService = (PortalConfigService) PortletServiceFactory.createPortletService(PortalConfigService.class, true);        ServletContext ctx = this.getServletContext();        portletWebApp = new PortletWebApplicationImpl(ctx, Thread.currentThread().getContextClassLoader());        if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;        Collection<ApplicationPortlet> appPortlets = portletWebApp.getAllApplicationPortlets();        for (ApplicationPortlet appPortlet : appPortlets) {            String portletClass = appPortlet.getApplicationPortletClassName();            String portletName = appPortlet.getApplicationPortletName();            try {                // instantiate portlet classes                Portlet portletInstance = (Portlet) Class.forName(portletClass).newInstance();                portletApps.put(portletName, appPortlet);                //portlets.put(portletClass, portletInstance);                portlets.put(portletName, portletInstance);                // mappings between names and classes                portletclasses.put(portletClass, portletName);                log.debug("Creating new portlet instance: " + portletClass);                // put portlet web app in registry            } catch (Exception e) {                String msg = "Unable to create jsr portlet instance: " + portletClass;                log.error(msg, e);                appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);                appPortlet.setApplicationPortletStatusMessage(msg);                portletWebApp.setWebApplicationStatusMessage("FAILURE to instantiate one or more portlet instances");                portletWebApp.setWebApplicationStatus(PortletStatus.FAILURE);            } finally {                registryService.addWebApplication(portletWebApp);            }        }        UserAttribute[] userAttrs = portletWebApp.getUserAttributes();        if (userAttrs != null) {            String key = null;            for (int i = 0; i < userAttrs.length; i++) {                key = userAttrs[i].getName().getContent();                userKeys.put(key, "");            }        }        SecurityConstraint[] secConstraints = portletWebApp.getSecurityConstraints();        if (secConstraints != null) {            for (int i = 0; i < secConstraints.length; i++) {                PortletCollection portlets = secConstraints[i].getPortletCollection();                PortletName[] names = portlets.getPortletName();                UserDataConstraint userConstraint = secConstraints[i].getUserDataConstraint();                TransportGuaranteeType guaranteeType = userConstraint.getTransportGuarantee();                if (guaranteeType.equals(TransportGuaranteeType.NONE)) {                    names = null;                }                if (names != null) {                    for (int j = 0; j < names.length; j++) {                        securePortlets.add(names[j].getContent());                    }                }            }        }        // create portlet context        portletContext = new PortletContextImpl(ctx);        // load in any authentication modules if found-- this is a GridSphere extension        AuthModuleService authModuleService = (AuthModuleService) PortletServiceFactory.createPortletService(AuthModuleService.class, true);        InputStream is = getServletContext().getResourceAsStream("/WEB-INF/authmodules.xml");        if (is != null) {            String authModulePath = this.getServletContext().getRealPath("/WEB-INF/authmodules.xml");            authModuleService.loadAuthModules(authModulePath, Thread.currentThread().getContextClassLoader());            log.info("loading authentication modules from: " + authModulePath);        } else {            log.debug("no auth module descriptor found");        }    }    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // if no lifecycle method exists, redirect to error page!        String method = (String) request.getAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);        if (method == null) {            response.sendRedirect("/" + configService.getProperty("gridsphere.deploy") +                    "/" + configService.getProperty("gridsphere.context") +                    "?" + SportletProperties.LAYOUT_PAGE_PARAM + "=" + "ErrorLayout" + "&" + "errorPage=unauthorized.jsp");            return;        }        if (method.equals(SportletProperties.INIT)) {            initJSRPortletWebapp();            if (portletWebApp.getWebApplicationStatus().equals(PortletStatus.FAILURE)) return;            Set set = portlets.keySet();            Iterator it = set.iterator();            while (it.hasNext()) {                String portletName = (String) it.next();                ApplicationPortletImpl appPortlet = (ApplicationPortletImpl) portletApps.get(portletName);                Portlet portlet = (Portlet) portlets.get(portletName);                log.debug("in PortletServlet: service(): Initializing portlet " + portletName);                PortletDefinition portletDef = portletWebApp.getPortletDefinition(portletName);                PortletConfig portletConfig = new PortletConfigImpl(getServletConfig(), portletDef, Thread.currentThread().getContextClassLoader());                try {                    portlet.init(portletConfig);                    portletConfigHash.put(portletName, portletConfig);                } catch (Exception e) {                    appPortlet.setApplicationPortletStatus(PortletStatus.FAILURE);                    StringWriter sw = new StringWriter();                    PrintWriter pout = new PrintWriter(sw);                    e.printStackTrace(pout);                    appPortlet.setApplicationPortletStatusMessage("Unable to initialize portlet " + portletName + "\n\n" + sw.getBuffer());                    log.error("in PortletServlet: service(): Unable to INIT portlet " + portletName, e);                    // PLT.5.5.2.1 Portlet that fails to initialize must not be placed in active service                    it.remove();                    portletWebApp.setWebApplicationStatus(PortletStatus.FAILURE);                    portletWebApp.setWebApplicationStatusMessage("Failed to initialize one or more portlets");                }            }            PortletManagerService manager = (PortletManagerService) PortletServiceFactory.createPortletService(PortletManagerService.class, true);            manager.addPortletWebApplication(portletWebApp);            return;        } else if (method.equals(SportletProperties.DESTROY)) {            Iterator it = portlets.keySet().iterator();            while (it.hasNext()) {                String portletName = (String) it.next();                Portlet portlet = (Portlet) portlets.get(portletName);                log.debug("in PortletServlet: service(): Destroying portlet " + portletName);                try {                    portlet.destroy();                    it.remove();                } catch (RuntimeException e) {                    log.error("Caught exception during portlet destroy", e);                }            }            return;        } else if (method.equals(SportletProperties.LOGIN)) {        } else if (method.equals(SportletProperties.LOGOUT)) {            request.getSession(true).invalidate();        }        // There must be a portlet ID to know which portlet to service        String pid = (String) request.getAttribute(SportletProperties.PORTLETID);        String cid = (String) request.getAttribute(SportletProperties.COMPONENT_ID);        if (pid == null) {            // it may be in the request parameter            pid = request.getParameter(SportletProperties.PORTLETID);            if (pid == null) {                log.debug("in PortletServlet: service(): No PortletID found in request!");                return;            }            request.setAttribute(SportletProperties.PORTLETID, pid);        }        log.debug("have a portlet id " + pid + " component id= " + cid);        String portletName = "";        int idx = pid.indexOf("#");        Portlet portlet = null;        if (idx > 0) {            portletName = pid.substring(idx + 1);            // this hack uses the portletclasses hash that identifies classname to portlet mappings        } else {            portletName = (String) portletclasses.get(pid);        }        if (portletName == null) {            log.debug("Check the layout descriptors to make sure the portlet identified as " + pid + " matches with the class and/or portlet name of the portlet.xml");            return;        }        portlet = (Portlet) portlets.get(portletName);        request.setAttribute(SportletProperties.PORTLET_CONFIG, portletConfigHash.get(portletName));        ApplicationPortlet appPortlet = registryService.getApplicationPortlet(pid);        if (appPortlet == null) {            log.error("Unable to get portlet from registry identified by: " + pid);            return;        }        // perform user conversion from gridsphere to JSR model        User user = (User) request.getAttribute(SportletProperties.PORTLET_USER);        Map<String, String> userInfo = new HashMap<String, String>();        ;        String userId = null;        if (user != null) {            userId = user.getID();            userInfo.putAll(userKeys);            if (userInfo.containsKey("user.name")) userInfo.put("user.name", user.getUserName());            if (userInfo.containsKey("user.id")) userInfo.put("user.id", user.getID());            if (userInfo.containsKey("user.email")) userInfo.put("user.email", user.getEmailAddress());            if (userInfo.containsKey("user.organization")) userInfo.put("user.organization", user.getOrganization());            if (userInfo.containsKey("user.lastlogintime"))                userInfo.put("user.lastlogintime", String.valueOf(user.getLastLoginTime()));            if (userInfo.containsKey("user.name.full")) userInfo.put("user.name.full", user.getFullName());            if (userInfo.containsKey("user.name.first")) userInfo.put("user.name.first", user.getFirstName());            if (userInfo.containsKey("user.name.last")) userInfo.put("user.name.last", user.getLastName());            if (userInfo.containsKey("user.timezone"))                userInfo.put("user.timezone", (String) user.getAttribute(User.TIMEZONE));            if (userInfo.containsKey("user.locale"))                userInfo.put("user.locale", (String) user.getAttribute(User.LOCALE));            if (userInfo.containsKey("user.theme")) userInfo.put("user.theme", (String) user.getAttribute(User.THEME));            if (userInfo.containsKey("user.login.id")) userInfo.put("user.login.id", user.getUserName());            Enumeration e = user.getAttributeNames();            while (e.hasMoreElements()) {                String key = (String) e.nextElement();                if (userInfo.containsKey(key)) userInfo.put(key, (String) user.getAttribute(key));            }            request.setAttribute(PortletRequest.USER_INFO, userInfo);        }        // portlet preferences        PortalContext portalContext = appPortlet.getPortalContext();        request.setAttribute(SportletProperties.PORTAL_CONTEXT, portalContext);        if (portlet == null) {            log.error("in PortletServlet: service(): No portlet matching " + pid + " found!");            return;        }        request.removeAttribute(SportletProperties.SSL_REQUIRED);        if (securePortlets.contains(pid)) {            request.setAttribute(SportletProperties.SSL_REQUIRED, "true");        }        if (method.equals(SportletProperties.SERVICE)) {            String action = (String) request.getAttribute(SportletProperties.PORTLET_ACTION_METHOD);            if (action != null) {                log.debug("in PortletServlet: action is not NULL");                if (action.equals(SportletProperties.DO_TITLE)) {                    RenderRequest renderRequest = new RenderRequestImpl(request, portletContext);                    RenderResponse renderResponse = new RenderResponseImpl(request, response);                    renderRequest.setAttribute(SportletProperties.RENDER_REQUEST, renderRequest);                    renderRequest.setAttribute(SportletProperties.RENDER_RESPONSE, renderResponse);                    log.debug("in PortletServlet: do title " + pid);                    try {                        doTitle(portlet, renderRequest, renderResponse);                    } catch (Exception e) {                        log.error("Error during doTitle:", e);                        request.getSession(true).setAttribute(SportletProperties.PORTLETERROR + pid, new PortletException(e));                    }                } else if (action.equals(SportletProperties.WINDOW_EVENT)) {                    // do nothing                } else if (action.equals(SportletProperties.ACTION_PERFORMED)) {                    // create portlet preferences manager

⌨️ 快捷键说明

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