Getting the client requested path for the current request

303 views
Skip to first unread message

Wouter Danes

unread,
Nov 13, 2013, 9:06:22 AM11/13/13
to hippo-c...@googlegroups.com
Hi happy Hippos,

I’m trying to set a cookie for the current page (when persisting some form data).
For this I need to set a “cookie path” on the cookie, which is the path part of the URL that’s requested by the browser.
I couldn’t find a way to get this through “regular” means, because:
  • The servlet request doesn’t know if something got forwarded by a proxy, so request.getPath() is useless
  • Tomcat doesn’t know whether the context path should be in the URL, so hstRequest.getPathInfo() is useless
We ended up with this code snippet:

String getPathInSite(HstRequest request) {
        HstRequestContext requestContext = request.getRequestContext();
        String path = requestContext.getResolvedSiteMapItem().getPathInfo();
        HstLinkCreator linkCreator = requestContext.getHstLinkCreator();
        HstLink link = linkCreator.create(path, getMount(request));
        return link.toUrlForm(requestContext, false);
}

Two questions:
  1. Is there a nicer way to do this that someone can think of?
  2. Does this use case present itself enough to expose this on the request context or as a method on the “base” component?
Regards,

Wouter

Ard Schrijvers

unread,
Nov 13, 2013, 10:32:21 AM11/13/13
to hippo-c...@googlegroups.com
Hey Wouter,

take a look at HstRequestUtils : there are quite some utilities to
recreate the current client request. Only thing you need to really
take into account is whether to show the context path or not, which
you can retrieve from the matched host.

Regards Ard
> --
> Hippo Community Group: The place for all discussions and announcements about
> Hippo CMS (and HST, repository etc. etc.)
> Most Hippo developers are active here and/or follow the Stackoverflow
> [hippo-cms] tag.
>
> The old Nabble archive can be found here:
> http://hippo.2275632.n2.nabble.com/Hippo-CMS-7-f2274273.html
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google Groups
> "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to hippo-communi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com

Wouter Danes

unread,
Nov 13, 2013, 11:13:07 AM11/13/13
to hippo-c...@googlegroups.com
Yeah, the part with the context path is the reason I used the linkcreator:
I really don¹t think the application should have to think about
implementation details like this.
Maybe it could be an addition to the HstRequestUtils as a method
³getBrowserRequestPath(HstRequest request)² or something like that.

Ard Schrijvers

unread,
Nov 13, 2013, 11:24:09 AM11/13/13
to hippo-c...@googlegroups.com
On Wed, Nov 13, 2013 at 5:13 PM, Wouter Danes <Wouter...@hinttech.com> wrote:
> Yeah, the part with the context path is the reason I used the linkcreator:
> I really don¹t think the application should have to think about
> implementation details like this.
> Maybe it could be an addition to the HstRequestUtils as a method
> ³getBrowserRequestPath(HstRequest request)² or something like that.

Yes, we can easily add that. I am not sure about the name above. The
argument type should be HttpServletRequest. For now, you can very
easily add this util yourself.

Most likely will include a 'remote ip filter' that recreates the
client request in the container before hitting the cms or hst webapp :
After we have that in place, you can always just use
request.getServerName() etc kind of methods without by accident
getting the proxied request server name

Regards Ard
Reply all
Reply to author
Forward
0 new messages