Hi,I'd reshape reshape "Using AJAX in Restlet":http://wiki.restlet.org/docs_1.1/g1/43-restlet/52-restlet.html
On Fri, Feb 15, 2008 at 8:55 AM, cleverpig <[EMAIL PROTECTED]> wrote: > Thanks,Evgeny! > Thank for share these code! > I had read them.I used TunnelService replacing custom finder that you > used,'cause I think TunnelService is easy for using.But I'd learned > how to using custom finder to implement same function from them. > > > > On Fri, Feb 15, 2008 at 12:40 AM, Evgeny Shepelyuk <[EMAIL PROTECTED]> wrote: > > cleverpig <greatcleverpig <at> gmail.com> writes: > > > > > > > > > > I post this draft in wiki.restlet.org--"Using AJAX in > > > Restlet":http://wiki.restlet.org/docs_1.1/g1/43-restlet/52-restlet.html > > > > > > I will start this tutorial with a simply example-"microblog",that's a > > > text based micro blog for demo how to using AJAX in Restlet. > > > Demo construction: > > > * Web client: call background service via JSON protocol in full > > > REST way(GET/PUT/POST/DELETE). > > > * Server side: uses db4o to work as store service provider,and > > > expose data in RESTful way. > > > * Server handle process: Application dispatch request to > > > Router,Router find corresponding reource,Resource handle request and > > > return representation. > > > > > > Pls make review and point out drawback. > > > > > > This is an example of usign prototype without proxying methods via POST. > > > > 1. HTML > > <html lang="en"> > > <head> > > <script language="javascript" type="text/javascript" > src="./prototype.js"></ > > script> > > <script type="text/javascript" language="JavaScript"> > > function callJSON() { > > new Ajax.Request('/ajax', { > > parameters: 'name=PUT', method: 'put', putBody: "PUT BODY", > > onComplete: function (transport) { > > alert(transport.responseText); > > } > > }); > > new Ajax.Request('/ajax', { > > parameters: 'name=POST', method: 'post', > > onComplete: function (transport) { > > alert(transport.responseText); > > } > > }); > > new Ajax.Request('/ajax', { > > parameters: 'name=DELETE', method: 'delete', > > onComplete: function (transport) { > > alert(transport.responseText); > > } > > }); > > } > > </script> > > </head> > > > > <body class="callingcards"> > > <input type="button" onclick="callJSON();" value="PRESSME"> > > </body> > > </html> > > > > 2. Server starting class > > > > import org.restlet.data.*; > > import org.restlet.*; > > > > public class Server { > > public static void main(String[] args) throws Exception { > > Component component = new Component(); > > component.getServers().add(Protocol.HTTP, 8182); > > component.getClients().add(Protocol.FILE); > > > > Application application = new > Application(component.getContext > > ()) { > > @Override > > public Restlet createRoot() { > > Router router = new Router(getContext()); > > > router.setFinderClass(PrototypeFinder.class); > > > > router.attach("", new > Directory(getContext(), > > "file:///C:/json-restlet")); > > router.attach("/ajax", AjaxResource.class); > > > > return router; > > } > > }; > > component.getDefaultHost().attach("", application); > > component.start(); > > } > > } > > > > 3. Custom Finder > > > > import org.restlet.Finder; > > import org.restlet.Context; > > import org.restlet.Handler; > > import org.restlet.data.Request; > > import org.restlet.data.Response; > > import org.restlet.data.Parameter; > > import org.restlet.data.Method; > > > > public class PrototypeFinder extends Finder { > > public PrototypeFinder(Context context, Class<? extends Handler> > > targetClass) { > > super(context, targetClass); > > } > > > > public void handle(Request request, Response response) { > > Parameter p = > request.getEntityAsForm().getFirst("_method"); > > request.setMethod(null != p ? Method.valueOf(p.getValue()) > : > > request.getMethod()); > > super.handle(request, response); > > } > > } > > > > 4. Resource class > > > > import org.restlet.resource.*; > > import org.restlet.Context; > > import org.restlet.ext.json.JsonRepresentation; > > import org.restlet.data.Request; > > import org.restlet.data.Response; > > import org.restlet.data.MediaType; > > import org.json.JSONObject; > > import org.json.JSONArray; > > > > import java.util.Arrays; > > > > public class AjaxResource extends Resource { > > > > public AjaxResource(Context context, Request request, Response > > response) { > > super(context, request, response); > > > > getVariants().add(new Variant(MediaType.TEXT_HTML)); > > getVariants().add(new Variant(MediaType.TEXT_PLAIN)); > > } > > > > public Representation represent(Variant variant) throws > > ResourceException { > > return new StringRepresentation("some data", > > MediaType.TEXT_ALL); > > } > > > > public void acceptRepresentation(Representation representation) > throws > > ResourceException { > > getResponse().setEntity(new StringRepresentation("PLAIN > TEXT > > for POST", MediaType.TEXT_PLAIN)); > > } > > > > public void removeRepresentations() throws ResourceException { > > getResponse().setEntity(new StringRepresentation("PLAIN > TEXT > > for DELETE", MediaType.TEXT_PLAIN)); > > } > > > > public void storeRepresentation(Representation representation) > throws > > ResourceException { > > getResponse().setEntity(new StringRepresentation("PLAIN > TEXT > > for PUT", MediaType.TEXT_PLAIN)); > > } > > > > public boolean allowPost() { > > return true; > > } > > > > public boolean allowDelete() { > > return true; > > } > > > > public boolean allowPut() { > > return true; > > } > > } > > > > > > > > > > -- > cleverpig > Location: Beijing > Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China > Zipcode: 100031 > Phone: 010-66415588-1113 > MSN: [EMAIL PROTECTED] > QQ: 149291732 > Skepe: cleverpigatmatrix > My Facebook ID:cleverpig > My Blog: hihere.sohu.com > My Tags: del.icio.us/cleverpig > My Twitter: twitter.com/cleverpig > My Organization: www.beijing-open-party.org > 一些值得关注的唧歪: > http://jiwai.de/t/jmatrix/ > http://jiwai.de/t/db4o/ > http://jiwai.de/t/matrix-community/ > -- cleverpig Location: Beijing Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China Zipcode: 100031 Phone: 010-66415588-1113 MSN: [EMAIL PROTECTED] QQ: 149291732 Skepe: cleverpigatmatrix My Facebook ID:cleverpig My Blog: hihere.sohu.com My Tags: del.icio.us/cleverpig My Twitter: twitter.com/cleverpig My Organization: www.beijing-open-party.org 一些值得关注的唧歪: http://jiwai.de/t/jmatrix/ http://jiwai.de/t/db4o/ http://jiwai.de/t/matrix-community/

