org.conqat.engine.service.shared.ServiceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.engine.service.shared;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.function.Function;
import org.conqat.engine.resource.util.UniformPathUtils;
import org.conqat.lib.commons.net.UrlUtils;
import org.conqat.lib.commons.string.StringUtils;
import com.google.common.net.UrlEscapers;
/**
* Utility methods for services.
*/
public class ServiceUtils {
/**
* Encodes the given url path segment. Slashes will be escaped. If you want to
* keep them you need to split the input before and supply them one by one.
*/
public static String encodePathSegment(String pathSegment) {
return UrlEscapers.urlPathSegmentEscaper().escape(pathSegment);
}
/**
* Encodes the given value as query parameter (includes slashes and + symbols).
*/
public static String encodeQueryParameter(String queryParameter) {
return UrlEscapers.urlFormParameterEscaper().escape(queryParameter);
}
/**
* Encodes the given uniform path by URL-encoding each segment while preserving
* the slashes.
*/
public static String encodeUniformPath(String uniformPath) {
boolean leadingSlash = uniformPath.startsWith("/");
String[] parts = UniformPathUtils.splitPath(uniformPath);
for (int i = 0; i < parts.length; i++) {
parts[i] = encodePathSegment(parts[i]);
}
if (leadingSlash) {
return "/" + UniformPathUtils.concatenate(parts);
}
return UniformPathUtils.concatenate(parts);
}
/** Uses Java API to check if the server address is valid */
public static boolean isValidServerAddress(String address) {
// Skip further checks if the path refers to an existing directory
if (new File(address).exists()) {
return true;
}
// Ensure that some kind of protocol is used
if (!address.contains("://")) {
return false;
}
try {
// Check whether the given address is a valid URI.
UrlUtils.convertUriFromUrl(address);
} catch (URISyntaxException e) {
return false;
}
return true;
}
/**
* Produces a specific error message if the identifier is invalid (e.g.,
* contains linebreaks, invisible unicode chars, or is empty). The error message
* contains the (truncated) text if it is not empty.
*
* @param identifierText
* the text of the identifier (e.g., "x" or "foo")
* @param identifierDescription
* a description of what this identifier should be (e.g., "Group
* name" or "Id"). This is used at the beginning of the error message
* and therefore should start with an uppercase letter.
*/
public static Optional getErrorMessageForInvalidIdentifiers(String identifierText,
String identifierDescription) {
Function truncate = identifier -> "\""
+ StringUtils.removeAll(StringUtils.truncateWithThreeDots(identifier, 9), "\n") + "\"";
if (identifierText.contains("\n")) {
return Optional.of(identifierDescription + " " + truncate.apply(identifierText) + " contains linebreaks");
}
if (identifierText.matches(".*\\p{C}.*")) {
return Optional.of(identifierDescription + " " + truncate.apply(identifierText)
+ " contains invisible Unicode control characters");
}
if (identifierText.equals(StringUtils.EMPTY_STRING)) {
return Optional.ofNullable(identifierDescription + " is empty");
}
return Optional.empty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy