package no.kantega.publishing.security.interceptors;

import no.kantega.publishing.common.Aksess;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Interceptor that intercepts all requests to render an OpenAksess administration GUI view, e.g.
 * {@link no.kantega.publishing.admin.content.action.NavigateController}.
 */
public class AdminViewInterceptor extends HandlerInterceptorAdapter {

    /**
     * Adds a set of model attributes that are used by all OA admin views.
     * @param request request to render an admin view
     * @param response corresponding response
     * @param handler typically the controller or method that handled the request.
     * @param modelAndView the ModelAndView that the handler returned
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        Map<String, Object> model = modelAndView.getModel();
        if (model == null) {
            model = new HashMap<String, Object>();
        }
        model.put("aksess_locale", Aksess.getDefaultAdminLocale());

        String reqUri = request.getRequestURI();
        int start = reqUri.indexOf("/admin/");
        if (start != -1) {
            reqUri = reqUri.substring(start+"/admin/".length());
            reqUri = reqUri.substring(0, reqUri.indexOf("/"));
            model.put(reqUri + "Selected", "selected");
        }
    }


}

@@ -1194,24 +1194,118 @@
 xception %7B%0A%0A
+        if (modelAndView == null) %7B%0A            modelAndView = new ModelAndView();%0A        %7D%0A%0A
         Map%3C

Handle that ModelAndView can be null.