📄 gridsphereservlet.java
字号:
try { if (file == null) { file = new File(path + fileName); } if (deleteFile == null) deleteFile = Boolean.FALSE; log.debug("in downloadFile"); log.debug("filename: " + fileName + " filepath= " + path); FileDataSource fds = new FileDataSource(file); log.debug("filename: " + fileName + " filepath= " + path + " content type=" + fds.getContentType()); res.setContentType(fds.getContentType()); res.setHeader("Content-Disposition", "attachment; filename=" + fileName); res.setHeader("Content-Length", String.valueOf(file.length())); DataHandler handler = new DataHandler(fds); handler.writeTo(res.getOutputStream()); if (deleteFile.booleanValue()) { file.delete(); } } catch (FileNotFoundException e) { throw new PortletException("Unable to find file!", e); } catch (SecurityException e) { // this gets thrown if a security policy applies to the file. see java.io.File for details. throw new PortletException("A security error occurred!", e); } catch (SocketException e) { throw new PortletException("A socket error occurred!", e); } } public boolean isDownload(HttpServletRequest req) { return (req.getAttribute(SportletProperties.FILE_DOWNLOAD_NAME) != null); } public void setTCKUser(HttpServletRequest req) { log.info("Setting a TCK user"); UserImpl u = new UserImpl(); u.setUserName("tckuser"); u.setUserID("tckuser"); u.setID("500"); req.setAttribute(SportletProperties.PORTLET_USER, u); req.setAttribute(SportletProperties.PORTLET_ROLE, new ArrayList()); isTCK = true; } public void setUserAndRoles(GridSphereEvent event) { // Retrieve user if there is one HttpServletRequest req = event.getHttpServletRequest(); HttpSession session = req.getSession(true); User user = null; String uid = (String) session.getAttribute(SportletProperties.PORTLET_USER); if (uid != null) { user = userManagerService.getUser(uid); } List<String> roles = new ArrayList<String>(); if (user != null) { UserPrincipal userPrincipal = new UserPrincipal(user.getUserName()); req.setAttribute(SportletProperties.PORTLET_USER_PRINCIPAL, userPrincipal); List<PortletRole> proles = roleService.getRolesForUser(user); for (PortletRole role : proles) { roles.add(role.getName()); } } // set user, role and groups in request req.setAttribute(SportletProperties.PORTLET_USER, user); req.setAttribute(SportletProperties.PORTLET_ROLE, roles); } // Dmitry Gavrilov (2005-03-17) // FIX for web container authorization private void checkWebContainerAuthorization(GridSphereEvent event) { PortletRequest request = event.getActionRequest(); PortletSession session = request.getPortletSession(); if (session.getAttribute(SportletProperties.PORTLET_USER) != null) return; if (!(event.hasAction() && event.getAction().getName().equals(SportletProperties.LOGOUT))) { Principal principal = request.getUserPrincipal(); if (principal != null) { // fix for OC4J. it must work in Tomcat also int indeDelimeter = principal.getName().lastIndexOf('/'); indeDelimeter = (indeDelimeter > 0) ? (indeDelimeter + 1) : 0; String login = principal.getName().substring(indeDelimeter); User user = userManagerService.getUserByUserName(login); if (user != null) { request.setAttribute(SportletProperties.PORTLET_USER, user); session.setAttribute(SportletProperties.PORTLET_USER, user.getID(), PortletSession.APPLICATION_SCOPE); } } } } /** * Handles logout requests * * @param event a <code>GridSphereEvent</code> */ protected void logout(GridSphereEvent event) { log.debug("in logout of GridSphere Servlet"); PortletRequest req = event.getActionRequest(); RenderResponse res = event.getRenderResponse(); //removeUserCookie(event); req.removeAttribute(SportletProperties.PORTLET_USER); req.removeAttribute(SportletProperties.PORTLET_USER_PRINCIPAL); try { portletManager.logoutAllPortletWebApplications(event.getHttpServletRequest(), event.getHttpServletResponse()); } catch (PortletDispatcherException e) { log.error("Failed to logout portlets!", e); } List<PortalFilter> portalFilters = portalFilterService.getPortalFilters(); for (PortalFilter filter : portalFilters) { filter.doAfterLogout(event.getHttpServletRequest(), event.getHttpServletResponse()); } try { String url = res.createRenderURL().toString(); log.error("Post logout redirect to " + url); event.getHttpServletResponse().sendRedirect(url); } catch (IOException e) { log.error("Unable to do a redirect!", e); } } /** * @see #doGet */ public final void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doGet(req, res); } /** * Return the servlet info. * * @return a string with the servlet information. */ public final String getServletInfo() { return "GridSphere Servlet"; } /** * Shuts down the GridSphere portlet container */ public final void destroy() { log.debug("in destroy: Shutting down services"); // Shutdown services PortletServiceFactory.shutdownServices(); System.gc(); } /** * Record the fact that a servlet context attribute was added. * * @param event The session attribute event */ public void attributeAdded(HttpSessionBindingEvent event) { try { log.debug("attributeAdded('" + event.getSession().getId() + "', '" + event.getName() + "', '" + event.getValue() + "')"); } catch (IllegalStateException e) { // do nothing } } /** * Record the fact that a servlet context attribute was removed. * * @param event The session attribute event */ public void attributeRemoved(HttpSessionBindingEvent event) { try { log.debug("attributeRemoved('" + event.getSession().getId() + "', '" + event.getName() + "', '" + event.getValue() + "')"); } catch (IllegalStateException e) { // do nothing } } /** * Record the fact that a servlet context attribute was replaced. * * @param event The session attribute event */ public void attributeReplaced(HttpSessionBindingEvent event) { try { log.debug("attributeReplaced('" + event.getSession().getId() + "', '" + event.getName() + "', '" + event.getValue() + "')"); } catch (IllegalStateException e) { // do nothing } } /** * Record the fact that this ui application has been destroyed. * * @param event The servlet context event */ public void contextDestroyed(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); log.debug("contextDestroyed()"); log.debug("contextName: " + ctx.getServletContextName()); log.debug("context path: " + ctx.getRealPath("")); } /** * Record the fact that this ui application has been initialized. * * @param event The servlet context event */ public void contextInitialized(ServletContextEvent event) { System.err.println("in contextInitialized of GridSphereServlet"); ServletContext ctx = event.getServletContext(); log.info("contextName: " + ctx.getServletContextName()); log.debug("context path: " + ctx.getRealPath("")); } /** * Record the fact that a session has been created. * * @param event The session event */ public void sessionCreated(HttpSessionEvent event) { System.err.println("sessionCreated('" + event.getSession().getId() + "')"); sessionManager.sessionCreated(event); } /** * Record the fact that a session has been destroyed. * * @param event The session event */ public void sessionDestroyed(HttpSessionEvent event) { sessionManager.sessionDestroyed(event); System.err.println("sessionDestroyed('" + event.getSession().getId() + "')"); } public void updateDatabase() { // loop thru users make sure first and last name are created from full name List<User> users = userManagerService.getUsers(); for (User user : users) { if (user.getFirstName().equals("") && user.getLastName().equals("")) { String full = user.getFullName(); int idx = full.lastIndexOf(" "); if (idx > 0) { user.setFirstName(full.substring(0, idx)); user.setLastName(full.substring(idx + 1)); } else { user.setFirstName(full); } Integer numLogins = user.getNumLogins(); if (numLogins == null) numLogins = 0; user.setNumLogins(numLogins++); userManagerService.saveUser(user); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -