Class 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.Logger LOGGER  
    • 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 new OembedService.
    • Field Detail

      • LOGGER

        public static final org.slf4j.Logger LOGGER
    • 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 new OembedService. This service depends on a HttpClient and can use a CacheManager for caching requests.
        Parameters:
        httpClient - Mandatory http client
        cacheManager - Optional cache manager
        endpoints - The static endpoints
        applicationName - 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 an OembedResponse for the URL url. If a cache manager is present, it tries that first. If an OembedResponse can 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 links
        baseUrl - 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 text textWithEmbeddableUrls for anchor tags and tries to find OembedEndpoint for those urls. If such an endpoint exists, it tries to get an OembedResponse of 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 urls
        baseUrl - An optional base url for resolving relative urls
        targetClass - 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)