📄 applicationportletimpl.java
字号:
/* * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id: JSRApplicationPortletImpl.java 4985 2006-08-04 09:54:28Z novotny $ */package org.gridsphere.portletcontainer.impl;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.gridsphere.portlet.impl.PortalContextImpl;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portletcontainer.ApplicationPortlet;import org.gridsphere.portletcontainer.PortletDispatcher;import org.gridsphere.portletcontainer.PortletPreferencesManager;import org.gridsphere.portletcontainer.PortletStatus;import org.gridsphere.portletcontainer.impl.descriptor.*;import org.gridsphere.services.core.persistence.PersistenceManagerException;import javax.portlet.PortalContext;import javax.portlet.Portlet;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.*;/** * The <code>ApplicationPortletImpl</code> is an implementation of the <code>ApplicationPortlet</code> interface * that uses Castor for XML to Java data bindings. * <p/> * The <code>ApplicationPortlet</code> represents the portlet application instance * defined in the portlet descriptor file. */public class ApplicationPortletImpl implements ApplicationPortlet, Comparable { private Log log = LogFactory.getLog(ApplicationPortletImpl.class); private PortletDefinition portletDef = null; private String portletClassName = null; private String webAppName = null; private Locale compareLocale = null; private Portlet portletInstance = null; private ServletContext context = null; private PortalContext portalContext = null; private javax.portlet.PreferencesValidator prefsValidator = null; private int expiration = 0; private Map<String, Set<String>> markupModes = new HashMap<String, Set<String>>(); private List<javax.portlet.WindowState> states = new ArrayList<javax.portlet.WindowState>(); private PortletDeploymentDescriptor portletDD = null; private String concreteID = null; private String portletName = null; private Locale[] supportedLocales = null; private Map<String, String> descsMap = null; private Map<String, String> dispsMap = null; private String requiredRole = ""; protected PortletStatus status = PortletStatus.SUCCESS; protected String statusMessage = "JSR Portlet loaded successfully"; protected PortletPreferencesManager prefsManager = null; /** * Default constructor is private */ protected ApplicationPortletImpl() { } /** * Constructs an instance of ApplicationPortletImpl * * @param pdd the <code>PortletDeploymentDescriptor</code> * @param portletDef the portlet definition * @param webApplication the ui application name for this application portlet * @param context the <code>ServletContext</code> containing this application portlet */ public ApplicationPortletImpl(PortletDeploymentDescriptor pdd, PortletDefinition portletDef, String webApplication, ServletContext context) { this.portletDef = portletDef; this.webAppName = webApplication; this.portletClassName = portletDef.getPortletClass().getContent(); this.portletName = portletDef.getPortletName().getContent(); this.context = context; PortletApp portletApp = pdd.getPortletWebApplication(); SupportedLocale[] locales = portletDef.getSupportedLocale(); supportedLocales = new Locale[locales.length]; for (int i = 0; i < locales.length; i++) { supportedLocales[i] = new Locale(locales[i].getContent()); } // create portal context portalContext = new PortalContextImpl(pdd.getPortletWebApplication()); portletName = portletDef.getPortletName().getContent(); concreteID = webAppName + "#" + portletName; // supported locales supportedLocales = new Locale[portletDef.getSupportedLocaleCount()]; SupportedLocale[] suppLocs = portletDef.getSupportedLocale(); for (int i = 0; i < suppLocs.length; i++) { supportedLocales[i] = new Locale(suppLocs[i].getContent(), "", ""); } // portlet descriptions Description[] descs = portletDef.getDescription(); descsMap = new HashMap<String, String>(); for (int i = 0; i < descs.length; i++) { descsMap.put(descs[i].getLang(), descs[i].getContent()); } // portlet display names DisplayName[] disps = portletDef.getDisplayName(); dispsMap = new HashMap<String, String>(); for (int i = 0; i < disps.length; i++) { dispsMap.put(disps[i].getLang(), disps[i].getContent()); } // get required role SecurityRoleRef[] secRoleRef = portletDef.getSecurityRoleRef(); for (int i = 0; i < secRoleRef.length; i++) { requiredRole = secRoleRef[i].getRoleName().getContent(); } //String id = portletDef.getPortletClass().getContent(); this.portletName = portletDef.getPortletName().getContent(); if (portletDef.getExpirationCache() != null) { expiration = portletDef.getExpirationCache().getContent(); } Supports[] supports = portletDef.getSupports(); // defined portlet modes for (int i = 0; i < supports.length; i++) { Set<String> modesAllowed = new HashSet<String>(); Supports s = (Supports) supports[i]; org.gridsphere.portletcontainer.impl.descriptor.PortletMode[] modes = (org.gridsphere.portletcontainer.impl.descriptor.PortletMode[]) s.getPortletMode(); for (int j = 0; j < modes.length; j++) { org.gridsphere.portletcontainer.impl.descriptor.PortletMode m = modes[j]; modesAllowed.add(m.getContent()); } modesAllowed.add(javax.portlet.PortletMode.VIEW.toString()); String mimeType = (String) s.getMimeType().getContent(); markupModes.put(mimeType, modesAllowed); } List<String> customStatesList = new ArrayList<String>(); CustomWindowState[] customStates = portletApp.getCustomWindowState(); if (customStates != null) { for (int i = 0; i < customStates.length; i++) { customStatesList.add(customStates[i].getWindowState().getContent()); } } // defined window states if (!customStatesList.isEmpty()) { for (int i = 0; i < supports.length; i++) { Supports s = (Supports) supports[i]; org.gridsphere.portletcontainer.impl.descriptor.WindowState[] statesAllowed = (org.gridsphere.portletcontainer.impl.descriptor.WindowState[]) s.getWindowState(); if (statesAllowed != null) { for (int j = 0; j < statesAllowed.length; j++) { org.gridsphere.portletcontainer.impl.descriptor.WindowState w = statesAllowed[j]; if (customStatesList.contains(w.getContent())) states.add(new javax.portlet.WindowState(w.getContent())); } } } } states.add(javax.portlet.WindowState.MAXIMIZED); states.add(javax.portlet.WindowState.MINIMIZED); states.add(javax.portlet.WindowState.NORMAL); org.gridsphere.portletcontainer.impl.descriptor.PortletPreferences prefDesc = portletDef.getPortletPreferences(); if (prefDesc != null) { org.gridsphere.portletcontainer.impl.descriptor.PreferencesValidator validator = prefDesc.getPreferencesValidator(); if (validator != null) { String validatorClass = validator.getContent(); if (validatorClass != null) { try { prefsValidator = (javax.portlet.PreferencesValidator) Class.forName(validatorClass).newInstance(); } catch (Exception e) { log.error("Unable to create validator: " + validatorClass + "! ", e); } } } } prefsManager = new PortletPreferencesManagerImpl(prefDesc, prefsValidator); } /** * Return the web application name associated with this application portlet * * @return the web application name */ public String getWebApplicationName() { return webAppName; } /** * Returns the id of a PortletApplication * * @return the id of the PortletApplication */ public String getApplicationPortletID() { return webAppName + "#" + portletName; } /** * Returns a PortletDispatcher for this ApplicationPortlet * * @return PortletDispatcher the proxy portlet for this ApplicationPortlet */ public PortletDispatcher getPortletDispatcher(HttpServletRequest req, HttpServletResponse res) { //System.err.println("in getPortletDispatcher: cid=" + req.getAttribute(SportletProperties.COMPONENT_ID)); String cid = (String) req.getAttribute(SportletProperties.COMPONENT_ID); String pid = (String) req.getAttribute(SportletProperties.PORTLETID); // TODO fix my hack to get any render params and pass them as queryInfo to the portlet Map params = (Map) req.getAttribute(SportletProperties.RENDER_PARAM_PREFIX + pid + "_" + cid); StringBuffer extraInfo = new StringBuffer(); //System.err.println("Dispatching: Looking for render params for " + SportletProperties.RENDER_PARAM_PREFIX + pid + "_" + cid); if (params == null) { params = new HashMap(); } //params.put(SportletProperties.COMPONENT_ID, cid); boolean firstParam = true; Iterator it = params.keySet().iterator(); try { while (it.hasNext()) { if (!firstParam) { extraInfo.append("&"); } else { extraInfo.append("?"); } String name = (String) it.next(); // Render parameters that are passed on from the portlet frame are persistent across client requests // They are render param names already prefixed. We prefix them again so are can be selectively retrieved // in the nasty request parameter filter GridSphereParameters String encname = URLEncoder.encode("pr_" + name, "UTF-8"); //String encname = URLEncoder.encode(name, "UTF-8"); Object val = params.get(name); if (val instanceof String[]) { String[] vals = (String[]) val; for (int j = 0; j < vals.length - 1; j++) { String encvalue = URLEncoder.encode(vals[j], "UTF-8"); extraInfo.append(encname); extraInfo.append("="); extraInfo.append(encvalue); extraInfo.append("&"); } String encvalue = URLEncoder.encode(vals[vals.length - 1], "UTF-8"); extraInfo.append(encname); extraInfo.append("="); extraInfo.append(encvalue); } else if (val instanceof String) { String aval = (String) params.get(name); if ((aval != null) && (!aval.equals(""))) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -