0% found this document useful (0 votes)
318 views8 pages

Cms Site Filter

This software is the confidential and proprietary information of hybris. You shall not disclose such confidential information and shall use it only in accordance with the terms of the license agreement.

Uploaded by

Farid Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
318 views8 pages

Cms Site Filter

This software is the confidential and proprietary information of hybris. You shall not disclose such confidential information and shall use it only in accordance with the terms of the license agreement.

Uploaded by

Farid Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

/*

* [y] hybris Platform


*
* Copyright (c) 2000-2010 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.ladbrokes.web.servlets;
import de.hybris.platform.catalog.CatalogService;
import de.hybris.platform.cms2.misc.CMSFilter;
import de.hybris.platform.cms2.misc.UrlUtils;
import de.hybris.platform.cms2.model.pages.AbstractPageModel;
import de.hybris.platform.cms2.model.preview.CMSPreviewTicketModel;
import de.hybris.platform.cms2.model.preview.PreviewDataModel;
import de.hybris.platform.cms2.model.site.CMSSiteModel;
import de.hybris.platform.cms2.servicelayer.services.CMSPreviewService;
import de.hybris.platform.cms2.servicelayer.services.CMSSiteService;
import de.hybris.platform.core.Registry;
import de.hybris.platform.jalo.c2l.LocalizableItem;
import de.hybris.platform.servicelayer.model.AbstractItemModel;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.session.SessionService;
import de.hybris.platform.servicelayer.user.UserService;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import com.ladbrokes.web.interceptors.ViewInterceptorHelper;
import com.ladbrokes.web.mappings.preview.DefaultCMSPageURLMappingHandler;
import com.ladbrokes.web.mappings.preview.URLMappingHandler;
import com.ladbrokes.web.servlets.util.FilterSpringUtil;

/**
* Responsible for setting up application - to main responsibility belongs:
* <p>
* <ul>
* <li>Matches current site by current URL</li>
* <li>Setting current site in session (through {@link CMSSiteService} )</li>
* <li>Setting current catalog version (through {@link CMSSiteService} )</li>
* <li>Setting session catalog versions (through {@link CatalogService} )</li>
* </ul>
* </p>
* <br/>
* <b>Note</b>: In former versions (i.e. 4.1.1 and earlier) as a preview mechani
sm we used
* {@link de.hybris.platform.cms2.misc.AbstractPreviewServlet} which actually is
obsolete. All necessary logic was
* adapted and moved here.
*
* @author karol.walczak
*
*/
public class CMSSiteFilter extends OncePerRequestFilter implements CMSFilter
{
@SuppressWarnings("unused")
private final static Logger LOG = Logger.getLogger(CMSSiteFilter.class);

@Override
protected void doFilterInternal(final HttpServletRequest httpRequest, fi
nal HttpServletResponse httpResponse,
final FilterChain filterChain) throws ServletException,
IOException
{
Registry.activateMasterTenant();
final String requestURL = httpRequest.getRequestURL().toString()
;
final ContextInformationLoader informationLoader = ContextInform
ationLoader.getInstance();
final ServletContext servletContext = this.getServletContext();
final WebApplicationContext wac = WebApplicationContextUtils.get
RequiredWebApplicationContext(servletContext);
final ViewInterceptorHelper viewDetection = (ViewInterceptorHelp
er) wac.getBean("mobileSubViewInterceptor");
if (viewDetection.deviceDetection(httpRequest, httpResponse).equ
als("iphone"))
{
//String redirectURL = processPreviewRequest(httpRequest
, informationLoader);
//redirectURL = "http://localhost-mobile:9001/ladbrokesw
eb/view/mobile";
//proceed filters
//filterChain.doFilter(httpRequest, httpResponse);
httpResponse.sendRedirect(httpResponse.encodeRedirectURL
("http://localhost-mobile:9001/ladbrokesweb/view/mobile"));
}
else
{
//check whether exits valid preview data
if (!isPreviewDataModelValid(httpRequest))
{
//process normal request (i.e. normal browser no
n-cmscockpit request)
proccessNormalRequest(httpRequest, informationLo
ader);
//proceed filters
filterChain.doFilter(httpRequest, httpResponse);
}
else if (StringUtils.contains(requestURL, PREVIEW_TOKEN)
)
{
final String redirectURL = processPreviewRequest
(httpRequest, informationLoader);
//redirect to computed URL
httpResponse.sendRedirect(httpResponse.encodeRed
irectURL(redirectURL));
}
else
{
//proceed filters
filterChain.doFilter(httpRequest, httpResponse);
}
}
}
/**
* Processing normal request (i.e. when user goes directly to that appli
cation - not from cmscockpit)
* <p/>
* <b>Note:</b> <br/>
* We preparing application by setting correct:
* <ul>
* <li>Current Site</li>
* <li>Current Catalog Versions</li>
* <li>Enabled language fallback</li>
* </ul>
*
* @see ContextInformationLoader#setCurrentSite(HttpServletRequest,Strin
g)
* @see ContextInformationLoader#setCatalogVersions(HttpServletRequest )
* @param httpRequest
* current request
* @param informationLoader
* default context information loader
*/
protected void proccessNormalRequest(final HttpServletRequest httpReques
t, final ContextInformationLoader informationLoader)
{
final String queryString = httpRequest.getQueryString();
final String currentRequestURL = httpRequest.getRequestURL().toS
tring();
String absoluteURL = currentRequestURL + (StringUtils.isBlank(q
ueryString) ? "" : queryString);
absoluteURL = "http://localhost-mobile:9001/ladbrokesweb/view/mo
bile";
//set current site
informationLoader.setCurrentSite(httpRequest, absoluteURL);
informationLoader.setCatalogVersions(httpRequest);
//set fall back language enabled
setFallbackLanguage(httpRequest, Boolean.TRUE);
}
/**
* Processing preview request (i.e. request with additional parameters l
ike {@link CMSFilter#PREVIEW_TOKEN} requested
* from cmscockpit) )
* <p/>
* <b>Note:</b> Processing preview data in order to generate target URL,
and load necessary information in user
* session
* <ul>
* <li>Initialize information (Active CMSSite, Catalog versions,Current
catalog version ) information getting from
* valid Preview Data</li>
* <li>Load all fake information (like: User, User group, Language, Time
...)
* <li>Generating target URL according to Preview Data
* </ul>
*
* @param httpRequest
* current request
* @param informationLoader
* default context information loader
*
* @return target URL
*/
protected String processPreviewRequest(final HttpServletRequest httpRequ
est, final ContextInformationLoader informationLoader)
{
String destionationURL = StringUtils.EMPTY;
final PreviewDataModel previewDataModel = getPreviewData(getPrev
iewTicketId(httpRequest), httpRequest);
//load necessary information
informationLoader.initialize(httpRequest, previewDataModel);
//load fake context information
informationLoader.loadFakeContextInformation(httpRequest, previe
wDataModel);
//generate destination URL
destionationURL = generatePreviewUrl(httpRequest, previewDataMod
el);
// persist changes
if (previewDataModel != null)
{
previewDataModel.setResourcePath(destionationURL);
informationLoader.storePreviewData(httpRequest, previewD
ataModel);
}
final CMSPreviewTicketModel ticket = getCmsPreviewService(httpRe
quest).createPreviewTicket(previewDataModel);
String parameterDelimiter = "?";
if (StringUtils.contains(destionationURL, "?"))
{
parameterDelimiter = "&";
}
final String finalURL = destionationURL + parameterDelimiter + P
REVIEW_TICKET_ID_PARAM + "=" + ticket.getId();
return finalURL;
}
/**
* Enables or disables language fall back
* <p/>
*
* @param httpRequest
* current request
* @param enabled
* enabled or disabled
*/
protected void setFallbackLanguage(final HttpServletRequest httpRequest,
final Boolean enabled)
{
final SessionService sessionService = getSessionService(httpRequ
est);
if (sessionService != null)
{
sessionService.setAttribute(LocalizableItem.LANGUAGE_FAL
LBACK_ENABLED, enabled);
sessionService.setAttribute(AbstractItemModel.LANGUAGE_F
ALLBACK_ENABLED_SERVICE_LAYER, enabled);
}
}
/**
* Generates target URL accordingly to valid Preview Data passed as a pa
rameter
* <p/>
*
* @param httpRequest
* current request
* @param previewDataModel
* valid data model contains all necessary information
* @return target URL
*/
protected String generatePreviewUrl(final HttpServletRequest httpRequest
, final PreviewDataModel previewDataModel)
{
String generatedPreviewUrl = StringUtils.EMPTY;
if (previewDataModel != null)
{
if (StringUtils.isBlank(generatedPreviewUrl))
{
final AbstractPageModel abstractPageModel = prev
iewDataModel.getPage();
if (abstractPageModel != null)
{
generatedPreviewUrl = getURLMappingHanld
er(httpRequest).getPageUrl(httpRequest, previewDataModel);
}
else
{
generatedPreviewUrl = previewDataModel.g
etResourcePath();
}
}
}
if (StringUtils.isBlank(generatedPreviewUrl))
{
generatedPreviewUrl = UrlUtils.extractHostInformationFro
mRequest(httpRequest, getCMSSiteService(httpRequest)
.getCurrentSite());
}
generatedPreviewUrl = StringUtils.removeStart(generatedPreviewUr
l, "/");
return generatedPreviewUrl;
}
/**
* Retrieves current mapping handler in order to generate proper target
URL for CMS Page
* <p/>
*
* @param httpRequest
* current request
* @return current mapping handler
*/
protected URLMappingHandler getURLMappingHanlder(final HttpServletReques
t httpRequest)
{
URLMappingHandler urlMappingHandler = FilterSpringUtil.getSpring
Bean(httpRequest, "cmsPageUrlHndlerMapping",
URLMappingHandler.class);
if (urlMappingHandler == null)
{
urlMappingHandler = new DefaultCMSPageURLMappingHandler(
);
}
return urlMappingHandler;
}
protected CMSSiteService getCMSSiteService(final HttpServletRequest http
Request)
{
return FilterSpringUtil.getSpringBean(httpRequest, "cmsSiteServi
ce", CMSSiteService.class);
}
protected CMSPreviewService getCMSPreviewService(final HttpServletReques
t httpRequest)
{
return FilterSpringUtil.getSpringBean(httpRequest, "cmsPreviewSe
rvice", CMSPreviewService.class);
}
protected UserService getUserService(final HttpServletRequest httpReques
t)
{
return FilterSpringUtil.getSpringBean(httpRequest, "userService"
, UserService.class);
}
protected CatalogService getCatalogService(final HttpServletRequest http
Request)
{
return FilterSpringUtil.getSpringBean(httpRequest, "catalogServi
ce", CatalogService.class);
}
protected ModelService getModelService(final HttpServletRequest httpRequ
est)
{
return FilterSpringUtil.getSpringBean(httpRequest, "i18nservice"
, ModelService.class);
}
protected SessionService getSessionService(final HttpServletRequest http
Request)
{
return FilterSpringUtil.getSpringBean(httpRequest, "sessionServi
ce", SessionService.class);
}
protected CMSPreviewService getCmsPreviewService(final HttpServletReques
t httpRequest)
{
return FilterSpringUtil.getSpringBean(httpRequest, "cmsPreviewSe
rvice", CMSPreviewService.class);
}
protected CMSSiteModel getCurrentCmsSite(final HttpServletRequest httpRe
quest)
{
return getCMSSiteService(httpRequest).getCurrentSite();
}
/**
* Retrieves {@link CMSFilter#PREVIEW_TICKET_ID_PARAM} from current requ
est
*
* @param httpRequest
* current request
* @return current ticket id
*/
private String getPreviewTicketId(final HttpServletRequest httpRequest)
{
String id = httpRequest.getParameter(PREVIEW_TICKET_ID_PARAM);
if (StringUtils.isBlank(id))
{
id = getSessionService(httpRequest).getAttribute(PREVIEW
_TICKET_ID_PARAM);
}
return id;
}
/**
* Checks whether current Preview Data is valid (not removed)
*
* @param httpRequest
* current request
* @return true whether is valid otherwise false
*/
private boolean isPreviewDataModelValid(final HttpServletRequest httpReq
uest)
{
return getPreviewData(getPreviewTicketId(httpRequest), httpReque
st) != null;
}
/**
* Retrieves current Preview Data according to given ticked id
*
* @param ticketId
* current ticket id
* @param httpRequest
* current request
* @return current Preview Data attached to given ticket if any otherwis
e null
*/
private PreviewDataModel getPreviewData(final String ticketId, final Htt
pServletRequest httpRequest)
{
PreviewDataModel ret = null;
final CMSPreviewTicketModel previewTicket = getCmsPreviewService
(httpRequest).getPreviewTicket(ticketId);
if (previewTicket != null)
{
ret = previewTicket.getPreviewData();
}
return ret;
}
}

You might also like