package org.xcolab.view.pages.content;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import org.xcolab.client.contents.ContentsClient;
import org.xcolab.client.contents.exceptions.ContentNotFoundException;
import org.xcolab.client.contents.pojo.ContentArticle;
import org.xcolab.client.contents.pojo.ContentPage;
import org.xcolab.client.members.pojo.Member;
import org.xcolab.view.errors.AccessDeniedPage;
import org.xcolab.view.errors.ErrorText;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class ContentController {

    @GetMapping("/page/{pageTitle}")
    public String showContentPage(HttpServletRequest request, HttpServletResponse response,
            Model model, Member member, @PathVariable String pageTitle) throws IOException {

        try {
            final ContentPage contentPage = ContentsClient.getContentPage(pageTitle);
            model.addAttribute("contentPage", contentPage);

            final ContentArticle contentArticle = ContentsClient
                    .getContentArticle(contentPage.getContentArticleId());

            if (!contentArticle.canView(member)) {
                return new AccessDeniedPage(member).toViewName(response);
            }

            model.addAttribute("contentArticleId", contentPage.getContentArticleId());

            if (contentPage.getMenuArticleId() != null) {
                model.addAttribute("menuArticleId", contentPage.getMenuArticleId());
            }

            //TODO: allow different active pages
            model.addAttribute("_activePageLink", "about");
        } catch (ContentNotFoundException e) {
            response.setStatus(HttpStatus.NOT_FOUND.value());
            return ErrorText.PAGE_NOT_FOUND.flashAndReturnView(request);
        }
        return "content/contentPage";
    }

    @GetMapping("/widgets/content")
    public String contentDisplay(HttpServletRequest request, Model model, @RequestParam Long contentArticleId) {
        model.addAttribute("articleId", contentArticleId);
        return "content/widget";
    }
}

@@ -29,24 +29,115 @@
 s.content;%0A%0A
+import org.springframework.http.CacheControl;%0Aimport org.springframework.http.HttpHeaders;%0A
 import org.s
@@ -828,16 +828,54 @@
 ception;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -2361,16 +2361,58 @@
 request,
+ HttpServletResponse response,%0A           
  Model m
@@ -2452,24 +2452,246 @@
 rticleId) %7B%0A
+        response.setHeader(HttpHeaders.CACHE_CONTROL,%0A                CacheControl.maxAge(30, TimeUnit.SECONDS)%0A                        .staleWhileRevalidate(36, TimeUnit.HOURS)%0A                        .getHeaderValue());%0A
         mode

Add browser caching to content widget