Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>Examples of how to construct Restlet applications mentioned in <a href="http://www.restlet.org/documentation/1.1/tutorial">Restlet Tutorials</a></p></td></tr></table><br /> For example groovy scripts, see <a href="http://svn.codehaus.org/groovy-contrib/groovyrestlet/trunk/src/test/groovy/org/lpny/groovyrestlet/examples/tutorials/">http://svn.codehaus.org/groovy-contrib/groovyrestlet/trunk/src/test/groovy/org/lpny/groovyrestlet/examples/tutorials/</a></p> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part02">Example of Part02</a></h5> <p>Example about creating a <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Client.html">Restlet Client</a> instance.</p> <p>You can use <strong>shortcut</strong> <code>client</code> constructor as shown in `#1`; also you can use the generic <code>restlet</code> constructor to create client instance.</p> <table class="wysiwyg-macro" data-macro-name="tip" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>you can use `restlet(ofClass:<a class="confluence-link unresolved" data-content-title="class instance" data-linked-resource-default-alias="class instance" href="#">class name</a>)` to create any instance of derived type of <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Restlet.html">Restlet</a>. In this case, you need to specify attribute `consArgs` which are an array of constructing parameters.</p></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Create client" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9Q3JlYXRlIGNsaWVudH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> //#1 using shortcut builder.client(protocol.HTTP) //#2 generic restlet constructor builder.restlet(ofClass:"org.restlet.Client", consArgs:[protocol.HTTP] as Object[]) </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part03">Example of Part03</a></h5> <p><em>At current moment, server construction does not support `restlet` way</em></p> <p>Nesting here indicates a parent-child relationship.</p> <table class="wysiwyg-macro" data-macro-name="tip" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>The child of a <code>server</code> component becoming the <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Server.html#setTarget(org.restlet.Restlet)">target</a> property of the <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Server.html">Server</a>.</p></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Create a server" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9Q3JlYXRlIGEgc2VydmVyfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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() </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part05">Example of Part05</a></h5> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Restlet example" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9UmVzdGxldCBleGFtcGxlfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> //using component shortcut constructor def component = builder.component{ current.servers.add(protocol.HTTP, 8182) restlet(uri:"/trace", handle: {req, resp-> println "To process request: ${req}" def message = """Resource URI: ${req.resourceRef} Root URI : ${req.rootRef} Routed part : ${req.resourceRef.baseRef} Remaining part: ${req.resourceRef.remainingPart} """ resp.setEntity(message, mediaType.TEXT_PLAIN) }) } </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part06">Example of Part06</a></h5> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Serving static files" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9U2VydmluZyBzdGF0aWMgZmlsZXN9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def ROOT_URI = "file:." builder.component{ current.servers.add(protocol.HTTP, 8182) current.clients.add(protocol.FILE) application(uri:"") { directory(root:ROOT_URI) } }.start() </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part09">Example of Part09</a></h5> <table class="wysiwyg-macro" data-macro-name="tip" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Using <code>current</code> to reference to the current instance.</p></td></tr></table> <p>By default any nested component will be automatically attached to its parent component according to their parent-child relationship. Here <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Guard.html">Guard</a> is automatically attached to its parent (<a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Application.html">Application</a> here) as its <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Application.html#setRoot(org.restlet.Restlet)">root</a>. Adding attribute <code>autoAttach:false</code> can disable this feature.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Guard" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9R3VhcmR9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> builder.component{ current.servers.add(protocol.HTTP, 8182) application(uri:"") { guard(scheme:challengeScheme.HTTP_BASIC, realm:"Tutorial").secrets.put("scott","tiger".toCharArray()) def dir = directory(autoAttach:false, root:"") current.root.next=dir } }.start() </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part10">Example of Part10</a></h5> <p>In Restlet, an attaching operation (on Router.attach) returns an instance of <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Route.html">Route</a>. It might be needed to do some post processing on a route. Attribute <code>postAttach</code> which refers a closure is used to support this.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Redirector example" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9UmVkaXJlY3RvciBleGFtcGxlfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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() </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part11">Example of Part11</a></h5> <table class="wysiwyg-macro" data-macro-name="tip" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>You can implement the <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Restlet.html#handle(org.restlet.data.Request,%20org.restlet.data.Response)">handle method</a> of a <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/Restlet.html">Restlet</a> using a groovy closure.</p></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Router" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9Um91dGVyfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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() </pre></td></tr></table> <h5><a href="http://www.restlet.org/documentation/1.1/tutorial#part12">Example of Part12</a></h5> <p>Same as handle closure of a Restlet, you can implement a simple <a href="http://www.restlet.org/documentation/1.1/api/org/restlet/resource/Resource.html">Restlet Resource</a> using groovy closures. Following attributes are supported:</p> <ul> <li>init: <code>init</code> method</li> <li>represent: for <code>represent()</code> and <code>represent(Variant)</code> methods <strong>HTTP GET</strong></li> <li>store: for <code>storeRepresentation()</code> method <strong>HTTP PUT</strong></li> <li>remove: for <code>remoteRepresentation()</code> method <strong>HTTP DELETE</strong></li> <li>accept: for <code>acceptRepresentation()</code> method <strong>HTTP POST</strong></li> <li>head: for <code>handleHead()</code> method</li> <li>options: for <code>handleOptions</code> method <table class="wysiwyg-macro" data-macro-name="tip" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Specify `self` parameter in the last of parameter list. This special `self` instance indicates the resource instance.</p></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="title=Resources" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9UmVzb3VyY2VzfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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() </pre></td></tr></table></li> </ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced