static Agent of(String name, String version) { return ImmutableAgent.builder() .name(name) .version(UserAgents.isValidVersion(version) ? version : DEFAULT_VERSION) .build(); } }
/** * Returns a new {@link UserAgent} instance whose {@link #informational} agents are this instance's agents plus the * given agent. */ default UserAgent addAgent(Agent agent) { return ImmutableUserAgent.builder() .from(this) .addInformational(agent) .build(); }
/** * Creates a new {@link ClientConfiguration} instance from the given {@link ServiceConfiguration}, filling in * empty/absent configuration with the defaults specified as constants in this class. */ public static ClientConfiguration of(ServiceConfiguration config) { return ClientConfiguration.builder() .sslSocketFactory(SslSocketFactories.createSslSocketFactory(config.security())) .trustManager(SslSocketFactories.createX509TrustManager(config.security())) .uris(config.uris()) .connectTimeout(config.connectTimeout().orElse(DEFAULT_CONNECT_TIMEOUT)) .readTimeout(config.readTimeout().orElse(DEFAULT_READ_TIMEOUT)) .writeTimeout(config.writeTimeout().orElse(DEFAULT_WRITE_TIMEOUT)) .enableGcmCipherSuites(config.enableGcmCipherSuites().orElse(DEFAULT_ENABLE_GCM_CIPHERS)) .proxy(config.proxy().map(ClientConfigurations::createProxySelector).orElse(ProxySelector.getDefault())) .proxyCredentials(config.proxy().flatMap(ProxyConfiguration::credentials)) .meshProxy(meshProxy(config.proxy())) .maxNumRetries(config.maxNumRetries().orElse(DEFAULT_MAX_NUM_RETRIES)) .nodeSelectionStrategy(DEFAULT_NODE_SELECTION_STRATEGY) .failedUrlCooldown(DEFAULT_FAILED_URL_COOLDOWN) .backoffSlotSize(config.backoffSlotSize().orElse(DEFAULT_BACKOFF_SLOT_SIZE)) .build(); }
@Value.Check default void check() { if (meshProxy().isPresent()) { checkArgument(maxNumRetries() == 0, "If meshProxy is configured then maxNumRetries must be 0"); checkArgument(uris().size() == 1, "If meshProxy is configured then uris must contain exactly 1 URI"); } if (nodeSelectionStrategy().equals(NodeSelectionStrategy.ROUND_ROBIN)) { checkArgument(!failedUrlCooldown().isNegative() && !failedUrlCooldown().isZero(), "If nodeSelectionStrategy is ROUND_ROBIN then failedUrlCooldown must be positive"); } }
@Value.Check default void check() { checkArgument(UserAgents.isValidName(name()), "Illegal agent name format: %s", name()); // Should never hit the following. checkArgument(UserAgents.isValidVersion(version()), "Illegal version format: %s. This is a bug", version()); }
/** * Adds informational {@link com.palantir.remoting3.clients.UserAgent.Agent}s to the given {@link UserAgent}, one * for the http-remoting library and one for the given service class. Version strings are extracted from the * packages' {@link Package#getImplementationVersion implementation version}, defaulting to 0.0.0 if no version can * be found. */ private static UserAgent augmentUserAgent(UserAgent agent, Class<?> serviceClass) { UserAgent augmentedAgent = agent; String maybeServiceVersion = serviceClass.getPackage().getImplementationVersion(); augmentedAgent = augmentedAgent.addAgent(UserAgent.Agent.of( serviceClass.getSimpleName(), maybeServiceVersion != null ? maybeServiceVersion : "0.0.0")); String maybeRemotingVersion = OkHttpClients.class.getPackage().getImplementationVersion(); augmentedAgent = augmentedAgent.addAgent(UserAgent.Agent.of( UserAgents.REMOTING_AGENT_NAME, maybeRemotingVersion != null ? maybeRemotingVersion : "0.0.0")); return augmentedAgent; }
private UserAgentInterceptor(UserAgent userAgent) { this.userAgent = UserAgents.format(userAgent); }
/** * Deprecated variant of {@link #create(ClientConfiguration, UserAgent, Class)}. * * @deprecated Use {@link #create(ClientConfiguration, UserAgent, Class)} */ @Deprecated public static OkHttpClient create(ClientConfiguration config, String userAgent, Class<?> serviceClass) { return create(config, UserAgents.tryParse(userAgent), serviceClass); }
@Value.Immutable @ImmutablesStyle interface ServiceHostAndPort { @Value.Parameter String serviceName(); @Value.Parameter String hostname(); @Value.Parameter int port(); } }
@Value.Check default void check() { if (nodeId().isPresent()) { checkArgument(UserAgents.isValidNodeId(nodeId().get()), "Illegal node id format: %s", nodeId().get()); } }
private static ImmutableList<ConnectionSpec> createConnectionSpecs(boolean enableGcmCipherSuites) { return ImmutableList.of( new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .cipherSuites(enableGcmCipherSuites ? CipherSuites.allCipherSuites() : CipherSuites.fastCipherSuites()) .build(), ConnectionSpec.CLEARTEXT); }
/** * Like {@link #parse}, but never fails and returns the primary agent {@code unknown/0.0.0} if no valid primary * agent can be parsed. */ public static UserAgent tryParse(String userAgent) { return parseInternal(Strings.nullToEmpty(userAgent), true /* lenient */); }
static Builder builder() { return new Builder(); }
private static ClientConfiguration createTestConfig(String... uris) { SslConfiguration sslConfiguration = SslConfiguration.of( Paths.get(ResourceHelpers.resourceFilePath("trustStore.jks"))); return ClientConfigurations.of( ImmutableList.copyOf(uris), SslSocketFactories.createSslSocketFactory(sslConfiguration), SslSocketFactories.createX509TrustManager(sslConfiguration)); } }
/** * Similar to {@link #create(Class, UserAgent, ClientConfiguration)}, but creates a mutable client that updates its * configuration transparently whenever the given {@link Refreshable refreshable} {@link ClientConfiguration} * changes. */ public static <T> T create(Class<T> serviceClass, UserAgent userAgent, Refreshable<ClientConfiguration> config) { // TODO(rfink): Add http-remoting agent as informational return create(serviceClass, UserAgents.format(userAgent), config); }
/** * @deprecated Use {@link #build(Class, UserAgent)}. */ @Deprecated public final <T> T build(Class<T> serviceClass, String userAgent) { return build(serviceClass, UserAgents.tryParse(userAgent)); }
/** * Parses the given string into a {@link UserAgent} or throws an {@link IllegalArgumentException} if no correctly * formatted primary user agent can be found. Incorrectly formatted informational agents are omitted. * <p> * Valid user agent strings loosely follow RFC 7230 (https://tools.ietf.org/html/rfc7230#section-3.2.6). */ public static UserAgent parse(String userAgent) { Preconditions.checkNotNull(userAgent, "userAgent must not be null"); return parseInternal(userAgent, false /* strict */); }