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

com.liferay.jenkins.results.parser.PortalHotfixRelease Maven / Gradle / Ivy

The 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.jenkins.results.parser;

import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.Element;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Michael Hashimoto
 */
public class PortalHotfixRelease {

	public PortalHotfixRelease(JSONObject jsonObject) {
		_portalHotfixReleaseURL = _getURL(
			jsonObject.getString("portalHotfixURL"));

		if (_portalHotfixReleaseURL == null) {
			throw new RuntimeException("Unable to get Portal Hotfix URL");
		}

		URL portalReleaseURL = _getURL(
			jsonObject.optString("portalReleaseURL"));

		if (portalReleaseURL != null) {
			_portalRelease = new PortalRelease(portalReleaseURL);
		}
		else {
			_portalRelease = null;
		}

		URL portalFixpackURL = _getURL(
			jsonObject.optString("portalFixpackURL"));

		if (portalFixpackURL != null) {
			_portalFixpackRelease = new PortalFixpackRelease(portalFixpackURL);
		}
		else {
			_portalFixpackRelease = null;
		}
	}

	public PortalHotfixRelease(URL portalHotfixReleaseURL) {
		_portalHotfixReleaseURL = portalHotfixReleaseURL;

		_portalFixpackRelease = null;
		_portalRelease = null;
	}

	public PortalHotfixRelease(
		URL portalHotfixReleaseURL, PortalFixpackRelease portalFixpackRelease,
		PortalRelease portalRelease) {

		_portalHotfixReleaseURL = portalHotfixReleaseURL;
		_portalFixpackRelease = portalFixpackRelease;
		_portalRelease = portalRelease;
	}

	public JSONObject getJSONObject() {
		JSONObject jsonObject = new JSONObject();

		PortalFixpackRelease portalFixpackRelease = getPortalFixpackRelease();

		if (portalFixpackRelease != null) {
			jsonObject.put(
				"portalFixpackURL", portalFixpackRelease.getPortalFixpackURL());
		}

		jsonObject.put("portalHotfixURL", getPortalHotfixReleaseURL());

		PortalRelease portalRelease = getPortalRelease();

		if (portalRelease != null) {
			jsonObject.put(
				"portalReleaseURL", portalRelease.getPortalBundleTomcatURL());
		}

		return jsonObject;
	}

	public Set getModifiedPackageNames() {
		Set packageNames = _getJSONPackageNames();

		if (packageNames == null) {
			packageNames = _getXMLPackageNames();
		}

		if ((packageNames == null) || packageNames.isEmpty()) {
			return new HashSet<>();
		}

		Set modifiedPackageNames = new HashSet<>();

		for (String packageName : packageNames) {
			if (JenkinsResultsParserUtil.isNullOrEmpty(packageName)) {
				continue;
			}

			if (packageName.contains("/")) {
				packageName = packageName.substring(
					packageName.lastIndexOf("/") + 1);
			}

			Matcher matcher = _packageNamePattern.matcher(packageName);

			if (!matcher.find()) {
				continue;
			}

			modifiedPackageNames.add(matcher.group("packageName"));
		}

		return modifiedPackageNames;
	}

	public PortalFixpackRelease getPortalFixpackRelease() {
		return _portalFixpackRelease;
	}

	public String getPortalHotfixReleaseName() {
		String portalHotfixReleaseURLString = String.valueOf(
			_portalHotfixReleaseURL);

		Matcher matcher = _hotfixURLPattern.matcher(
			portalHotfixReleaseURLString);

		if (!matcher.find()) {
			return null;
		}

		return matcher.group("hotfixName");
	}

	public URL getPortalHotfixReleaseURL() {
		return _portalHotfixReleaseURL;
	}

	public String getPortalHotfixReleaseVersion() {
		String portalHotfixReleaseURLString = String.valueOf(
			_portalHotfixReleaseURL);

		Matcher matcher = _hotfixURLPattern.matcher(
			portalHotfixReleaseURLString);

		if (!matcher.find()) {
			return null;
		}

		return matcher.group("hotfixVersion");
	}

	public PortalRelease getPortalRelease() {
		return _portalRelease;
	}

	private Element _getFixpackDocumentationElement() {
		synchronized (_portalHotfixReleaseURL) {
			if (_fixpackDocumentationElement != null) {
				return _fixpackDocumentationElement;
			}

			File tempDir = new File(
				JenkinsResultsParserUtil.getDistinctTimeStamp());

			try {
				tempDir.mkdirs();

				File hotfixFile = new File(tempDir, "hotfix.zip");

				JenkinsResultsParserUtil.toFile(
					getPortalHotfixReleaseURL(), hotfixFile);

				JenkinsResultsParserUtil.unzip(hotfixFile, tempDir);

				File fixpackDocumentationFile = new File(
					tempDir, "fixpack_documentation.xml");

				if (!fixpackDocumentationFile.exists()) {
					return null;
				}

				Document document = Dom4JUtil.parse(
					JenkinsResultsParserUtil.read(fixpackDocumentationFile));

				_fixpackDocumentationElement = document.getRootElement();

				return _fixpackDocumentationElement;
			}
			catch (Exception exception) {
				return null;
			}
			finally {
				JenkinsResultsParserUtil.delete(tempDir);
			}
		}
	}

	private JSONObject _getFixpackDocumentationJSONObject() {
		synchronized (_portalHotfixReleaseURL) {
			if (_fixpackDocumentationJSONObject != null) {
				return _fixpackDocumentationJSONObject;
			}

			File tempDir = new File(
				JenkinsResultsParserUtil.getDistinctTimeStamp());

			try {
				tempDir.mkdirs();

				File hotfixFile = new File(tempDir, "hotfix.zip");

				JenkinsResultsParserUtil.toFile(
					getPortalHotfixReleaseURL(), hotfixFile);

				JenkinsResultsParserUtil.unzip(hotfixFile, tempDir);

				File fixpackDocumentationFile = new File(
					tempDir, "fixpack_documentation.json");

				if (!fixpackDocumentationFile.exists()) {
					return null;
				}

				_fixpackDocumentationJSONObject = new JSONObject(
					JenkinsResultsParserUtil.read(fixpackDocumentationFile));

				return _fixpackDocumentationJSONObject;
			}
			catch (Exception exception) {
				return null;
			}
			finally {
				JenkinsResultsParserUtil.delete(tempDir);
			}
		}
	}

	private Set _getJSONPackageNames() {
		JSONObject fixpackDocumentationJSONObject =
			_getFixpackDocumentationJSONObject();

		if (fixpackDocumentationJSONObject == null) {
			return null;
		}

		Set packageNames = new HashSet<>();

		JSONObject filesJSONObject =
			fixpackDocumentationJSONObject.getJSONObject("files");

		JSONArray jarFilesJSONArray = filesJSONObject.getJSONArray("jar_files");

		for (int i = 0; i < jarFilesJSONArray.length(); i++) {
			JSONObject jarFileJSONObject = jarFilesJSONArray.getJSONObject(i);

			packageNames.add(jarFileJSONObject.optString("new_name"));
		}

		JSONArray lpkgFilesJSONArray = filesJSONObject.getJSONArray(
			"lpkg_files");

		for (int i = 0; i < lpkgFilesJSONArray.length(); i++) {
			JSONObject lpkgFilesJSONObject = lpkgFilesJSONArray.getJSONObject(
				i);

			JSONArray modifiedJarsJSONArray = lpkgFilesJSONObject.getJSONArray(
				"modified_jars");

			for (int j = 0; j < modifiedJarsJSONArray.length(); j++) {
				JSONObject modifiedJarJSONObject =
					modifiedJarsJSONArray.getJSONObject(j);

				packageNames.add(modifiedJarJSONObject.optString("new_name"));
			}
		}

		return packageNames;
	}

	private URL _getURL(String urlString) {
		try {
			return new URL(urlString);
		}
		catch (MalformedURLException malformedURLException) {
			return null;
		}
	}

	private Set _getXMLPackageNames() {
		Element fixpackDocumentationElement = _getFixpackDocumentationElement();

		if (fixpackDocumentationElement == null) {
			return null;
		}

		Set packageNames = new HashSet<>();

		Element checksumsElement = fixpackDocumentationElement.element(
			"checksums");

		List fileChecksumElements = checksumsElement.elements(
			"file-checksum");

		for (Element fileChecksumElement : fileChecksumElements) {
			packageNames.add(fileChecksumElement.attributeValue("file"));
		}

		return packageNames;
	}

	private static final Pattern _hotfixURLPattern = Pattern.compile(
		"https?://.+/(?liferay-(hotfix|security-de|security-dxp|" +
			"dxp-\\d{4}.q\\d+.\\d+-hotfix)-" +
				"(?\\d+)(-\\d{6}-\\d)?(-)?(\\d{4})?)");
	private static final Pattern _packageNamePattern = Pattern.compile(
		"(?[\\.\\w]+|[\\-\\w]+)(-\\d.*)?\\.jar");

	private Element _fixpackDocumentationElement;
	private JSONObject _fixpackDocumentationJSONObject;
	private final PortalFixpackRelease _portalFixpackRelease;
	private final URL _portalHotfixReleaseURL;
	private final PortalRelease _portalRelease;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy