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

com.liferay.source.formatter.check.XMLWebFileCheck Maven / Gradle / Ivy

There is a newer version: 1.0.1437
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.source.formatter.check;

import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.util.PropertiesUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringUtil;

import java.io.IOException;

import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Hugo Huijser
 */
public class XMLWebFileCheck extends BaseFileCheck {

	@Override
	public boolean isLiferaySourceCheck() {
		return true;
	}

	@Override
	protected String doProcess(
			String fileName, String absolutePath, String content)
		throws IOException {

		if (fileName.endsWith("portal-web/docroot/WEB-INF/web.xml")) {
			content = _formatSecurityConstraints(
				content, _getURLPatterns(absolutePath));
		}
		else if (fileName.endsWith(
					"portal-web/docroot/WEB-INF/shielded-container-web.xml")) {

			content = _formatServletMappings(
				content, _getURLPatterns(absolutePath));
		}

		return content;
	}

	private String _formatSecurityConstraints(
		String content, Set urlPatterns) {

		int x = content.indexOf("");

		if (x == -1) {
			return content;
		}

		x = content.indexOf("", x);

		if (x == -1) {
			return content;
		}

		x = content.indexOf("", x);

		if (x == -1) {
			return content;
		}

		int y = content.indexOf("", x - 3);

		if (y == -1) {
			return content;
		}

		y = content.lastIndexOf("", y);

		if (y == -1) {
			return content;
		}

		StringBundler sb = new StringBundler((3 * urlPatterns.size()) + 1);

		sb.append("\t\t\t/c/portal/protected\n");

		for (String urlPattern : urlPatterns) {
			sb.append("\t\t\t/");
			sb.append(urlPattern);
			sb.append("/c/portal/protected\n");
		}

		return StringBundler.concat(
			content.substring(0, x - 3), sb, content.substring(y + 15));
	}

	private String _formatServletMappings(
		String content, Set urlPatterns) {

		StringBundler sb = new StringBundler(6 * urlPatterns.size());

		for (String urlPattern : urlPatterns) {
			sb.append("\t\n");
			sb.append("\t\tI18n Servlet\n");
			sb.append("\t\t/");
			sb.append(urlPattern);
			sb.append("/*\n");
			sb.append("\t\n");
		}

		int x = content.indexOf("");

		if (x == -1) {
			return content;
		}

		x = content.indexOf("I18n Servlet", x);

		if (x == -1) {
			return content;
		}

		x = content.lastIndexOf("", x) - 1;

		int y = content.lastIndexOf(
			"I18n Servlet");

		if (y == -1) {
			return content;
		}

		y = content.indexOf("", y);

		if (y == -1) {
			return content;
		}

		return StringBundler.concat(
			content.substring(0, x), sb, content.substring(y + 19));
	}

	private Set _getURLPatterns(String absolutePath)
		throws IOException {

		Properties properties = new Properties();

		PropertiesUtil.load(
			properties,
			getPortalContent(
				"portal-impl/src/portal.properties", absolutePath));

		String[] locales = StringUtil.split(
			properties.getProperty(PropsKeys.LOCALES));

		Arrays.sort(locales);

		Set urlPatterns = new TreeSet<>();

		for (String locale : locales) {
			int pos = locale.indexOf(CharPool.UNDERLINE);

			String languageCode = locale.substring(0, pos);

			urlPatterns.add(languageCode);

			urlPatterns.add(locale);

			urlPatterns.add(
				StringUtil.replace(locale, CharPool.UNDERLINE, CharPool.DASH));
		}

		return urlPatterns;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy