View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.test.fixture;
35  
36  import info.magnolia.cms.filters.OncePerRequestAbstractMgnlFilter;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.context.SimpleContext;
39  import info.magnolia.module.cache.CacheModule;
40  import info.magnolia.module.cache.CachePolicyResult;
41  import info.magnolia.module.cache.cachepolicy.result.CachePolicyResultProvider;
42  import info.magnolia.module.cache.commands.FlushCachesCommand;
43  import info.magnolia.module.cache.inject.CacheFactoryProvider;
44  import info.magnolia.repository.RepositoryConstants;
45  import info.magnolia.voting.Voter;
46  import info.magnolia.voting.voters.URIRegexVoter;
47  
48  import java.io.IOException;
49  
50  import javax.inject.Inject;
51  import javax.inject.Provider;
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.Session;
55  import javax.servlet.FilterChain;
56  import javax.servlet.ServletException;
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  /**
64   * Filter for testing of {@link CacheModule}.
65   */
66  public class CacheMonitorFilter extends OncePerRequestAbstractMgnlFilter {
67  
68      private static final Logger log = LoggerFactory.getLogger(CacheMonitorFilter.class);
69  
70      private static final String PARAMETER_ACTION = "mgnlCacheAction";
71      private static final String ACTION_FLUSH = "flush";
72      private static final String ACTION_RESULT = "result";
73      private static final String ACTION_CREATE_PAGE = "createPage";
74      private static final String ACTION_OVERVIEW = "overview";
75      private static final String ACTION_STOP = "stop";
76  
77      private final CacheModule cacheModule;
78      private final CacheFactoryProvider cacheFactoryProvider;
79      private final Provider<CachePolicyResultProvider> cachePolicyResultProviderProvider;
80  
81      private CachePolicyResult latestCachePolicyResult;
82  
83      @Inject
84      public CacheMonitorFilter(CacheModule cacheModule, CacheFactoryProvider cacheFactoryProvider, Provider<CachePolicyResultProvider> cachePolicyResultProviderProvider) {
85          this.cacheModule = cacheModule;
86          this.cacheFactoryProvider = cacheFactoryProvider;
87          this.cachePolicyResultProviderProvider = cachePolicyResultProviderProvider;
88          URIRegexVoter uriRegexVoter = new URIRegexVoter();
89          uriRegexVoter.setPattern("/.magnolia/jcrprop*");
90          this.setBypasses(new Voter[]{uriRegexVoter});
91      }
92  
93      @Override
94      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
95          final String action = request.getParameter(PARAMETER_ACTION);
96  
97          if (ACTION_FLUSH.equals(action)) {
98              this.flush();
99          } else if (ACTION_RESULT.equals(action)) {
100             response.getWriter().append(this.getResult());
101         } else if (ACTION_OVERVIEW.equals(action)) {
102             response.getWriter().append(this.getOverview());
103         } else if (ACTION_STOP.equals(action)) {
104             this.stop();
105         } else if (ACTION_CREATE_PAGE.equals(action)) {
106             try {
107                 this.createPage();
108             } catch (RepositoryException e) {
109                 log.error("Unable to create page: ", e);
110             }
111         } else {
112             chain.doFilter(request, response);
113             latestCachePolicyResult = cachePolicyResultProviderProvider.get().getCachePolicyResult();
114         }
115     }
116 
117     protected Node createPage() throws RepositoryException {
118         Session session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
119         Node node = session.getRootNode().getNode("ftl-sample-site");
120         return node;
121     }
122 
123     private void flush() {
124         try {
125             new FlushCachesCommand(cacheFactoryProvider).execute(new SimpleContext());
126         } catch (Exception e) {
127             log.error("Unable to flush cache", e);
128         }
129     }
130 
131     private String getOverview() {
132         return String.valueOf(cacheModule.getCacheFactory().getCacheNames());
133     }
134 
135     private String getResult() {
136         return String.valueOf(latestCachePolicyResult);
137     }
138 
139     private void stop() {
140         cacheModule.stop(null);
141     }
142 }