Hi all,
i've faced a similar problem, so let me share how i solved it.
I'm running Restlet within a tomcat instance, and I want to have
representations encoded using JSP pages. I also want to delegate the JSP
encoding to the tomcat JSP engine to re-use other resources/jsps from my
webapp.
What I do is that I assume that my Request is a HttpRequest, and extract the
tomcat RequestDispatcher to invoke my jsps from within my restlet resources.
I have to use custom http request and response proxies to be able to get
back the result of the encoding.
Here's a snippet of code, invoked from within Resource.getRepresentation() :
-------------------
org.restlet.data.Request request; // the current request
// check that this RestletQueryContext actually contains a
HttpRequest
if (request instanceof HttpRequest &&
((HttpRequest) request).getHttpCall() instanceof ServletCall) {
ServletCall httpCall = (ServletCall) ((HttpRequest)
request).getHttpCall();
// fetch the HTTP dispatcher
RequestDispatcher dispatcher =
httpCall.getRequest().getRequestDispatcher("representation.jsp");
HttpServletRequest proxyReq = new
HttpServletRequestWrapper(httpCall.getRequest());
// Overload the http response stream to grab the JSP output into
a dedicated proxy buffer
// The BufferedServletResponseWrapper is a custom response
wrapper that 'hijacks' the
// output of the JSP engine and stores it on the side instead of
forwarding it to the original
// HTTP response.
// This is needed to avoid having the JSP engine mess with the
actual HTTP stream of the
// current request, which must stay under the control of the
restlet engine.
BufferedServletResponseWrapper proxyResp = new
BufferedServletResponseWrapper(httpCall.getResponse());
// Add any objects to be encoded in the http request scope
proxyReq.setAttribute("myobjects", someObjects);
// Actual JSP encoding
dispatcher.include(proxyReq, proxyResp);
// Return the content of the proxy buffer
Representation rep = new
InputRepresentation(proxyResp.toInputStream(),someMediaType);
-------------------
And there you go, the representation inputstream is filled with the result
of the JSP engine.
hope this helps,
--p.
jlouvel wrote:
>
> Hi all,
>
> The closest thing that exists right now is the XTC project integrating
> Restlet and Facelets:
> http://trac.sarugo.org/xtc
>
> Another idea would be to develop a Restlet's Request and Response wrapper
> for the Servlet API. Then, we could try to create a JspRepresentation and
> ask to an embedded JSP processor (Tomcat's Jasper?) to write it. This
> seems
> touchy but feasible. See existing RFE:
>
> "Request and Response wrapper for Servlet API"
> http://restlet.tigris.org/issues/show_bug.cgi?id=512
>
> Also, I've entered a new RFE:
>
> "Support JSP representations"
> http://restlet.tigris.org/issues/show_bug.cgi?id=753
>
> Anyone wants to investigate?
>
> Best regards,
> Jerome Louvel
> --
> Restlet ~ Founder and Lead developer ~ http://www.restlet.org
> Noelios Technologies ~ Co-founder ~ http://www.noelios.com
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1271904
>
>
--
View this message in context:
http://n2.nabble.com/JSP-Servlet-Representation-tp2384688p2683397.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1878751