Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleCreate a server
builder.server(protocol:protocol.HTTP,port:8182){
    restlet(handle:{req, resp->
         resp.setEntity("Hello World", mediaType.TEXT_PLAIN)
    
    })
}.start()
//or you can define restlet first
def restlet = builder.restlet(handle:{req, resp->
    resp.setEntity("Hello World", mediaType.TEXT_PLAIN)
})
builder.server(protocol:protocol.HTTP,port:8182, target:restlet).start()

...

Code Block
titleServing static files
def ROOT_URI = "file:."
builder.component{
     current.servers.add(protocol.HTTP, 8182)
     current.clients.add(protocol.FILE)
      application(uri:"") {
        
        directory(root:ROOT_URI)
     }
}.start()
Example of Part09

...

Code Block
titleRedirector example
builder.component{
    current.servers.add(protocol.HTTP, 8182)
    application(uri:""){
        def router = router{
            def target = "http://www.google.com/search?q= {keywords}"
            redirector(uri:"/search",targetTemplate:target,
                  mode:redirectorMode.MODE_CLIENT_TEMPORARY,
                 postAttach:{route->
                         route.extractQuery("keywords","kwd",true)
                             })
       }
   }
}.start()
Example of Part11

...

Code Block
titleRouter
builder.component{
    current.servers.add(protocol.HTTP, 8182)

    application(uri:""){
        router{
            def guard = guard(uri:"/docs", scheme:challengeScheme.HTTP_BASIC, realm:"Restlet Tutorials")
             guard.secrets.put("scott", "tiger".toCharArray())
             guard.next = directory(root:"", autoAttach:false)
              restlet(uri:"/users/ {user}", handle:{req,resp->
               resp.setEntity("Account of user \"${req.attributes.get('user')}\"",mediaType.TEXT_PLAIN)
            })

            restlet(uri:"/users/{user}/orders", handle:{req, resp->
                resp.setEntity("Orders or user \"${req.attributes.get('user')}\"",mediaType.TEXT_PLAIN)
            })
            restlet(uri:"/users/{user}/orders/{order}", handle:{req, resp->
               def attrs = req.attributes
               def message = "Order \"${attrs.get('order')}\" for User \"${attrs.get('user')}\""
               resp.setEntity(message, mediaType.TEXT_PLAIN)
            })
       }
     }
}.start()
Example of Part12

...

  • init: init method
  • represent: for represent() and represent(Variant) methods HTTP GET
  • store: for storeRepresentation() method HTTP PUT
  • remove: for remoteRepresentation() method HTTP DELETE
  • accept: for acceptRepresentation() method HTTP POST
  • head: for handleHead() method
  • options: for handleOptions method
    Tip

    Specify `self` parameter in the last of parameter list. This special `self` instance indicates the resource instance.

    Code Block
    titleResources
    
    builder.component{
        current.servers.add(protocol.HTTP, 8182)
    
        application(uri:""){
            router{
                resource("/users/{user}",
                         init:{ctx, req, resp, self->
                              self.getVariants().add(new Variant(mediaType.TEXT_PLAIN))
                         },
                         represent:{variant, self->
                               return new StringRepresentation( "Account of user \"$ {self.request.attributes.get('user')}".toString(),
                                                               mediaType.TEXT_PLAIN);
                         })
                resource("/users/{user}/orders", ofClass:OrdersResource)
            }
    
    

...

  • }.start()