Java tutorial
/* * Copyright 2013 Robert Gacki <robert.gacki@cgi.com> * * 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 de.otto.mongodb.profiler.web; import de.otto.mongodb.profiler.ProfiledConnection; import de.otto.mongodb.profiler.ProfiledDatabase; import de.otto.mongodb.profiler.ProfilerService; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.view.RedirectView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.management.ManagementFactory; import java.lang.management.MemoryUsage; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class PageInterceptor extends HandlerInterceptorAdapter { public static final String MAIN_NAVIGATION_VIEW_MODEL_ATTRIBUTE = "mainNavigation"; public static final String FOOTER_VIEW_MODEL_ATTRIBUTE = "footer"; private final ProfilerService profilerService; private final DateTime runningSince; public PageInterceptor(ProfilerService profilerService) { this.profilerService = profilerService; this.runningSince = DateTime.now().withZone(DateTimeZone.UTC); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (handler instanceof HandlerMethod && ((HandlerMethod) handler).getMethod().isAnnotationPresent(Page.class) && (modelAndView.getView() instanceof RedirectView == false)) { addMainNavigationViewModel(((HandlerMethod) handler), modelAndView); addFooterViewModel(modelAndView, request.getLocale()); } } protected void addMainNavigationViewModel(HandlerMethod method, ModelAndView modelAndView) { final List<? extends ProfiledConnection> connections = profilerService.getConnections(); final List<MainNavigationViewModel.Connection> connectionModels = new ArrayList<>(); final List<MainNavigationViewModel.Database> databaseModels = new ArrayList<>(); for (ProfiledConnection connection : connections) { final MainNavigationViewModel.Connection connectionModel = new MainNavigationViewModel.Connection( connection.getId(), connection.getName()); connectionModels.add(connectionModel); final List<? extends ProfiledDatabase> databases = profilerService.getDatabases(connection); for (ProfiledDatabase database : databases) { databaseModels.add(new MainNavigationViewModel.Database(database.getName(), connectionModel)); } } final Page page = method.getMethodAnnotation(Page.class); final MainNavigationViewModel model = new MainNavigationViewModel(page.mainNavigation(), connectionModels, databaseModels); modelAndView.addObject(MAIN_NAVIGATION_VIEW_MODEL_ATTRIBUTE, model); } protected void addFooterViewModel(ModelAndView modelAndView, Locale locale) { final MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); final FooterViewModel model = new FooterViewModel(heap, runningSince, locale); modelAndView.addObject(FOOTER_VIEW_MODEL_ATTRIBUTE, model); } }