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

io.vrap.rmf.base.client.UserAgentUtils Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client;

import static java.util.stream.Collectors.joining;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Stream;

import org.apache.commons.lang3.SystemUtils;
import org.slf4j.LoggerFactory;

final class UserAgentUtils {
    private UserAgentUtils() {
    }

    final static String unknownUserAgent = "commercetools-java-v2/unknown";
    static final String userAgent = "commercetools-sdk-java-v2";

    static String obtainUserAgent() {
        try {
            return userAgent(Collections.emptyList());
        }
        catch (final Exception e) {
            LoggerFactory.getLogger(UserAgentUtils.class).error("cannot determine user agent", e);
            return unknownUserAgent;
        }
    }

    static String obtainUserAgent(List additionalSolutionInfos) {
        try {
            return userAgent(additionalSolutionInfos);
        }
        catch (final Exception e) {
            LoggerFactory.getLogger(UserAgentUtils.class).error("cannot determine user agent", e);
            return unknownUserAgent;
        }
    }

    private static String userAgent(List additionalSolutionInfos) {
        return MessageFormat
                .format("{0}/{1} Java/{2} ({3}; {4}) {5}", userAgent, BuildInfo.VERSION,
                    SystemUtils.JAVA_RUNTIME_VERSION, SystemUtils.OS_NAME, SystemUtils.OS_ARCH,
                    getSolutionInfoString(additionalSolutionInfos))
                .trim();
    }

    private static String getSolutionInfoString(List additionalSolutionInfos) {
        return Stream.of(SolutionInfoService.getInstance().getSolutionInfos(), additionalSolutionInfos)
                .flatMap(Collection::stream)
                .map(UserAgentUtils::format)
                .collect(joining(" "));
    }

    public static String format(final SolutionInfo solutionInfo) {
        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(solutionInfo.getName()).append("/").append(solutionInfo.getVersion());
        if (isNotEmpty(solutionInfo.getWebsite()) && isNotEmpty(solutionInfo.getEmergencyContact())) {
            stringBuilder.append(" (");
            final String details = Stream.of(solutionInfo.getWebsite(), solutionInfo.getEmergencyContact())
                    .filter(Objects::nonNull)
                    .map(s -> "+" + s)
                    .collect(joining("; "));
            stringBuilder.append(details);
            stringBuilder.append(")");
        }
        return stringBuilder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy