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

com.slack.api.util.http.UserAgentInterceptor Maven / Gradle / Ivy

There is a newer version: 1.39.0
Show newest version
package com.slack.api.util.http;

import com.slack.api.meta.SlackApiClientLibraryVersion;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;

/**
 * An OkHttpClient interceptor that adds the SDK User-Agent to all the outgoing HTTP requests by this SDK.
 */
@Slf4j
public class UserAgentInterceptor implements Interceptor {

    public static final String USER_AGENT_PREFIX = "Java-Slack-SDK; ";

    private final String userAgent;

    public UserAgentInterceptor(Map additionalInfo) {
        this.userAgent = buildDefaultUserAgent(additionalInfo);
    }

    public static String buildDefaultUserAgent(Map additionalInfo) {
        // NOTE: UserAgentInterceptor.class.getPackage().getImplementationVersion() returns null on AWS Lambda
        String libraryVersion = SlackApiClientLibraryVersion.get();
        String library = "slack-api-client/" + libraryVersion + "";
        String jvm = "" + System.getProperty("java.vm.name") + "/" + System.getProperty("java.version") + "";
        String os = "" + System.getProperty("os.name") + "/" + System.getProperty("os.version") + "";
        String lastPart = "";
        for (Map.Entry each : additionalInfo.entrySet()) {
            lastPart += " " + each.getKey() + "/" + each.getValue() + ";";
        }
        return USER_AGENT_PREFIX + library + "; " + jvm + "; " + os + ";" + lastPart;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        // Modify "User-Agent" header
        Request request = chain.request().newBuilder().header("User-Agent", userAgent).build();
        return chain.proceed(request);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy