Java tutorial
/* * Dashboard * Copyright 2014 Christian Robert * * 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.perdian.apps.dashboard.mvc.portal; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.servlet.View; import de.perdian.apps.dashboard.DashboardException; /** * Main controller from which the dashboard itself (the HTML page that is being * displayed in the browser) is being delivered to the client. * * @author Christian Robert */ @Controller class PortalController { private ResourceLoader resourceLoader = null; private List<PortalModule> modules = null; @RequestMapping("**/*.board") View doDashboard(ServletWebRequest webRequest) throws IOException { Resource dashboardFileResource = this.computeDashboardFileResource(webRequest); PortalView applicationView = new PortalView(); applicationView.setModules(this.getModules()); applicationView.setJsResource(dashboardFileResource); return applicationView; } // ------------------------------------------------------------------------- // --- Helper methods ------------------------------------------------------ // ------------------------------------------------------------------------- private Resource computeDashboardFileResource(ServletWebRequest webRequest) throws IOException { String dashboardFileLocation = this.computeDashboardFileLocation(webRequest); Resource dashboardParentResource = this.getResourceLoader() .getResource(PortalConstants.DASHBOARD_RESOURCE_BASE); Resource dashboardFileResource = dashboardParentResource.createRelative(dashboardFileLocation); if (dashboardFileResource != null && dashboardFileResource.exists()) { return dashboardFileResource; } else { throw new DashboardException("Cannot find requested dashboard file at: " + dashboardFileLocation); } } private String computeDashboardFileLocation(ServletWebRequest webRequest) { String requestFileLocation = webRequest.getRequest().getServletPath(); int lastSlashIndex = requestFileLocation.lastIndexOf("."); String requestFileWithoutExtension = lastSlashIndex < 0 ? requestFileLocation : requestFileLocation.substring(0, lastSlashIndex); return requestFileWithoutExtension + PortalConstants.DASHBOARD_RESOURCE_EXTENSION; } // ------------------------------------------------------------------------- // --- Property access methods --------------------------------------------- // ------------------------------------------------------------------------- ResourceLoader getResourceLoader() { return this.resourceLoader; } @Autowired void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } List<PortalModule> getModules() { return this.modules; } @Autowired void setModules(List<PortalModule> modules) { if (modules == null) { this.modules = null; } else { List<PortalModule> sortedModules = new ArrayList<>(modules); Collections.sort(sortedModules, new PortalModule.PriorityComparator()); this.modules = sortedModules; } } }