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 de.otto.mongodb.profiler.util.Logger; import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Calendar; import java.util.Locale; import static com.google.common.base.Preconditions.checkNotNull; @NoCache public abstract class AbstractController { private static final Logger logger = Logger.getLogger(AbstractController.class); protected static final String JSON_TYPE_1 = "application/json"; protected static final String JSON_TYPE_2 = "text/json"; public static final class MainNavigation { public static final String DATABASES = "databases"; public static final String CONNECTIONS = "connections"; } private final ProfilerService profilerService; protected AbstractController(final ProfilerService profilerService) { this.profilerService = checkNotNull(profilerService); } @ExceptionHandler(ResourceNotFoundException.class) public void onResourceNotFound(HttpServletResponse response) throws IOException { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource not found!"); } @ExceptionHandler(RuntimeException.class) public void onRuntimeException(RuntimeException e, HttpServletResponse response) throws IOException { logger.error(e, "Unhandled exception caught."); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } protected ProfilerService getProfilerService() { return profilerService; } protected ProfiledConnection requireConnection(String id) throws ResourceNotFoundException { final ProfiledConnection connection = profilerService.getConnection(id); if (connection == null) { throw new ResourceNotFoundException(String.format("Connection [%s] not found!", id)); } return connection; } protected ProfiledDatabase requireDatabase(String connection, String name) throws ResourceNotFoundException { return requireDatabase(requireConnection(connection), name); } protected ProfiledDatabase requireDatabase(ProfiledConnection connection, String name) throws ResourceNotFoundException { final ProfiledDatabase database = profilerService.getDatabase(connection, name); if (database == null) { throw new ResourceNotFoundException( String.format("Database [%s] not found for connection [%s]!", name, connection)); } return database; } protected static long lowerBoundary(int diffHours) { final Calendar cal = Calendar.getInstance(Locale.ENGLISH); cal.add(Calendar.HOUR, diffHours); return cal.getTimeInMillis(); } }