All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portlet.internal.PortletConfigImpl Maven / Gradle / Ivy

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.internal;

import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.model.Portlet;
import com.liferay.portal.kernel.model.PortletApp;
import com.liferay.portal.kernel.model.PortletConstants;
import com.liferay.portal.kernel.model.PublicRenderParameter;
import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
import com.liferay.portal.kernel.portlet.PortletBag;
import com.liferay.portal.kernel.portlet.PortletBagPool;
import com.liferay.portal.kernel.portlet.PortletModeFactory;
import com.liferay.portal.kernel.portlet.WindowStateFactory;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.language.LanguageResources;
import com.liferay.portlet.StrutsResourceBundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.portlet.PortletContext;
import javax.portlet.PortletMode;
import javax.portlet.WindowState;

import javax.xml.namespace.QName;

/**
 * @author Brian Wing Shun Chan
 * @author Eduardo Lundgren
 * @author Shuyang Zhou
 * @author Neil Griffin
 */
public class PortletConfigImpl implements LiferayPortletConfig {

	public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
		_portlet = portlet;
		_portletContext = portletContext;

		_portletInfos = PortletResourceBundle.getPortletInfos(
			_portlet.getPortletInfo());

		_copyRequestParameters = GetterUtil.getBoolean(
			getInitParameter("copy-request-parameters"));
		_portletApp = portlet.getPortletApp();

		String portletName = portlet.getPortletName();

		if (portletName == null) {
			portletName = portlet.getRootPortletId();
		}

		int pos = portletName.indexOf(PortletConstants.WAR_SEPARATOR);

		if (pos != -1) {
			portletName = portletName.substring(0, pos);
		}

		_portletName = portletName;

		String className = LiferayPortletConfig.class.getName();

		_containerRuntimeOptionPrefix = className.concat(_portletName);
	}

	@Override
	public Map getContainerRuntimeOptions() {
		Map portletAppContainerRuntimeOptions =
			_portletApp.getContainerRuntimeOptions();

		Map containerRuntimeOptions = new HashMap<>();

		String className = LiferayPortletConfig.class.getName();

		for (Map.Entry portletAppContainerRuntimeOption :
				portletAppContainerRuntimeOptions.entrySet()) {

			String name = portletAppContainerRuntimeOption.getKey();

			if (!name.startsWith(className)) {
				containerRuntimeOptions.put(
					name, portletAppContainerRuntimeOption.getValue());
			}
		}

		// PLT 8.4: If the deployment descriptor contains
		//  elements of the same name defined at both
		// the portlet and the portlet application levels, the returned Map
		// contains a the value defined at the portlet level.

		for (Map.Entry portletAppContainerRuntimeOption :
				portletAppContainerRuntimeOptions.entrySet()) {

			String name = portletAppContainerRuntimeOption.getKey();

			if (name.startsWith(_containerRuntimeOptionPrefix)) {
				containerRuntimeOptions.put(
					name.substring(_containerRuntimeOptionPrefix.length()),
					portletAppContainerRuntimeOption.getValue());
			}
		}

		Set keySet = containerRuntimeOptions.keySet();

		keySet.retainAll(
			SetUtil.fromEnumeration(
				_portletContext.getContainerRuntimeOptions()));

		return Collections.unmodifiableMap(containerRuntimeOptions);
	}

	@Override
	public String getDefaultNamespace() {
		return _portletApp.getDefaultNamespace();
	}

	@Override
	public String getInitParameter(String name) {
		if (name == null) {
			throw new IllegalArgumentException();
		}

		Map initParams = _portlet.getInitParams();

		return initParams.get(name);
	}

	@Override
	public Enumeration getInitParameterNames() {
		Map initParams = _portlet.getInitParams();

		return Collections.enumeration(initParams.keySet());
	}

	@Override
	public Portlet getPortlet() {
		return _portlet;
	}

	@Override
	public PortletContext getPortletContext() {
		return _portletContext;
	}

	@Override
	public String getPortletId() {
		return _portlet.getPortletId();
	}

	@Override
	public Enumeration getPortletModes(String mimeType) {
		Map> portletModeMap = _portlet.getPortletModes();

		Set portletModeNames = portletModeMap.get(mimeType);

		if (portletModeNames == null) {
			return Collections.emptyEnumeration();
		}

		List portletModes = new ArrayList<>(
			portletModeNames.size());

		for (String portletModeName : portletModeNames) {
			portletModes.add(
				PortletModeFactory.getPortletMode(
					portletModeName, _portletApp.getSpecMajorVersion()));
		}

		return Collections.enumeration(portletModes);
	}

	@Override
	public String getPortletName() {
		return _portletName;
	}

	@Override
	public Enumeration getProcessingEventQNames() {
		return Collections.enumeration(
			toJavaxQNames(_portlet.getProcessingEvents()));
	}

	@Override
	public Map getPublicRenderParameterDefinitions() {
		Map publicRenderParameterDefinitions = new HashMap<>();

		for (PublicRenderParameter publicRenderParameter :
				_portlet.getPublicRenderParameters()) {

			com.liferay.portal.kernel.xml.QName qName =
				publicRenderParameter.getQName();

			publicRenderParameterDefinitions.put(
				publicRenderParameter.getIdentifier(),
				new QName(
					qName.getNamespaceURI(), qName.getLocalPart(),
					qName.getNamespacePrefix()));
		}

		return publicRenderParameterDefinitions;
	}

	@Override
	public Enumeration getPublicRenderParameterNames() {
		List publicRenderParameterNames = new ArrayList<>();

		for (PublicRenderParameter publicRenderParameter :
				_portlet.getPublicRenderParameters()) {

			publicRenderParameterNames.add(
				publicRenderParameter.getIdentifier());
		}

		return Collections.enumeration(publicRenderParameterNames);
	}

	@Override
	public Enumeration getPublishingEventQNames() {
		return Collections.enumeration(
			toJavaxQNames(_portlet.getPublishingEvents()));
	}

	@Override
	public ResourceBundle getResourceBundle(Locale locale) {
		String resourceBundleClassName = _portlet.getResourceBundle();

		if (Validator.isNull(resourceBundleClassName)) {
			String resourceBundleId = _portlet.getPortletId();

			ResourceBundle resourceBundle = _resourceBundles.get(
				resourceBundleId);

			if (resourceBundle == null) {
				resourceBundle = new PortletResourceBundle(
					LanguageResources.getResourceBundle(locale), _portletInfos);

				_resourceBundles.put(resourceBundleId, resourceBundle);
			}

			return resourceBundle;
		}

		ResourceBundle resourceBundle = null;

		if (!_portletApp.isWARFile() &&
			resourceBundleClassName.equals(
				StrutsResourceBundle.class.getName())) {

			String resourceBundleId = StringBundler.concat(
				_portlet.getPortletId(), locale.getLanguage(),
				locale.getCountry(), locale.getVariant());

			resourceBundle = _resourceBundles.get(resourceBundleId);

			if (resourceBundle == null) {
				resourceBundle = new StrutsResourceBundle(_portletName, locale);
			}

			_resourceBundles.put(resourceBundleId, resourceBundle);
		}
		else {
			PortletBag portletBag = PortletBagPool.get(
				_portlet.getRootPortletId());

			if (portletBag != null) {
				resourceBundle = portletBag.getResourceBundle(locale);
			}
		}

		return new PortletResourceBundle(resourceBundle, _portletInfos);
	}

	@Override
	public Enumeration getSupportedLocales() {
		List supportedLocales = new ArrayList<>();

		for (String languageId : _portlet.getSupportedLocales()) {
			supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
		}

		return Collections.enumeration(supportedLocales);
	}

	@Override
	public Enumeration getWindowStates(String mimeType) {
		Map> windowStateMap = _portlet.getWindowStates();

		Set windowStateNames = windowStateMap.get(mimeType);

		if (windowStateNames == null) {
			return Collections.emptyEnumeration();
		}

		List windowStates = new ArrayList<>(
			windowStateNames.size());

		for (String windowStateName : windowStateNames) {
			windowStates.add(
				WindowStateFactory.getWindowState(
					windowStateName, _portletApp.getSpecMajorVersion()));
		}

		return Collections.enumeration(windowStates);
	}

	@Override
	public boolean isCopyRequestParameters() {
		return _copyRequestParameters;
	}

	@Override
	public boolean isWARFile() {
		return _portletApp.isWARFile();
	}

	protected Set toJavaxQNames(
		Set liferayQNames) {

		Set javaxQNames = new HashSet<>();

		for (com.liferay.portal.kernel.xml.QName liferayQName : liferayQNames) {
			QName javaxQName = new QName(
				liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
				liferayQName.getNamespacePrefix());

			javaxQNames.add(javaxQName);
		}

		return javaxQNames;
	}

	private final String _containerRuntimeOptionPrefix;
	private final boolean _copyRequestParameters;
	private final Portlet _portlet;
	private final PortletApp _portletApp;
	private final PortletContext _portletContext;
	private final Map _portletInfos;
	private final String _portletName;
	private final Map _resourceBundles =
		new ConcurrentHashMap<>();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy