Class TemplatedBodyBuilder

All Implemented Interfaces:
ExecutableSearchBuilder<HTTPSearch>, Component, DestructableComponent, InitializableComponent

public class TemplatedBodyBuilder
extends AbstractHTTPSearchBuilder
An ExecutableSearchBuilder that generates a request by evaluating Templates against the currently resolved attributes within an AttributeResolutionContext to produce a URL and body, via GET or POST, and a configurable cache key.
  • Field Details

    • log

      @Nonnull private final org.slf4j.Logger log
      Class logger.
    • urlTemplate

      @NonnullAfterInit private Template urlTemplate
      URL template to be evaluated.
    • bodyTemplate

      @NonnullAfterInit private Template bodyTemplate
      Body template to be evaluated.
    • cacheKeyTemplate

      @NonnullAfterInit private Template cacheKeyTemplate
      Cache key template to be evaluated.
    • urlTemplateText

      @NonnullAfterInit private String urlTemplateText
      Text of URL template to be evaluated.
    • bodyTemplateText

      @NonnullAfterInit private String bodyTemplateText
      Text of body template to be evaluated.
    • cacheKeyTemplateText

      @NonnullAfterInit private String cacheKeyTemplateText
      Text of cache key template to be evaluated.
    • engine

      @NonnullAfterInit private org.apache.velocity.app.VelocityEngine engine
      VelocityEngine.
    • method

      @Nonnull @NotEmpty private String method
      HTTP method.
    • mimeType

      @Nonnull @NotEmpty private String mimeType
      MIME type.
    • charset

      @Nullable private String charset
      Character set.
    • contentType

      @NonnullAfterInit private org.apache.http.entity.ContentType contentType
      Content type.
    • paramEscaper

      @Nonnull private final Escaper paramEscaper
      Escaper for form parameters.
    • fragmentEscaper

      @Nonnull private final Escaper fragmentEscaper
      Escaper for fragments.
    • pathEscaper

      @Nonnull private final Escaper pathEscaper
      Escaper for path segments.
    • xmlAttributeEscaper

      @Nonnull private final Escaper xmlAttributeEscaper
      Escaper for XML Attributes.
    • xmlContentEscaper

      @Nonnull private final Escaper xmlContentEscaper
      Escaper for XML content.
    • customObject

      @Nullable private Object customObject
      A custom object to inject into the template.
  • Constructor Details

    • TemplatedBodyBuilder

      public TemplatedBodyBuilder()
      Constructor.
  • Method Details

    • getURLTemplate

      @NonnullAfterInit public Template getURLTemplate()
      Get the URL template to be evaluated.
      Returns:
      template
    • getBodyTemplate

      @NonnullAfterInit public Template getBodyTemplate()
      Get the body template to be evaluated.
      Returns:
      template
    • getCacheKeyTemplate

      @Nullable public Template getCacheKeyTemplate()
      Get the cache key template to be evaluated.
      Returns:
      template
    • getURLTemplateText

      @NonnullAfterInit public String getURLTemplateText()
      Get the URL template text to be evaluated.
      Returns:
      template text
    • setURLTemplateText

      public void setURLTemplateText​(@Nullable String text)
      Set the URL template to be evaluated.
      Parameters:
      text - template to be evaluated
    • getBodyTemplateText

      @NonnullAfterInit public String getBodyTemplateText()
      Get the body template text to be evaluated.
      Returns:
      template text
    • setBodyTemplateText

      public void setBodyTemplateText​(@Nullable String text)
      Set the body template to be evaluated.
      Parameters:
      text - template to be evaluated
    • getCacheKeyTemplateText

      @Nullable public String getCacheKeyTemplateText()
      Get the cache key template text to be evaluated.
      Returns:
      template text
    • setCacheKeyTemplateText

      public void setCacheKeyTemplateText​(@Nullable String text)
      Set the cache key template to be evaluated.
      Parameters:
      text - template to be evaluated
    • getVelocityEngine

      @NonnullAfterInit public org.apache.velocity.app.VelocityEngine getVelocityEngine()
      Get the VelocityEngine to be used.
      Returns:
      template engine
    • setVelocityEngine

      public void setVelocityEngine​(@Nonnull org.apache.velocity.app.VelocityEngine velocityEngine)
      Set the VelocityEngine to be used.
      Parameters:
      velocityEngine - engine to be used
    • setMethod

      public void setMethod​(@Nonnull @NotEmpty String m)
      Set the HTTP method.

      Defaults to "POST".

      Parameters:
      m - method
    • setMIMEType

      public void setMIMEType​(@Nonnull @NotEmpty String type)
      Set the MIME type.

      Defaults to "text/plain".

      Parameters:
      type - MIME type
    • setCharacterSet

      public void setCharacterSet​(@Nullable String c)
      Set the character set.
      Parameters:
      c - character set
    • setCustomObject

      public void setCustomObject​(@Nullable Object object)
      Set the custom (externally provided) object.
      Parameters:
      object - the custom object
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      Overrides:
      doInitialize in class AbstractInitializableComponent
      Throws:
      ComponentInitializationException
    • merge

      @Nonnull @NotEmpty protected String merge​(@Nonnull Template template, @Nonnull org.apache.velocity.VelocityContext context)
      Invokes Template.merge(org.apache.velocity.context.Context) on the supplied template and context.
      Parameters:
      template - template to merge
      context - to merge
      Returns:
      result of the merge operation
    • getHttpRequest

      @Nonnull protected org.apache.http.client.methods.HttpUriRequest getHttpRequest​(@Nonnull AttributeResolutionContext resolutionContext, @Nonnull Map<String,​List<IdPAttributeValue>> dependencyAttributes) throws ResolutionException
      Default implementation just supports GET and builds a request around a URL.
      Overrides:
      getHttpRequest in class AbstractHTTPSearchBuilder
      Parameters:
      resolutionContext - the context of the resolution
      dependencyAttributes - made available to the request
      Returns:
      the HttpRequest to use
      Throws:
      ResolutionException - if an error occurs
    • getResultCacheKey

      @Nonnull @NotEmpty protected String getResultCacheKey​(@Nonnull org.apache.http.client.methods.HttpUriRequest request, @Nonnull AttributeResolutionContext resolutionContext, @Nonnull Map<String,​List<IdPAttributeValue>> dependencyAttributes)
      Default implementation just allows caching of GET requests and returns the URI itself.
      Overrides:
      getResultCacheKey in class AbstractHTTPSearchBuilder
      Parameters:
      request - the HTTP request about to be executed
      resolutionContext - the attribute resolution context
      dependencyAttributes - dependencies
      Returns:
      the cache key