You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tiles.apache.org by Nicolas LE BAS <ma...@nlebas.net> on 2011/10/24 23:55:44 UTC
Tiles Request API
Hi all,
I've been playing a bit with the Tiles Request API (awaiting github :))
and I'd like to share a couple of thoughts about it.
The ability to call any Renderer with any kind of Request is even better
than I thought. I've written several Renderers in no time, and I can now
assemble multiple versions of a single web site from half a dozen
languages. I've written a couple of Request implementations, too, both
to support the tiles Taglib in Velocity and Freemarker, and to support
calling Tiles "offline" from a batch. It works well, but it is a bit
stretchy at times.
IMHO the Request interface could be made easier to implement, and that
could perhaps favor its adoption by other projects. More to the point:
(+) The Request interface provides access to a number of things that are
absolutely needed: accessing attributes by scope, as well as headers and
params; indeed it improves on the HttpServletRequest interface in these
matters.
(-) Yet I believe it would benefit from a diet. Because more methods are
more work to do when implementing it, but also because:
* getApplicationContext is only used as way to access the Application
scope. I'm afraid there may be two solutions to a single purpose here. I
understand why Renderers need to know the ApplicationContext, but
perhaps it should be provided to them as separate object during
initialization, and the application scope should be accessible only
through the Request interface to avoid confusion.
* isResponseCommitted, dispatch and include: those are a pain to
implement. These concepts are unrelated to the request we are currently
processing: they belong to the ApplicationContext (like
getRequestDispatcher belongs to the ServletContext). Besides, I'm not
sure why we need "dispatch" and "isResponseCommitted" from a theoretical
point of view. Forwarding a request is a controller behaviour, not
something a renderer should attempt. It's only used by Tiles as an
optimization of "include" for the servlet environment, and optimizations
should not be exposed in the API imho.
* setContentType is a controller concept, a renderer should not attempt
to change the content type. Besides, Tiles is not using it.
* getOutputStream would be useful if we wanted to render binary data,
but none of our current technologies actually support it. Perhaps a
future addition?
* getRequestObjects seems only here for historical purpose; it is not used.
(+) It's good that the request body is not accessible. It should never
be used in rendering anyway. The response headers are missing, too
(except for ContentType), and that's a good thing, too. They're a
concern of the controller, not the renderer, and because they're not
there, the API helps to avoid mistakes.
Overall it's a good API and we can do much by extending Tiles 3 that we
could not do with Tiles 2. IMHO it still needs improvement, but perhaps
that can be postponed for a while.
It's just my opinion anyway, but I hope this can help in improving tiles
further.
Nick
Re: Tiles Request API
Posted by Antonio Petrelli <an...@gmail.com>.
2011/10/26 Nicolas LE BAS <ma...@nlebas.net>
> On 11-10-26 04:08 AM, Antonio Petrelli wrote:
>
>> You don't have to have Tiles in mind, the request interface should work
>> outside of Tiles too.
>> The application context may be useful if you see it in a general
>> perspective.
>>
>
> :-) Believe me, I don't focus on Tiles! Nor on servlet/portlet.
>
> And I agree the ApplicationContext is useful. I just don't believe in the
> Request interface as a single entry point for everything: this is
> restricting its possible use cases.
>
>
> Sorry but you are wrong, getRequestDispatcher is also a method of
>> HttpServletRequest:
>> http://download.oracle.com/**javaee/1.4/api/javax/servlet/**
>> ServletRequest.html#**getRequestDispatcher(java.**lang.String)<http://download.oracle.com/javaee/1.4/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)>
>> This is the method that is used inside Tiles. Moreover, from the Javadoc:
>> <snip>
>> The difference between this method and
>> ServletContext.**getRequestDispatcher(java.**lang.String) is that this
>> method
>> can take a relative path.
>> </snip>
>> So, essentially, it is more useful than the ServletContext's one.
>>
>>
> Since you've separated "path" and "Request" everywhere in the API (which
> is a good idea imho), there is no concept of "path" in the Request anymore,
> so a relative path has nothing to be relative to.
>
> Note that getRequestDispatcher doesn't exist in PortletRequest, only in
> PortletContext.
>
>
> And UrlPreparer is the only one that calls "include" directly (which will
>>> raise the "forceInclude" flag, and that sounds buggy coming from a
>>> Preparer).
>>>
>>
>> About UrlPreparer, it is a backward compatibility class. Sincerely I don't
>> remember why I should include it always.
>>
>> Probably because forwarding may close the response stream, preventing
> the actual rendering.
>
>
> Back to the difficult point, summing up (correct me if I forget something):
>
> I think the dispatch/include methods should be moved from Request to
> ApplicationContext because the decision of what processes a Request should
> be independant of the Request itself.
> You think they should stay in the Request because they should act
> consistently based on the history of the Request (and the underlying
> HttpServletRequest).
>
> But why can't we have both? I see no technical problem with it.
> And if Request.getApplicationContext(**) stays, we can even have the
> Request as a single entry point.
>
> How about that?
>
Ok this way I like it :-D
Antonio
Re: Tiles Request API
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 11-10-26 04:08 AM, Antonio Petrelli wrote:
> You don't have to have Tiles in mind, the request interface should work
> outside of Tiles too.
> The application context may be useful if you see it in a general
> perspective.
:-) Believe me, I don't focus on Tiles! Nor on servlet/portlet.
And I agree the ApplicationContext is useful. I just don't believe in
the Request interface as a single entry point for everything: this is
restricting its possible use cases.
> Sorry but you are wrong, getRequestDispatcher is also a method of
> HttpServletRequest:
> http://download.oracle.com/javaee/1.4/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)
> This is the method that is used inside Tiles. Moreover, from the Javadoc:
> <snip>
> The difference between this method and
> ServletContext.getRequestDispatcher(java.lang.String) is that this method
> can take a relative path.
> </snip>
> So, essentially, it is more useful than the ServletContext's one.
>
Since you've separated "path" and "Request" everywhere in the API (which
is a good idea imho), there is no concept of "path" in the Request
anymore, so a relative path has nothing to be relative to.
Note that getRequestDispatcher doesn't exist in PortletRequest, only in
PortletContext.
>> And UrlPreparer is the only one that calls "include" directly (which will
>> raise the "forceInclude" flag, and that sounds buggy coming from a
>> Preparer).
>
> About UrlPreparer, it is a backward compatibility class. Sincerely I don't
> remember why I should include it always.
>
Probably because forwarding may close the response stream, preventing
the actual rendering.
Back to the difficult point, summing up (correct me if I forget something):
I think the dispatch/include methods should be moved from Request to
ApplicationContext because the decision of what processes a Request
should be independant of the Request itself.
You think they should stay in the Request because they should act
consistently based on the history of the Request (and the underlying
HttpServletRequest).
But why can't we have both? I see no technical problem with it.
And if Request.getApplicationContext() stays, we can even have the
Request as a single entry point.
How about that?
Nick
Re: Tiles Request API
Posted by Antonio Petrelli <an...@gmail.com>.
2011/10/26 Nicolas LE BAS <ma...@nlebas.net>
> On 11-10-25 03:29 PM, Antonio Petrelli wrote:
>
>> 2011/10/25 Nicolas LE BAS<ma...@nlebas.net>
>>
>>> Actually, no, the way I said it is confusing. I want to remove
>>> getApplicationContext from Request, and have the renderers that need it
>>> implement ApplicationContextAware instead.
>>>
>>> The application scope can be accessed through
>>> getContext(APPLICATION_SCOPE)
>>> instead of getApplicationContext().****getApplicationScope().
>>>
>>>
>>> ApplicationContext itself is fine by me.
>>>
>>
>> Mmm... no, getApplicationContext is useful at least to retrieve resources
>> from a request.
>>
>
> Do you mean "resources" as in "ApplicationContext.**getResource"?
> I can't see a use case except from Renderer, not a Request.
You don't have to have Tiles in mind, the request interface should work
outside of Tiles too.
The application context may be useful if you see it in a general
perspective.
>
>
> Nevertheless even if you just want a common abstraction to javax.servlet
>> and
>>
>>> javax.portlet, both "include" and "dispatch" belong to
>>> ApplicationContext.
>>> Just like getRequestDispatcher belongs to SerlvetContext and
>>> PortletContext,
>>> not their Request counterparts.
>>>
>>> Those 2 methods are for deciding who should deal with the request. It's
>>> not
>>> the guy who submits the Request object who should decide that, nor the
>>> Renderer. It belongs elsewhere, both JavaEE and in tiles. The API should
>>> reflect that fact, and that would actually make it more simple to use
>>> and to
>>> implement.
>>>
>>
>>
>> Not entirely true. It is true that the request dispatcher belongs to the
>> application context, however the fact that a dispatch transforms into an
>> include is decided at request level (through isResponseCommitted and a
>> special attribute in request scope). This is useful to allow calling to
>> "dispatch" even if it is not possible. Since Tiles is a composition
>> framework, it is a pretty frequent need.
>>
>
> Agreed, it's a bit more complex. Let's elaborate:
>
> When Tiles wants to include foreign content (like the output of a JSP for
> instance), it is sometimes better to call "forward" instead of "include"
> (i.e. in the early stages, when no content has actually been rendered yet).
>
> Thus Tiles is optimizing "include" by calling "dispatch" instead, and
> relies on the implementation of "dispatch" to make the optimizing decision
> based on a request-scoped attribute("FORCE_INCLUDE") and
> isResponseCommitted.
>
> Is it correct?
>
Yes.
> Yet I fail to see how that makes "dispatch" a part of Request. These data
> are publicly readable and writable from the Request interface. The
> algorithm can work just as well from another class.
>
> And it's even less relevant concerning "include".
>
Sorry but you are wrong, getRequestDispatcher is also a method of
HttpServletRequest:
http://download.oracle.com/javaee/1.4/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)
This is the method that is used inside Tiles. Moreover, from the Javadoc:
<snip>
The difference between this method and
ServletContext.getRequestDispatcher(java.lang.String) is that this method
can take a relative path.
</snip>
So, essentially, it is more useful than the ServletContext's one.
>
> Actually I believe DispatchRenderer is the right place for the optimizing
> algorithm, while ApplicationContext is the right place for the actual
> including/forwarding.
>
> By the way, DispatchRenderer is currently the only thing that actually
> calls "dispatch".
>
I disagree. You can even use the request interface without a renderer, thus
if you dispatch/include a resource, it should work consistently,
"enhancing" the ability of including/forwarding of the ServletRequest with
the inclusion of the ability to force the inclusion.
> And UrlPreparer is the only one that calls "include" directly (which will
> raise the "forceInclude" flag, and that sounds buggy coming from a
> Preparer).
About UrlPreparer, it is a backward compatibility class. Sincerely I don't
remember why I should include it always.
>
> The reasoning that I made is:
>> request headers: read but never written;
>> response headers: written but never read.
>> Therefore the read and write operations are orthogonal and can be
>> (probably
>> confusingly) combined. Probably a Map is not the right interface,
>> probably a
>> specific clearer interface should be better.
>>
>
> Agreed. Servlets have a specific interface; but Map can be used by many
> things like ELs without further effort.
> I still vote for 2 Maps (one unmodifiable, the other unreadable).
>
+1
Antonio
Re: Tiles Request API
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 11-10-25 03:29 PM, Antonio Petrelli wrote:
> 2011/10/25 Nicolas LE BAS<ma...@nlebas.net>
>> Actually, no, the way I said it is confusing. I want to remove
>> getApplicationContext from Request, and have the renderers that need it
>> implement ApplicationContextAware instead.
>>
>> The application scope can be accessed through getContext(APPLICATION_SCOPE)
>> instead of getApplicationContext().**getApplicationScope().
>>
>> ApplicationContext itself is fine by me.
>
> Mmm... no, getApplicationContext is useful at least to retrieve resources
> from a request.
Do you mean "resources" as in "ApplicationContext.getResource"?
I can't see a use case except from Renderer, not a Request.
> Nevertheless even if you just want a common abstraction to javax.servlet and
>> javax.portlet, both "include" and "dispatch" belong to ApplicationContext.
>> Just like getRequestDispatcher belongs to SerlvetContext and PortletContext,
>> not their Request counterparts.
>>
>> Those 2 methods are for deciding who should deal with the request. It's not
>> the guy who submits the Request object who should decide that, nor the
>> Renderer. It belongs elsewhere, both JavaEE and in tiles. The API should
>> reflect that fact, and that would actually make it more simple to use and to
>> implement.
>
>
> Not entirely true. It is true that the request dispatcher belongs to the
> application context, however the fact that a dispatch transforms into an
> include is decided at request level (through isResponseCommitted and a
> special attribute in request scope). This is useful to allow calling to
> "dispatch" even if it is not possible. Since Tiles is a composition
> framework, it is a pretty frequent need.
Agreed, it's a bit more complex. Let's elaborate:
When Tiles wants to include foreign content (like the output of a JSP
for instance), it is sometimes better to call "forward" instead of
"include" (i.e. in the early stages, when no content has actually been
rendered yet).
Thus Tiles is optimizing "include" by calling "dispatch" instead, and
relies on the implementation of "dispatch" to make the optimizing
decision based on a request-scoped attribute("FORCE_INCLUDE") and
isResponseCommitted.
Is it correct?
Yet I fail to see how that makes "dispatch" a part of Request. These
data are publicly readable and writable from the Request interface. The
algorithm can work just as well from another class.
And it's even less relevant concerning "include".
Actually I believe DispatchRenderer is the right place for the
optimizing algorithm, while ApplicationContext is the right place for
the actual including/forwarding.
By the way, DispatchRenderer is currently the only thing that actually
calls "dispatch".
And UrlPreparer is the only one that calls "include" directly (which
will raise the "forceInclude" flag, and that sounds buggy coming from a
Preparer).
> The reasoning that I made is:
> request headers: read but never written;
> response headers: written but never read.
> Therefore the read and write operations are orthogonal and can be (probably
> confusingly) combined. Probably a Map is not the right interface, probably a
> specific clearer interface should be better.
Agreed. Servlets have a specific interface; but Map can be used by many
things like ELs without further effort.
I still vote for 2 Maps (one unmodifiable, the other unreadable).
Thanks
Nick.
Re: Tiles Request API
Posted by Antonio Petrelli <an...@gmail.com>.
2011/10/25 Nicolas LE BAS <ma...@nlebas.net>
> On 11-10-25 03:51 AM, Antonio Petrelli wrote:
>
>> 2011/10/24 Nicolas LE BAS<ma...@nlebas.net>
>>
>>> * getApplicationContext is only used as way to access the Application
>>> scope. I'm afraid there may be two solutions to a single purpose here. I
>>> understand why Renderers need to know the ApplicationContext, but perhaps
>>> it
>>> should be provided to them as separate object during initialization, and
>>> the
>>> application scope should be accessible only through the Request interface
>>> to
>>> avoid confusion.
>>>
>>>
>> So you want to remove ApplicationContext.**getApplicationScope()? If yes
>> notice that this way, all the objects that need a connection to the
>> application scope won't be able to do it at startup-time, because they
>> need
>> a request. Sincerely I am -1 for it.
>>
>>
> Actually, no, the way I said it is confusing. I want to remove
> getApplicationContext from Request, and have the renderers that need it
> implement ApplicationContextAware instead.
>
> The application scope can be accessed through getContext(APPLICATION_SCOPE)
> instead of getApplicationContext().**getApplicationScope().
>
> ApplicationContext itself is fine by me.
Mmm... no, getApplicationContext is useful at least to retrieve resources
from a request.
Nevertheless even if you just want a common abstraction to javax.servlet and
> javax.portlet, both "include" and "dispatch" belong to ApplicationContext.
> Just like getRequestDispatcher belongs to SerlvetContext and PortletContext,
> not their Request counterparts.
>
> Those 2 methods are for deciding who should deal with the request. It's not
> the guy who submits the Request object who should decide that, nor the
> Renderer. It belongs elsewhere, both JavaEE and in tiles. The API should
> reflect that fact, and that would actually make it more simple to use and to
> implement.
Not entirely true. It is true that the request dispatcher belongs to the
application context, however the fact that a dispatch transforms into an
include is decided at request level (through isResponseCommitted and a
special attribute in request scope). This is useful to allow calling to
"dispatch" even if it is not possible. Since Tiles is a composition
framework, it is a pretty frequent need.
The response headers are missing, too (except for ContentType), and that's
>>> a good thing, too. They're a concern of the controller, not the renderer,
>>> and because they're not there, the API helps to avoid mistakes.
>>>
>>
>> Well, no. The Request.getHeader* methods return a map that can write to
>> response headers. See this class (setValue method):
>> http://svn.eu.apache.org/**repos/asf/tiles/framework/**
>> trunk/tiles-request/tiles-**request-servlet/src/main/java/**
>> org/apache/tiles/request/**servlet/extractor/**HeaderExtractor.java<http://svn.eu.apache.org/repos/asf/tiles/framework/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/HeaderExtractor.java>
>>
>
> I admit that I totally missed that part. It's far from obvious: the javadoc
> says: "Return an immutable Map that maps header names to the set of all
> values specified in the request".
> It feels weird, too, because that's a Map you can write to, but never read
> the data back again. That's what the servlet API does, but that's not what
> people expect of a Map. As a user, I'd rather have separate maps for request
> headers and response headers.
>
The reasoning that I made is:
request headers: read but never written;
response headers: written but never read.
Therefore the read and write operations are orthogonal and can be (probably
confusingly) combined. Probably a Map is not the right interface, probably a
specific clearer interface should be better.
Thanks
Antonio
Re: Tiles Request API
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 11-10-25 03:51 AM, Antonio Petrelli wrote:
> 2011/10/24 Nicolas LE BAS<ma...@nlebas.net>
>> * getApplicationContext is only used as way to access the Application
>> scope. I'm afraid there may be two solutions to a single purpose here. I
>> understand why Renderers need to know the ApplicationContext, but perhaps it
>> should be provided to them as separate object during initialization, and the
>> application scope should be accessible only through the Request interface to
>> avoid confusion.
>>
>
> So you want to remove ApplicationContext.getApplicationScope()? If yes
> notice that this way, all the objects that need a connection to the
> application scope won't be able to do it at startup-time, because they need
> a request. Sincerely I am -1 for it.
>
Actually, no, the way I said it is confusing. I want to remove
getApplicationContext from Request, and have the renderers that need it
implement ApplicationContextAware instead.
The application scope can be accessed through
getContext(APPLICATION_SCOPE) instead of
getApplicationContext().getApplicationScope().
ApplicationContext itself is fine by me.
>
>> * isResponseCommitted, dispatch and include: those are a pain to implement.
>> These concepts are unrelated to the request we are currently processing:
>> they belong to the ApplicationContext (like getRequestDispatcher belongs to
>> the ServletContext). Besides, I'm not sure why we need "dispatch" and
>> "isResponseCommitted" from a theoretical point of view. Forwarding a request
>> is a controller behaviour, not something a renderer should attempt. It's
>> only used by Tiles as an optimization of "include" for the servlet
>> environment, and optimizations should not be exposed in the API imho.
>>
>
> I think that this doubt is due to the fact that you are changing the scope
> of the Request API. What I had in mind is to abstract requests in
> requests-based technologies (Servlets and Portlets) and have a common API
> with related view technologies.
You're right, I'm widening the scope a lot, I realize that now.
I agree that isResponseCommitted is still useful, to support
setContentType and the response headers.
Nevertheless even if you just want a common abstraction to javax.servlet
and javax.portlet, both "include" and "dispatch" belong to
ApplicationContext. Just like getRequestDispatcher belongs to
SerlvetContext and PortletContext, not their Request counterparts.
Those 2 methods are for deciding who should deal with the request. It's
not the guy who submits the Request object who should decide that, nor
the Renderer. It belongs elsewhere, both JavaEE and in tiles. The API
should reflect that fact, and that would actually make it more simple to
use and to implement.
> Probably the right thing to do is to create an extended interface so to
> have:
> * a base Request containing context attributes and a bunch of more methods
> (you're doing a great analysis so I'll leave it to you :-D )
> * an extended Request for Java EE requests.
> This way, specific requests for Velocity+Servlets and Freemarker+Servlets
> should wrap an instance of Request of the second type.
> WDYT?
Sounds like a fair compromise :) But my priorities are elsewhere right
now. Perhaps next month!
>> The response headers are missing, too (except for ContentType), and that's
>> a good thing, too. They're a concern of the controller, not the renderer,
>> and because they're not there, the API helps to avoid mistakes.
>
> Well, no. The Request.getHeader* methods return a map that can write to
> response headers. See this class (setValue method):
> http://svn.eu.apache.org/repos/asf/tiles/framework/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/HeaderExtractor.java
I admit that I totally missed that part. It's far from obvious: the
javadoc says: "Return an immutable Map that maps header names to the set
of all values specified in the request".
It feels weird, too, because that's a Map you can write to, but never
read the data back again. That's what the servlet API does, but that's
not what people expect of a Map. As a user, I'd rather have separate
maps for request headers and response headers.
Thanks for the infos anyway, I learn a bit more every time :)
Nick
Re: Tiles Request API
Posted by Antonio Petrelli <an...@gmail.com>.
2011/10/24 Nicolas LE BAS <ma...@nlebas.net>
> I've been playing a bit with the Tiles Request API (awaiting github :))
It seems that the Infra team made a mistake in synchronizing SVN to GIT
(they put only a branch, strange) so you have to wait a bit more.
> * getApplicationContext is only used as way to access the Application
> scope. I'm afraid there may be two solutions to a single purpose here. I
> understand why Renderers need to know the ApplicationContext, but perhaps it
> should be provided to them as separate object during initialization, and the
> application scope should be accessible only through the Request interface to
> avoid confusion.
>
So you want to remove ApplicationContext.getApplicationScope()? If yes
notice that this way, all the objects that need a connection to the
application scope won't be able to do it at startup-time, because they need
a request. Sincerely I am -1 for it.
> * isResponseCommitted, dispatch and include: those are a pain to implement.
> These concepts are unrelated to the request we are currently processing:
> they belong to the ApplicationContext (like getRequestDispatcher belongs to
> the ServletContext). Besides, I'm not sure why we need "dispatch" and
> "isResponseCommitted" from a theoretical point of view. Forwarding a request
> is a controller behaviour, not something a renderer should attempt. It's
> only used by Tiles as an optimization of "include" for the servlet
> environment, and optimizations should not be exposed in the API imho.
>
I think that this doubt is due to the fact that you are changing the scope
of the Request API. What I had in mind is to abstract requests in
requests-based technologies (Servlets and Portlets) and have a common API
with related view technologies.
Probably the right thing to do is to create an extended interface so to
have:
* a base Request containing context attributes and a bunch of more methods
(you're doing a great analysis so I'll leave it to you :-D )
* an extended Request for Java EE requests.
This way, specific requests for Velocity+Servlets and Freemarker+Servlets
should wrap an instance of Request of the second type.
WDYT?
> * setContentType is a controller concept, a renderer should not attempt to
> change the content type. Besides, Tiles is not using it.
>
I agree, but anyway I will move it to the request of the second type, see
above.
> * getOutputStream would be useful if we wanted to render binary data, but
> none of our current technologies actually support it. Perhaps a future
> addition?
>
This was just to have a resemblance to Servlet/Portlet response. To move to
the request of the second type, IMHO.
> * getRequestObjects seems only here for historical purpose; it is not used.
>
+1 to remove it.
>
> The response headers are missing, too (except for ContentType), and that's
> a good thing, too. They're a concern of the controller, not the renderer,
> and because they're not there, the API helps to avoid mistakes.
>
Well, no. The Request.getHeader* methods return a map that can write to
response headers. See this class (setValue method):
http://svn.eu.apache.org/repos/asf/tiles/framework/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/HeaderExtractor.java
Overall it's a good API and we can do much by extending Tiles 3 that we
> could not do with Tiles 2. IMHO it still needs improvement, but perhaps that
> can be postponed for a while.
>
It's never been released yet so you can still play with it without fear of
breaking anything.
It's just my opinion anyway, but I hope this can help in improving tiles
> further.
>
Absolutely!
Thanks
Antonio