Class HTTPResource
java.lang.Object
net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
net.shibboleth.ext.spring.resource.HTTPResource
- All Implemented Interfaces:
Component
,DestructableComponent
,IdentifiedComponent
,InitializableComponent
,Resource
,Aware
,BeanNameAware
,InitializingBean
,InputStreamSource
,Resource
- Direct Known Subclasses:
FileBackedHTTPResource
public class HTTPResource extends AbstractIdentifiedInitializableComponent implements Resource, BeanNameAware, InitializingBean, Resource
Resource for looking up HTTP URLs. Allows injection and therefore configuration of an Apache
HttpClient
. Code
based on OpenSAML HTTPMetadataResolver
and UrlResource
.-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
HTTPResource.ConnectionClosingInputStream
A wrapper around the entity contentInputStream
represented by anHttpResponse
that closes the stream and the HttpResponse whenHTTPResource.ConnectionClosingInputStream.close()
is invoked. -
Field Summary
Fields Modifier and Type Field Description private org.apache.http.client.HttpClient
httpClient
HTTP Client used to pull the resource.private HttpClientContextHandler
httpClientContextHandler
Optional handler to pre- and post-process context.private org.slf4j.Logger
log
Class logger.private URL
resourceURL
URL to the Resource. -
Constructor Summary
Constructors Constructor Description HTTPResource(org.apache.http.client.HttpClient client, String url)
Constructor.HTTPResource(org.apache.http.client.HttpClient client, URL url)
Constructor. -
Method Summary
Modifier and Type Method Description void
afterPropertiesSet()
protected org.apache.http.client.cache.HttpCacheContext
buildHttpClientContext()
Build theHttpCacheContext
instance which will be used to invoke theHttpClient
request.protected void
closeResponse(org.apache.http.HttpResponse response)
Close the HTTP response.long
contentLength()
HTTPResource
createRelative(String relativePath)
Based onUrlResource
.Resource
createRelativeResource(String relativePath)
boolean
exists()
String
getDescription()
File
getFile()
Based onUrlResource
.String
getFilename()
This implementation returns the name of the file that this URL refers to.InputStream
getInputStream()
protected org.apache.http.HttpResponse
getResourceHeaders()
Attempts to fetch only the headers for a given resource.protected String
getResponseHeader(String what)
Send a Head to the client and interrogate the response for a particular response header.URI
getURI()
URL
getURL()
boolean
isOpen()
boolean
isReadable()
long
lastModified()
protected void
reportCachingStatus(org.apache.http.client.cache.HttpCacheContext context)
Print out to the log whether we hit the apache cache or not.void
setBeanName(String name)
void
setHttpClientContextHandler(HttpClientContextHandler handler)
Set a handler to manipulate theHttpClientContext
.Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
doInitialize, getId, setId
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
destroy, doDestroy, initialize, isDestroyed, isInitialized
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.io.Resource
isFile, readableChannel
-
Field Details
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
httpClient
@Nonnull private final org.apache.http.client.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.http.client.HttpClient client, @Nonnull @NotEmpty @ParameterName(name="url") String url) throws IOExceptionConstructor.- 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.http.client.HttpClient client, @Nonnull @ParameterName(name="url") URL url) throws IOExceptionConstructor.- 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
-
setHttpClientContextHandler
Set a handler to manipulate theHttpClientContext
.- Parameters:
handler
- the handler to install- Since:
- 5.4.0
-
buildHttpClientContext
@Nonnull protected org.apache.http.client.cache.HttpCacheContext buildHttpClientContext()Build theHttpCacheContext
instance which will be used to invoke theHttpClient
request.- Returns:
- a new instance of
HttpCacheContext
-
reportCachingStatus
protected void reportCachingStatus(org.apache.http.client.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
- Specified by:
getInputStream
in interfaceInputStreamSource
- Specified by:
getInputStream
in interfaceResource
- Throws:
IOException
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Throws:
Exception
-
setBeanName
- Specified by:
setBeanName
in interfaceBeanNameAware
-
exists
public boolean exists() -
isReadable
public boolean isReadable()- Specified by:
isReadable
in interfaceResource
- Specified by:
isReadable
in interfaceResource
-
isOpen
public boolean isOpen() -
getURL
- Specified by:
getURL
in interfaceResource
- Specified by:
getURL
in interfaceResource
- Throws:
IOException
-
getURI
- Specified by:
getURI
in interfaceResource
- Specified by:
getURI
in interfaceResource
- Throws:
IOException
-
getFile
Based onUrlResource
.- Specified by:
getFile
in interfaceResource
- Specified by:
getFile
in interfaceResource
- Throws:
IOException
-
getResourceHeaders
Attempts to fetch only the headers for a given resource. If HEAD requests are unsupported then a more costly GET request is performed.- 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
- Specified by:
contentLength
in interfaceResource
- Specified by:
contentLength
in interfaceResource
- Throws:
IOException
-
lastModified
- Specified by:
lastModified
in interfaceResource
- Specified by:
lastModified
in interfaceResource
- Throws:
IOException
-
createRelative
Based onUrlResource
.- Specified by:
createRelative
in interfaceResource
- Throws:
IOException
-
createRelativeResource
- Specified by:
createRelativeResource
in interfaceResource
- Throws:
IOException
-
getFilename
This implementation returns the name of the file that this URL refers to.- Specified by:
getFilename
in interfaceResource
- Specified by:
getFilename
in interfaceResource
- See Also:
URL.getFile()
,File.getName()
-
getDescription
- Specified by:
getDescription
in interfaceResource
- Specified by:
getDescription
in interfaceResource
-
closeResponse
protected void closeResponse(@Nullable org.apache.http.HttpResponse response)Close the HTTP response.- Parameters:
response
- the HTTP response
-