Class HTTPResource
- All Implemented Interfaces:
Component,DestructableComponent,IdentifiedComponent,InitializableComponent,Resource,Aware,BeanNameAware,InitializingBean,InputStreamSource,Resource
- Direct Known Subclasses:
FileBackedHTTPResource
HttpClient. Code
based on OpenSAML HTTPMetadataResolver and UrlResource.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final org.apache.hc.client5.http.classic.HttpClientHTTP Client used to pull the resource.private HttpClientContextHandlerOptional handler to pre- and post-process context.private final org.slf4j.LoggerClass logger.private final URLURL to the Resource. -
Constructor Summary
ConstructorsConstructorDescriptionHTTPResource(org.apache.hc.client5.http.classic.HttpClient client, String url) Constructor.HTTPResource(org.apache.hc.client5.http.classic.HttpClient client, URL url) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected org.apache.hc.client5.http.cache.HttpCacheContextBuild theHttpCacheContextinstance which will be used to invoke theHttpClientrequest.protected voidcloseResponse(org.apache.hc.core5.http.ClassicHttpResponse response) Close the HTTP response.longDetermine the content length for this resource.createRelative(String relativePath) Based onUrlResource.createRelative(String relativePath, HttpClientContextHandler handler) Create a resource relative to us, but use the supplied handler.createRelativeResource(String relativePath) Create a resource relative to this resource.booleanexists()Return whether this resource actually exists in physical form.Return a description for this resource, to be used for error output when working with the resource.getFile()Return a File handle for this resource.Determine a filename for this resource, i.e.Return anInputStream.protected org.apache.hc.core5.http.HttpResponseAttempts to fetch only the headers for a given resource.protected StringgetResponseHeader(String what) Send a Head to the client and interrogate the response for a particular response header.getURI()Return a URI handle for this resource.getURL()Return a URL handle for this resource.booleanisFile()Determine whether this resource represents a file in a file system.booleanisOpen()Return whether this resource represents a handle with an open stream.booleanReturn whether the contents of this resource can be read, e.g.longDetermine the last-modified timestamp for this resource.protected voidreportCachingStatus(org.apache.hc.client5.http.cache.HttpCacheContext context) Print out to the log whether we hit the apache cache or not.voidsetBeanName(String name) voidSet a handler to manipulate theHttpClientContext.Methods inherited from class net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
doInitialize, ensureId, getId, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, setIdMethods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, doDestroy, initialize, isDestroyed, isInitializedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.core.io.Resource
getContentAsByteArray, getContentAsString, readableChannel
-
Field Details
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
httpClient
@Nonnull private final org.apache.hc.client5.http.classic.HttpClient httpClientHTTP Client used to pull the resource. -
resourceURL
URL to the Resource. -
httpClientContextHandler
Optional handler to pre- and post-process context.
-
-
Constructor Details
-
HTTPResource
public HTTPResource(@Nonnull @ParameterName(name="client") org.apache.hc.client5.http.classic.HttpClient client, @Nonnull @NotEmpty @ParameterName(name="url") String url) throws IOException Constructor.- Parameters:
client- the client we use to connect with.url- URL to the remote data- Throws:
IOException- if the URL was badly formed
-
HTTPResource
public HTTPResource(@Nonnull @ParameterName(name="") org.apache.hc.client5.http.classic.HttpClient client, @Nonnull @ParameterName(name="url") URL url) throws IOException Constructor.- Parameters:
client- the client we use to connect with.url- URL to the remote data- Throws:
IOException- if the URL was badly formed
-
-
Method Details
-
buildHttpClientContext
@Nonnull protected org.apache.hc.client5.http.cache.HttpCacheContext buildHttpClientContext()Build theHttpCacheContextinstance which will be used to invoke theHttpClientrequest.- Returns:
- a new instance of
HttpCacheContext
-
reportCachingStatus
protected void reportCachingStatus(@Nonnull org.apache.hc.client5.http.cache.HttpCacheContext context) Print out to the log whether we hit the apache cache or not. http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html- Parameters:
context- the context of the request
-
getInputStream
Return anInputStream.It is expected that each call creates a fresh stream.
This requirement is particularly important when you consider an API such as JavaMail, which needs to be able to read the stream multiple times when creating mail attachments. For such a use case, it is required that each
getInputStream()call returns a fresh stream.- Specified by:
getInputStreamin interfaceInputStreamSource- Specified by:
getInputStreamin interfaceResource- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
IOException- if the stream could not be opened
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception
-
setBeanName
- Specified by:
setBeanNamein interfaceBeanNameAware
-
exists
public boolean exists()Return whether this resource actually exists in physical form.This method performs a definitive existence check, whereas the existence of a
Resourcehandle only guarantees a valid descriptor handle. -
isFile
public boolean isFile()Determine whether this resource represents a file in a file system.A value of
truestrongly suggests (but does not guarantee) that aResource.getFile()call will succeed.This is conservatively
falseby default. -
isReadable
public boolean isReadable()Return whether the contents of this resource can be read, e.g. viaResource.getInputStream()orResource.getFile().Will be
truefor typical resource descriptors; note that actual content reading may still fail when attempted. However, a value offalseis a definitive indication that the resource content cannot be read.- Specified by:
isReadablein interfaceResource- Specified by:
isReadablein interfaceResource- Returns:
- whether the contents of this resource can be read.
- See Also:
-
isOpen
public boolean isOpen()Return whether this resource represents a handle with an open stream. If true, the InputStream cannot be read multiple times, and must be read and closed to avoid resource leaks.Will be
falsefor typical resource descriptors. -
getURL
Return a URL handle for this resource.- Specified by:
getURLin interfaceResource- Specified by:
getURLin interfaceResource- Returns:
- a URL handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
-
getURI
Return a URI handle for this resource.- Specified by:
getURIin interfaceResource- Specified by:
getURIin interfaceResource- Returns:
- a URI handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
-
getFile
Return a File handle for this resource. Based onUrlResource.- Specified by:
getFilein interfaceResource- Specified by:
getFilein interfaceResource- Returns:
- a File handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
getResourceHeaders
Attempts to fetch only the headers for a given resource. If HEAD requests are unsupported then a more costly GET request is performed. NOTE This method returns a closedClassicHttpResponse. See JSSH-25. It turns out that for the cases we need it this is OK and all three of them have regression tests. If they fail, this will be revisisted.- Returns:
- the response from the request
- Throws:
IOException- thrown if there is a problem contacting the resource
-
getResponseHeader
Send a Head to the client and interrogate the response for a particular response header.- Parameters:
what- the repsonse header to look at- Returns:
- the value of that response, or null if things failed
- Throws:
IOException- from lower levels.
-
contentLength
Determine the content length for this resource.- Specified by:
contentLengthin interfaceResource- Specified by:
contentLengthin interfaceResource- Returns:
- the content length for this resource.
- Throws:
IOException- if the resource cannot be resolved (in the file system or as some other known physical resource type)
-
lastModified
Determine the last-modified timestamp for this resource.- Specified by:
lastModifiedin interfaceResource- Specified by:
lastModifiedin interfaceResource- Returns:
- the last-modified timestamp for this resource.
- Throws:
IOException- if the resource cannot be resolved (in the file system or as some other known physical resource type)
-
createRelative
Based onUrlResource.- Specified by:
createRelativein interfaceResource- Throws:
IOException
-
createRelativeResource
Create a resource relative to this resource.- Specified by:
createRelativeResourcein interfaceResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- Throws:
IOException- if the relative resource cannot be determined
-
getFilename
Determine a filename for this resource, i.e. typically the last part of the path: for example, "myfile.txt". This implementation returns the name of the file that this URL refers to.- Specified by:
getFilenamein interfaceResource- Specified by:
getFilenamein interfaceResource- Returns:
nullif this type of resource does not have a filename, otherwise the file name.- See Also:
-
getDescription
Return a description for this resource, to be used for error output when working with the resource.Implementations are also encouraged to return this value from their
toStringmethod.- Specified by:
getDescriptionin interfaceResource- Specified by:
getDescriptionin interfaceResource- Returns:
- a description for this resource.
- See Also:
-
closeResponse
protected void closeResponse(@Nullable org.apache.hc.core5.http.ClassicHttpResponse response) Close the HTTP response.- Parameters:
response- the HTTP response