/*
 * #%L
 * BroadleafCommerce Common Libraries
 * %%
 * Copyright (C) 2009 - 2013 Broadleaf Commerce
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.broadleafcommerce.common.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.broadleafcommerce.common.web.deeplink.DeepLink;
import org.broadleafcommerce.common.web.deeplink.DeepLinkService;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * An abstract controller that provides convenience methods and resource declarations for its  children
 * 
 * Operations that are shared between all controllers belong here.   To use composition rather than
 * extension, implementors can utilize BroadleafControllerUtility.
 * 
 * @see BroadleafControllerUtility
 * 
 * @author apazzolini
 * @author bpolster
 */
public abstract class BroadleafAbstractController {
    
    
    /**
     * A helper method that returns whether or not the given request was invoked via an AJAX call
     * 
     * @param request
     * @return - whether or not it was an AJAX request
     */
    protected boolean isAjaxRequest(HttpServletRequest request) {
        return BroadleafControllerUtility.isAjaxRequest(request);       
    }
    
    /**
     * Returns the current servlet context path. This will return a "/" if the application is
     * deployed as root. If it's not deployed as root, it will return the context path BOTH a 
     * leading slash but without a trailing slash.
     * 
     * @param request
     * @return the context path
     */
    protected String getContextPath(HttpServletRequest request) {
        String ctxPath = request.getContextPath();
        if (StringUtils.isBlank(ctxPath)) {
            return "/";
        } else {
            if (ctxPath.charAt(0) != '/') {
                ctxPath = '/' + ctxPath;
            }
            if (ctxPath.charAt(ctxPath.length() - 1) != '/') {
                ctxPath = ctxPath + '/';
            }
            
            return ctxPath;
        }
        
    }
    
    protected <T> void addDeepLink(ModelAndView model, DeepLinkService<T> service, T item) {
        if (service == null) {
            return;
        }

        BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
        if (brc.isAdminMode()) {
            List<DeepLink> links = service.getLinks(item);
            if (links.size() == 1) {
                model.addObject("adminDeepLink", links.get(0));
            } else {
                model.addObject("adminDeepLink", links);
            }
        }
    }

}

@@ -975,119 +975,369 @@
 org.
-springframework.web.servlet.ModelAndView;%0A%0Aimport java.util.List;%0A%0Aimport javax.servlet.http.HttpServletRequest
+codehaus.jackson.JsonGenerationException;%0Aimport org.codehaus.jackson.map.JsonMappingException;%0Aimport org.codehaus.jackson.map.ObjectMapper;%0Aimport org.springframework.web.servlet.ModelAndView;%0A%0Aimport java.io.IOException;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse
 ;%0A%0A/
@@ -3469,12 +3469,923 @@
  %7D%0A    %7D
+%0A    %0A    /**%0A     * Typically, controller methods are set to return a String that points to the necessary template path.%0A     * %0A     * However, there may be occasions where the error state for a controller action should instead return%0A     * JSON instead of a fully rendered template. This convenience method will achieve that by setting the%0A     * appropriate headers and serializing the given map.%0A     * %0A     * @param response%0A     * @param responseMap%0A     * @throws JsonGenerationException%0A     * @throws JsonMappingException%0A     * @throws IOException%0A     */%0A    protected String jsonResponse(HttpServletResponse response, Map%3CString, Object%3E responseMap) %0A            throws JsonGenerationException, JsonMappingException, IOException %7B%0A        response.setHeader(%22Content-Type%22, %22application/json%22);%0A        new ObjectMapper().writeValue(response.getWriter(), responseMap);%0A        return null;%0A    %7D
 %0A%0A%7D%0A

Add convenience jsonResponse method