Package ac.simons.oembed
Class OembedService
- java.lang.Object
-
- ac.simons.oembed.OembedService
-
public class OembedService extends java.lang.Object- Author:
- Michael J. Simons, 2014-12-31
-
-
Field Summary
Fields Modifier and Type Field Description static org.slf4j.LoggerLOGGER
-
Constructor Summary
Constructors Constructor Description OembedService(org.apache.http.client.HttpClient httpClient, net.sf.ehcache.CacheManager cacheManager, java.util.List<OembedEndpoint> endpoints, java.lang.String applicationName)Creates a newOembedService.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringembedUrls(java.lang.String textWithEmbeddableUrls, java.lang.String baseUrl)<T> TembedUrls(java.lang.String textWithEmbeddableUrls, java.lang.String baseUrl, java.lang.Class<? extends T> targetClass)Scans the texttextWithEmbeddableUrlsfor anchor tags and tries to findOembedEndpointfor those urls.org.jsoup.nodes.DocumentembedUrls(org.jsoup.nodes.Document document)A convenience method to embed urls in an existing document.java.lang.StringgetCacheName()longgetDefaultCacheAge()java.util.Optional<OembedResponse>getOembedResponseFor(java.lang.String url)Tries to find anOembedResponsefor the URLurl.OembedParsergetParser(OembedResponse.Format format)Returns an instance of anOembedParserfor the givenOembedResponse.Format.booleanisAutodiscovery()voidsetAutodiscovery(boolean autodiscovery)Updates to configuration of oembed autodiscovery.voidsetCacheName(java.lang.String cacheName)Changes the name of the cache used.voidsetDefaultCacheAge(long defaultCacheAge)Changes the default cache age.
-
-
-
Constructor Detail
-
OembedService
public OembedService(org.apache.http.client.HttpClient httpClient, net.sf.ehcache.CacheManager cacheManager, java.util.List<OembedEndpoint> endpoints, java.lang.String applicationName)Creates a newOembedService. This service depends on aHttpClientand can use aCacheManagerfor caching requests.- Parameters:
httpClient- Mandatory http clientcacheManager- Optional cache managerendpoints- The static endpointsapplicationName- Optional application name
-
-
Method Detail
-
isAutodiscovery
public boolean isAutodiscovery()
- Returns:
- the current configuration of oembed autodiscovery
-
setAutodiscovery
public void setAutodiscovery(boolean autodiscovery)
Updates to configuration of oembed autodiscovery.- Parameters:
autodiscovery- New flag wether oembed endpoints should be autodiscovered
-
getCacheName
public java.lang.String getCacheName()
- Returns:
- The name of the cached used by this service
-
setCacheName
public void setCacheName(java.lang.String cacheName)
Changes the name of the cache used. If a cache manager is present, it clears the old cache and removes it.- Parameters:
cacheName- The new cache name
-
getDefaultCacheAge
public long getDefaultCacheAge()
- Returns:
- The default time in seconds responses are cached.
-
setDefaultCacheAge
public void setDefaultCacheAge(long defaultCacheAge)
Changes the default cache age.- Parameters:
defaultCacheAge- New default cache age in seconds
-
getOembedResponseFor
public java.util.Optional<OembedResponse> getOembedResponseFor(java.lang.String url)
Tries to find anOembedResponsefor the URLurl. If a cache manager is present, it tries that first. If anOembedResponsecan be discovered and a cache manager is present, that response will be cached.- Parameters:
url- The URL that might be represented by oembed.- Returns:
- An oembed response
-
embedUrls
public java.lang.String embedUrls(java.lang.String textWithEmbeddableUrls, java.lang.String baseUrl)- Parameters:
textWithEmbeddableUrls- Text that may contain linksbaseUrl- Base url for constructing absolute links- Returns:
- A string with urls embedded
- See Also:
embedUrls(java.lang.String, java.lang.String, java.lang.Class)
-
embedUrls
public <T> T embedUrls(java.lang.String textWithEmbeddableUrls, java.lang.String baseUrl, java.lang.Class<? extends T> targetClass)Scans the texttextWithEmbeddableUrlsfor anchor tags and tries to findOembedEndpointfor those urls. If such an endpoint exists, it tries to get anOembedResponseof that url from the endpoint. This response will then be rendered as html and is used to replace the anchor tag.- Type Parameters:
T- Type of the resulting document with embedded links- Parameters:
textWithEmbeddableUrls- Text that contains embeddable urlsbaseUrl- An optional base url for resolving relative urlstargetClass- The concrete classe for the document node- Returns:
- The same text with embedded urls if such urls existed
-
embedUrls
public org.jsoup.nodes.Document embedUrls(org.jsoup.nodes.Document document)
A convenience method to embed urls in an existing document.- Parameters:
document- Existing document, will be modified- Returns:
- Modified document with embedded urls
- See Also:
embedUrls(java.lang.String, java.lang.String, java.lang.Class)
-
getParser
public OembedParser getParser(OembedResponse.Format format)
Returns an instance of anOembedParserfor the givenOembedResponse.Format.- Parameters:
format- The format for which a parser is needed- Returns:
- The parser if a parser for the given format exists
-
-