Commit 60b94b7c authored by Matija Obreza's avatar Matija Obreza
Browse files

Added a filter to add variables to requests (GA)

parent 9ebad3c0
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Component("envVarFilter")
public class EnvVariablesFilter extends OncePerRequestFilter {
@Value("${google.analytics.account}")
private String googleAnalyticsAccount;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (StringUtils.isNotBlank(googleAnalyticsAccount)) {
request.setAttribute("googleAnalyticsAccount", googleAnalyticsAccount);
}
filterChain.doFilter(request, response);
}
}
\ No newline at end of file
......@@ -45,7 +45,7 @@
<mvc:default-servlet-handler/>
<!--Controllers-->
<context:component-scan base-package="org.genesys2.server.servlet.controller"/>
<context:component-scan base-package="org.genesys2.server.servlet.filter,org.genesys2.server.servlet.controller" />
<aop:aspectj-autoproxy/>
......
......@@ -29,6 +29,19 @@
</div>
</div>
<c:if test="${googleAnalyticsAccount ne null}">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '${googleAnalyticsAccount}']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</c:if>
<!-- Processing time:
<div class="footer clearfix" style="font-size: 80%"><spring:message code="page.rendertime" arguments="${springExecuteTime}" /></div>
......
......@@ -42,6 +42,16 @@
<filter-name>org.springframework.security.filterChainProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>envVarFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<description>Adds variables to all requests</description>
</filter>
<filter-mapping>
<filter-name>envVarFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment