com.exxonmobile.ace.hybris.storefront.controllers.pages.DefaultPageController.java Source code

Java tutorial

Introduction

Here is the source code for com.exxonmobile.ace.hybris.storefront.controllers.pages.DefaultPageController.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2013 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 * 
 *  
 */
package com.exxonmobile.ace.hybris.storefront.controllers.pages;

import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;
import de.hybris.platform.cms2.model.pages.ContentPageModel;
import com.exxonmobile.ace.hybris.storefront.breadcrumb.ResourceBreadcrumbBuilder;
import com.exxonmobile.ace.hybris.storefront.breadcrumb.impl.ContentPageBreadcrumbBuilder;
import com.exxonmobile.ace.hybris.storefront.constants.WebConstants;
import com.exxonmobile.ace.hybris.storefront.controllers.ControllerConstants;
import com.exxonmobile.ace.hybris.storefront.controllers.util.GlobalMessages;

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

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UrlPathHelper;

/**
 * Error handler to show a CMS managed error page. This is the catch-all controller that handles all GET requests that
 * are not handled by other controllers.
 */
@Controller
@Scope("tenant")
public class DefaultPageController extends AbstractPageController {
    private static final String ERROR_CMS_PAGE = "notFound";

    private final UrlPathHelper urlPathHelper = new UrlPathHelper();

    @Resource(name = "simpleBreadcrumbBuilder")
    private ResourceBreadcrumbBuilder resourceBreadcrumbBuilder;

    @Resource(name = "b2bContentPageBreadcrumbBuilder")
    private ContentPageBreadcrumbBuilder contentPageBreadcrumbBuilder;

    @RequestMapping(method = RequestMethod.GET)
    public String get(final Model model, final HttpServletRequest request, final HttpServletResponse response)
            throws CMSItemNotFoundException {
        // Check for CMS Page where label or id is like /page
        final ContentPageModel pageForRequest = getContentPageForRequest(request);
        if (pageForRequest != null) {
            storeCmsPageInModel(model, pageForRequest);
            setUpMetaDataForContentPage(model, pageForRequest);
            model.addAttribute(WebConstants.BREADCRUMBS_KEY,
                    contentPageBreadcrumbBuilder.getBreadcrumbs(pageForRequest));
            return getViewForPage(pageForRequest);
        }

        // No page found - display the notFound page with error from controller
        storeCmsPageInModel(model, getContentPageForLabelOrId(ERROR_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ERROR_CMS_PAGE));
        model.addAttribute(WebConstants.MODEL_KEY_ADDITIONAL_BREADCRUMB,
                resourceBreadcrumbBuilder.getBreadcrumbs("breadcrumb.not.found"));
        GlobalMessages.addErrorMessage(model, "system.error.page.not.found");

        response.setStatus(HttpServletResponse.SC_NOT_FOUND);

        return ControllerConstants.Views.Pages.Error.ErrorNotFoundPage;
    }

    /**
     * Lookup the CMS Content Page for this request.
     * 
     * @param request
     *           The request
     * @return the CMS content page
     */
    protected ContentPageModel getContentPageForRequest(final HttpServletRequest request) {
        // Get the path for this request.
        // Note that the path begins with a '/'
        final String lookupPathForRequest = urlPathHelper.getLookupPathForRequest(request);

        try {
            // Lookup the CMS Content Page by label. Note that the label value must begin with a '/'.
            return getCmsPageService().getPageForLabel(lookupPathForRequest);
        } catch (final CMSItemNotFoundException ignore) {
            // Ignore exception
        }
        return null;
    }
}