1 5 package org.exoplatform.portal.faces.listener.portlet; 6 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import javax.faces.context.FacesContext; 11 import javax.portlet.PortletMode; 12 import javax.portlet.WindowState; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import org.exoplatform.container.PortalContainer; 16 import org.exoplatform.container.SessionContainer; 17 import org.exoplatform.faces.core.event.ExoActionEvent; 18 import org.exoplatform.faces.core.event.ExoActionListener; 19 import org.exoplatform.portal.faces.component.UIPage; 20 import org.exoplatform.portal.faces.component.UIPortal; 21 import org.exoplatform.portal.faces.component.UIPortlet; 22 import org.exoplatform.portal.session.ExoPortal; 23 import org.exoplatform.portal.session.RequestInfo; 24 import org.exoplatform.services.organization.UserProfile; 25 import org.exoplatform.services.portletcontainer.PortletContainerService; 26 import org.exoplatform.services.portletcontainer.pci.ActionInput; 27 import org.exoplatform.services.portletcontainer.pci.ActionOutput; 28 import org.exoplatform.services.portletcontainer.pci.Output; 29 import org.exoplatform.services.log.LogUtil; 30 35 public class PortletActionListener extends ExoActionListener { 36 private PortletContainerService portletContainer_ ; 37 38 public void execute(ExoActionEvent event) throws Exception { 39 UIPortlet uiPortlet = (UIPortlet) event.getSource() ; 40 FacesContext context = FacesContext.getCurrentInstance() ; 41 SessionContainer scontainer = SessionContainer.getInstance() ; 42 UserProfile uprofile = 43 (UserProfile) scontainer.getComponentInstanceOfType(UserProfile.class) ; 44 HttpServletRequest request = 45 (HttpServletRequest ) context.getExternalContext().getRequest(); 46 RequestInfo rinfo = (RequestInfo) scontainer.getComponentInstanceOfType(RequestInfo.class); 47 48 String type = rinfo.getPortletActionType() ; 49 String portletMode = rinfo.getPortletMode() ; 50 String windowState = rinfo.getPortletWindowState() ; 51 if (portletMode != null) { 52 uiPortlet.setPortletMode(portletMode) ; 53 Map renderParams = copyRequestParameterMap(request.getParameterMap()) ; 54 uiPortlet.setRenderParameters(renderParams); 55 uiPortlet.setUpdateCache(true) ; 56 } 57 58 if (type != null ) { 59 if (type.equals("action")) { 60 HttpServletResponse response = 61 (HttpServletResponse ) context.getExternalContext().getResponse(); 62 63 PortletContainerService portletContainer = getPortletContainerService() ; 64 ActionInput actionInput = new ActionInput(); 67 actionInput.setWindowID(uiPortlet.getWindowId()); 69 actionInput.setUserAttributes(uprofile.getUserInfoMap()); 70 actionInput.setPortletMode(uiPortlet.getPortletMode()); 71 actionInput.setWindowState(uiPortlet.getWindowState()); 72 actionInput.setMarkup("text/html"); 73 actionInput.setStateChangeAuthorized(true); 74 ActionOutput output = null; 75 try { 76 output = portletContainer.processAction(request, response, actionInput); 77 String location = (String ) output.getProperties().get(Output.SEND_REDIRECT); 78 if(location != null){ 79 response.sendRedirect(location); 80 String login = (String ) output.getProperties().get(Output.LOGIN); 82 if(login != null){ 83 String password = (String ) output.getProperties().get(Output.PASSWORD); 84 request.getSession().setAttribute(Output.LOGIN, login); 85 request.getSession().setAttribute(Output.PASSWORD, password); 86 } 87 String logout = (String ) output.getProperties().get(Output.LOGOUT); 88 if(logout != null){ 89 request.getSession().invalidate(); 90 } 91 FacesContext.getCurrentInstance().responseComplete(); 92 } 93 PortletMode mode = output.getNextMode() ; 94 WindowState state = output.getNextState() ; 95 if (mode != null ) { 96 uiPortlet.setPortletMode(mode) ; 97 } 98 if (state != null) { 99 windowState = state.toString() ; 100 } 101 uiPortlet.setRenderParameters(output.getRenderParameters()) ; 102 } catch (Exception ex) { 103 LogUtil.getLog(getClass()).error("Handle portlet action error: ", ex) ; 104 } 105 uiPortlet.setUpdateCache(true) ; 106 } else { 107 Map renderParams = copyRequestParameterMap(request.getParameterMap()) ; 108 uiPortlet.setRenderParameters(renderParams); 109 uiPortlet.setUpdateCache(true) ; 110 } 111 } 112 113 if (windowState != null ) { 114 UIPage uiPage = (UIPage)uiPortlet.getAncestorOfType(UIPage.class) ; 115 if (windowState.equals(WindowState.MAXIMIZED.toString())) { 116 uiPortlet.setWindowState(WindowState.MAXIMIZED) ; 117 if (uiPage != null) uiPage.setMaximizedPortlet(uiPortlet) ; 118 } else if (windowState.equals(WindowState.MINIMIZED.toString())) { 119 uiPortlet.setWindowState(WindowState.MINIMIZED) ; 120 if(uiPage != null) uiPage.setMaximizedPortlet(null) ; 121 } else { 122 uiPortlet.setWindowState(WindowState.NORMAL) ; 123 if(uiPage != null) uiPage.setMaximizedPortlet(null) ; 124 } 125 } 126 } 127 128 private Map copyRequestParameterMap(Map map) { 129 Map temp = new HashMap (10) ; 130 Iterator keys = map.keySet().iterator() ; 131 while (keys.hasNext()) { 132 String key = (String ) keys.next() ; 133 temp.put(key, map.get(key)) ; 134 } 135 return temp ; 136 } 137 138 protected void postExecute(ExoActionEvent action) { 139 UIPortal portal = (UIPortal)SessionContainer.getComponent(ExoPortal.class) ; 140 if(!portal.hasQueuEvent()) { 141 FacesContext.getCurrentInstance().renderResponse() ; 142 } 143 } 144 145 private PortletContainerService getPortletContainerService() { 146 if ( portletContainer_ == null) { 147 portletContainer_ = 148 (PortletContainerService) PortalContainer.getInstance(). 149 getComponentInstanceOfType(PortletContainerService.class); 150 } 151 return portletContainer_ ; 152 } 153 } | Popular Tags |