Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Services.WebApp/3.3/Controllers/WebAppControllerSelector.cs @ 14783

Last change on this file since 14783 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Concurrent;
24using System.Collections.Generic;
25using System.Linq;
26using System.Net.Http;
27using System.Reflection;
28using System.Web.Http;
29using System.Web.Http.Controllers;
30using System.Web.Http.Dispatcher;
31
32namespace HeuristicLab.Services.WebApp.Controllers {
33  public class WebAppHttpControllerSelector : DefaultHttpControllerSelector {
34    private readonly HttpConfiguration configuration;
35    private readonly IDictionary<string, HttpControllerDescriptor> controllers;
36    private readonly PluginManager pluginManager = PluginManager.Instance;
37
38    public WebAppHttpControllerSelector(HttpConfiguration configuration)
39      : base(configuration) {
40      this.configuration = configuration;
41      controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>();
42      LoadAppControllers();
43    }
44
45    private void LoadAppControllers() {
46      var assembly = Assembly.GetExecutingAssembly();
47      var assemblyTypes = assembly.GetTypes();
48      var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
49      foreach (var apiController in apiControllers) {
50        var apiControllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
51        controllers.Add(apiControllerName, new HttpControllerDescriptor(configuration, apiControllerName, apiController));
52      }
53    }
54
55    public override HttpControllerDescriptor SelectController(HttpRequestMessage request) {
56      if (request == null) {
57        throw new ArgumentNullException("request");
58      }
59      var parts = request.RequestUri.AbsolutePath.Split('/');
60      int startIndex = parts.TakeWhile(part => part.ToLower() != "api").Count();
61      if (parts.Length < startIndex + 2) {
62        throw new ArgumentException("invalid request path");
63      }
64      string pluginName = parts[startIndex + 1].ToLower();
65      string controllerName = parts[startIndex + 2].ToLower();
66      // load controller
67      if (pluginName == "app") {
68        // from main app
69        HttpControllerDescriptor controller;
70        controllers.TryGetValue(controllerName, out controller);
71        return controller;
72      }
73      // from plugin
74      var plugin = pluginManager.GetPlugin(pluginName);
75      if (plugin == null) {
76        throw new ArgumentException(string.Format("invalid plugin '{0}'", pluginName));
77      }
78      return plugin.GetController(controllerName);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.