Hi,
I am planning to add support for additional content types to the
SlingPostServlet that would allow processing Web Forms 2.0, JSON
(allowing an effective-round-tripping from Sling.js), XML (in JCR
document notation), ATOM (in terms of the Atom publishing protocol).
To do this I would like to propose a small API change: extracting a
ResponseBuilder interface from our current HtmlResponse and renaming
HtmlResponse subsequently to HtmlResponseBuilder. This would allow us
to create a doPost method that would start like this (I think we
should OSGi-ify this later to make content type handling pluggable)
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws IOException {
log.info("content-type: " + request.getContentType());
String responseType;
//try to determine the request content type and set the response
accordingly. Ordinary
//web forms (multipart/form-data or application/x-www-form-
urlencoded) are getting HTML
//response
if (request.getContentType().equals("application/x-www-form
+xml")) {
responseType = "application/x-www-form+xml";
//TODO: handle Web Forms 2.0 POST
} else if (request.getContentType().equals("application/json")||
request.getContentType().equals("text/x-json")) {
responseType = "application/json";
//TODO: handle JSON POST
} else if (request.getContentType().equals("text/xml")) {
responseType = "text/xml";
//TODO: handle XML POST
} else if (request.getContentType().equals("application/atom
+xml")) {
responseType = "application/atom+xml";
//TODO: handle Atom POST
} else {
responseType = "text/html";
}
//check for user-specified override of response type
if (request.getHeader("Accept")!
=null&&request.getHeader("Accept").length()!=0) {
responseType = request.getHeader("Accept");
}
ResponseBuilder responseBuilder;
if (responseType.equals("application/json")) {
responseBuilder = new JsonResponseBuilder();
} else if (responseType.equals("application/x-www-form+xml")) {
responseBuilder = new WebForms20ResponseBuilder();
} else if (responseType.equals("text/xml")) {
responseBuilder = new XmlResponseBuilder();
} else if (responseType.equals("application/atom+xml")) {
responseBuilder = new AppResponseBuilder();
} else {
responseBuilder = new HtmlResponseBuilder();
}
the general idea is not to break the current API, so that HtmlResponse
stays the default, but that users sending a JSON request will get a
JSON response, unless they explicitly request a different responseType
using the Accept header.
If this sounds good to you, I would go on by writing the Web Forms 2.0
part first and submitting a patch (on Youtube) including the proposed
API changes.
regards,
Lars