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;
}
}