📄 gridsphereservlet.java
字号:
/** @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>* @version $Id: GridSphereServlet.java 4956 2006-07-26 16:15:56Z novotny $*/package org.gridsphere.servlets;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.gridsphere.layout.PortletLayoutEngine;import org.gridsphere.layout.PortletPageFactory;import org.gridsphere.portlet.impl.PortletContextImpl;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portlet.service.PortletServiceException;import org.gridsphere.portlet.service.spi.PortletServiceFactory;import org.gridsphere.portlet.service.spi.impl.descriptor.PortletServiceCollection;import org.gridsphere.portletcontainer.GridSphereEvent;import org.gridsphere.portletcontainer.PortletDispatcherException;import org.gridsphere.portletcontainer.impl.GridSphereEventImpl;import org.gridsphere.portletcontainer.impl.PortletServiceDescriptor;import org.gridsphere.portletcontainer.impl.PortletSessionManager;import org.gridsphere.services.core.filter.PortalFilter;import org.gridsphere.services.core.filter.PortalFilterService;import org.gridsphere.services.core.persistence.PersistenceManagerException;import org.gridsphere.services.core.persistence.PersistenceManagerRdbms;import org.gridsphere.services.core.persistence.PersistenceManagerService;import org.gridsphere.services.core.registry.PortletManagerService;import org.gridsphere.services.core.security.role.PortletRole;import org.gridsphere.services.core.security.role.RoleManagerService;import org.gridsphere.services.core.user.User;import org.gridsphere.services.core.user.UserManagerService;import org.gridsphere.services.core.user.UserPrincipal;import org.gridsphere.services.core.user.impl.UserImpl;import org.hibernate.StaleObjectStateException;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.portlet.*;import javax.servlet.*;import javax.servlet.http.*;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.net.SocketException;import java.security.Principal;import java.util.ArrayList;import java.util.List;/** * The <code>GridSphereServlet</code> is the GridSphere portlet container. * All portlet requests get proccessed by the GridSphereServlet before they * are rendered. */public class GridSphereServlet extends HttpServlet implements ServletContextListener, HttpSessionListener { private Log log = LogFactory.getLog(GridSphereServlet.class); /* GridSphere Portlet Registry Service */ private PortletManagerService portletManager = null; /* GridSphere Access Control Service */ private RoleManagerService roleService = null; private UserManagerService userManagerService = null; /* GridSphere Portlet layout Engine handles rendering */ private PortletLayoutEngine layoutEngine = PortletLayoutEngine.getInstance(); private PortletSessionManager sessionManager = PortletSessionManager.getInstance(); private PortalFilterService portalFilterService = null; private boolean firstDoGet = true; private boolean isTCK = false; /** * Initializes the GridSphere portlet container * * @param config the <code>ServletConfig</code> * @throws ServletException if an error occurs during initialization */ public final void init(ServletConfig config) throws ServletException { super.init(config); log.info("in init of GridSphereServlet"); String descriptorPath = config.getServletContext().getRealPath("/WEB-INF/GridSphereServices.xml"); // add core gridsphere services to ServiceFactory PortletServiceDescriptor descriptor = null; try { log.debug("loading from: " + descriptorPath); descriptor = new PortletServiceDescriptor(descriptorPath); PortletServiceCollection serviceCollection = descriptor.getServiceCollection(); PortletServiceFactory.addServices("gridsphere", config.getServletContext(), serviceCollection, Thread.currentThread().getContextClassLoader()); } catch (PersistenceManagerException e) { //log.error("error unmarshalling " + servicesPath + " using " + servicesMappingPath + " : " + e.getMessage()); throw new PortletServiceException("error unmarshalling " + descriptorPath, e); } PortletLayoutEngine layoutEngine = PortletLayoutEngine.getInstance(); layoutEngine.init(config.getServletContext()); } private void initializeServices() throws PortletServiceException { roleService = (RoleManagerService) PortletServiceFactory.createPortletService(RoleManagerService.class, true); userManagerService = (UserManagerService) PortletServiceFactory.createPortletService(UserManagerService.class, true); portletManager = (PortletManagerService) PortletServiceFactory.createPortletService(PortletManagerService.class, true); portalFilterService = (PortalFilterService) PortletServiceFactory.createPortletService(PortalFilterService.class, true); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { PersistenceManagerService pms = null; pms = (PersistenceManagerService) PortletServiceFactory.createPortletService(PersistenceManagerService.class, true); PersistenceManagerRdbms pm = null; try { log.info("Starting a database transaction"); pm = pms.createGridSphereRdbms(); pm.beginTransaction(); processRequest(req, res); // Commit and cleanup log.info("Committing the database transaction"); pm.endTransaction(); } catch (StaleObjectStateException staleEx) { log.error("This interceptor does not implement optimistic concurrency control!"); log.error("Your application will not work until you add compensation actions!"); // Rollback, close everything, possibly compensate for any permanent changes // during the conversation, and finally restart business conversation. Maybe // give the user of the application a chance to merge some of his work with // fresh data... what you do here depends on your applications design. //throw staleEx; } catch (Throwable ex) { ex.printStackTrace(); pm.endTransaction(); try { pm.rollbackTransaction(); } catch (Throwable rbEx) { log.error("Could not rollback transaction after exception!", rbEx); } // Let others handle it... maybe another interceptor for exceptions? //throw new ServletException(ex); } } /** * Processes GridSphere portal framework requests * * @param req the <code>HttpServletRequest</code> * @param res the <code>HttpServletResponse</code> * @throws IOException if an I/O error occurs * @throws ServletException if a servlet error occurs */ public void processRequest(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { if (firstDoGet) { initializeServices(); updateDatabase(); firstDoGet = false; } long startTime = System.currentTimeMillis(); PortletContext ctx = new PortletContextImpl(getServletContext()); GridSphereEvent event = new GridSphereEventImpl(ctx, req, res); // check to see if user has been authorized by means of container managed authorization checkWebContainerAuthorization(event); List<PortalFilter> portalFilters = portalFilterService.getPortalFilters(); for (PortalFilter filter : portalFilters) { filter.doBeforeEveryRequest(req, res); } // Used for TCK tests if (isTCK) { req.setAttribute(SportletProperties.LAYOUT_PAGE, PortletPageFactory.TCK_PAGE); setTCKUser(req); } else { setUserAndRoles(event); } // Handle user login and logout if (event.hasAction()) { String actionName = event.getAction().getName(); if (actionName.equals(SportletProperties.LOGOUT)) { logout(event); long endTime = System.currentTimeMillis(); System.err.println("Page render time = " + (endTime - startTime) + " (ms) request= " + req.getQueryString()); return; } } layoutEngine.actionPerformed(event); // perform a redirect-after-POST! if (event.hasAction() && req.getMethod().toUpperCase().equals("POST")) { String requestURL = (String) req.getAttribute(SportletProperties.PORTAL_REDIRECT_PATH); if (req.getParameter("ajax") == null) { log.debug("redirect after POST to: " + requestURL); res.sendRedirect(requestURL); return; } } // is this a file download operation? if (isDownload(req)) { try { downloadFile(req, res); return; } catch (PortletException e) { log.error("Unable to download file!", e); req.setAttribute(SportletProperties.FILE_DOWNLOAD_ERROR, e); } } // Used for TCK tests if (isTCK) { setTCKUser(req); } else { setUserAndRoles(event); } layoutEngine.service(event); for (PortalFilter portalFilter : portalFilters) { PortalFilter filter = (PortalFilter) portalFilter; filter.doAfterEveryRequest(req, res); } //log.debug("Portlet service factory stats"); //factory.logStatistics(); long endTime = System.currentTimeMillis(); System.err.println("Page render time = " + (endTime - startTime) + " (ms) request= " + req.getQueryString()); sessionManager.dumpSessions(); System.err.println("after dump"); //event.getRenderResponse().createRenderURL(); } /** * Method to set the response headers to perform file downloads to a browser * * @param req the HttpServletRequest * @param res the HttpServletResponse * @throws PortletException if a portlet exception occurs * @throws IOException if an IO error occurs */ public void downloadFile(HttpServletRequest req, HttpServletResponse res) throws PortletException, IOException { String fileName = (String) req.getAttribute(SportletProperties.FILE_DOWNLOAD_NAME); if (fileName == null) return; String path = (String) req.getAttribute(SportletProperties.FILE_DOWNLOAD_PATH); Boolean deleteFile = (Boolean) req.getAttribute(SportletProperties.FILE_DELETE); File file = (File) req.getAttribute(SportletProperties.FILE_DOWNLOAD_BINARY); req.removeAttribute(SportletProperties.FILE_DOWNLOAD_NAME); req.removeAttribute(SportletProperties.FILE_DOWNLOAD_PATH); req.removeAttribute(SportletProperties.FILE_DELETE); req.removeAttribute(SportletProperties.FILE_DOWNLOAD_BINARY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -