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

org.conqat.engine.service.shared.client.ServiceClientUris Maven / Gradle / Ivy

There is a newer version: 2025.1.0-rc2
Show newest version
package org.conqat.engine.service.shared.client;

import java.util.List;

import org.conqat.engine.index.shared.CommitDescriptor;
import org.conqat.engine.service.shared.ServiceUtils;
import org.conqat.lib.commons.collections.ListMap;
import org.conqat.lib.commons.collections.Pair;
import org.conqat.lib.commons.string.StringUtils;
import org.conqat.lib.commons.uniformpath.UniformPath;

/** Methods to build Teamscale service URIs. */
public class ServiceClientUris {

	/** Prefix used to recognize projects. */
	public static final String LEGACY_PROJECT_PREFIX = "p/";

	/**
	 * Name of the boolean query parameter used to indicate recursive queries.
	 */
	public static final String RECURSIVE_PARAMETER = "recursive";

	/** Baselines parameter for findings service. */
	public static final String BASELINE_PARAMETER = "baseline";

	/** Include changed code findings parameter. */
	public static final String INCLUDE_CHANGED_CODE_FINDINGS_PARAMETER = "include-changed-code-findings";

	/** Principal metric index parameter for metric distribution service */
	public static final String PRINCIPAL_METRIC_INDEX_PARAMETER = "principal-metric-index";

	/** Metric index parameter for metric distribution service */
	public static final String METRIC_INDEXES_PARAMETER = "metric-indexes";

	/** Boundaries parameter for metric distribution service */
	public static final String BOUNDARY_PARAMETER = "boundaries";

	/** Name of the timestamp parameter. */
	public static final String TIMESTAMP_PARAMETER_NAME = "t";

	private ServiceClientUris() {
		// Utils class should not be instantiated
	}

	/** Constructs a global service uri for services with api prefix. */
	public static String getGlobal(String serviceName, ServerDetails serverDetails, Pair... options) {
		return getGlobal(serviceName, serverDetails, toOptionsMap(options));
	}

	/** Constructs a global service uri for services with api prefix. */
	public static String getGlobal(String serviceName, ServerDetails serverDetails, ListMap options) {
		return StringUtils.ensureEndsWith(serverDetails.getUrl(), "/") //
				+ "api/" + serviceName + createOptionString(options);
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			Pair... options) {
		return getProject(serviceName, serverDetails, projectName, toOptionsMap(options));
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			ListMap options) {
		return getProject(serviceName, serverDetails, projectName, StringUtils.EMPTY_STRING, options);
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			UniformPath uniformPath, Pair... options) {
		return getProject(serviceName, serverDetails, projectName, uniformPath.toString(), options);
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			String uniformPath, Pair... options) {
		return getProject(serviceName, serverDetails, projectName, uniformPath, toOptionsMap(options));
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			UniformPath uniformPath, ListMap options) {
		return getProject(serviceName, serverDetails, projectName, uniformPath.toString(), options);
	}

	/** Constructs a project service uri for services with api prefix. */
	public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
			String uniformPath, ListMap options) {
		return StringUtils.ensureEndsWith(serverDetails.getUrl(), "/") //
				+ "api/projects/" + projectName + "/" //
				+ serviceName + "/" //
				+ ServiceUtils.encodeUniformPath(uniformPath) //
				+ createOptionString(options);
	}

	/** Class for building URI's for legacy services. */
	public static class Legacy {

		/** Constructs a global service uri for services of the legacy framework. */
		public static String getGlobal(String serviceName, ServerDetails serverDetails, Pair... options) {
			return getGlobal(serviceName, serverDetails, toOptionsMap(options));
		}

		/** Constructs a global service uri for services of the legacy framework. */
		public static String getGlobal(String serviceName, ServerDetails serverDetails,
				ListMap options) {
			return getServiceUrl(serverDetails, null, serviceName) + createOptionString(options);
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				Pair... options) {
			return getProject(serviceName, serverDetails, projectName, toOptionsMap(options));
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				String uniformPath, Pair... options) {
			return getProject(serviceName, serverDetails, projectName, uniformPath, toOptionsMap(options));
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				String uniformPath, ListMap options) {
			return getServiceUrl(serverDetails, projectName, serviceName) //
					+ ServiceUtils.encodeUniformPath(uniformPath) //
					+ createOptionString(options);
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				UniformPath uniformPath, Pair options) {
			return getProject(serviceName, serverDetails, projectName, uniformPath, toOptionsMap(options));
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				UniformPath uniformPath, ListMap options) {
			return getProject(serviceName, serverDetails, projectName, uniformPath.toString(), options);
		}

		/** Constructs a project service uri for services of the legacy framework. */
		public static String getProject(String serviceName, ServerDetails serverDetails, String projectName,
				ListMap options) {
			return getServiceUrl(serverDetails, projectName, serviceName) //
					+ createOptionString(options);
		}

		/** Constructs the URL of a service. */
		private static String getServiceUrl(ServerDetails serverDetails, String projectName, String serviceName) {
			String url = serverDetails.getUrl();
			url = StringUtils.ensureEndsWith(url, "/");

			if (projectName != null) {
				url += StringUtils.ensureEndsWith(LEGACY_PROJECT_PREFIX + projectName, "/");
			}
			url = StringUtils.ensureEndsWith(url, "/");

			url += serviceName;
			url = StringUtils.ensureEndsWith(url, "/");

			return url;
		}
	}

	/**
	 * @return the given options as a {@link ListMap} or {@code null}, if no options
	 *         are given.
	 */
	private static ListMap toOptionsMap(Pair... options) {
		if (options.length == 0) {
			return null;
		}
		ListMap optionsMap = new ListMap<>();
		for (Pair option : options) {
			if (option != null) {
				optionsMap.add(option.getFirst().toString(), option.getSecond().toString());
			}
		}
		return optionsMap;
	}

	/**
	 * Creates an encoded URL options string starting with ? from the
	 * given options. The given parameters are interpreted as parameter names and
	 * values, where each odd numbered parameter is a parameter name and each even
	 * one is the corresponding value.
	 */
	public static String createOptionString(String... parametersAndValues) {
		return createOptionString(ListMap.of(parametersAndValues));
	}

	/**
	 * Creates an encoded URL options string starting with ? from the
	 * given options.
	 */
	private static String createOptionString(ListMap options) {
		StringBuilder sb = new StringBuilder();
		if (options != null) {
			String separator = "?";
			for (String option : options.getKeys()) {
				List values = options.getCollection(option);
				for (String value : values) {
					value = ServiceUtils.encodeQueryParameter(value);
					sb.append(separator).append(option).append('=').append(value);
					separator = "&";
				}
			}
		}
		return sb.toString();
	}

	/**
	 * Creates a timestamp parameter pair. When the timestamp is null the method
	 * returns null.
	 */
	public static Pair createCommitDescriptorOption(CommitDescriptor commitDescriptor) {
		if (commitDescriptor == null) {
			return null;
		}
		return Pair.createPair(TIMESTAMP_PARAMETER_NAME, commitDescriptor.toServiceCallFormat());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy