/**
* 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.source.formatter.checks;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.PropertiesUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.source.formatter.util.FileUtil;
import java.io.File;
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 isPortalCheck() {
return true;
}
@Override
protected String doProcess(
String fileName, String absolutePath, String content)
throws Exception {
if (fileName.endsWith("portal-web/docroot/WEB-INF/web.xml")) {
content = _formatWebXML(content);
}
return content;
}
private String _formatWebXML(String content) throws Exception {
Properties properties = new Properties();
File propertiesFile = new File(
getBaseDirName(), "portal-impl/src/portal.properties");
String propertiesContent = FileUtil.read(propertiesFile);
PropertiesUtil.load(properties, propertiesContent);
String[] locales = StringUtil.split(
properties.getProperty(PropsKeys.LOCALES));
Arrays.sort(locales);
Set<String> 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);
}
StringBundler sb = new StringBundler(6 * urlPatterns.size());
for (String urlPattern : urlPatterns) {
sb.append("\t<servlet-mapping>\n");
sb.append("\t\t<servlet-name>I18n Servlet</servlet-name>\n");
sb.append("\t\t<url-pattern>/");
sb.append(urlPattern);
sb.append("/*</url-pattern>\n");
sb.append("\t</servlet-mapping>\n");
}
int x = content.indexOf("<servlet-mapping>");
x = content.indexOf("<servlet-name>I18n Servlet</servlet-name>", x);
x = content.lastIndexOf("<servlet-mapping>", x) - 1;
int y = content.lastIndexOf(
"<servlet-name>I18n Servlet</servlet-name>");
y = content.indexOf("</servlet-mapping>", y) + 19;
String newContent =
content.substring(0, x) + sb.toString() + content.substring(y);
x = newContent.indexOf("<security-constraint>");
x = newContent.indexOf("<web-resource-collection>", x);
x = newContent.indexOf("<url-pattern>", x) - 3;
y = newContent.indexOf("</web-resource-collection>", x);
y = newContent.lastIndexOf("</url-pattern>", y) + 15;
sb = new StringBundler(3 * urlPatterns.size() + 1);
sb.append("\t\t\t<url-pattern>/c/portal/protected</url-pattern>\n");
for (String urlPattern : urlPatterns) {
sb.append("\t\t\t<url-pattern>/");
sb.append(urlPattern);
sb.append("/c/portal/protected</url-pattern>\n");
}
newContent =
newContent.substring(0, x) + sb.toString() +
newContent.substring(y);
return newContent;
}
}