Class GravatarAvatarRequest
java.lang.Object
com.github.natche.gravatarjavaclient.avatar.GravatarAvatarRequest
A class for building a Gravatar Avatar request, requesting the resource, and saving
the resource to a
File or a BufferedImage.
Read the official Avatar image request API docs here.-
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whether the provided object is equal to this request.static GravatarAvatarRequestConstructs and returns a new GravatarAvatarRequest.static GravatarAvatarRequestConstructs and returns a new GravatarAvatarRequest.Reads from the URL constructed from the current state of this request usinggetRequestUrl().Returns the default image type this request will use.Returns the default image URL.getHash()Returns the hash for this request.Returns the protocol this request will use.Returns the rating this request will use.Returns the URL for requesting the Avatar based on the current state of this.Returns whether this request should append the jpg suffix to the hash.intgetSize()Returns the size of the image this request should return.Returns whether this request will use full URL parameters.inthashCode()Returns a hashcode for this request.booleanSaves the image obtained from the URL constructed from this to the provided file as a jpg.booleanSaves the image obtained from the URL constructed from this to the provided file as a png.booleanSaves the image obtained from the URL constructed from this to the provided file.setDefaultImageType(GravatarDefaultImageType imageType) Sets the default image type this request will use.setDefaultImageUrl(String defaultImageUrl) Sets the default image URL.setForceDefaultImage(GravatarForceDefaultImage forceDefaultImage) Sets whether this request should force the default image.setProtocol(GravatarProtocol protocol) Sets the protocol this request will use.setRating(GravatarRating rating) Sets the rating this request will use.setShouldAppendJpgSuffix(GravatarUseJpgSuffix useJpgSuffix) Returns whether this request should append the jpg suffix to the hash.setSize(int imageSize) Sets the size of the image this request should return.setUseFullUrlParameters(GravatarUseFullUrlParameters useFullUrlParameters) Sets whether this request should use full URL parameters.Returns whether this request should force the default image.toString()Returns a string representation of this request.
-
Method Details
-
fromEmail
Constructs and returns a new GravatarAvatarRequest.- Parameters:
email- the email for this request- Throws:
NullPointerException- if the provided email is nullIllegalArgumentException- if the provided email is empty or not a valid email address
-
fromHash
Constructs and returns a new GravatarAvatarRequest.- Parameters:
hash- the hash for this request- Throws:
NullPointerException- if the provided hash is nullIllegalArgumentException- if the provided hash is empty
-
getHash
Returns the hash for this request.- Returns:
- the hash for this request
-
setDefaultImageUrl
Sets the default image URL. Note,defaultImageTypeis set tonullif this method is invoked.Conditions which must be met for the Gravatar API endpoint to return a default image:
- The image must be publicly available (e.g. cannot be on an intranet, on a local development machine, behind HTTP Auth or some other firewall etc). Default images are passed through a security scan to avoid malicious content.
- The image must be accessible via HTTP or HTTPS on the standard ports, 80 and 443, respectively.
- The image must have a recognizable image extension (jpg, jpeg, gif, png, heic)
- The image must not include a query string (if it does, it will be ignored)
- Parameters:
defaultImageUrl- the default image URL- Returns:
- this builder
- Throws:
NullPointerException- if the provided image URL is nullIllegalArgumentException- if the provided image URL is empty or invalid
-
getDefaultImageUrl
Returns the default image URL.- Returns:
- the default image URL
-
setUseFullUrlParameters
@CanIgnoreReturnValue public GravatarAvatarRequest setUseFullUrlParameters(GravatarUseFullUrlParameters useFullUrlParameters) Sets whether this request should use full URL parameters.- Parameters:
useFullUrlParameters- whether this request should use full URL parameters- Returns:
- this builder
- Throws:
NullPointerException- if the provided value is null
-
getUseFullUrlParameters
Returns whether this request will use full URL parameters.- Returns:
- whether this request will use full URL parameters
-
setProtocol
Sets the protocol this request will use.- Parameters:
protocol- the protocol- Returns:
- this builder
- Throws:
NullPointerException- if the provided protocol is null
-
getProtocol
Returns the protocol this request will use.- Returns:
- the protocol this request will use
-
setDefaultImageType
@CanIgnoreReturnValue public GravatarAvatarRequest setDefaultImageType(GravatarDefaultImageType imageType) Sets the default image type this request will use. NotedefaultImageUrlis set tonullafter setting the internal state of this request to the provided type.- Parameters:
imageType- the default image type this request will use- Returns:
- this builder
- Throws:
NullPointerException- if the provided image type is null
-
getDefaultImageType
Returns the default image type this request will use.- Returns:
- the default image type this request will use
-
setForceDefaultImage
@CanIgnoreReturnValue public GravatarAvatarRequest setForceDefaultImage(GravatarForceDefaultImage forceDefaultImage) Sets whether this request should force the default image.- Parameters:
forceDefaultImage- whether this request should force the default image- Returns:
- this builder
- Throws:
NullPointerException- if the provided value is null
-
shouldForceDefaultImage
Returns whether this request should force the default image.- Returns:
- whether this request should force the default image
-
setRating
Sets the rating this request will use. If the resulting image's rating is above this rating, the default image will be used.- Parameters:
rating- the rating this request will use- Returns:
- this builder
- Throws:
NullPointerException- if the provided rating is null
-
getRating
Returns the rating this request will use.- Returns:
- the rating this request will use
-
setShouldAppendJpgSuffix
@CanIgnoreReturnValue public GravatarAvatarRequest setShouldAppendJpgSuffix(GravatarUseJpgSuffix useJpgSuffix) Returns whether this request should append the jpg suffix to the hash.- Parameters:
useJpgSuffix- whether this request should append the jpg suffix to the hash- Returns:
- this builder
- Throws:
NullPointerException- if the provided value is null
-
getShouldAppendJpgSuffix
Returns whether this request should append the jpg suffix to the hash.- Returns:
- whether this request should append the jpg suffix to the hash
-
setSize
Sets the size of the image this request should return.- Parameters:
imageSize- the size of the image this request should return- Returns:
- this builder
- Throws:
IllegalArgumentException- if the provided image size is not in the rangeIMAGE_SIZE_RANGE
-
getSize
public int getSize()Returns the size of the image this request should return.- Returns:
- the size of the image this request should return
-
getRequestUrl
Returns the URL for requesting the Avatar based on the current state of this.- Returns:
- the URL for requesting the Avatar based on the current state of this
-
getBufferedImage
Reads from the URL constructed from the current state of this request usinggetRequestUrl(). The content is encoded into a newBufferedImageand returned.- Returns:
- a new
BufferedImage - Throws:
GravatarJavaClientException- if an exception occurs reading from the URL
-
saveAsPng
Saves the image obtained from the URL constructed from this to the provided file as a png.- Parameters:
saveTo- the file to save the image to- Returns:
- whether the save operation was successful
- Throws:
NullPointerException- if the provided file is nullIllegalArgumentException- if the provided file is a directory
-
saveAsJpg
Saves the image obtained from the URL constructed from this to the provided file as a jpg.- Parameters:
saveTo- the file to save the image to- Returns:
- whether the save operation was successful
- Throws:
NullPointerException- if the provided file is nullIllegalArgumentException- if the provided file is a directory
-
saveTo
Saves the image obtained from the URL constructed from this to the provided file. Note, while the file could be named "Image.png", if "jpg" is provided as the format, the image will be of jpg format yet still named your provided name of "Image.png".- Parameters:
saveTo- the file to save the image toformat- the format to use; seeImageIO.getWriterFormatNames()- Returns:
- whether the save operation was successful
- Throws:
NullPointerException- if any parameter is nullIllegalArgumentException- if the provided format is empty or the provided file is a directory
-
toString
Returns a string representation of this request. -
hashCode
public int hashCode()Returns a hashcode for this request. -
equals
Returns whether the provided object is equal to this request.
-