public HttpControllerDescriptor SelectController(HttpRequestMessage request) { var routeData = request.GetRouteData(); var module = request.FindModuleInfo(); // Handle the app-api queries if (routeData.Route.RouteTemplate.Contains("DesktopModules/2sxc/API/app-api/") && module.DesktopModule.ModuleName == "2sxc-app") { var portalSettings = PortalSettings.Current; var sexy = request.GetSxcOfModuleContext(); // previously we assumed that there is a sub-folder with a future-app-id, but 2015-05-15 decided it's probably not worth trying, because each request currently needs tokens anyhow // if ((string) routeData.Values["appFolder"] != "auto-detect-app" && (string) routeData.Values["appFolder"] != sexy.App.Folder) // throw new HttpException("AppFolder was not correct - was " + routeData.Values["appFolder"] + " but should be " + sexy.App.Folder); var controllerTypeName = routeData.Values["controller"] + "Controller"; var controllerPath = Path.Combine(SexyContent.AppBasePath(portalSettings), sexy.App.Folder, "Api/" + controllerTypeName + ".cs"); if (File.Exists(HostingEnvironment.MapPath(controllerPath))) { var assembly = BuildManager.GetCompiledAssembly(controllerPath); var type = assembly.GetType(controllerTypeName); return new HttpControllerDescriptor(_config, controllerTypeName, type); } throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "Controller " + controllerTypeName + " not found in app.")); } return PreviousSelector.SelectController(request); }
internal static SxcInstance GetSxcOfApiRequest(HttpRequestMessage request) { string cbidHeader = "ContentBlockId"; var moduleInfo = request.FindModuleInfo(); // get url parameters and provide override values to ensure all configuration is // preserved in AJAX calls List<KeyValuePair<string, string>> urlParams = null; var requestParams = request.GetQueryNameValuePairs(); var origParams = requestParams.Where(p => p.Key == "originalparameters").ToList(); if (origParams.Any()) { var paramSet = origParams.First().Value; // Workaround for deserializing KeyValuePair -it requires lowercase properties(case sensitive), which seems to be a bug in some Newtonsoft.Json versions: http://stackoverflow.com/questions/11266695/json-net-case-insensitive-property-deserialization var items = Json.Deserialize<List<UpperCaseStringKeyValuePair>>(paramSet); urlParams = items.Select(a => new KeyValuePair<string, string>(a.Key, a.Value)).ToList(); //urlParams = requestParams // .Where(keyValuePair => keyValuePair.Key.IndexOf("orig", StringComparison.Ordinal) == 0) // .Select(pair => new KeyValuePair<string, string>(pair.Key.Substring(4), pair.Value)) // .ToList(); } // first, check the special overrides //var origparams = requestParams.Select(np => np.Key == "urlparameters").ToList(); //if (origparams.Any()) //{ // var paramSet = origparams.First(); //} // then add remaining params IContentBlock contentBlock = new ModuleContentBlock(moduleInfo, urlParams); // check if we need an inner block if (request.Headers.Contains(cbidHeader)) { var cbidh = request.Headers.GetValues(cbidHeader).FirstOrDefault(); int cbid; Int32.TryParse(cbidh, out cbid); if (cbid < 0) // negative id, so it's an inner block contentBlock = new EntityContentBlock(contentBlock, cbid); } return contentBlock.SxcInstance; }
public HttpControllerDescriptor SelectController(HttpRequestMessage request) { var routeData = request.GetRouteData(); var module = request.FindModuleInfo(); // Handle the app-api queries if (!routeData.Route.RouteTemplate.Contains("DesktopModules/2sxc/API/app-api/") || module.DesktopModule.ModuleName != "2sxc-app") return PreviousSelector.SelectController(request); var controllerTypeName = routeData.Values["controller"] + "Controller"; try { var portalSettings = PortalSettings.Current; var sexy = Helpers.GetSxcOfApiRequest(request);// request.GetSxcOfModuleContext(); // previously we assumed that there is a sub-folder with a future-app-id, but 2015-05-15 decided it's probably not worth trying, because each request currently needs tokens anyhow // if ((string) routeData.Values["appFolder"] != "auto-detect-app" && (string) routeData.Values["appFolder"] != sexy.App.Folder) // throw new HttpException("AppFolder was not correct - was " + routeData.Values["appFolder"] + " but should be " + sexy.App.Folder); var controllerPath = Path.Combine(AppHelpers.AppBasePath(portalSettings), sexy.App.Folder, "Api/" + controllerTypeName + ".cs"); if (File.Exists(HostingEnvironment.MapPath(controllerPath))) { var assembly = BuildManager.GetCompiledAssembly(controllerPath); var type = assembly.GetType(controllerTypeName); return new HttpControllerDescriptor(_config, controllerTypeName, type); } } catch (Exception e) { var exception = new Exception("Error while selecting / compiling a controller for the request. Pls check the event-log and the code. See the inner exception for more details.", e); DotNetNuke.Services.Exceptions.Exceptions.LogException(exception); throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception.Message, e)); } throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "Controller " + controllerTypeName + " not found in app.")); }
protected virtual ModuleInfo FindModuleInfo(HttpRequestMessage request) { return request.FindModuleInfo(); }
public void FindModuleInfoTriesAllProviders() { //Arrange var request = new HttpRequestMessage(); var configuration = new HttpConfiguration(); var provider = new Mock<ITabAndModuleInfoProvider>(); ModuleInfo moduleInfo; provider.Setup(x => x.TryFindModuleInfo(request, out moduleInfo)).Returns(false); configuration.AddTabAndModuleInfoProvider(provider.Object); configuration.AddTabAndModuleInfoProvider(provider.Object); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration; //Act request.FindModuleInfo(); //Assert provider.Verify(x => x.TryFindModuleInfo(request, out moduleInfo), Times.Exactly(2)); }
public void FindModuleInfoStopCallingProvidersAfterOneSuccess() { //Arrange var request = new HttpRequestMessage(); var configuration = new HttpConfiguration(); var provider = new Mock<ITabAndModuleInfoProvider>(); ModuleInfo moduleInfo; provider.Setup(x => x.TryFindModuleInfo(request, out moduleInfo)).Returns(true); configuration.AddTabAndModuleInfoProvider(provider.Object); configuration.AddTabAndModuleInfoProvider(provider.Object); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration; //Act request.FindModuleInfo(); //Assert provider.Verify(x => x.TryFindModuleInfo(request, out moduleInfo), Times.Once()); }