- 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 ApacheHttpClient. Code based on OpenSAMLHTTPMetadataResolverandUrlResource.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classHTTPResource.ConnectionClosingInputStreamA wrapper around the entity contentInputStreamrepresented by anHttpResponsethat closes the stream and the HttpResponse whenHTTPResource.ConnectionClosingInputStream.close()is invoked.
-
Field Summary
Fields Modifier and Type Field Description private org.apache.http.client.HttpClienthttpClientHTTP Client used to pull the resource.private HttpClientContextHandlerhttpClientContextHandlerOptional handler to pre- and post-process context.private org.slf4j.LoggerlogClass logger.private URLresourceURLURL 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()protected org.apache.http.client.cache.HttpCacheContextbuildHttpClientContext()Build theHttpCacheContextinstance which will be used to invoke theHttpClientrequest.protected voidcloseResponse(org.apache.http.HttpResponse response)Close the HTTP response.longcontentLength()HTTPResourcecreateRelative(String relativePath)Based onUrlResource.ResourcecreateRelativeResource(String relativePath)booleanexists()StringgetDescription()FilegetFile()Based onUrlResource.StringgetFilename()This implementation returns the name of the file that this URL refers to.InputStreamgetInputStream()protected org.apache.http.HttpResponsegetResourceHeaders()Attempts 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.URIgetURI()URLgetURL()booleanisOpen()booleanisReadable()longlastModified()protected voidreportCachingStatus(org.apache.http.client.cache.HttpCacheContext context)Print out to the log whether we hit the apache cache or not.voidsetBeanName(String name)voidsetHttpClientContextHandler(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 Detail
-
log
@Nonnull private final org.slf4j.Logger log
Class logger.
-
httpClient
@Nonnull private final org.apache.http.client.HttpClient httpClient
HTTP Client used to pull the resource.
-
resourceURL
@Nonnull private final URL resourceURL
URL to the Resource.
-
httpClientContextHandler
@Nullable private HttpClientContextHandler httpClientContextHandler
Optional handler to pre- and post-process context.
-
-
Constructor Detail
-
HTTPResource
public HTTPResource(@Nonnull @ParameterName(name="client") org.apache.http.client.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.http.client.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 Detail
-
setHttpClientContextHandler
public void setHttpClientContextHandler(@Nonnull HttpClientContextHandler handler)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 theHttpCacheContextinstance which will be used to invoke theHttpClientrequest.- 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
@Nonnull public InputStream getInputStream() throws IOException
- Specified by:
getInputStreamin interfaceInputStreamSource- Specified by:
getInputStreamin interfaceResource- Throws:
IOException
-
afterPropertiesSet
public void afterPropertiesSet() throws Exception- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception
-
setBeanName
public void setBeanName(String name)
- Specified by:
setBeanNamein interfaceBeanNameAware
-
exists
public boolean exists()
-
isReadable
public boolean isReadable()
- Specified by:
isReadablein interfaceResource- Specified by:
isReadablein interfaceResource
-
isOpen
public boolean isOpen()
-
getURL
public URL getURL() throws IOException
- Specified by:
getURLin interfaceResource- Specified by:
getURLin interfaceResource- Throws:
IOException
-
getURI
public URI getURI() throws IOException
- Specified by:
getURIin interfaceResource- Specified by:
getURIin interfaceResource- Throws:
IOException
-
getFile
public File getFile() throws IOException
Based onUrlResource.- Specified by:
getFilein interfaceResource- Specified by:
getFilein interfaceResource- Throws:
IOException
-
getResourceHeaders
protected org.apache.http.HttpResponse getResourceHeaders() throws IOExceptionAttempts 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
@Nullable protected String getResponseHeader(String what) throws IOException
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
public long contentLength() throws IOException- Specified by:
contentLengthin interfaceResource- Specified by:
contentLengthin interfaceResource- Throws:
IOException
-
lastModified
public long lastModified() throws IOException- Specified by:
lastModifiedin interfaceResource- Specified by:
lastModifiedin interfaceResource- Throws:
IOException
-
createRelative
public HTTPResource createRelative(String relativePath) throws IOException
Based onUrlResource.- Specified by:
createRelativein interfaceResource- Throws:
IOException
-
createRelativeResource
public Resource createRelativeResource(String relativePath) throws IOException
- Specified by:
createRelativeResourcein interfaceResource- Throws:
IOException
-
getFilename
public String getFilename()
This implementation returns the name of the file that this URL refers to.- Specified by:
getFilenamein interfaceResource- Specified by:
getFilenamein interfaceResource- See Also:
URL.getFile(),File.getName()
-
getDescription
public String getDescription()
- Specified by:
getDescriptionin interfaceResource- Specified by:
getDescriptionin interfaceResource
-
closeResponse
protected void closeResponse(@Nullable org.apache.http.HttpResponse response)Close the HTTP response.- Parameters:
response- the HTTP response
-
-