Interface GroovyContext

  • All Superinterfaces:
    ratpack.handling.Context, ratpack.registry.Registry

    public interface GroovyContext
    extends ratpack.handling.Context
    Subclass of Context that adds Groovy friendly variants of methods.
    • Field Summary

      • Fields inherited from interface ratpack.handling.Context

        TYPE
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void byContent​(Closure<?> closure)
      Groovy friendly overload of Context.byContent(Action).
      void byMethod​(Closure<?> closure)
      Groovy friendly overload of Context.byMethod(Action).
      static GroovyContext from​(ratpack.handling.Context ctx)
      Creates a Groovy context from a context.
      GroovyContext getContext()
      default GroovyContext header​(java.lang.CharSequence name, java.lang.Object... values)
      void onClose​(Closure<?> closure)
      Adds a request close handler.
      • Methods inherited from interface ratpack.handling.Context

        byContent, byMethod, clientError, error, file, getAllPathTokens, getDirectChannelAccess, getExecution, getFileSystemBinding, getPathBinding, getPathTokens, getRequest, getResponse, getServerConfig, header, insert, insert, lastModified, lastModified, next, next, notFound, onClose, parse, parse, parse, parse, parse, parse, redirect, redirect, redirect, redirect, redirect, redirect, render
      • Methods inherited from interface ratpack.registry.Registry

        first, first, get, get, getAll, getAll, join, maybeGet, maybeGet
    • Method Detail

      • from

        static GroovyContext from​(ratpack.handling.Context ctx)
        Creates a Groovy context from a context.
        Parameters:
        ctx - the actual context
        Returns:
        a Groovy context
      • getContext

        GroovyContext getContext()
        Specified by:
        getContext in interface ratpack.handling.Context
      • byMethod

        void byMethod​(@DelegatesTo(value=GroovyByMethodSpec.class,strategy=1)
                      Closure<?> closure)
               throws java.lang.Exception
        Groovy friendly overload of Context.byMethod(Action).
         import ratpack.groovy.test.handling.GroovyRequestFixture
         import static ratpack.groovy.Groovy.groovyHandler
        
         def handler = groovyHandler {
           byMethod {
             def message = "hello!"
             get {
               render "$message from GET request"
             }
             post {
               render "$message from POST request"
             }
           }
         }
        
         def result = GroovyRequestFixture.handle(handler) {
           method "get"
         }
        
         assert result.rendered(CharSequence) == "hello! from GET request"
        
         result = GroovyRequestFixture.handle(handler) {
           method "post"
         }
        
         assert result.rendered(CharSequence) == "hello! from POST request"
         
        Parameters:
        closure - defines the action to take for different HTTP methods
        Throws:
        java.lang.Exception - any thrown by the closure
      • byContent

        void byContent​(@DelegatesTo(value=GroovyByContentSpec.class,strategy=1)
                       Closure<?> closure)
                throws java.lang.Exception
        Groovy friendly overload of Context.byContent(Action).
        
         import ratpack.groovy.test.handling.GroovyRequestFixture
         import static ratpack.groovy.Groovy.groovyHandler
        
         def handler = groovyHandler {
           byContent {
             def message = "hello!"
             json {
               render "{\"msg\": \"$message\"}"
             }
             html {
               render "<p>$message</p>"
             }
           }
         }
        
         def result = GroovyRequestFixture.handle(handler) {
           header("Accept", "application/json");
         }
        
         assert result.rendered(CharSequence) == "{\"msg\": \"hello!\"}"
         assert result.headers.get("content-type") == "application/json"
        
         result = GroovyRequestFixture.handle(handler) {
           header("Accept", "text/plain; q=1.0, text/html; q=0.8, application/json; q=0.7");
         }
        
         assert result.rendered(CharSequence) == "<p>hello!</p>";
         assert result.headers.get("content-type") == "text/html;charset=UTF-8";
         
        Parameters:
        closure - defines the action to take for the different content types
        Throws:
        java.lang.Exception - any thrown by the closure
      • onClose

        void onClose​(@DelegatesTo(value=ratpack.handling.RequestOutcome.class,strategy=1)
                     Closure<?> closure)
        Adds a request close handler.
        Parameters:
        closure - A closure to call when the request is closed
      • header

        default GroovyContext header​(java.lang.CharSequence name,
                                     java.lang.Object... values)
        Specified by:
        header in interface ratpack.handling.Context