You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Jakob Külzer <ja...@gmail.com> on 2012/04/17 23:05:50 UTC

Using Filter and RequestDispatcher to substitute responses

Hello everybody,

I'm just starting to get my head wrapped around Sling and Day CQ 5 and
I'm running against an issue that I don't really understand properly.
I'm still quite new to Sling, so if you see something where I've got
an incorrect understanding feel free to jump in and correct me.
Thanks!

What I am trying to do is to replace certain request with output from
a different JSP. For example, a page contains an embedded component
which, as far as I understand, is referenced via a Sling resourceType.
When Sling renders this page, the JSP will be included; however, based
on certain conditions I'd like to render a different JSP. My naive
approach is to register a filter in the include chain, create a
RequestDispatcher for the new JSP and call the include() method.

My first question is, am I using the appropriate approach for this?
Should I look at another mechanism in Sling to do something like this?

Second, assuming a filter is the correct approach, why does it not
render my JSP?

Here're the relevant lines of code:

---->8--------------------------------------------------------------
@org.apache.felix.scr.annotations.Component(immediate = true, metatype = false)
@Service(Filter.class)
@Properties({ @Property(name = "sling.filter.scope", value = {
"include", "forward" }) })
public class ComponentFilter implements Filter {

 public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
 final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
 final Resource resource = ...; // Get resource for the JSP.
 if (localResource != null) {
   final RequestDispatcher requestDispatcher =
slingRequest.getRequestDispatcher(resource);
   requestDispatcher.include(request, response);
 } else {
  chain.doFilter(request, response);
 }
 }

}
---->8--------------------------------------------------------------

The resource is properly resolved:

*DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
path=/apps/myapp/components/demo/replacment.jsp

However, I get this error:

17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
/content/geometrixx/en/test.html HTTP/1.1]
org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
extension html, cannot render resource JcrNodeResource, type=nt:file,
superType=null, path=/apps/myapp/components/demo/replacment.jsp

I'm not sure what to make of this. Any ideas?

Any help is appreciated! Thank you.


-- 
Cheers,
Jakob

RE: Using Filter and RequestDispatcher to substitute responses

Posted by Igor Sechyn <is...@pro-vision.de>.
Hi Jakob,

Yes that is exactly what it is. So while trying to resolve a servlet/script for the resource, sling would use the resourceType value specified in the requestDispatcherOptions instead of the value specified in the resource. 

Cheers, Igor.

pro!vision GmbH
Telefon +49 (69) 8700328-21
Geschäftsführer: Karim Khan, Stefan Seifert
Sitz der Gesellschaft: Berlin
AG Charlottenburg, HRB78141, USt-IdNr. DE209954974

>-----Original Message-----
>From: Jakob Külzer [mailto:jakob.kuelzer@gmail.com]
>Sent: Wednesday, April 18, 2012 9:19 PM
>To: users@sling.apache.org
>Subject: Re: Using Filter and RequestDispatcher to substitute responses
>
>Hello Igor,
>
>thank you for your response. I've looked at the
>RequestDispatcherOptions object. However the docs are not quite clear
>to me, it says "resource type". Does this refer to a Sling
>resourceType, like "myapp/components/comp"?
>
>On Wed, Apr 18, 2012 at 3:17 AM, Igor Sechyn <is...@pro-vision.de>
>wrote:
>> Hi Jakob,
>>
>> You might want to have a look at the
>>
>> - getRequestDispatcher(Resource resource,  RequestDispatcherOptions
>options)
>>
>>  method of the SlingHttpServletRequest. In the options you can actually
>force a resource type, which will be used during dispatching instead of the
>value in your original resource.
>>
>> Cheers, Igor.
>>
>>
>> pro!vision GmbH
>> Telefon +49 (69) 8700328-21
>> Geschäftsführer: Karim Khan, Stefan Seifert
>> Sitz der Gesellschaft: Berlin
>> AG Charlottenburg, HRB78141, USt-IdNr. DE209954974
>>
>>
>>>-----Original Message-----
>>>From: Jakob Külzer [mailto:jakob.kuelzer@gmail.com]
>>>Sent: Tuesday, April 17, 2012 11:06 PM
>>>To: users@sling.apache.org
>>>Subject: Using Filter and RequestDispatcher to substitute responses
>>>
>>>Hello everybody,
>>>
>>>I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>>>I'm running against an issue that I don't really understand properly.
>>>I'm still quite new to Sling, so if you see something where I've got
>>>an incorrect understanding feel free to jump in and correct me.
>>>Thanks!
>>>
>>>What I am trying to do is to replace certain request with output from
>>>a different JSP. For example, a page contains an embedded component
>>>which, as far as I understand, is referenced via a Sling resourceType.
>>>When Sling renders this page, the JSP will be included; however, based
>>>on certain conditions I'd like to render a different JSP. My naive
>>>approach is to register a filter in the include chain, create a
>>>RequestDispatcher for the new JSP and call the include() method.
>>>
>>>My first question is, am I using the appropriate approach for this?
>>>Should I look at another mechanism in Sling to do something like this?
>>>
>>>Second, assuming a filter is the correct approach, why does it not
>>>render my JSP?
>>>
>>>Here're the relevant lines of code:
>>>
>>>---->8--------------------------------------------------------------
>>>@org.apache.felix.scr.annotations.Component(immediate = true,
>metatype =
>>>false)
>>>@Service(Filter.class)
>>>@Properties({ @Property(name = "sling.filter.scope", value = {
>>>"include", "forward" }) })
>>>public class ComponentFilter implements Filter {
>>>
>>> public void doFilter(ServletRequest request, ServletResponse
>>>response, FilterChain chain) throws IOException, ServletException {
>>> final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>>>request;
>>> final Resource resource = ...; // Get resource for the JSP.
>>> if (localResource != null) {
>>>   final RequestDispatcher requestDispatcher =
>>>slingRequest.getRequestDispatcher(resource);
>>>   requestDispatcher.include(request, response);
>>> } else {
>>>  chain.doFilter(request, response);
>>> }
>>> }
>>>
>>>}
>>>---->8--------------------------------------------------------------
>>>
>>>The resource is properly resolved:
>>>
>>>*DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>>>path=/apps/myapp/components/demo/replacment.jsp
>>>
>>>However, I get this error:
>>>
>>>17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>>>/content/geometrixx/en/test.html HTTP/1.1]
>>>org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>>>extension html, cannot render resource JcrNodeResource, type=nt:file,
>>>superType=null, path=/apps/myapp/components/demo/replacment.jsp
>>>
>>>I'm not sure what to make of this. Any ideas?
>>>
>>>Any help is appreciated! Thank you.
>>>
>>>
>>>--
>>>Cheers,
>>>Jakob
>
>
>
>--
>Cheers,
>Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hello Igor,

thank you for your response. I've looked at the
RequestDispatcherOptions object. However the docs are not quite clear
to me, it says "resource type". Does this refer to a Sling
resourceType, like "myapp/components/comp"?

On Wed, Apr 18, 2012 at 3:17 AM, Igor Sechyn <is...@pro-vision.de> wrote:
> Hi Jakob,
>
> You might want to have a look at the
>
> - getRequestDispatcher(Resource resource,  RequestDispatcherOptions options)
>
>  method of the SlingHttpServletRequest. In the options you can actually force a resource type, which will be used during dispatching instead of the value in your original resource.
>
> Cheers, Igor.
>
>
> pro!vision GmbH
> Telefon +49 (69) 8700328-21
> Geschäftsführer: Karim Khan, Stefan Seifert
> Sitz der Gesellschaft: Berlin
> AG Charlottenburg, HRB78141, USt-IdNr. DE209954974
>
>
>>-----Original Message-----
>>From: Jakob Külzer [mailto:jakob.kuelzer@gmail.com]
>>Sent: Tuesday, April 17, 2012 11:06 PM
>>To: users@sling.apache.org
>>Subject: Using Filter and RequestDispatcher to substitute responses
>>
>>Hello everybody,
>>
>>I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>>I'm running against an issue that I don't really understand properly.
>>I'm still quite new to Sling, so if you see something where I've got
>>an incorrect understanding feel free to jump in and correct me.
>>Thanks!
>>
>>What I am trying to do is to replace certain request with output from
>>a different JSP. For example, a page contains an embedded component
>>which, as far as I understand, is referenced via a Sling resourceType.
>>When Sling renders this page, the JSP will be included; however, based
>>on certain conditions I'd like to render a different JSP. My naive
>>approach is to register a filter in the include chain, create a
>>RequestDispatcher for the new JSP and call the include() method.
>>
>>My first question is, am I using the appropriate approach for this?
>>Should I look at another mechanism in Sling to do something like this?
>>
>>Second, assuming a filter is the correct approach, why does it not
>>render my JSP?
>>
>>Here're the relevant lines of code:
>>
>>---->8--------------------------------------------------------------
>>@org.apache.felix.scr.annotations.Component(immediate = true, metatype =
>>false)
>>@Service(Filter.class)
>>@Properties({ @Property(name = "sling.filter.scope", value = {
>>"include", "forward" }) })
>>public class ComponentFilter implements Filter {
>>
>> public void doFilter(ServletRequest request, ServletResponse
>>response, FilterChain chain) throws IOException, ServletException {
>> final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>>request;
>> final Resource resource = ...; // Get resource for the JSP.
>> if (localResource != null) {
>>   final RequestDispatcher requestDispatcher =
>>slingRequest.getRequestDispatcher(resource);
>>   requestDispatcher.include(request, response);
>> } else {
>>  chain.doFilter(request, response);
>> }
>> }
>>
>>}
>>---->8--------------------------------------------------------------
>>
>>The resource is properly resolved:
>>
>>*DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>>path=/apps/myapp/components/demo/replacment.jsp
>>
>>However, I get this error:
>>
>>17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>>/content/geometrixx/en/test.html HTTP/1.1]
>>org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>>extension html, cannot render resource JcrNodeResource, type=nt:file,
>>superType=null, path=/apps/myapp/components/demo/replacment.jsp
>>
>>I'm not sure what to make of this. Any ideas?
>>
>>Any help is appreciated! Thank you.
>>
>>
>>--
>>Cheers,
>>Jakob



-- 
Cheers,
Jakob

RE: Using Filter and RequestDispatcher to substitute responses

Posted by Igor Sechyn <is...@pro-vision.de>.
Hi Jakob,

You might want to have a look at the 

- getRequestDispatcher(Resource resource,  RequestDispatcherOptions options)

 method of the SlingHttpServletRequest. In the options you can actually force a resource type, which will be used during dispatching instead of the value in your original resource. 

Cheers, Igor.


pro!vision GmbH
Telefon +49 (69) 8700328-21
Geschäftsführer: Karim Khan, Stefan Seifert
Sitz der Gesellschaft: Berlin
AG Charlottenburg, HRB78141, USt-IdNr. DE209954974


>-----Original Message-----
>From: Jakob Külzer [mailto:jakob.kuelzer@gmail.com]
>Sent: Tuesday, April 17, 2012 11:06 PM
>To: users@sling.apache.org
>Subject: Using Filter and RequestDispatcher to substitute responses
>
>Hello everybody,
>
>I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>I'm running against an issue that I don't really understand properly.
>I'm still quite new to Sling, so if you see something where I've got
>an incorrect understanding feel free to jump in and correct me.
>Thanks!
>
>What I am trying to do is to replace certain request with output from
>a different JSP. For example, a page contains an embedded component
>which, as far as I understand, is referenced via a Sling resourceType.
>When Sling renders this page, the JSP will be included; however, based
>on certain conditions I'd like to render a different JSP. My naive
>approach is to register a filter in the include chain, create a
>RequestDispatcher for the new JSP and call the include() method.
>
>My first question is, am I using the appropriate approach for this?
>Should I look at another mechanism in Sling to do something like this?
>
>Second, assuming a filter is the correct approach, why does it not
>render my JSP?
>
>Here're the relevant lines of code:
>
>---->8--------------------------------------------------------------
>@org.apache.felix.scr.annotations.Component(immediate = true, metatype =
>false)
>@Service(Filter.class)
>@Properties({ @Property(name = "sling.filter.scope", value = {
>"include", "forward" }) })
>public class ComponentFilter implements Filter {
>
> public void doFilter(ServletRequest request, ServletResponse
>response, FilterChain chain) throws IOException, ServletException {
> final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>request;
> final Resource resource = ...; // Get resource for the JSP.
> if (localResource != null) {
>   final RequestDispatcher requestDispatcher =
>slingRequest.getRequestDispatcher(resource);
>   requestDispatcher.include(request, response);
> } else {
>  chain.doFilter(request, response);
> }
> }
>
>}
>---->8--------------------------------------------------------------
>
>The resource is properly resolved:
>
>*DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>path=/apps/myapp/components/demo/replacment.jsp
>
>However, I get this error:
>
>17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>/content/geometrixx/en/test.html HTTP/1.1]
>org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>extension html, cannot render resource JcrNodeResource, type=nt:file,
>superType=null, path=/apps/myapp/components/demo/replacment.jsp
>
>I'm not sure what to make of this. Any ideas?
>
>Any help is appreciated! Thank you.
>
>
>--
>Cheers,
>Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
If you have your page template check for the user agent, you can redirect
the URL to currentPage.getPath()+".mobile.html" and that should be handled
by your mobile.jsp in that page component.

That being said, for mobile, the example given by cq5 shows a completely
separate site tree where page components inherited
wcm/components/mobile/page which has further device detecting API to then
redirect to other selectors like ".smart.html" or ".touch.html". If you
choose to go down this path you can either redirect to the mobile site from
the desktop site or be redirected from the web server via some mod_rewrite
which can inspect the user agent string and redirect to the mobile site if
it matches some mobile specific strings.

Sarwar
On Apr 18, 2012 3:26 AM, "Jakob Külzer" <ja...@gmail.com> wrote:

> Hello Sarwar,
>
> Thank you for your explanations. I didn't know about the cheat sheet.
> Now it's bookmarked, thank you.
>
> From the cheatsheet i understand that the only way to influence the
> script selection for a given node is through the HTTP method, selector
> and extension, correct?
>
> Just out of curiosity, how could I influence the script selection
> outside of these parameters, if possible? While selector and extension
> provide a lot of flexibility, they are insufficient for what I have in
> mind.
>
> Maybe an example will make clear what i am trying to accomplish. The
> original idea was born as i implemented a custom solution on Day CQ 4.
> In order to support, for example, mobile versions of components, i
> introduced the concept of a rendition. At the beginning of the request
> the template would determine the rendition and store it in the
> request. Then I added a custom component loader that would check if
> there was a rendition specific script. For example, if rendition was
> set to "mobile", it would look for a mobile.jsp and render it instead
> of the actual component JSP. This way the implemented site
> transparently switches to mobile (or whatever rendition has been
> selected) markup while maintaining the same URL structure. This turned
> out to be a quite flexible and useful model and now I would like to
> implement similar functionality for Sling based systems. Any
> suggestions how I could go about this?
>
> Regards,
> Jakob
>
>
> On Tue, Apr 17, 2012 at 6:28 PM, Sarwar Bhuiyan
> <sa...@gmail.com> wrote:
> > It looks at the sling:resourceType and then selects a script (either a
> JSP
> > in a component folder or a Sling Servlet where the resource type is
> > specified).  There is one other way of doing it which is the legacy way
> of
> > registering a Sling Servlet to a specific path.  All this is being done
> by
> > the SlingMainServlet which is in essence a dispatcher which selects a
> > script or servlet based on a combination of sling:resourceType,
> selectors,
> > and extensions (or the fixed path).  The other way would be to register
> > your own servlet with the http service in OSGi and then in that servlet
> > itself do a forward() or redirect() to a specific path.
> >
> > In Sling, we tend not to do forwards or redirects to JSPs but rather to
> > paths to resources which then have a backing scripts which Sling selects.
> >  I take it you've seen the Sling Cheatsheet which shows the different
> ways
> > a script can be chosen (it doesn't include using a Sling Servlet though
> but
> > you can see examples of those in the Sling source code).  This way, you
> > never have to directly call a JSP yourself and this level of indirection
> > allows you to be able to do stuff like attaching ACLs to the node path
> > you're calling or change the backing implementation later on without
> > changing the outside URLs.
> >
> > Hope this makes sense somewhat.
> >
> > Sarwar
> >
> > On Tue, Apr 17, 2012 at 10:55 PM, Jakob Külzer <jakob.kuelzer@gmail.com
> >wrote:
> >
> >> Hello Sarwar,
> >>
> >> thank you for your response. I've considered using selectors, however
> >> that alters the URL for the page, which is undesirable.
> >>
> >> The other approach, using if-else statements in the JSP is something I
> >> have prototyped using a taglib, which works fine, but it requires that
> >> each JSP has to be wrapped into the tag.
> >>
> >> Ideally, i could handle this completely transparently. The key idea is
> >> to keep the actual JSPs clear of the actual switching behaviour. After
> >> all, the same behaviour will be used in many different JSPs, and I
> >> don't like the idea of having to rely on developers manually cutting
> >> and pasting code between JSPs.
> >>
> >> I was wondering how Sling selects which script actually to render.
> >> From my understanding that happens when Sling evaluates the
> >> sling:resourceType field. Is there a way to hook up to that decision
> >> process?
> >>
> >> Regards,
> >> Jakob
> >>
> >> On Tue, Apr 17, 2012 at 5:36 PM, Sarwar Bhuiyan
> >> <sa...@gmail.com> wrote:
> >> > You could use different scripts in the same component and call them
> based
> >> > on your condition.  So supposing your component is called
> >> >
> >> > mycomponent/
> >> >   mycomponent.jsp
> >> >   optiona.jsp
> >> >   optionb.jsp
> >> >
> >> > In your mycomponent.jsp, you can do something like:
> >> >
> >> >
> >> > <%if(condition){%>
> >> >   <cq:include script="optiona.jsp"/>
> >> > <%else {%}
> >> >   <cq:include script="optionb.jsp"/>
> >> > <%}%>
> >> >
> >> >
> >> >
> >> > Sarwar
> >> >
> >> >
> >> > On Tue, Apr 17, 2012 at 10:34 PM, Sarwar Bhuiyan
> >> > <sa...@gmail.com>wrote:
> >> >
> >> >> You could use a selector.  So supposing your component is called
> >> >>
> >> >> mycomponent/
> >> >>
> >> >>
> >> >> On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <
> jakob.kuelzer@gmail.com
> >> >wrote:
> >> >>
> >> >>> Hello everybody,
> >> >>>
> >> >>> I'm just starting to get my head wrapped around Sling and Day CQ 5
> and
> >> >>> I'm running against an issue that I don't really understand
> properly.
> >> >>> I'm still quite new to Sling, so if you see something where I've got
> >> >>> an incorrect understanding feel free to jump in and correct me.
> >> >>> Thanks!
> >> >>>
> >> >>> What I am trying to do is to replace certain request with output
> from
> >> >>> a different JSP. For example, a page contains an embedded component
> >> >>> which, as far as I understand, is referenced via a Sling
> resourceType.
> >> >>> When Sling renders this page, the JSP will be included; however,
> based
> >> >>> on certain conditions I'd like to render a different JSP. My naive
> >> >>> approach is to register a filter in the include chain, create a
> >> >>> RequestDispatcher for the new JSP and call the include() method.
> >> >>>
> >> >>> My first question is, am I using the appropriate approach for this?
> >> >>> Should I look at another mechanism in Sling to do something like
> this?
> >> >>>
> >> >>> Second, assuming a filter is the correct approach, why does it not
> >> >>> render my JSP?
> >> >>>
> >> >>> Here're the relevant lines of code:
> >> >>>
> >> >>> ---->8--------------------------------------------------------------
> >> >>> @org.apache.felix.scr.annotations.Component(immediate = true,
> metatype
> >> =
> >> >>> false)
> >> >>> @Service(Filter.class)
> >> >>> @Properties({ @Property(name = "sling.filter.scope", value = {
> >> >>> "include", "forward" }) })
> >> >>> public class ComponentFilter implements Filter {
> >> >>>
> >> >>>  public void doFilter(ServletRequest request, ServletResponse
> >> >>> response, FilterChain chain) throws IOException, ServletException {
> >> >>>  final SlingHttpServletRequest slingRequest =
> (SlingHttpServletRequest)
> >> >>> request;
> >> >>>  final Resource resource = ...; // Get resource for the JSP.
> >> >>>  if (localResource != null) {
> >> >>>   final RequestDispatcher requestDispatcher =
> >> >>> slingRequest.getRequestDispatcher(resource);
> >> >>>   requestDispatcher.include(request, response);
> >> >>>  } else {
> >> >>>  chain.doFilter(request, response);
> >> >>>  }
> >> >>>  }
> >> >>>
> >> >>> }
> >> >>> ---->8--------------------------------------------------------------
> >> >>>
> >> >>> The resource is properly resolved:
> >> >>>
> >> >>> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
> >> >>> path=/apps/myapp/components/demo/replacment.jsp
> >> >>>
> >> >>> However, I get this error:
> >> >>>
> >> >>> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
> >> >>> /content/geometrixx/en/test.html HTTP/1.1]
> >> >>> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
> >> >>> extension html, cannot render resource JcrNodeResource,
> type=nt:file,
> >> >>> superType=null, path=/apps/myapp/components/demo/replacment.jsp
> >> >>>
> >> >>> I'm not sure what to make of this. Any ideas?
> >> >>>
> >> >>> Any help is appreciated! Thank you.
> >> >>>
> >> >>>
> >> >>> --
> >> >>> Cheers,
> >> >>> Jakob
> >> >>>
> >> >>
> >> >>
> >>
> >>
> >>
> >> --
> >> Cheers,
> >> Jakob
> >>
>
>
>
> --
> Cheers,
> Jakob
>

Re: Using Filter and RequestDispatcher to substitute responses

Posted by "David G." <da...@gmail.com>.
and that should read:

Also, you could use Sling Rewriter Pipelines/Processors [1] to write
all your links to include the mobile selector (page.mobile.html)  --
not .com :)

On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com> wrote:
> Jakob,
>
> Im dont mean to be snide, but if you dont want to/can't meet your
> requirements using a restful architecture, why are you using Sling as
> your framework? It seems like you'd be much better served using some
> other framework. I do sympathize as I came from a more lax MVC-based
> web-app background and its easy to slip back into a behavior-based
> mindset (rather than a content-based mindset).
>
> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
> all your links to include the mobile selector (page.mobile.com) if to
> make links on your mobile site point to "mobile" resource
> representations.
>
> [1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html
>
>
> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
>> Hi,
>>
>> I'll have a look at these links, thank you.
>>
>> I agree that what I'm toying with is breaking the RESTfulness of
>> Sling, but at the end of the day we're not building RESTful webapps,
>> we're building consumer websites. Therefore REST is not that much of
>> an issue.
>>
>> However, the more I read about Sling, the more i realize I might be
>> approaching this the in the wrong way. But let's assume i'd be
>> building a mobile version using selectors, i'd have to ensure that all
>> rendered internal links have the appropriate selector attached,
>> otherwise a client would "break out" of the specific version of the
>> website. Does Sling support me with link externalization?
>>
>> Cheers,
>> Jakob
>>
>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>> so, I think you're not getting a direct answer because you "url is the same"
>>> requirement is not exactly in line with the fundamentals of sling.
>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>> want something different, then you use a different url. The focus is on
>>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>> pretty much breaks a lot of the power of sling and forces you to put
>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>> do the job rather than writing custom code.
>>>
>>>
>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>
>>> --
>>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Cheers,
>> Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by "David G." <da...@gmail.com>.
Ah - I see.

If its not a "blanket" rule like adding a ".mobile" selector to every
<a href="[relative-link]"> youll most likely have to do just update
your JSPs to use the appropriate selectors/suffixes, and split your
JSPs apart (or rename the include JSPs). Without knowing all the
variations/patterns/etc. of what youre trying do its hard to come up
with suggestions other than altering the links themselves in your
content -- i doubt there will be a "silver bullet" solution for you
though.

If you want to do handle the path mapping, for example serving:
    example.com/content/site/en/home.html
from
    example.com/en/home.html

I would suggest setting a one-way mapping on your jcr resource resolver for:
     /content/site/en < /en
and then using your webserver to re-write requests coming on in
example.com to rewrite
     /en/(.*) -> /content/site/en/$1




On Wed, Apr 18, 2012 at 4:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
> Hey David,
>
> No problem. ;) Yeah, we're about to move to Day 5 and/or have projects
> that only run on Day 5 which I'm moved onto now, so I'm trying to
> learn as much as I can.
>
> Day CQ 4 is not based on Sling. In general Day CQ 4 is a very...
> inflexible platform that here and there shows ideas similar to Sling,
> but nowhere as refined. In order to get our stuff working on CQ 4 I
> ended up decompiling a lot of it's core jar files... ;)
>
> By "externalizing" (in lieu of a better word) i mean translating an
> internal reference like /content/site/lang/some/page to an external
> usable path like /lang/some/page.<selectors>.<extension></suffix>. For
> our use cases (consumer sites) rewriting the URLs to a "user friendly"
> URL is important.
>
> Hope that makes sense.
>
> Cheers,
> Jakob
>
> On Wed, Apr 18, 2012 at 4:37 PM, David G. <da...@gmail.com> wrote:
>> Jakob,
>>
>> Ah - sorry, i missed that you are on CQ. That's a very good reason to
>> be using Sling (whether you want to or not :)).
>> Did CQ4 not use Sling (CQ4 was before my time)?
>>
>> The CQ Link Rewriter uses pipelines to do thinks like remove invalid
>> links and and rewrite outgoing urls (if you're using URI mapping via
>> etc/map or in the jcrresourceresolver).
>>
>> Check out the package Justin E. put together for an example of
>> Pipelines -its a bit more instructive than the Sling docs. [1]
>>
>> Can you expound on what you mean by "externalizing" links? Do you just
>> mean adding specific selectors to links based on some criteria?
>>
>> [1] http://www.wemblog.com/2011/08/how-to-remove-html-extension-from-url.html
>>
>>
>>
>>
>>
>> On Wed, Apr 18, 2012 at 3:44 PM, Jakob Külzer <ja...@gmail.com> wrote:
>>> Hello David,
>>> no, you're not rude at all, in fact I am grateful for suggestions. As
>>> i've acknowledged earlier, i might be doing the completely wrong
>>> thing. As for now, I'm trying to understand how Sling works and if I
>>> can port the successful patterns that we implemented on Day CQ 4
>>> (ancient!) on the much newer Day CQ 5. With that said, the reason why
>>> I'm looking at Sling is obviously that CQ 5 is built on Sling.
>>>
>>> The rewriter pipeline is a very interesting piece, I'll check it out.
>>> Thank you for the pointer. How do other people solve the issue of
>>> correctly externalizing internal links? Does anybody have experience
>>> with this?
>>>
>>> Cheers,
>>> Jakob
>>>
>>> On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com> wrote:
>>>> Jakob,
>>>>
>>>> Im dont mean to be snide, but if you dont want to/can't meet your
>>>> requirements using a restful architecture, why are you using Sling as
>>>> your framework? It seems like you'd be much better served using some
>>>> other framework. I do sympathize as I came from a more lax MVC-based
>>>> web-app background and its easy to slip back into a behavior-based
>>>> mindset (rather than a content-based mindset).
>>>>
>>>> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
>>>> all your links to include the mobile selector (page.mobile.com) if to
>>>> make links on your mobile site point to "mobile" resource
>>>> representations.
>>>>
>>>> [1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html
>>>>
>>>>
>>>> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> I'll have a look at these links, thank you.
>>>>>
>>>>> I agree that what I'm toying with is breaking the RESTfulness of
>>>>> Sling, but at the end of the day we're not building RESTful webapps,
>>>>> we're building consumer websites. Therefore REST is not that much of
>>>>> an issue.
>>>>>
>>>>> However, the more I read about Sling, the more i realize I might be
>>>>> approaching this the in the wrong way. But let's assume i'd be
>>>>> building a mobile version using selectors, i'd have to ensure that all
>>>>> rendered internal links have the appropriate selector attached,
>>>>> otherwise a client would "break out" of the specific version of the
>>>>> website. Does Sling support me with link externalization?
>>>>>
>>>>> Cheers,
>>>>> Jakob
>>>>>
>>>>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>>>>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>>>>> so, I think you're not getting a direct answer because you "url is the same"
>>>>>> requirement is not exactly in line with the fundamentals of sling.
>>>>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>>>>> want something different, then you use a different url. The focus is on
>>>>>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>>>>>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>>>>>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>>>>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>>>>> pretty much breaks a lot of the power of sling and forces you to put
>>>>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>>>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>>>>> do the job rather than writing custom code.
>>>>>>
>>>>>>
>>>>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>>>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>>>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>>>>
>>>>>> --
>>>>>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>>>>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Cheers,
>>>>> Jakob
>>>
>>>
>>>
>>> --
>>> Cheers,
>>> Jakob
>
>
>
> --
> Cheers,
> Jakob

RE: Using Filter and RequestDispatcher to substitute responses

Posted by Igor Sechyn <is...@pro-vision.de>.
If you are looking at the url mapping in the context of cq 5 you should probably read [1]. It describes how to add custom configuration for the JcrResourceResolver to enable the url mapping (stripping of /content/site/ for instance from your urls). For more details check out the map(request, path) method of the ResourceResolver Interface. 
In cq5 environment you also need to take care of the dispatcher configuration in your web server to get the cache invalidation working correctly, see [2]

[1] http://dev.day.com/content/kb/home/cq5/CQ5Troubleshooting/CQ5UpgradeIssues/CQ53Upgrade/CQ53LostResourceResolverMappingsAfterUpgrade.html 
[2] http://dev.day.com/content/kb/home/cq5/CQ5SystemAdministration/HowToFlushMappedContent.html
 
Cheers, Igor.

pro!vision GmbH
Telefon +49 (69) 8700328-21
Geschäftsführer: Karim Khan, Stefan Seifert
Sitz der Gesellschaft: Berlin
AG Charlottenburg, HRB78141, USt-IdNr. DE209954974


>-----Original Message-----
>From: Jakob Külzer [mailto:jakob.kuelzer@gmail.com]
>Sent: Wednesday, April 18, 2012 10:56 PM
>To: users@sling.apache.org
>Subject: Re: Using Filter and RequestDispatcher to substitute responses
>
>Hey David,
>
>No problem. ;) Yeah, we're about to move to Day 5 and/or have projects
>that only run on Day 5 which I'm moved onto now, so I'm trying to
>learn as much as I can.
>
>Day CQ 4 is not based on Sling. In general Day CQ 4 is a very...
>inflexible platform that here and there shows ideas similar to Sling,
>but nowhere as refined. In order to get our stuff working on CQ 4 I
>ended up decompiling a lot of it's core jar files... ;)
>
>By "externalizing" (in lieu of a better word) i mean translating an
>internal reference like /content/site/lang/some/page to an external
>usable path like /lang/some/page.<selectors>.<extension></suffix>. For
>our use cases (consumer sites) rewriting the URLs to a "user friendly"
>URL is important.
>
>Hope that makes sense.
>
>Cheers,
>Jakob
>
>On Wed, Apr 18, 2012 at 4:37 PM, David G. <da...@gmail.com>
>wrote:
>> Jakob,
>>
>> Ah - sorry, i missed that you are on CQ. That's a very good reason to
>> be using Sling (whether you want to or not :)).
>> Did CQ4 not use Sling (CQ4 was before my time)?
>>
>> The CQ Link Rewriter uses pipelines to do thinks like remove invalid
>> links and and rewrite outgoing urls (if you're using URI mapping via
>> etc/map or in the jcrresourceresolver).
>>
>> Check out the package Justin E. put together for an example of
>> Pipelines -its a bit more instructive than the Sling docs. [1]
>>
>> Can you expound on what you mean by "externalizing" links? Do you just
>> mean adding specific selectors to links based on some criteria?
>>
>> [1] http://www.wemblog.com/2011/08/how-to-remove-html-extension-
>from-url.html
>>
>>
>>
>>
>>
>> On Wed, Apr 18, 2012 at 3:44 PM, Jakob Külzer <ja...@gmail.com>
>wrote:
>>> Hello David,
>>> no, you're not rude at all, in fact I am grateful for suggestions. As
>>> i've acknowledged earlier, i might be doing the completely wrong
>>> thing. As for now, I'm trying to understand how Sling works and if I
>>> can port the successful patterns that we implemented on Day CQ 4
>>> (ancient!) on the much newer Day CQ 5. With that said, the reason why
>>> I'm looking at Sling is obviously that CQ 5 is built on Sling.
>>>
>>> The rewriter pipeline is a very interesting piece, I'll check it out.
>>> Thank you for the pointer. How do other people solve the issue of
>>> correctly externalizing internal links? Does anybody have experience
>>> with this?
>>>
>>> Cheers,
>>> Jakob
>>>
>>> On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com>
>wrote:
>>>> Jakob,
>>>>
>>>> Im dont mean to be snide, but if you dont want to/can't meet your
>>>> requirements using a restful architecture, why are you using Sling as
>>>> your framework? It seems like you'd be much better served using some
>>>> other framework. I do sympathize as I came from a more lax MVC-based
>>>> web-app background and its easy to slip back into a behavior-based
>>>> mindset (rather than a content-based mindset).
>>>>
>>>> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
>>>> all your links to include the mobile selector (page.mobile.com) if to
>>>> make links on your mobile site point to "mobile" resource
>>>> representations.
>>>>
>>>> [1] http://sling.apache.org/site/rewriting-the-output-through-
>pipelines.html
>>>>
>>>>
>>>> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer
><ja...@gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> I'll have a look at these links, thank you.
>>>>>
>>>>> I agree that what I'm toying with is breaking the RESTfulness of
>>>>> Sling, but at the end of the day we're not building RESTful webapps,
>>>>> we're building consumer websites. Therefore REST is not that much of
>>>>> an issue.
>>>>>
>>>>> However, the more I read about Sling, the more i realize I might be
>>>>> approaching this the in the wrong way. But let's assume i'd be
>>>>> building a mobile version using selectors, i'd have to ensure that all
>>>>> rendered internal links have the appropriate selector attached,
>>>>> otherwise a client would "break out" of the specific version of the
>>>>> website. Does Sling support me with link externalization?
>>>>>
>>>>> Cheers,
>>>>> Jakob
>>>>>
>>>>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org>
>wrote:
>>>>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>>>>> so, I think you're not getting a direct answer because you "url is the
>same"
>>>>>> requirement is not exactly in line with the fundamentals of sling.
>>>>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>>>>> want something different, then you use a different url. The focus is on
>>>>>> content, so you have /content/stuff/myEntry, and if you want to view
>it on a
>>>>>> normal browser you go to .../myEntry.html, if you want it for mobile,
>you go
>>>>>> to myEntry.mobile, and if you want a summary, you go to
>myEntry.summary
>>>>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>>>>> pretty much breaks a lot of the power of sling and forces you to put
>>>>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>>>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>>>>> do the job rather than writing custom code.
>>>>>>
>>>>>>
>>>>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>>>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>>>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>>>>
>>>>>> --
>>>>>> View this message in context: http://apache-
>sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-
>responses-tp3918391p3919064.html
>>>>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Cheers,
>>>>> Jakob
>>>
>>>
>>>
>>> --
>>> Cheers,
>>> Jakob
>
>
>
>--
>Cheers,
>Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hey David,

No problem. ;) Yeah, we're about to move to Day 5 and/or have projects
that only run on Day 5 which I'm moved onto now, so I'm trying to
learn as much as I can.

Day CQ 4 is not based on Sling. In general Day CQ 4 is a very...
inflexible platform that here and there shows ideas similar to Sling,
but nowhere as refined. In order to get our stuff working on CQ 4 I
ended up decompiling a lot of it's core jar files... ;)

By "externalizing" (in lieu of a better word) i mean translating an
internal reference like /content/site/lang/some/page to an external
usable path like /lang/some/page.<selectors>.<extension></suffix>. For
our use cases (consumer sites) rewriting the URLs to a "user friendly"
URL is important.

Hope that makes sense.

Cheers,
Jakob

On Wed, Apr 18, 2012 at 4:37 PM, David G. <da...@gmail.com> wrote:
> Jakob,
>
> Ah - sorry, i missed that you are on CQ. That's a very good reason to
> be using Sling (whether you want to or not :)).
> Did CQ4 not use Sling (CQ4 was before my time)?
>
> The CQ Link Rewriter uses pipelines to do thinks like remove invalid
> links and and rewrite outgoing urls (if you're using URI mapping via
> etc/map or in the jcrresourceresolver).
>
> Check out the package Justin E. put together for an example of
> Pipelines -its a bit more instructive than the Sling docs. [1]
>
> Can you expound on what you mean by "externalizing" links? Do you just
> mean adding specific selectors to links based on some criteria?
>
> [1] http://www.wemblog.com/2011/08/how-to-remove-html-extension-from-url.html
>
>
>
>
>
> On Wed, Apr 18, 2012 at 3:44 PM, Jakob Külzer <ja...@gmail.com> wrote:
>> Hello David,
>> no, you're not rude at all, in fact I am grateful for suggestions. As
>> i've acknowledged earlier, i might be doing the completely wrong
>> thing. As for now, I'm trying to understand how Sling works and if I
>> can port the successful patterns that we implemented on Day CQ 4
>> (ancient!) on the much newer Day CQ 5. With that said, the reason why
>> I'm looking at Sling is obviously that CQ 5 is built on Sling.
>>
>> The rewriter pipeline is a very interesting piece, I'll check it out.
>> Thank you for the pointer. How do other people solve the issue of
>> correctly externalizing internal links? Does anybody have experience
>> with this?
>>
>> Cheers,
>> Jakob
>>
>> On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com> wrote:
>>> Jakob,
>>>
>>> Im dont mean to be snide, but if you dont want to/can't meet your
>>> requirements using a restful architecture, why are you using Sling as
>>> your framework? It seems like you'd be much better served using some
>>> other framework. I do sympathize as I came from a more lax MVC-based
>>> web-app background and its easy to slip back into a behavior-based
>>> mindset (rather than a content-based mindset).
>>>
>>> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
>>> all your links to include the mobile selector (page.mobile.com) if to
>>> make links on your mobile site point to "mobile" resource
>>> representations.
>>>
>>> [1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html
>>>
>>>
>>> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
>>>> Hi,
>>>>
>>>> I'll have a look at these links, thank you.
>>>>
>>>> I agree that what I'm toying with is breaking the RESTfulness of
>>>> Sling, but at the end of the day we're not building RESTful webapps,
>>>> we're building consumer websites. Therefore REST is not that much of
>>>> an issue.
>>>>
>>>> However, the more I read about Sling, the more i realize I might be
>>>> approaching this the in the wrong way. But let's assume i'd be
>>>> building a mobile version using selectors, i'd have to ensure that all
>>>> rendered internal links have the appropriate selector attached,
>>>> otherwise a client would "break out" of the specific version of the
>>>> website. Does Sling support me with link externalization?
>>>>
>>>> Cheers,
>>>> Jakob
>>>>
>>>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>>>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>>>> so, I think you're not getting a direct answer because you "url is the same"
>>>>> requirement is not exactly in line with the fundamentals of sling.
>>>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>>>> want something different, then you use a different url. The focus is on
>>>>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>>>>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>>>>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>>>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>>>> pretty much breaks a lot of the power of sling and forces you to put
>>>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>>>> do the job rather than writing custom code.
>>>>>
>>>>>
>>>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>>>
>>>>> --
>>>>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>>>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>>>
>>>>
>>>>
>>>> --
>>>> Cheers,
>>>> Jakob
>>
>>
>>
>> --
>> Cheers,
>> Jakob



-- 
Cheers,
Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by "David G." <da...@gmail.com>.
Jakob,

Ah - sorry, i missed that you are on CQ. That's a very good reason to
be using Sling (whether you want to or not :)).
Did CQ4 not use Sling (CQ4 was before my time)?

The CQ Link Rewriter uses pipelines to do thinks like remove invalid
links and and rewrite outgoing urls (if you're using URI mapping via
etc/map or in the jcrresourceresolver).

Check out the package Justin E. put together for an example of
Pipelines -its a bit more instructive than the Sling docs. [1]

Can you expound on what you mean by "externalizing" links? Do you just
mean adding specific selectors to links based on some criteria?

[1] http://www.wemblog.com/2011/08/how-to-remove-html-extension-from-url.html





On Wed, Apr 18, 2012 at 3:44 PM, Jakob Külzer <ja...@gmail.com> wrote:
> Hello David,
> no, you're not rude at all, in fact I am grateful for suggestions. As
> i've acknowledged earlier, i might be doing the completely wrong
> thing. As for now, I'm trying to understand how Sling works and if I
> can port the successful patterns that we implemented on Day CQ 4
> (ancient!) on the much newer Day CQ 5. With that said, the reason why
> I'm looking at Sling is obviously that CQ 5 is built on Sling.
>
> The rewriter pipeline is a very interesting piece, I'll check it out.
> Thank you for the pointer. How do other people solve the issue of
> correctly externalizing internal links? Does anybody have experience
> with this?
>
> Cheers,
> Jakob
>
> On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com> wrote:
>> Jakob,
>>
>> Im dont mean to be snide, but if you dont want to/can't meet your
>> requirements using a restful architecture, why are you using Sling as
>> your framework? It seems like you'd be much better served using some
>> other framework. I do sympathize as I came from a more lax MVC-based
>> web-app background and its easy to slip back into a behavior-based
>> mindset (rather than a content-based mindset).
>>
>> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
>> all your links to include the mobile selector (page.mobile.com) if to
>> make links on your mobile site point to "mobile" resource
>> representations.
>>
>> [1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html
>>
>>
>> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
>>> Hi,
>>>
>>> I'll have a look at these links, thank you.
>>>
>>> I agree that what I'm toying with is breaking the RESTfulness of
>>> Sling, but at the end of the day we're not building RESTful webapps,
>>> we're building consumer websites. Therefore REST is not that much of
>>> an issue.
>>>
>>> However, the more I read about Sling, the more i realize I might be
>>> approaching this the in the wrong way. But let's assume i'd be
>>> building a mobile version using selectors, i'd have to ensure that all
>>> rendered internal links have the appropriate selector attached,
>>> otherwise a client would "break out" of the specific version of the
>>> website. Does Sling support me with link externalization?
>>>
>>> Cheers,
>>> Jakob
>>>
>>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>>> so, I think you're not getting a direct answer because you "url is the same"
>>>> requirement is not exactly in line with the fundamentals of sling.
>>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>>> want something different, then you use a different url. The focus is on
>>>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>>>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>>>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>>> pretty much breaks a lot of the power of sling and forces you to put
>>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>>> do the job rather than writing custom code.
>>>>
>>>>
>>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>>
>>>> --
>>>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>>
>>>
>>>
>>> --
>>> Cheers,
>>> Jakob
>
>
>
> --
> Cheers,
> Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hello David,
no, you're not rude at all, in fact I am grateful for suggestions. As
i've acknowledged earlier, i might be doing the completely wrong
thing. As for now, I'm trying to understand how Sling works and if I
can port the successful patterns that we implemented on Day CQ 4
(ancient!) on the much newer Day CQ 5. With that said, the reason why
I'm looking at Sling is obviously that CQ 5 is built on Sling.

The rewriter pipeline is a very interesting piece, I'll check it out.
Thank you for the pointer. How do other people solve the issue of
correctly externalizing internal links? Does anybody have experience
with this?

Cheers,
Jakob

On Wed, Apr 18, 2012 at 3:34 PM, David G. <da...@gmail.com> wrote:
> Jakob,
>
> Im dont mean to be snide, but if you dont want to/can't meet your
> requirements using a restful architecture, why are you using Sling as
> your framework? It seems like you'd be much better served using some
> other framework. I do sympathize as I came from a more lax MVC-based
> web-app background and its easy to slip back into a behavior-based
> mindset (rather than a content-based mindset).
>
> Also, you could use Sling Rewriter Pipelines/Processors [1] to write
> all your links to include the mobile selector (page.mobile.com) if to
> make links on your mobile site point to "mobile" resource
> representations.
>
> [1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html
>
>
> On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
>> Hi,
>>
>> I'll have a look at these links, thank you.
>>
>> I agree that what I'm toying with is breaking the RESTfulness of
>> Sling, but at the end of the day we're not building RESTful webapps,
>> we're building consumer websites. Therefore REST is not that much of
>> an issue.
>>
>> However, the more I read about Sling, the more i realize I might be
>> approaching this the in the wrong way. But let's assume i'd be
>> building a mobile version using selectors, i'd have to ensure that all
>> rendered internal links have the appropriate selector attached,
>> otherwise a client would "break out" of the specific version of the
>> website. Does Sling support me with link externalization?
>>
>> Cheers,
>> Jakob
>>
>> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>>> so, I think you're not getting a direct answer because you "url is the same"
>>> requirement is not exactly in line with the fundamentals of sling.
>>> Specifically Sling is like a "RESTful" web service, and by definition if you
>>> want something different, then you use a different url. The focus is on
>>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>>> (plus using whatever url constructs).  Saying that "the url can't change"
>>> pretty much breaks a lot of the power of sling and forces you to put
>>> "if...else if..." somewhere to process other inputs like get parmeters.
>>> Also check out [3] since I think it does a decent job of utilizing sling to
>>> do the job rather than writing custom code.
>>>
>>>
>>> [1] http://sling.apache.org/site/the-sling-engine.html
>>> [2] http://sling.apache.org/site/dispatching-requests.html
>>> [3] http://sling.apache.org/site/46-line-blog.html
>>>
>>> --
>>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>>> Sent from the Sling - Users mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Cheers,
>> Jakob



-- 
Cheers,
Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by "David G." <da...@gmail.com>.
Jakob,

Im dont mean to be snide, but if you dont want to/can't meet your
requirements using a restful architecture, why are you using Sling as
your framework? It seems like you'd be much better served using some
other framework. I do sympathize as I came from a more lax MVC-based
web-app background and its easy to slip back into a behavior-based
mindset (rather than a content-based mindset).

Also, you could use Sling Rewriter Pipelines/Processors [1] to write
all your links to include the mobile selector (page.mobile.com) if to
make links on your mobile site point to "mobile" resource
representations.

[1] http://sling.apache.org/site/rewriting-the-output-through-pipelines.html


On Wed, Apr 18, 2012 at 2:55 PM, Jakob Külzer <ja...@gmail.com> wrote:
> Hi,
>
> I'll have a look at these links, thank you.
>
> I agree that what I'm toying with is breaking the RESTfulness of
> Sling, but at the end of the day we're not building RESTful webapps,
> we're building consumer websites. Therefore REST is not that much of
> an issue.
>
> However, the more I read about Sling, the more i realize I might be
> approaching this the in the wrong way. But let's assume i'd be
> building a mobile version using selectors, i'd have to ensure that all
> rendered internal links have the appropriate selector attached,
> otherwise a client would "break out" of the specific version of the
> website. Does Sling support me with link externalization?
>
> Cheers,
> Jakob
>
> On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
>> I'd suggest you take a look around in [1] and more specifically [2].  Even
>> so, I think you're not getting a direct answer because you "url is the same"
>> requirement is not exactly in line with the fundamentals of sling.
>> Specifically Sling is like a "RESTful" web service, and by definition if you
>> want something different, then you use a different url. The focus is on
>> content, so you have /content/stuff/myEntry, and if you want to view it on a
>> normal browser you go to .../myEntry.html, if you want it for mobile, you go
>> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
>> (plus using whatever url constructs).  Saying that "the url can't change"
>> pretty much breaks a lot of the power of sling and forces you to put
>> "if...else if..." somewhere to process other inputs like get parmeters.
>> Also check out [3] since I think it does a decent job of utilizing sling to
>> do the job rather than writing custom code.
>>
>>
>> [1] http://sling.apache.org/site/the-sling-engine.html
>> [2] http://sling.apache.org/site/dispatching-requests.html
>> [3] http://sling.apache.org/site/46-line-blog.html
>>
>> --
>> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
>> Sent from the Sling - Users mailing list archive at Nabble.com.
>
>
>
> --
> Cheers,
> Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hi,

I'll have a look at these links, thank you.

I agree that what I'm toying with is breaking the RESTfulness of
Sling, but at the end of the day we're not building RESTful webapps,
we're building consumer websites. Therefore REST is not that much of
an issue.

However, the more I read about Sling, the more i realize I might be
approaching this the in the wrong way. But let's assume i'd be
building a mobile version using selectors, i'd have to ensure that all
rendered internal links have the appropriate selector attached,
otherwise a client would "break out" of the specific version of the
website. Does Sling support me with link externalization?

Cheers,
Jakob

On Wed, Apr 18, 2012 at 12:33 AM, maikhorma <MH...@nbme.org> wrote:
> I'd suggest you take a look around in [1] and more specifically [2].  Even
> so, I think you're not getting a direct answer because you "url is the same"
> requirement is not exactly in line with the fundamentals of sling.
> Specifically Sling is like a "RESTful" web service, and by definition if you
> want something different, then you use a different url. The focus is on
> content, so you have /content/stuff/myEntry, and if you want to view it on a
> normal browser you go to .../myEntry.html, if you want it for mobile, you go
> to myEntry.mobile, and if you want a summary, you go to myEntry.summary
> (plus using whatever url constructs).  Saying that "the url can't change"
> pretty much breaks a lot of the power of sling and forces you to put
> "if...else if..." somewhere to process other inputs like get parmeters.
> Also check out [3] since I think it does a decent job of utilizing sling to
> do the job rather than writing custom code.
>
>
> [1] http://sling.apache.org/site/the-sling-engine.html
> [2] http://sling.apache.org/site/dispatching-requests.html
> [3] http://sling.apache.org/site/46-line-blog.html
>
> --
> View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
> Sent from the Sling - Users mailing list archive at Nabble.com.



-- 
Cheers,
Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by maikhorma <MH...@NBME.org>.
I'd suggest you take a look around in [1] and more specifically [2].  Even
so, I think you're not getting a direct answer because you "url is the same"
requirement is not exactly in line with the fundamentals of sling.
Specifically Sling is like a "RESTful" web service, and by definition if you
want something different, then you use a different url. The focus is on
content, so you have /content/stuff/myEntry, and if you want to view it on a
normal browser you go to .../myEntry.html, if you want it for mobile, you go
to myEntry.mobile, and if you want a summary, you go to myEntry.summary
(plus using whatever url constructs).  Saying that "the url can't change"
pretty much breaks a lot of the power of sling and forces you to put
"if...else if..." somewhere to process other inputs like get parmeters. 
Also check out [3] since I think it does a decent job of utilizing sling to
do the job rather than writing custom code.


[1] http://sling.apache.org/site/the-sling-engine.html
[2] http://sling.apache.org/site/dispatching-requests.html
[3] http://sling.apache.org/site/46-line-blog.html

--
View this message in context: http://apache-sling.73963.n3.nabble.com/Using-Filter-and-RequestDispatcher-to-substitute-responses-tp3918391p3919064.html
Sent from the Sling - Users mailing list archive at Nabble.com.

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Wednesday, April 18, 2012, Jakob Külzer wrote:

>In order to support, for example, mobile versions of components, i
> introduced the concept of a rendition. At the beginning of the request
> the template would determine the rendition and store it in the
> request. Then I added a custom component loader that would check if
> there was a rendition specific script...

You might create a filter that computes an additional selector that selects
your rendition, and stores that in the request attributes.

You can then include rendition scripts with the sling:include JSP tag (or
servlet equivalent), supplying that selector in the "addSelectors" option,
see http://markmail.org/message/zq3yz3dw3qvnby3r for example.

Sling will then select scripts/servlets that support this selector, if
present, and otherwise use the default rendition.

-Bertrand

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hello Sarwar,

Thank you for your explanations. I didn't know about the cheat sheet.
Now it's bookmarked, thank you.

>From the cheatsheet i understand that the only way to influence the
script selection for a given node is through the HTTP method, selector
and extension, correct?

Just out of curiosity, how could I influence the script selection
outside of these parameters, if possible? While selector and extension
provide a lot of flexibility, they are insufficient for what I have in
mind.

Maybe an example will make clear what i am trying to accomplish. The
original idea was born as i implemented a custom solution on Day CQ 4.
In order to support, for example, mobile versions of components, i
introduced the concept of a rendition. At the beginning of the request
the template would determine the rendition and store it in the
request. Then I added a custom component loader that would check if
there was a rendition specific script. For example, if rendition was
set to "mobile", it would look for a mobile.jsp and render it instead
of the actual component JSP. This way the implemented site
transparently switches to mobile (or whatever rendition has been
selected) markup while maintaining the same URL structure. This turned
out to be a quite flexible and useful model and now I would like to
implement similar functionality for Sling based systems. Any
suggestions how I could go about this?

Regards,
Jakob


On Tue, Apr 17, 2012 at 6:28 PM, Sarwar Bhuiyan
<sa...@gmail.com> wrote:
> It looks at the sling:resourceType and then selects a script (either a JSP
> in a component folder or a Sling Servlet where the resource type is
> specified).  There is one other way of doing it which is the legacy way of
> registering a Sling Servlet to a specific path.  All this is being done by
> the SlingMainServlet which is in essence a dispatcher which selects a
> script or servlet based on a combination of sling:resourceType, selectors,
> and extensions (or the fixed path).  The other way would be to register
> your own servlet with the http service in OSGi and then in that servlet
> itself do a forward() or redirect() to a specific path.
>
> In Sling, we tend not to do forwards or redirects to JSPs but rather to
> paths to resources which then have a backing scripts which Sling selects.
>  I take it you've seen the Sling Cheatsheet which shows the different ways
> a script can be chosen (it doesn't include using a Sling Servlet though but
> you can see examples of those in the Sling source code).  This way, you
> never have to directly call a JSP yourself and this level of indirection
> allows you to be able to do stuff like attaching ACLs to the node path
> you're calling or change the backing implementation later on without
> changing the outside URLs.
>
> Hope this makes sense somewhat.
>
> Sarwar
>
> On Tue, Apr 17, 2012 at 10:55 PM, Jakob Külzer <ja...@gmail.com>wrote:
>
>> Hello Sarwar,
>>
>> thank you for your response. I've considered using selectors, however
>> that alters the URL for the page, which is undesirable.
>>
>> The other approach, using if-else statements in the JSP is something I
>> have prototyped using a taglib, which works fine, but it requires that
>> each JSP has to be wrapped into the tag.
>>
>> Ideally, i could handle this completely transparently. The key idea is
>> to keep the actual JSPs clear of the actual switching behaviour. After
>> all, the same behaviour will be used in many different JSPs, and I
>> don't like the idea of having to rely on developers manually cutting
>> and pasting code between JSPs.
>>
>> I was wondering how Sling selects which script actually to render.
>> From my understanding that happens when Sling evaluates the
>> sling:resourceType field. Is there a way to hook up to that decision
>> process?
>>
>> Regards,
>> Jakob
>>
>> On Tue, Apr 17, 2012 at 5:36 PM, Sarwar Bhuiyan
>> <sa...@gmail.com> wrote:
>> > You could use different scripts in the same component and call them based
>> > on your condition.  So supposing your component is called
>> >
>> > mycomponent/
>> >   mycomponent.jsp
>> >   optiona.jsp
>> >   optionb.jsp
>> >
>> > In your mycomponent.jsp, you can do something like:
>> >
>> >
>> > <%if(condition){%>
>> >   <cq:include script="optiona.jsp"/>
>> > <%else {%}
>> >   <cq:include script="optionb.jsp"/>
>> > <%}%>
>> >
>> >
>> >
>> > Sarwar
>> >
>> >
>> > On Tue, Apr 17, 2012 at 10:34 PM, Sarwar Bhuiyan
>> > <sa...@gmail.com>wrote:
>> >
>> >> You could use a selector.  So supposing your component is called
>> >>
>> >> mycomponent/
>> >>
>> >>
>> >> On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <jakob.kuelzer@gmail.com
>> >wrote:
>> >>
>> >>> Hello everybody,
>> >>>
>> >>> I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>> >>> I'm running against an issue that I don't really understand properly.
>> >>> I'm still quite new to Sling, so if you see something where I've got
>> >>> an incorrect understanding feel free to jump in and correct me.
>> >>> Thanks!
>> >>>
>> >>> What I am trying to do is to replace certain request with output from
>> >>> a different JSP. For example, a page contains an embedded component
>> >>> which, as far as I understand, is referenced via a Sling resourceType.
>> >>> When Sling renders this page, the JSP will be included; however, based
>> >>> on certain conditions I'd like to render a different JSP. My naive
>> >>> approach is to register a filter in the include chain, create a
>> >>> RequestDispatcher for the new JSP and call the include() method.
>> >>>
>> >>> My first question is, am I using the appropriate approach for this?
>> >>> Should I look at another mechanism in Sling to do something like this?
>> >>>
>> >>> Second, assuming a filter is the correct approach, why does it not
>> >>> render my JSP?
>> >>>
>> >>> Here're the relevant lines of code:
>> >>>
>> >>> ---->8--------------------------------------------------------------
>> >>> @org.apache.felix.scr.annotations.Component(immediate = true, metatype
>> =
>> >>> false)
>> >>> @Service(Filter.class)
>> >>> @Properties({ @Property(name = "sling.filter.scope", value = {
>> >>> "include", "forward" }) })
>> >>> public class ComponentFilter implements Filter {
>> >>>
>> >>>  public void doFilter(ServletRequest request, ServletResponse
>> >>> response, FilterChain chain) throws IOException, ServletException {
>> >>>  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>> >>> request;
>> >>>  final Resource resource = ...; // Get resource for the JSP.
>> >>>  if (localResource != null) {
>> >>>   final RequestDispatcher requestDispatcher =
>> >>> slingRequest.getRequestDispatcher(resource);
>> >>>   requestDispatcher.include(request, response);
>> >>>  } else {
>> >>>  chain.doFilter(request, response);
>> >>>  }
>> >>>  }
>> >>>
>> >>> }
>> >>> ---->8--------------------------------------------------------------
>> >>>
>> >>> The resource is properly resolved:
>> >>>
>> >>> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>> >>> path=/apps/myapp/components/demo/replacment.jsp
>> >>>
>> >>> However, I get this error:
>> >>>
>> >>> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>> >>> /content/geometrixx/en/test.html HTTP/1.1]
>> >>> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>> >>> extension html, cannot render resource JcrNodeResource, type=nt:file,
>> >>> superType=null, path=/apps/myapp/components/demo/replacment.jsp
>> >>>
>> >>> I'm not sure what to make of this. Any ideas?
>> >>>
>> >>> Any help is appreciated! Thank you.
>> >>>
>> >>>
>> >>> --
>> >>> Cheers,
>> >>> Jakob
>> >>>
>> >>
>> >>
>>
>>
>>
>> --
>> Cheers,
>> Jakob
>>



-- 
Cheers,
Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
It looks at the sling:resourceType and then selects a script (either a JSP
in a component folder or a Sling Servlet where the resource type is
specified).  There is one other way of doing it which is the legacy way of
registering a Sling Servlet to a specific path.  All this is being done by
the SlingMainServlet which is in essence a dispatcher which selects a
script or servlet based on a combination of sling:resourceType, selectors,
and extensions (or the fixed path).  The other way would be to register
your own servlet with the http service in OSGi and then in that servlet
itself do a forward() or redirect() to a specific path.

In Sling, we tend not to do forwards or redirects to JSPs but rather to
paths to resources which then have a backing scripts which Sling selects.
 I take it you've seen the Sling Cheatsheet which shows the different ways
a script can be chosen (it doesn't include using a Sling Servlet though but
you can see examples of those in the Sling source code).  This way, you
never have to directly call a JSP yourself and this level of indirection
allows you to be able to do stuff like attaching ACLs to the node path
you're calling or change the backing implementation later on without
changing the outside URLs.

Hope this makes sense somewhat.

Sarwar

On Tue, Apr 17, 2012 at 10:55 PM, Jakob Külzer <ja...@gmail.com>wrote:

> Hello Sarwar,
>
> thank you for your response. I've considered using selectors, however
> that alters the URL for the page, which is undesirable.
>
> The other approach, using if-else statements in the JSP is something I
> have prototyped using a taglib, which works fine, but it requires that
> each JSP has to be wrapped into the tag.
>
> Ideally, i could handle this completely transparently. The key idea is
> to keep the actual JSPs clear of the actual switching behaviour. After
> all, the same behaviour will be used in many different JSPs, and I
> don't like the idea of having to rely on developers manually cutting
> and pasting code between JSPs.
>
> I was wondering how Sling selects which script actually to render.
> From my understanding that happens when Sling evaluates the
> sling:resourceType field. Is there a way to hook up to that decision
> process?
>
> Regards,
> Jakob
>
> On Tue, Apr 17, 2012 at 5:36 PM, Sarwar Bhuiyan
> <sa...@gmail.com> wrote:
> > You could use different scripts in the same component and call them based
> > on your condition.  So supposing your component is called
> >
> > mycomponent/
> >   mycomponent.jsp
> >   optiona.jsp
> >   optionb.jsp
> >
> > In your mycomponent.jsp, you can do something like:
> >
> >
> > <%if(condition){%>
> >   <cq:include script="optiona.jsp"/>
> > <%else {%}
> >   <cq:include script="optionb.jsp"/>
> > <%}%>
> >
> >
> >
> > Sarwar
> >
> >
> > On Tue, Apr 17, 2012 at 10:34 PM, Sarwar Bhuiyan
> > <sa...@gmail.com>wrote:
> >
> >> You could use a selector.  So supposing your component is called
> >>
> >> mycomponent/
> >>
> >>
> >> On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <jakob.kuelzer@gmail.com
> >wrote:
> >>
> >>> Hello everybody,
> >>>
> >>> I'm just starting to get my head wrapped around Sling and Day CQ 5 and
> >>> I'm running against an issue that I don't really understand properly.
> >>> I'm still quite new to Sling, so if you see something where I've got
> >>> an incorrect understanding feel free to jump in and correct me.
> >>> Thanks!
> >>>
> >>> What I am trying to do is to replace certain request with output from
> >>> a different JSP. For example, a page contains an embedded component
> >>> which, as far as I understand, is referenced via a Sling resourceType.
> >>> When Sling renders this page, the JSP will be included; however, based
> >>> on certain conditions I'd like to render a different JSP. My naive
> >>> approach is to register a filter in the include chain, create a
> >>> RequestDispatcher for the new JSP and call the include() method.
> >>>
> >>> My first question is, am I using the appropriate approach for this?
> >>> Should I look at another mechanism in Sling to do something like this?
> >>>
> >>> Second, assuming a filter is the correct approach, why does it not
> >>> render my JSP?
> >>>
> >>> Here're the relevant lines of code:
> >>>
> >>> ---->8--------------------------------------------------------------
> >>> @org.apache.felix.scr.annotations.Component(immediate = true, metatype
> =
> >>> false)
> >>> @Service(Filter.class)
> >>> @Properties({ @Property(name = "sling.filter.scope", value = {
> >>> "include", "forward" }) })
> >>> public class ComponentFilter implements Filter {
> >>>
> >>>  public void doFilter(ServletRequest request, ServletResponse
> >>> response, FilterChain chain) throws IOException, ServletException {
> >>>  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
> >>> request;
> >>>  final Resource resource = ...; // Get resource for the JSP.
> >>>  if (localResource != null) {
> >>>   final RequestDispatcher requestDispatcher =
> >>> slingRequest.getRequestDispatcher(resource);
> >>>   requestDispatcher.include(request, response);
> >>>  } else {
> >>>  chain.doFilter(request, response);
> >>>  }
> >>>  }
> >>>
> >>> }
> >>> ---->8--------------------------------------------------------------
> >>>
> >>> The resource is properly resolved:
> >>>
> >>> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
> >>> path=/apps/myapp/components/demo/replacment.jsp
> >>>
> >>> However, I get this error:
> >>>
> >>> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
> >>> /content/geometrixx/en/test.html HTTP/1.1]
> >>> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
> >>> extension html, cannot render resource JcrNodeResource, type=nt:file,
> >>> superType=null, path=/apps/myapp/components/demo/replacment.jsp
> >>>
> >>> I'm not sure what to make of this. Any ideas?
> >>>
> >>> Any help is appreciated! Thank you.
> >>>
> >>>
> >>> --
> >>> Cheers,
> >>> Jakob
> >>>
> >>
> >>
>
>
>
> --
> Cheers,
> Jakob
>

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Jakob Külzer <ja...@gmail.com>.
Hello Sarwar,

thank you for your response. I've considered using selectors, however
that alters the URL for the page, which is undesirable.

The other approach, using if-else statements in the JSP is something I
have prototyped using a taglib, which works fine, but it requires that
each JSP has to be wrapped into the tag.

Ideally, i could handle this completely transparently. The key idea is
to keep the actual JSPs clear of the actual switching behaviour. After
all, the same behaviour will be used in many different JSPs, and I
don't like the idea of having to rely on developers manually cutting
and pasting code between JSPs.

I was wondering how Sling selects which script actually to render.
>From my understanding that happens when Sling evaluates the
sling:resourceType field. Is there a way to hook up to that decision
process?

Regards,
Jakob

On Tue, Apr 17, 2012 at 5:36 PM, Sarwar Bhuiyan
<sa...@gmail.com> wrote:
> You could use different scripts in the same component and call them based
> on your condition.  So supposing your component is called
>
> mycomponent/
>   mycomponent.jsp
>   optiona.jsp
>   optionb.jsp
>
> In your mycomponent.jsp, you can do something like:
>
>
> <%if(condition){%>
>   <cq:include script="optiona.jsp"/>
> <%else {%}
>   <cq:include script="optionb.jsp"/>
> <%}%>
>
>
>
> Sarwar
>
>
> On Tue, Apr 17, 2012 at 10:34 PM, Sarwar Bhuiyan
> <sa...@gmail.com>wrote:
>
>> You could use a selector.  So supposing your component is called
>>
>> mycomponent/
>>
>>
>> On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <ja...@gmail.com>wrote:
>>
>>> Hello everybody,
>>>
>>> I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>>> I'm running against an issue that I don't really understand properly.
>>> I'm still quite new to Sling, so if you see something where I've got
>>> an incorrect understanding feel free to jump in and correct me.
>>> Thanks!
>>>
>>> What I am trying to do is to replace certain request with output from
>>> a different JSP. For example, a page contains an embedded component
>>> which, as far as I understand, is referenced via a Sling resourceType.
>>> When Sling renders this page, the JSP will be included; however, based
>>> on certain conditions I'd like to render a different JSP. My naive
>>> approach is to register a filter in the include chain, create a
>>> RequestDispatcher for the new JSP and call the include() method.
>>>
>>> My first question is, am I using the appropriate approach for this?
>>> Should I look at another mechanism in Sling to do something like this?
>>>
>>> Second, assuming a filter is the correct approach, why does it not
>>> render my JSP?
>>>
>>> Here're the relevant lines of code:
>>>
>>> ---->8--------------------------------------------------------------
>>> @org.apache.felix.scr.annotations.Component(immediate = true, metatype =
>>> false)
>>> @Service(Filter.class)
>>> @Properties({ @Property(name = "sling.filter.scope", value = {
>>> "include", "forward" }) })
>>> public class ComponentFilter implements Filter {
>>>
>>>  public void doFilter(ServletRequest request, ServletResponse
>>> response, FilterChain chain) throws IOException, ServletException {
>>>  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>>> request;
>>>  final Resource resource = ...; // Get resource for the JSP.
>>>  if (localResource != null) {
>>>   final RequestDispatcher requestDispatcher =
>>> slingRequest.getRequestDispatcher(resource);
>>>   requestDispatcher.include(request, response);
>>>  } else {
>>>  chain.doFilter(request, response);
>>>  }
>>>  }
>>>
>>> }
>>> ---->8--------------------------------------------------------------
>>>
>>> The resource is properly resolved:
>>>
>>> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>>> path=/apps/myapp/components/demo/replacment.jsp
>>>
>>> However, I get this error:
>>>
>>> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>>> /content/geometrixx/en/test.html HTTP/1.1]
>>> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>>> extension html, cannot render resource JcrNodeResource, type=nt:file,
>>> superType=null, path=/apps/myapp/components/demo/replacment.jsp
>>>
>>> I'm not sure what to make of this. Any ideas?
>>>
>>> Any help is appreciated! Thank you.
>>>
>>>
>>> --
>>> Cheers,
>>> Jakob
>>>
>>
>>



-- 
Cheers,
Jakob

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
You could use different scripts in the same component and call them based
on your condition.  So supposing your component is called

mycomponent/
   mycomponent.jsp
   optiona.jsp
   optionb.jsp

In your mycomponent.jsp, you can do something like:


<%if(condition){%>
   <cq:include script="optiona.jsp"/>
<%else {%}
   <cq:include script="optionb.jsp"/>
<%}%>



Sarwar


On Tue, Apr 17, 2012 at 10:34 PM, Sarwar Bhuiyan
<sa...@gmail.com>wrote:

> You could use a selector.  So supposing your component is called
>
> mycomponent/
>
>
> On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <ja...@gmail.com>wrote:
>
>> Hello everybody,
>>
>> I'm just starting to get my head wrapped around Sling and Day CQ 5 and
>> I'm running against an issue that I don't really understand properly.
>> I'm still quite new to Sling, so if you see something where I've got
>> an incorrect understanding feel free to jump in and correct me.
>> Thanks!
>>
>> What I am trying to do is to replace certain request with output from
>> a different JSP. For example, a page contains an embedded component
>> which, as far as I understand, is referenced via a Sling resourceType.
>> When Sling renders this page, the JSP will be included; however, based
>> on certain conditions I'd like to render a different JSP. My naive
>> approach is to register a filter in the include chain, create a
>> RequestDispatcher for the new JSP and call the include() method.
>>
>> My first question is, am I using the appropriate approach for this?
>> Should I look at another mechanism in Sling to do something like this?
>>
>> Second, assuming a filter is the correct approach, why does it not
>> render my JSP?
>>
>> Here're the relevant lines of code:
>>
>> ---->8--------------------------------------------------------------
>> @org.apache.felix.scr.annotations.Component(immediate = true, metatype =
>> false)
>> @Service(Filter.class)
>> @Properties({ @Property(name = "sling.filter.scope", value = {
>> "include", "forward" }) })
>> public class ComponentFilter implements Filter {
>>
>>  public void doFilter(ServletRequest request, ServletResponse
>> response, FilterChain chain) throws IOException, ServletException {
>>  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
>> request;
>>  final Resource resource = ...; // Get resource for the JSP.
>>  if (localResource != null) {
>>   final RequestDispatcher requestDispatcher =
>> slingRequest.getRequestDispatcher(resource);
>>   requestDispatcher.include(request, response);
>>  } else {
>>  chain.doFilter(request, response);
>>  }
>>  }
>>
>> }
>> ---->8--------------------------------------------------------------
>>
>> The resource is properly resolved:
>>
>> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
>> path=/apps/myapp/components/demo/replacment.jsp
>>
>> However, I get this error:
>>
>> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
>> /content/geometrixx/en/test.html HTTP/1.1]
>> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
>> extension html, cannot render resource JcrNodeResource, type=nt:file,
>> superType=null, path=/apps/myapp/components/demo/replacment.jsp
>>
>> I'm not sure what to make of this. Any ideas?
>>
>> Any help is appreciated! Thank you.
>>
>>
>> --
>> Cheers,
>> Jakob
>>
>
>

Re: Using Filter and RequestDispatcher to substitute responses

Posted by Sarwar Bhuiyan <sa...@gmail.com>.
You could use a selector.  So supposing your component is called

mycomponent/


On Tue, Apr 17, 2012 at 10:05 PM, Jakob Külzer <ja...@gmail.com>wrote:

> Hello everybody,
>
> I'm just starting to get my head wrapped around Sling and Day CQ 5 and
> I'm running against an issue that I don't really understand properly.
> I'm still quite new to Sling, so if you see something where I've got
> an incorrect understanding feel free to jump in and correct me.
> Thanks!
>
> What I am trying to do is to replace certain request with output from
> a different JSP. For example, a page contains an embedded component
> which, as far as I understand, is referenced via a Sling resourceType.
> When Sling renders this page, the JSP will be included; however, based
> on certain conditions I'd like to render a different JSP. My naive
> approach is to register a filter in the include chain, create a
> RequestDispatcher for the new JSP and call the include() method.
>
> My first question is, am I using the appropriate approach for this?
> Should I look at another mechanism in Sling to do something like this?
>
> Second, assuming a filter is the correct approach, why does it not
> render my JSP?
>
> Here're the relevant lines of code:
>
> ---->8--------------------------------------------------------------
> @org.apache.felix.scr.annotations.Component(immediate = true, metatype =
> false)
> @Service(Filter.class)
> @Properties({ @Property(name = "sling.filter.scope", value = {
> "include", "forward" }) })
> public class ComponentFilter implements Filter {
>
>  public void doFilter(ServletRequest request, ServletResponse
> response, FilterChain chain) throws IOException, ServletException {
>  final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)
> request;
>  final Resource resource = ...; // Get resource for the JSP.
>  if (localResource != null) {
>   final RequestDispatcher requestDispatcher =
> slingRequest.getRequestDispatcher(resource);
>   requestDispatcher.include(request, response);
>  } else {
>  chain.doFilter(request, response);
>  }
>  }
>
> }
> ---->8--------------------------------------------------------------
>
> The resource is properly resolved:
>
> *DEBUG* script: JcrNodeResource, type=nt:file, superType=null,
> path=/apps/myapp/components/demo/replacment.jsp
>
> However, I get this error:
>
> 17.04.2012 16:13:53.136 *ERROR* [127.0.0.1 [1334693633099] GET
> /content/geometrixx/en/test.html HTTP/1.1]
> org.apache.sling.servlets.get.impl.DefaultGetServlet No renderer for
> extension html, cannot render resource JcrNodeResource, type=nt:file,
> superType=null, path=/apps/myapp/components/demo/replacment.jsp
>
> I'm not sure what to make of this. Any ideas?
>
> Any help is appreciated! Thank you.
>
>
> --
> Cheers,
> Jakob
>