ytrstu / vosao

Automatically exported from code.google.com/p/vosao
GNU Lesser General Public License v2.1
0 stars 0 forks source link

Getting header information and setting header information for content pages #302

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a case where I want to automatically pre-set the checkbox option based 
on the user's User-Agent header. I know this can be done with javascript, but 
I'd ideally like to do it with response.getHeader(). The issue is that 
SiteFilter has a method that does not pass response or request to the page 
business. Ideally, can the request and response be sent to the page business so 
I can access and set information from my plugin? It is also important because 
you can't set the headers any other way (eg, if one content page was going to 
be an attachment, you can't use content-disposition.. or if you want to set the 
content type to text/xml... you can't do that either).

Here is the code in SiteFilter (below this I'll show you what I'd like instead):

    private void renderPage(HttpServletRequest request, 
            HttpServletResponse response, final PageEntity page) 
            throws IOException {
        String contentType = StringUtils.isEmpty(page.getContentType()) ?
                "text/html" : page.getContentType();
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        Writer out = response.getWriter();
        String language = getBusiness().getLanguage();
        String content = getBusiness().getPageBusiness().render(page, language);
        out.write(content);
        if (!isLoggedIn(request) && page.isCached()) {
            getSystemService().getPageCache().put(page.getFriendlyURL(),
                    language, content, contentType);
        }
    }

is the code below a better solution?

    private void renderPage(HttpServletRequest request, 
            HttpServletResponse response, final PageEntity page) 
            throws IOException {
        String contentType = StringUtils.isEmpty(page.getContentType()) ?
                "text/html" : page.getContentType();
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        Writer out = response.getWriter();
        String language = getBusiness().getLanguage();

        /* pass the request/response to something we can access from a velocity implementation */
        /* i don't know what would be best to pass to.. this is just an example */
        page.setRequest(request);
        page.setResponse(response);

        String content = getBusiness().getPageBusiness().render(page, language);
        out.write(content);
        if (!isLoggedIn(request) && page.isCached()) {
            getSystemService().getPageCache().put(page.getFriendlyURL(),
                    language, content, contentType);
        }
    }

Original issue reported on code.google.com by roland.q...@gmail.com on 7 Aug 2010 at 1:31

GoogleCodeExporter commented 9 years ago
Maybe you could also just have some kind of map for both headers sent by the 
browser, and headers to send to the client?

Original comment by roland.q...@gmail.com on 7 Aug 2010 at 1:33

GoogleCodeExporter commented 9 years ago
Attached is a perfect example of what I'm talking about. The sitemap.xml is 
sending a text/html content type header (because of the code in renderPage()). 
Need to be able to set the headers from the sitemap plugin (you also need to 
fix the sitemap plugin to reflect that too).

Original comment by roland.q...@gmail.com on 7 Aug 2010 at 1:40

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
If you need to read User-Agent header you can do it by:

1. Use $request.getHeader("User-Agent") variable in Velocity templates.
2. Use VosaoContext.getInstance().getRequest().getHeader("User-Agent") in Java 
code.

If you need to change http response headers then I will add response to the 
VosaoContext object. Will it suffice for your needs?

Original comment by kinyelo@gmail.com on 7 Aug 2010 at 7:10

GoogleCodeExporter commented 9 years ago
Implemented as VosaoContext response property and Velocity variable "response". 
r867

Original comment by kinyelo@gmail.com on 8 Aug 2010 at 6:30

GoogleCodeExporter commented 9 years ago
Perfect! Thanks for that!

Original comment by roland.q...@gmail.com on 8 Aug 2010 at 10:42