View Javadoc
1   package org.exoplatform.platform.welcomescreens.web;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.*;
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.exoplatform.commons.utils.PropertyManager;
10  import org.exoplatform.container.ExoContainerContext;
11  import org.exoplatform.container.PortalContainer;
12  import org.exoplatform.platform.common.software.register.UnlockService;
13  import org.exoplatform.platform.welcomescreens.service.TermsAndConditionsService;
14  import org.exoplatform.web.filter.Filter;
15  
16  /**
17   * Filter responsible of Terms and conditions displaying.
18   * Call TermsAndConditions service to know if TermsAndConditions are checked, if not, forward to TermsAndConditions page
19   * <p>
20   * 2 conditions to forward to termes and conditions page:
21   * <ul>
22   * <li>Request URI is not a login URI. In this case we need to execute TermsAndConditions process after login process</li>
23   * <li>TermsAndConditions is not checked</li>
24   * </ul>
25   * 
26   * @author Clement
27   *
28   */
29  public class TermsAndConditionsFilter implements Filter {
30  
31    private static final String PLF_WELCOME_SCREENS_SERVLET_CTX = "/trial";
32    private static final String TC_SERVLET_URL = "/terms-and-conditions";
33    private static final String INITIAL_URI_PARAM_NAME = "initialURI";
34    private static final String LOGIN_URI = "/login";
35    private static final String DOLOGIN_URI = "/dologin";
36  
37    public TermsAndConditionsFilter() {
38  
39    }
40  
41    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
42      HttpServletRequest httpServletRequest = (HttpServletRequest)request;
43      HttpServletResponse httpServletResponse = (HttpServletResponse)response;
44  
45      TermsAndConditionsService termsAndConditionsService = PortalContainer.getInstance().getComponentInstanceOfType(TermsAndConditionsService.class);
46      UnlockService unlockService = PortalContainer.getInstance().getComponentInstanceOfType(UnlockService.class);
47  
48      boolean tcChecked = termsAndConditionsService.isTermsAndConditionsChecked();
49      
50      String requestUri = httpServletRequest.getRequestURI();
51      String loginRequestUri = httpServletRequest.getContextPath() + LOGIN_URI;
52      String dologinRequestUri = httpServletRequest.getContextPath() + DOLOGIN_URI;
53      boolean isLoginUri = (requestUri.contains(loginRequestUri) || requestUri.contains(dologinRequestUri));
54      boolean isRestUri = requestUri.contains(ExoContainerContext.getCurrentContainer().getContext().getRestContextName());
55      boolean isDevMod = PropertyManager.isDevelopping();
56      if(! isLoginUri && ! isRestUri && ! tcChecked && !isDevMod && unlockService.showTermsAndConditions()) {
57        // Get full url
58        String reqUri = httpServletRequest.getRequestURI().toString();
59        String queryString = httpServletRequest.getQueryString();
60        if (queryString != null) {
61            reqUri =new StringBuffer(reqUri).append("?").append(queryString).toString();
62        }
63        
64        // Get plf extension servlet context (because TermsAndConditionsFilter and terms-and-conditions servlet declaration are not on same context (webapp))
65        ServletContext welcomrScreensContext = httpServletRequest.getSession().getServletContext().getContext(PLF_WELCOME_SCREENS_SERVLET_CTX);
66        // Forward to resource from this context: 
67        String uriTarget = (new StringBuilder()).append(TC_SERVLET_URL + "?" + INITIAL_URI_PARAM_NAME + "=").append(reqUri).toString();
68        welcomrScreensContext.getRequestDispatcher(uriTarget).forward(httpServletRequest, httpServletResponse);
69        return;
70      }
71      chain.doFilter(request, response);
72    }
73  }