Freigeben über


UserAgentPolicy Class

  • java.lang.Object
    • com.azure.core.http.policy.UserAgentPolicy

Implements

public class UserAgentPolicy
implements HttpPipelinePolicy

The UserAgentPolicy class is an implementation of the HttpPipelinePolicy interface. This policy is used to add a "User-Agent" header to each HttpRequest.

This class is useful when you need to add a specific "User-Agent" header for all requests in a pipeline. It ensures that the "User-Agent" header is set correctly for each request. The "User-Agent" header is used to provide the server with information about the software used by the client.

Code sample:

In this example, a UserAgentPolicy is created with a "User-Agent" header value of "MyApp/1.0". Once added to the pipeline, requests will have their "User-Agent" header set to "MyApp/1.0" by the UserAgentPolicy.

UserAgentPolicy userAgentPolicy = new UserAgentPolicy("MyApp/1.0");

Field Summary

Modifier and Type Field and Description
static final String APPEND_USER_AGENT_CONTEXT_KEY

Key for Context to add a value which will be appended to the User-Agent supplied in this policy in an ad-hoc manner.

static final String OVERRIDE_USER_AGENT_CONTEXT_KEY

Key for Context to add a value which will override the User-Agent supplied in this policy in an ad-hoc manner.

Constructor Summary

Constructor Description
UserAgentPolicy()

Creates a UserAgentPolicy with a default user agent string.

UserAgentPolicy(String userAgent)

Creates a UserAgentPolicy with userAgent as the header value.

UserAgentPolicy(String sdkName, String sdkVersion, Configuration configuration, ServiceVersion version)

Deprecated

Creates a UserAgentPolicy with the sdkName and sdkVersion in the User-Agent header value.

UserAgentPolicy(String applicationId, String sdkName, String sdkVersion, Configuration configuration)

Creates a UserAgentPolicy with the sdkName and sdkVersion in the User-Agent header value.

Method Summary

Modifier and Type Method and Description
Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next)

Updates the "User-Agent" header with the value supplied in the policy.

HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)

Updates the "User-Agent" header with the value supplied in the policy synchronously.

Methods inherited from java.lang.Object

Field Details

APPEND_USER_AGENT_CONTEXT_KEY

public static final String APPEND_USER_AGENT_CONTEXT_KEY

Key for Context to add a value which will be appended to the User-Agent supplied in this policy in an ad-hoc manner.

OVERRIDE_USER_AGENT_CONTEXT_KEY

public static final String OVERRIDE_USER_AGENT_CONTEXT_KEY

Key for Context to add a value which will override the User-Agent supplied in this policy in an ad-hoc manner.

Constructor Details

UserAgentPolicy

public UserAgentPolicy()

Creates a UserAgentPolicy with a default user agent string.

UserAgentPolicy

public UserAgentPolicy(String userAgent)

Creates a UserAgentPolicy with userAgent as the header value. If userAgent is null, then the default user agent value is used.

Parameters:

userAgent - The user agent string to add to request headers.

UserAgentPolicy

@Deprecated
public UserAgentPolicy(String sdkName, String sdkVersion, Configuration configuration, ServiceVersion version)

Deprecated

Creates a UserAgentPolicy with the sdkName and sdkVersion in the User-Agent header value.

If the passed configuration contains true for AZURE_TELEMETRY_DISABLED the platform information won't be included in the user agent.

Parameters:

sdkName - Name of the client library.
sdkVersion - Version of the client library.
configuration - Configuration store that will be checked for PROPERTY_AZURE_TELEMETRY_DISABLED. If null is passed the getGlobalConfiguration() will be checked.
version - ServiceVersion of the service to be used when making requests.

UserAgentPolicy

public UserAgentPolicy(String applicationId, String sdkName, String sdkVersion, Configuration configuration)

Creates a UserAgentPolicy with the sdkName and sdkVersion in the User-Agent header value.

If the passed configuration contains true for AZURE_TELEMETRY_DISABLED the platform information won't be included in the user agent.

Parameters:

applicationId - User specified application Id.
sdkName - Name of the client library.
sdkVersion - Version of the client library.
configuration - Configuration store that will be checked for PROPERTY_AZURE_TELEMETRY_DISABLED. If null is passed the getGlobalConfiguration() will be checked.

Method Details

process

public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next)

Updates the "User-Agent" header with the value supplied in the policy.

The context will be checked for Override-User-Agent and Append-User-Agent. Override-User-Agent will take precedence over the value supplied in the policy, Append-User-Agent will be appended to the value supplied in the policy.

Parameters:

context - request context
next - The next policy to invoke.

Returns:

A publisher that initiates the request upon subscription and emits a response on completion.

processSync

public HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)

Updates the "User-Agent" header with the value supplied in the policy synchronously.

The context will be checked for Override-User-Agent and Append-User-Agent. Override-User-Agent will take precedence over the value supplied in the policy, Append-User-Agent will be appended to the value supplied in the policy.

Parameters:

context - request context
next - The next policy to invoke.

Returns:

A response.

Applies to