AddStuffInterceptor.java
/**
* Copyright 2014 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.genesys.server.mvc;
import java.time.LocalDate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class AddStuffInterceptor implements HandlerInterceptor, InitializingBean {
// @Autowired
// private RequestTracker requestTracker;
@Value("${build.name}")
private String buildName;
@Value("${build.revision}")
private String buildRevision;
@Value("${cdn.servers}")
private String[] cdnServers;
@Value("${base.url}")
private String baseUrl;
@Value("${google.analytics.account}")
private String googleAnalyticsAccount;
@Value("${genesys.catalog.url}")
private String genesysCatalogUrl;
public abstract interface RandomString {
/// Get a random CDN server
public String getNext();
/// Return all CDN servers in a JS array
public String getServerList();
}
private RandomString randomCdnServer;
@Override
public void afterPropertiesSet() throws Exception {
googleAnalyticsAccount = StringUtils.defaultIfBlank(googleAnalyticsAccount, null);
final String allCdnJs = new ObjectMapper().writeValueAsString(cdnServers);
randomCdnServer = new RandomString() {
@Override
public String getNext() {
return cdnServers[0];
}
@Override
public String getServerList() {
return allCdnJs;
}
};
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView modelAndView) throws Exception {
final long startTime = (Long) request.getAttribute("springStartTime");
final long endTime = System.currentTimeMillis();
final long executeTime = endTime - startTime;
request.setAttribute("springExecuteTime", executeTime);
// try {
// // arg0.setAttribute("lastGet", requestTracker.getLastGet());
// } catch (BeanCreationException e) {
// // No requestTracker bean
// }
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
final long startTime = System.currentTimeMillis();
request.setAttribute("springStartTime", startTime);
request.setAttribute("buildName", buildName);
request.setAttribute("buildRevision", buildRevision);
request.setAttribute("cdnServers", randomCdnServer);
request.setAttribute("baseUrl", baseUrl);
request.setAttribute("currentYear", LocalDate.now().getYear());
request.setAttribute("genesysCatalogUrl", genesysCatalogUrl);
if (googleAnalyticsAccount != null) {
request.setAttribute("googleAnalyticsAccount", googleAnalyticsAccount);
}
return true;
}
}