/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.events;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.util.ServerDetector;
import com.liferay.portal.model.Layout;
import com.liferay.portal.model.LayoutSet;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PropsValues;
import com.liferay.portal.util.WebKeys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Daniel Sanz
 */
public class ParallelRenderPreAction extends Action {

	@Override
	public void run(HttpServletRequest request, HttpServletResponse response)
		throws ActionException {

		if (!PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE ||
			!ServerDetector.isTomcat()) {

			return;
		}

		try {
			_servicePre(request);
		}
		catch (Exception e) {
			throw new ActionException(e);
		}
	}

	private void _servicePre(HttpServletRequest request) throws Exception {
		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
			WebKeys.THEME_DISPLAY);

		if (themeDisplay == null) {
			return;
		}

		Layout layout = themeDisplay.getLayout();

		if (layout == null) {
			return;
		}

		layout.getTypeSettingsProperties();

		LayoutSet layoutSet = layout.getLayoutSet();

		if (layoutSet != null) {
			layoutSet.getSettingsProperties();
		}
	}

}