C
- the type of the configuration classpublic abstract class HttpExternalImageProvider<C> extends ImageProvider
Constructor and Description |
---|
HttpExternalImageProvider(String identifier,
String pathToDefaultImage,
long disablingDuration)
Create a new external image provider
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertNotDisabled(String imageIdentifier,
C configuration)
Test that the image provider was not disabled.
|
void |
cleanup()
Cleanup held resources.
|
protected void |
disableTemporarily(String reason,
C configuration)
Temporarily disable the image provider
|
protected void |
enable()
Re-enable the provider if it is currently temporarily disabled.
|
protected ByteArrayImage |
extractImageFromEntity(org.apache.http.HttpEntity entity,
Date imageLastModified)
Extract the image from the response entity.
|
protected abstract C |
getConfiguration() |
protected int |
getConnectionManagerTimeout() |
protected int |
getConnectionTimeout() |
String |
getDefaultImageVersionString()
The version string of the default image.
|
protected org.apache.http.client.config.RequestConfig |
getDefaultRequestConfig()
Prepare and return a request configuration that is used as default for all requests started
by the shared HttpClient instance.
|
protected Long |
getDisabledUntilTimestamp() |
protected org.apache.http.impl.client.CloseableHttpClient |
getHttpClient()
Get the HTTP client to be used for all requests.
|
protected abstract String |
getImageURL(String imageIdentifier,
C configuration)
Get the URL under which the image with the given identifier can be downloaded
|
protected Date |
getLastModifiedHeaderValue(org.apache.http.client.methods.CloseableHttpResponse response)
Get last modified header value as date, will fall back to current time if header is not set
|
protected Set<Integer> |
getNotDisablingStatusCodes() |
protected org.apache.http.protocol.HttpContext |
getRequestContext(URI imageUrl,
String imageIdentifier,
C configuration)
Get request context to be used when downloading the given image.
|
protected List<org.apache.http.Header> |
getRequestHeaders(URI imageUrl,
String imageIdentifier,
C configuration)
Get the HTTP request headers to add when downloading the given image.
|
protected int |
getSocketTimeout() |
boolean |
hasDefaultImage(String imageIdentifier)
Return whether the provider has a default image for the case when there is no image for a
given identifier.
|
protected void |
imageNotDownloaded(String imageIdentifier)
Invoked when the image URL was null or the image could not be downloaded and the cause was
not one that would disable the provider.
|
protected void |
imageSuccessfullyDownloaded(String imageIdentifier,
Date lastModificationDate,
org.apache.http.client.methods.CloseableHttpResponse response)
Invoked by loadImage after successfully parsing and extracting the image.
|
protected boolean |
isDisabled(C configuration)
Test whether the image provider was temporarily disabled.
|
protected boolean |
isDisablingException(IOException e)
Determines if a caught IO exception is one which should disable the image provider
temporarily.
|
boolean |
isExternalProvider()
Return whether this provider loads image from an external resource (like another server) and
not from the local database or file system.
|
boolean |
isProvideDefaultImageIfDisabled() |
Image |
loadDefaultImage()
Load the default image.
|
Image |
loadImage(String imageIdentifier)
Load the unscaled image
|
protected org.apache.http.impl.client.CloseableHttpClient |
prepareHttpClient(org.apache.http.impl.client.HttpClientBuilder builder)
Prepare the HTTP client that should be used by all requests.
|
protected void |
setHttpClient(org.apache.http.impl.client.CloseableHttpClient httpClient,
boolean replace)
Set the HTTP client instance that should be used for all requests.
|
void |
setProvideDefaultImageIfDisabled(boolean provideDefaultImageIfDisabled)
Set whether the default image can be returned even if the provider is temporarily disabled.
|
protected void |
validateContentType(String contentType)
Validate that the content type of the response is an image mime type.
|
protected void |
validateStatusCode(org.apache.http.StatusLine statusLine)
Checks the status code of a response for an image request.
|
canLoad, getDefaultImageMimeType, getDefaultImageResourceLastModificationTimestamp, getIdentifier, getTimeToLive, getVersionString, imageChanged, isAuthorized
public HttpExternalImageProvider(String identifier, String pathToDefaultImage, long disablingDuration)
identifier
- The identifier of the provider. The identifier has to be unique among all
providers that are registered for an image type.pathToDefaultImage
- If the path starts with file: it is interpreted as a file URI otherwise it is
interpreted as the name of a resource containing the default image. This resource
will be loaded with the class loader of this class. If null, there will be no
default image.disablingDuration
- the amount of milliseconds to disable the image provider temporarily in certain
situations e.g. if the external server is unreachable. If 0 or less the provider
is never disabled.protected void assertNotDisabled(String imageIdentifier, C configuration) throws ImageNotFoundException
imageIdentifier
- the Id of the image that should be loaded. Only needed to create a useful
exception.configuration
- the current configuration of the image provider, can be null. Default
implementation ignores this parameter.ImageTemporarilyNotFoundException
- in case the provider is disabled and the provided image can thus not be loadedImageNotFoundException
public void cleanup()
protected void disableTemporarily(String reason, C configuration)
reason
- The reason that caused the disabling of the providerconfiguration
- the current configuration of the image provider, can be null. Default
implementation ignores this parameter.protected void enable()
protected ByteArrayImage extractImageFromEntity(org.apache.http.HttpEntity entity, Date imageLastModified) throws ExternalImageProviderServiceException, ImageNotFoundException
entity
- the entity to handleimageLastModified
- the date of the last modification of the image contained in the entity. This could
for instance be the value of the Last-Modified header.ExternalImageProviderServiceException
- in case validateContentType(String)
threw this exceptionImageNotFoundException
- in case the entity is not a valid imageprotected abstract C getConfiguration()
protected int getConnectionManagerTimeout()
protected int getConnectionTimeout()
public String getDefaultImageVersionString() throws ImageNotFoundException
ImageProvider
getDefaultImageVersionString
in class ImageProvider
ImageNotFoundException
- in case there is no default imageImageProvider.getVersionString(String)
protected org.apache.http.client.config.RequestConfig getDefaultRequestConfig()
getSocketTimeout()
,
getConnectionTimeout()
and getConnectionManagerTimeout()
.protected Long getDisabledUntilTimestamp()
protected org.apache.http.impl.client.CloseableHttpClient getHttpClient()
prepareHttpClient(HttpClientBuilder)
.protected abstract String getImageURL(String imageIdentifier, C configuration) throws ImageNotFoundException
imageIdentifier
- the ID of the image to downloadconfiguration
- the configuration of the image provider, can be nullImageNotFoundException
- can be thrown to indicate that image does not exist imageprotected Date getLastModifiedHeaderValue(org.apache.http.client.methods.CloseableHttpResponse response)
response
- the response of the image requestprotected Set<Integer> getNotDisablingStatusCodes()
protected org.apache.http.protocol.HttpContext getRequestContext(URI imageUrl, String imageIdentifier, C configuration)
imageUrl
- the URL pointing to the imageimageIdentifier
- the ID of the image to getconfiguration
- the configuration of the image provider, can be nullprotected List<org.apache.http.Header> getRequestHeaders(URI imageUrl, String imageIdentifier, C configuration)
imageUrl
- the URL pointing to the imageimageIdentifier
- the ID of the image to getconfiguration
- the configuration of the image provider, can be nullprotected int getSocketTimeout()
public boolean hasDefaultImage(String imageIdentifier)
ImageProvider
hasDefaultImage
in class ImageProvider
imageIdentifier
- the identifier for which no image was foundprotected void imageNotDownloaded(String imageIdentifier)
imageIdentifier
- the ID of the image that failedprotected void imageSuccessfullyDownloaded(String imageIdentifier, Date lastModificationDate, org.apache.http.client.methods.CloseableHttpResponse response)
imageIdentifier
- the ID of the imagelastModificationDate
- the last modification date of the as returned by
getLastModifiedHeaderValue(CloseableHttpResponse)
response
- the response that contained the imageprotected boolean isDisabled(C configuration)
configuration
- the current configuration of the image provider, can be null. Default
implementation ignores this parameter.protected boolean isDisablingException(IOException e)
ConnectTimeoutException
,
SocketTimeoutException
and SocketException
are considered as disabling
exceptions.e
- the exception to checkpublic boolean isExternalProvider()
ImageProvider
isExternalProvider
in class ImageProvider
public boolean isProvideDefaultImageIfDisabled()
public Image loadDefaultImage() throws ImageNotFoundException
ImageProvider
loadDefaultImage
in class ImageProvider
ImageNotFoundException
- in case there is no default image.public Image loadImage(String imageIdentifier) throws ImageNotFoundException, AuthorizationException
ImageProvider
loadImage
in class ImageProvider
imageIdentifier
- The identifier of the imageImageNotFoundException
- in case there is no image for the imageIdentifier or loading failedAuthorizationException
- in case the current user is not allowed to access the imageprotected org.apache.http.impl.client.CloseableHttpClient prepareHttpClient(org.apache.http.impl.client.HttpClientBuilder builder)
getDefaultRequestConfig()
.builder
- the HTTP client builderprotected void setHttpClient(org.apache.http.impl.client.CloseableHttpClient httpClient, boolean replace)
getHttpClient()
.httpClient
- the HTTP client to set. Can be null, to reset the client.replace
- if true the current HTTP client instance will be closed and replaced with the new
one. If false and there is a current HTTP client the current one is not updated.public void setProvideDefaultImageIfDisabled(boolean provideDefaultImageIfDisabled)
provideDefaultImageIfDisabled
- true to return the default imageprotected void validateContentType(String contentType) throws ExternalImageProviderServiceException
contentType
- the content typeExternalImageProviderServiceException
- in case the content type is not an image/* mime type. This exception will lead to
temporarily disabling the provider.protected void validateStatusCode(org.apache.http.StatusLine statusLine) throws ImageNotFoundException, ExternalImageProviderServiceException
statusLine
- the status line containing the status codeImageNotFoundException
- in case the status code is one of the codes in
getNotDisablingStatusCodes()
ExternalImageProviderServiceException
- in case the status code is not 200 and not one of the codes returned by
getNotDisablingStatusCodes()
. The image provider will be disabled
temporarily.Copyright © 2019 Communote team. All rights reserved.