Package org.restlet.data
Class ClientInfo
- java.lang.Object
-
- org.restlet.data.ClientInfo
-
public final class ClientInfo extends java.lang.Object
Client specific data related to a call. When extracted from a request, most of these data are directly taken from the underlying headers. There are some exceptions: agentAttributes and mainAgentProduct which are taken from the agent name (for example the "user-agent" header for HTTP requests).
As described by the HTTP specification, the "user-agent" can be seen as a ordered list of products name (ie a name and a version) and/or comments.
Each HTTP client (mainly browsers and web crawlers) defines its own "user-agent" header which can be seen as the "signature" of the client. Unfortunately, there is no rule to identify clearly a kind a client and its version (let's say Firefox 2.x, Internet Explorer IE 7.0, Opera, etc) according to its signature. Each signature follow its own rules which may vary according to the version of the client.
In order to help retrieving interesting data such as product name (Firefox, IE, etc), version, operating system, Restlet users has the ability to define their own way to extract data from the "user-agent" header. It is based on a list of templates declared in a file called "agent.properties" and located in the classpath in the sub directory "org/restlet/data". Each template describes a typical user-agent string and allows to use predefined variables that help to retrieve the content of the agent name, version, operating system.
The "user-agent" string is confronted to the each template from the beginning of the property file to the end. The loop stops at the first matched template.
Here is a sample of such template:
#Firefox for Windows Mozilla/{mozillaVersion} (Windows; U; {agentOs}; {osData}; rv:{releaseVersion}) Gecko/{geckoReleaseDate} {agentName}/{agentVersion}
This template matches the "user-agent" string of the Firefox client for windows:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20060918 Firefox/2.0
At this time, six predefined variables are used:
list of predefined variables Name Description agentName Name of the user agent (i.e.: Firefox) agentVersion Version of the user agent agentOs Operating system of the user agent agentComment Comment string, that is to say a sequence of characters enclosed "(", or ")" commentAttribute A sequence of characters enclosed by ";", "(", or ")" facultativeData A sequence of characters that can be empty
These variables are used to generate aProduct
instance with the main data (name, version, comment). This instance is accessible via thegetMainAgentProduct()
method. All other variables used in the template aims at catching a sequence of characters and are accessible via thegetAgentAttributes()
method.- Author:
- Jerome Louvel
-
-
Constructor Summary
Constructors Constructor Description ClientInfo()
Constructor.ClientInfo(java.util.List<? extends Variant> variants)
Constructor from a list of variants.ClientInfo(MediaType mediaType)
Constructor from a media type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(Metadata... metadata)
Updates the client preferences to accept the given metadata (media types, character sets, etc.) with a 1.0 quality in addition to existing ones.void
accept(Metadata metadata, float quality)
Updates the client preferences to accept the given metadata (media types, character sets, etc.) with a given quality in addition to existing ones.java.util.List<Preference<CharacterSet>>
getAcceptedCharacterSets()
Returns the modifiable list of character set preferences.java.util.List<Preference<Encoding>>
getAcceptedEncodings()
Returns the modifiable list of encoding preferences.java.util.List<Preference<Language>>
getAcceptedLanguages()
Returns the modifiable list of language preferences.java.util.List<Preference<MediaType>>
getAcceptedMediaTypes()
Returns the modifiable list of media type preferences.java.util.List<Preference<MediaType>>
getAcceptedPatches()
Returns the modifiable list of patch preferences.java.lang.String
getAddress()
Returns the immediate client's IP address.java.lang.String
getAgent()
Returns the agent name (ex: "Restlet-Framework/2.0").java.util.Map<java.lang.String,java.lang.String>
getAgentAttributes()
Returns a list of attributes taken from the name of the user agent.java.lang.String
getAgentName()
Returns the name of the user agent.java.util.List<Product>
getAgentProducts()
Returns the list of product tokens from the user agent name.java.lang.String
getAgentVersion()
Returns the version of the user agent.java.util.List<java.security.cert.Certificate>
getCertificates()
Returns the client certificates.java.lang.String
getCipherSuite()
Returns the SSL Cipher Suite, if available and accessible.java.util.List<Expectation>
getExpectations()
Returns the client expectations.java.util.List<java.lang.String>
getForwardedAddresses()
Returns the list of forwarded IP addresses.java.lang.String
getFrom()
Returns the email address of the human user controlling the user agent.Product
getMainAgentProduct()
Returns a Product object based on the name of the user agent.int
getPort()
Returns the port number which sent the call.CharacterSet
getPreferredCharacterSet(java.util.List<CharacterSet> supported)
Returns the preferred character set among a list of supported ones, based on the client preferences.Encoding
getPreferredEncoding(java.util.List<Encoding> supported)
Returns the preferred encoding among a list of supported ones, based on the client preferences.Language
getPreferredLanguage(java.util.List<Language> supported)
Returns the preferred language among a list of supported ones, based on the client preferences.MediaType
getPreferredMediaType(java.util.List<MediaType> supported)
Returns the preferred media type among a list of supported ones, based on the client preferences.static <T extends Metadata>
TgetPreferredMetadata(java.util.List<T> supported, java.util.List<Preference<T>> preferences)
Returns the preferred metadata taking into account both metadata supported by the server and client preferences.MediaType
getPreferredPatch(java.util.List<MediaType> supported)
Returns the preferred patch among a list of supported ones, based on the client preferences.java.util.List<java.security.Principal>
getPrincipals()
Returns the additional client principals.java.util.List<Role>
getRoles()
Returns the authenticated user roles.java.lang.String
getUpstreamAddress()
Returns the IP address of the upstream client component.User
getUser()
Returns the authenticated user.boolean
isAuthenticated()
Indicates if the identifier or principal has been authenticated.void
setAcceptedCharacterSets(java.util.List<Preference<CharacterSet>> acceptedCharacterSets)
Sets the character set preferences.void
setAcceptedEncodings(java.util.List<Preference<Encoding>> acceptedEncodings)
Sets the encoding preferences.void
setAcceptedLanguages(java.util.List<Preference<Language>> acceptedLanguages)
Sets the language preferences.void
setAcceptedMediaTypes(java.util.List<Preference<MediaType>> acceptedMediaTypes)
Sets the media type preferences.void
setAcceptedPatches(java.util.List<Preference<MediaType>> acceptedPatches)
Sets the patch preferences.void
setAddress(java.lang.String address)
Sets the client's IP address.void
setAgent(java.lang.String agent)
Sets the agent name (ex: "Restlet-Framework/2.0").void
setAgentAttributes(java.util.Map<java.lang.String,java.lang.String> agentAttributes)
Sets a list of attributes taken from the name of the user agent.void
setAgentProducts(java.util.List<Product> agentProducts)
Sets the list of product tokens from the user agent name.void
setAuthenticated(boolean authenticated)
Indicates if the identifier or principal has been authenticated.void
setCertificates(java.util.List<java.security.cert.Certificate> certificates)
Sets the new client certificates.void
setCipherSuite(java.lang.String cipherSuite)
Sets the SSL Cipher Suite, if available and accessible.void
setExpectations(java.util.List<Expectation> expectations)
Sets the client expectations.void
setForwardedAddresses(java.util.List<java.lang.String> forwardedAddresses)
Sets the list of forwarded IP addresses.void
setFrom(java.lang.String from)
Sets the email address of the human user controlling the user agent.void
setPort(int port)
Sets the port number which sent the call.void
setPrincipals(java.util.List<java.security.Principal> principals)
Sets the additional client principals.void
setRoles(java.util.List<Role> roles)
Sets the authenticated user roles.void
setUser(User user)
Sets the authenticated user.
-
-
-
Constructor Detail
-
ClientInfo
public ClientInfo()
Constructor.
-
ClientInfo
public ClientInfo(java.util.List<? extends Variant> variants)
Constructor from a list of variants. Note that only media types are taken into account.- Parameters:
variants
- The variants corresponding to the accepted media types.
-
ClientInfo
public ClientInfo(MediaType mediaType)
Constructor from a media type.- Parameters:
mediaType
- The preferred media type.
-
-
Method Detail
-
getPreferredMetadata
public static <T extends Metadata> T getPreferredMetadata(java.util.List<T> supported, java.util.List<Preference<T>> preferences)
Returns the preferred metadata taking into account both metadata supported by the server and client preferences.- Parameters:
supported
- The metadata supported by the server.preferences
- The client preferences.- Returns:
- The preferred metadata.
-
accept
public void accept(Metadata... metadata)
Updates the client preferences to accept the given metadata (media types, character sets, etc.) with a 1.0 quality in addition to existing ones.- Parameters:
metadata
- The metadata to accept.
-
accept
public void accept(Metadata metadata, float quality)
Updates the client preferences to accept the given metadata (media types, character sets, etc.) with a given quality in addition to existing ones.- Parameters:
metadata
- The metadata to accept.quality
- The quality to set.
-
getAcceptedCharacterSets
public java.util.List<Preference<CharacterSet>> getAcceptedCharacterSets()
Returns the modifiable list of character set preferences. Creates a new instance if no one has been set.
Note that when used with HTTP connectors, this property maps to the "Accept-Charset" header.- Returns:
- The character set preferences.
-
getAcceptedEncodings
public java.util.List<Preference<Encoding>> getAcceptedEncodings()
Returns the modifiable list of encoding preferences. Creates a new instance if no one has been set.
Note that when used with HTTP connectors, this property maps to the "Accept-Encoding" header.- Returns:
- The encoding preferences.
-
getAcceptedLanguages
public java.util.List<Preference<Language>> getAcceptedLanguages()
Returns the modifiable list of language preferences. Creates a new instance if no one has been set.
Note that when used with HTTP connectors, this property maps to the "Accept-Language" header.- Returns:
- The language preferences.
-
getAcceptedMediaTypes
public java.util.List<Preference<MediaType>> getAcceptedMediaTypes()
Returns the modifiable list of media type preferences. Creates a new instance if no one has been set.
Note that when used with HTTP connectors, this property maps to the "Accept" header.- Returns:
- The media type preferences.
-
getAcceptedPatches
public java.util.List<Preference<MediaType>> getAcceptedPatches()
Returns the modifiable list of patch preferences. Creates a new instance if no one has been set.
Note that when used with HTTP connectors, this property maps to the "Accept-Patch" header.- Returns:
- The patch preferences.
-
getAddress
public java.lang.String getAddress()
Returns the immediate client's IP address. If the real client is separated from the server by a proxy server, this will return the IP address of the proxy.- Returns:
- The immediate client's IP address.
- See Also:
getUpstreamAddress()
,getForwardedAddresses()
-
getAgent
public java.lang.String getAgent()
Returns the agent name (ex: "Restlet-Framework/2.0"). Note that when used with HTTP connectors, this property maps to the "User-Agent" header.- Returns:
- The agent name.
-
getAgentAttributes
public java.util.Map<java.lang.String,java.lang.String> getAgentAttributes()
Returns a list of attributes taken from the name of the user agent.- Returns:
- A list of attributes taken from the name of the user agent.
- See Also:
getAgent()
-
getAgentName
public java.lang.String getAgentName()
Returns the name of the user agent.- Returns:
- The name of the user agent.
- See Also:
getAgent()
-
getAgentProducts
public java.util.List<Product> getAgentProducts()
Returns the list of product tokens from the user agent name.- Returns:
- The list of product tokens from the user agent name.
- See Also:
getAgent()
-
getAgentVersion
public java.lang.String getAgentVersion()
Returns the version of the user agent.- Returns:
- The version of the user agent.
- See Also:
getAgent()
-
getCertificates
public java.util.List<java.security.cert.Certificate> getCertificates()
Returns the client certificates. Those certificates are available when a request is received via an HTTPS connection, corresponding to the SSL/TLS certificates.- Returns:
- The client certificates.
- See Also:
SSLSession.getPeerCertificates()
-
getCipherSuite
public java.lang.String getCipherSuite()
Returns the SSL Cipher Suite, if available and accessible.- Returns:
- The SSL Cipher Suite, if available and accessible.
- See Also:
SSLSession.getCipherSuite()
-
getExpectations
public java.util.List<Expectation> getExpectations()
Returns the client expectations.- Returns:
- The client expectations.
-
getForwardedAddresses
public java.util.List<java.lang.String> getForwardedAddresses()
Returns the list of forwarded IP addresses. This is useful when the user agent is separated from the origin server by a chain of intermediary components. Creates a new instance if no one has been set.
The first address is the one of the immediate client component and the last address should correspond to the origin client (frequently a user agent).
This information is only safe for intermediary components within your local network. Other addresses could easily be changed by setting a fake header and should not be trusted for serious security checks.
Note that your HTTP server connectors need to have a special "useForwardedForHeader" parameter explicitly set to "true" in order to activate this feature, due to potential security issues.- Returns:
- The list of forwarded IP addresses.
- See Also:
getUpstreamAddress()
, Wikipedia page for the "X-Forwarded-For" HTTP header
-
getFrom
public java.lang.String getFrom()
Returns the email address of the human user controlling the user agent. Default value is null.- Returns:
- The email address of the human user controlling the user agent.
-
getMainAgentProduct
public Product getMainAgentProduct()
Returns a Product object based on the name of the user agent.- Returns:
- A Product object based on name of the user agent.
-
getPort
public int getPort()
Returns the port number which sent the call. If no port is specified, -1 is returned.- Returns:
- The port number which sent the call.
-
getPreferredCharacterSet
public CharacterSet getPreferredCharacterSet(java.util.List<CharacterSet> supported)
Returns the preferred character set among a list of supported ones, based on the client preferences.- Parameters:
supported
- The supported character sets.- Returns:
- The preferred character set.
-
getPreferredEncoding
public Encoding getPreferredEncoding(java.util.List<Encoding> supported)
Returns the preferred encoding among a list of supported ones, based on the client preferences.- Parameters:
supported
- The supported encodings.- Returns:
- The preferred encoding.
-
getPreferredLanguage
public Language getPreferredLanguage(java.util.List<Language> supported)
Returns the preferred language among a list of supported ones, based on the client preferences.- Parameters:
supported
- The supported languages.- Returns:
- The preferred language.
-
getPreferredMediaType
public MediaType getPreferredMediaType(java.util.List<MediaType> supported)
Returns the preferred media type among a list of supported ones, based on the client preferences.- Parameters:
supported
- The supported media types.- Returns:
- The preferred media type.
-
getPreferredPatch
public MediaType getPreferredPatch(java.util.List<MediaType> supported)
Returns the preferred patch among a list of supported ones, based on the client preferences.- Parameters:
supported
- The supported patches.- Returns:
- The preferred patch.
-
getPrincipals
public java.util.List<java.security.Principal> getPrincipals()
Returns the additional client principals. Note thatgetUser()
andgetRoles()
methods already return user and role principals.- Returns:
- The additional client principals.
-
getRoles
public java.util.List<Role> getRoles()
Returns the authenticated user roles.- Returns:
- The authenticated user roles.
-
getUpstreamAddress
public java.lang.String getUpstreamAddress()
Returns the IP address of the upstream client component. In general this will correspond the the user agent IP address. This is useful if there are intermediary components like proxies and load balancers. If the supportinggetForwardedAddresses()
method returns a non empty list, the IP address will be the first element. Otherwise, the value ofgetAddress()
will be returned.
Note that your HTTP server connectors need to have a special "useForwardedForHeader" parameter explicitly set to "true" in order to activate this feature, due to potential security issues.- Returns:
- The most upstream IP address.
- See Also:
getAddress()
,getForwardedAddresses()
-
getUser
public User getUser()
Returns the authenticated user.- Returns:
- The authenticated user.
-
isAuthenticated
public boolean isAuthenticated()
Indicates if the identifier or principal has been authenticated. The application is responsible for updating this property, relying on aAuthenticator
or manually.- Returns:
- True if the identifier or principal has been authenticated.
-
setAcceptedCharacterSets
public void setAcceptedCharacterSets(java.util.List<Preference<CharacterSet>> acceptedCharacterSets)
Sets the character set preferences. Note that when used with HTTP connectors, this property maps to the "Accept-Charset" header.- Parameters:
acceptedCharacterSets
- The character set preferences.
-
setAcceptedEncodings
public void setAcceptedEncodings(java.util.List<Preference<Encoding>> acceptedEncodings)
Sets the encoding preferences. Note that when used with HTTP connectors, this property maps to the "Accept-Encoding" header.- Parameters:
acceptedEncodings
- The encoding preferences.
-
setAcceptedLanguages
public void setAcceptedLanguages(java.util.List<Preference<Language>> acceptedLanguages)
Sets the language preferences. Note that when used with HTTP connectors, this property maps to the "Accept-Language" header.- Parameters:
acceptedLanguages
- The language preferences.
-
setAcceptedMediaTypes
public void setAcceptedMediaTypes(java.util.List<Preference<MediaType>> acceptedMediaTypes)
Sets the media type preferences. Note that when used with HTTP connectors, this property maps to the "Accept" header.- Parameters:
acceptedMediaTypes
- The media type preferences.
-
setAcceptedPatches
public void setAcceptedPatches(java.util.List<Preference<MediaType>> acceptedPatches)
Sets the patch preferences. Note that when used with HTTP connectors, this property maps to the "Accept-Patch" header.- Parameters:
acceptedPatches
- The media type preferences.
-
setAddress
public void setAddress(java.lang.String address)
Sets the client's IP address.- Parameters:
address
- The client's IP address.
-
setAgent
public void setAgent(java.lang.String agent)
Sets the agent name (ex: "Restlet-Framework/2.0"). Note that when used with HTTP connectors, this property maps to the "User-Agent" header.- Parameters:
agent
- The agent name.
-
setAgentAttributes
public void setAgentAttributes(java.util.Map<java.lang.String,java.lang.String> agentAttributes)
Sets a list of attributes taken from the name of the user agent.- Parameters:
agentAttributes
- A list of attributes taken from the name of the user agent.
-
setAgentProducts
public void setAgentProducts(java.util.List<Product> agentProducts)
Sets the list of product tokens from the user agent name.- Parameters:
agentProducts
- The list of product tokens from the user agent name.
-
setAuthenticated
public void setAuthenticated(boolean authenticated)
Indicates if the identifier or principal has been authenticated. The application is responsible for updating this property, relying on aAuthenticator
or manually.- Parameters:
authenticated
- True if the identifier or principal has been authenticated.
-
setCertificates
public void setCertificates(java.util.List<java.security.cert.Certificate> certificates)
Sets the new client certificates.- Parameters:
certificates
- The client certificates.- See Also:
getCertificates()
-
setCipherSuite
public void setCipherSuite(java.lang.String cipherSuite)
Sets the SSL Cipher Suite, if available and accessible.- Parameters:
cipherSuite
- The SSL Cipher Suite, if available and accessible.
-
setExpectations
public void setExpectations(java.util.List<Expectation> expectations)
Sets the client expectations.- Parameters:
expectations
- The client expectations.
-
setForwardedAddresses
public void setForwardedAddresses(java.util.List<java.lang.String> forwardedAddresses)
Sets the list of forwarded IP addresses.- Parameters:
forwardedAddresses
- The list of forwarded IP addresses.- See Also:
getForwardedAddresses()
-
setFrom
public void setFrom(java.lang.String from)
Sets the email address of the human user controlling the user agent.- Parameters:
from
- The email address of the human user controlling the user agent.
-
setPort
public void setPort(int port)
Sets the port number which sent the call.- Parameters:
port
- The port number which sent the call.
-
setPrincipals
public void setPrincipals(java.util.List<java.security.Principal> principals)
Sets the additional client principals.- Parameters:
principals
- The additional client principals.- See Also:
getPrincipals()
-
setRoles
public void setRoles(java.util.List<Role> roles)
Sets the authenticated user roles.- Parameters:
roles
- The authenticated user roles.
-
setUser
public void setUser(User user)
Sets the authenticated user.- Parameters:
user
- The authenticated user.
-
-