PageRenderTime 274ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/web/store/src/main/java/org/yes/cart/web/filter/ShopResolverFilter.java

http://yes-cart.googlecode.com/
Java | 175 lines | 104 code | 39 blank | 32 comment | 10 complexity | 83ba6ad07a85273198066f6c7415891f MD5 | raw file
  1. package org.yes.cart.web.filter;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.BeansException;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.ApplicationContextAware;
  8. import org.springframework.web.context.ServletContextAware;
  9. import org.yes.cart.domain.entity.Shop;
  10. import org.yes.cart.service.domain.SystemService;
  11. import org.yes.cart.shoppingcart.ShoppingCart;
  12. import org.yes.cart.shoppingcart.impl.ChangeCurrencyEventCommandImpl;
  13. import org.yes.cart.shoppingcart.impl.SetShopCartCommandImpl;
  14. import org.yes.cart.util.ShopCodeContext;
  15. import org.yes.cart.web.application.ApplicationDirector;
  16. import org.yes.cart.web.support.request.HttpServletRequestWrapper;
  17. import org.yes.cart.web.support.service.LanguageService;
  18. import javax.servlet.*;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.text.MessageFormat;
  24. import java.util.Collections;
  25. import java.util.Date;
  26. /**
  27. * User: Igor Azarny iazarny@yahoo.com
  28. * Date: 2011-May-17
  29. * Time: 4:46:09 PM
  30. * <p/>
  31. * Shop resolver filter.
  32. * If shop can not be resolved by server/domain name
  33. * filter redirect to default url.
  34. */
  35. public class ShopResolverFilter extends AbstractFilter implements Filter, ApplicationContextAware, ServletContextAware {
  36. private static final Logger LOG = LoggerFactory.getLogger(ShopCodeContext.getShopCode());
  37. private final SystemService systemService;
  38. private ApplicationContext applicationContext;
  39. private ServletContext servletContext;
  40. private final LanguageService languageService;
  41. /**
  42. * @param systemService service
  43. */
  44. public ShopResolverFilter(
  45. final ApplicationDirector applicationDirector,
  46. final SystemService systemService,
  47. final LanguageService languageService) {
  48. super(applicationDirector);
  49. this.systemService = systemService;
  50. this.languageService = languageService;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public ServletRequest doBefore(final ServletRequest servletRequest,
  56. final ServletResponse servletResponse) throws IOException, ServletException {
  57. if (LOG.isDebugEnabled()) {
  58. LOG.debug(MessageFormat.format("Request id {0} start at {1}",
  59. servletRequest.toString(),
  60. (new Date()).getTime()));
  61. }
  62. final String serverDomainName = servletRequest.getServerName().toLowerCase();
  63. final Shop shop = getApplicationDirector().getShopByDomainName(serverDomainName);
  64. if (shop == null) {
  65. final String url = systemService.getDefaultShopURL();
  66. if (LOG.isInfoEnabled()) {
  67. LOG.info("Shop can not be resolved. Redirect to : " + url);
  68. }
  69. ((HttpServletResponse) servletResponse).sendRedirect(url);
  70. return null;
  71. }
  72. setDefaultValues(shop);
  73. ApplicationDirector.setCurrentShop(shop);
  74. ShopCodeContext.setShopCode(shop.getCode());
  75. //ApplicationDirector.setCurrentServletContext(servletContext);
  76. ApplicationDirector.setCurrentMailTemplateFolder(servletContext.getRealPath(shop.getMailFolder()) + File.separator);
  77. return getModifiedRequest(servletRequest, shop);
  78. }
  79. /**
  80. * Set default values. Mostly for new cart.
  81. * @param shop shop
  82. */
  83. private void setDefaultValues(final Shop shop) {
  84. final ShoppingCart shoppingCart = ApplicationDirector.getShoppingCart();
  85. if (shoppingCart.getCurrencyCode() == null) { // new cart only may satisfy this condition
  86. new SetShopCartCommandImpl(applicationContext, Collections.singletonMap(SetShopCartCommandImpl.CMD_KEY, shop.getShopId()))
  87. .execute(shoppingCart);
  88. new ChangeCurrencyEventCommandImpl(applicationContext, Collections.singletonMap(ChangeCurrencyEventCommandImpl.CMD_KEY, shop.getDefaultCurrency()))
  89. .execute(shoppingCart);
  90. }
  91. }
  92. /**
  93. * Create http serlet wrapper to handle multi store requests.
  94. *
  95. * @param servletRequest current request
  96. * @param shop resolved shop
  97. * @return servlet wrapper
  98. */
  99. private ServletRequest getModifiedRequest(final ServletRequest servletRequest, final Shop shop) {
  100. final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
  101. final String servletPath = httpServletRequest.getServletPath();
  102. if (StringUtils.isNotEmpty(servletPath)) {
  103. final String newServletPath = shop.getMarkupFolder() + servletPath;
  104. try {
  105. if (LOG.isDebugEnabled()) {
  106. LOG.debug("New servlet path is :" + newServletPath);
  107. }
  108. return new HttpServletRequestWrapper(httpServletRequest, newServletPath);
  109. } catch (/*MalformedURL*/Exception e) {
  110. if (LOG.isErrorEnabled()) {
  111. LOG.error("Wrong URL for path : " + newServletPath, e);
  112. }
  113. }
  114. }
  115. return servletRequest;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public void doAfter(final ServletRequest servletRequest,
  121. final ServletResponse servletResponse) throws IOException, ServletException {
  122. if (LOG.isDebugEnabled()) {
  123. LOG.debug(MessageFormat.format("Request id {0} end at {1}",
  124. servletRequest.toString(),
  125. (new Date()).getTime()));
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
  130. this.applicationContext = applicationContext;
  131. }
  132. /** {@inheritDoc} */
  133. public void setServletContext(final ServletContext servletContext) {
  134. this.servletContext = servletContext;
  135. }
  136. }