/// <summary> /// Reads the handler settings on an own app domain which is unloaded afterwards /// </summary> public static bool ReadSettingsInOwnDomain(string jobAssemblyPath, out HandlerSettings handlerSettings) { handlerSettings = null; var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase, ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName, LoaderOptimization = LoaderOptimization.MultiDomainHost, ShadowCopyFiles = "true", AppDomainInitializerArguments = null }); var proxy = (JobAssemblyLoader)domain.CreateInstanceAndUnwrap(typeof(JobAssemblyLoader).Assembly.FullName, typeof(JobAssemblyLoader).FullName); var loadingSuccess = proxy.LoadAssembly(jobAssemblyPath); if (!loadingSuccess) { return(false); } handlerSettings = proxy.GetHandlerSettings(); AppDomain.Unload(domain); return(true); }
protected override void InitializeEvents(HandlerSettings settings) { // LocalizationService.SavedLanguage += EventSavedItem; LocalizationService.DeletedLanguage += EventDeletedItem; LocalizationService.SavedLanguage += LocalizationService_SavedLanguage; }
public bool LoadAssembly(string jobScriptAssemblyPath) { var jobScriptAssembly = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(jobScriptAssemblyPath)); Type handlerInitializerType = null; foreach (var type in jobScriptAssembly.GetTypes()) { if (typeof(IHandlerInitializer).IsAssignableFrom(type)) { handlerInitializerType = type; break; } } if (handlerInitializerType == null) { return(false); } // Initialize the handler initializer var jobInstance = (IHandlerInitializer)Activator.CreateInstance(handlerInitializerType); // Read the settings _handlerSettings = jobInstance.GetHandlerSettings(); _customSettings = jobInstance.GetCustomHandlerSettings(); return(true); }
protected override void TerminateEvents(HandlerSettings settings) { ContentService.Saved -= EventSavedItem; ContentService.Deleted -= EventDeletedItem; ContentService.Moved -= EventMovedItem; ContentService.Trashed -= EventMovedItem; }
/// <summary> /// Should we save this value to disk? /// </summary> /// <remarks> /// In general we save everything to disk, even if we are not going to remimport it later /// but you can stop this with RulesOnExport = true in the settings /// </remarks> protected override bool ShouldExport(XElement node, HandlerSettings config) { // We export trashed items by default, (but we don't import them by default) var trashed = node.Element("Info")?.Element("Trashed").ValueOrDefault(false); if (trashed.GetValueOrDefault(false) && !config.GetSetting <bool>("ExportTrashed", true)) { return(false); } if (config.GetSetting("RulesOnExport", false)) { // we run the import rules (but not the base rules as that would confuse.) if (!ImportTrashedItem(node, config)) { return(false); } if (!ImportPaths(node, config)) { return(false); } } return(true); }
public static MidFunc HandleQueries(HandlerSettings options, Func <IDictionary <string, object>, Type> getInputType = null, Func <IDictionary <string, object>, Type> getOutputType = null, string queryPath = "/query") { Guard.EnsureNotNull(options, "options"); var acceptableMethods = new[] { "GET", "POST" }; return(next => env => { var context = new OwinContext(env); if (!context.Request.Path.StartsWithSegments(new PathString(queryPath))) { return next(env); } if (!acceptableMethods.Contains(context.Request.Method)) { return next(env); } return BuildHandlerCall() .ExecuteWithExceptionHandling(context, options); }); }
virtual public void ImportSecondPass(string file, TObject item, HandlerSettings config, SyncUpdateCallback callback) { if (IsTwoPass) { try { syncFileService.EnsureFileExists(file); var flags = SerializerFlags.None; if (config.BatchSave) { flags |= SerializerFlags.DoNotSave; } using (var stream = syncFileService.OpenRead(file)) { var node = XElement.Load(stream); serializer.DeserializeSecondPass(item, node, flags); stream.Dispose(); } } catch (Exception ex) { logger.Warn <TObject>($"Second Import Failed: {ex.Message}"); } } }
internal static async Task ExecuteWithExceptionHandling( this Func <IOwinContext, HandlerSettings, Task> actionToRun, IOwinContext context, HandlerSettings options) { Exception caughtException; try { await actionToRun(context, options); return; } catch (Exception ex) { caughtException = ex; } var httpStatusException = caughtException as HttpStatusException; if (httpStatusException != null) { await context.HandleHttpStatusException(httpStatusException, options); return; } var invalidOperationException = caughtException as InvalidOperationException; if (invalidOperationException != null) { await context.HandleBadRequest(invalidOperationException, options); return; } await context.HandleInternalServerError(caughtException, options); }
protected override void InitializeEvents(HandlerSettings settings) { MemberTypeService.Saved += EventSavedItem; MemberTypeService.Deleted += EventDeletedItem; MemberTypeService.Moved += EventMovedItem; MemberTypeService.SavedContainer += EventContainerSaved; }
/// <summary> /// don't think you can get dictionary items via the entity service :( /// </summary> public IEnumerable <uSyncAction> ExportAll(Guid parent, string folder, HandlerSettings config, SyncUpdateCallback callback) { var actions = new List <uSyncAction>(); var items = new List <IDictionaryItem>(); if (parent == Guid.Empty) { items = localizationService.GetRootDictionaryItems().ToList(); } else { items = localizationService.GetDictionaryItemChildren(parent).ToList(); } int count = 0; foreach (var item in items) { count++; callback?.Invoke(item.ItemKey, count, items.Count); actions.AddRange(Export(item, folder, config)); actions.AddRange(ExportAll(item.Key, folder, config, callback)); } return(actions); }
public HandlerSettings GetHandlerSettings() { var handlerSettings = new HandlerSettings(); FillHandlerSettings(handlerSettings); return(handlerSettings); }
public IEnumerable <HandlerConfigPair> GetValidHandlers(string actionName, uSyncSettings settings) { var validHandlers = new List <HandlerConfigPair>(); foreach (var syncHandler in this) { var config = settings.Handlers.FirstOrDefault(x => x.Alias.InvariantEquals(syncHandler.Alias)); if (config == null) { config = new HandlerSettings(syncHandler.Alias, settings.EnableMissingHandlers) { GuidNames = new OverriddenValue <bool>(settings.UseGuidNames, false), UseFlatStructure = new OverriddenValue <bool>(settings.UseFlatStructure, false) }; } if (config != null && config.Enabled) { validHandlers.Add(new HandlerConfigPair() { Handler = syncHandler, Settings = config }); } } return(validHandlers.OrderBy(x => x.Handler.Priority)); }
public IEnumerable <uSyncAction> ReportFolder(string folder, HandlerSettings config, SyncUpdateCallback callback) { List <uSyncAction> actions = new List <uSyncAction>(); var files = syncFileService.GetFiles(folder, "*.config"); int count = 0; int total = files.Count(); foreach (string file in files) { count++; callback?.Invoke(Path.GetFileNameWithoutExtension(file), count, total); actions.Add(ReportItem(file)); } foreach (var children in syncFileService.GetDirectories(folder)) { actions.AddRange(ReportFolder(children, config, callback)); } return(actions); }
public static MidFunc HandleCommands(HandlerSettings options, string commandPath = "/commands") { Guard.EnsureNotNull(options, "options"); return(next => env => { // PUT "/{guid}" with a Json body. var context = new OwinContext(env); if (!context.Request.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase)) { // Not a PUT, pass through. return next(env); } var path = context.Request.Path; if (!path.StartsWithSegments(new PathString(commandPath), out path)) { // not routed to us return next(env); } var commandIdString = path.Value.Substring(1); Guid commandId; if (!Guid.TryParse(commandIdString, out commandId)) { // Resource is not a GUID, pass through return next(env); } return BuildHandlerCall(commandId).ExecuteWithExceptionHandling(context, options); }); }
public IEnumerable <uSyncAction> ImportAll(string folder, HandlerSettings config, bool force, SyncUpdateCallback callback = null) { var sw = Stopwatch.StartNew(); logger.Debug(handlerType, "{alias} ImportAll: {fileName}", this.Alias, Path.GetFileName(folder)); var actions = new List <uSyncAction>(); var updates = new Dictionary <string, TObject>(); runtimeCache.ClearByKey($"keycache_{this.Alias}"); actions.AddRange(ImportFolder(folder, config, updates, force, callback)); if (updates.Any()) { ProcessSecondPasses(updates, actions, config, callback); } runtimeCache.ClearByKey($"keycache_{this.Alias}"); callback?.Invoke("Done", 3, 3); sw.Stop(); logger.Debug(handlerType, "{alias} Import Complete {elapsedMilliseconds}ms", this.Alias, sw.ElapsedMilliseconds); return(actions); }
private bool ImportPaths(XElement node, HandlerSettings config) { var include = config.GetSetting("Include", "") .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (include.Length > 0) { var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty); if (!string.IsNullOrWhiteSpace(path) && !include.Any(x => path.InvariantStartsWith(x))) { logger.LogDebug("Not processing item, {0} path {1} not in include path", node.Attribute("Alias").ValueOrDefault("unknown"), path); return(false); } } var exclude = config.GetSetting("Exclude", "") .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (exclude.Length > 0) { var path = node.Element("Info")?.Element("Path").ValueOrDefault(string.Empty); if (!string.IsNullOrWhiteSpace(path) && exclude.Any(x => path.InvariantStartsWith(x))) { logger.LogDebug("Not processing item, {0} path {1} is excluded", node.Attribute("Alias").ValueOrDefault("unknown"), path); return(false); } } return(true); }
public static MidFunc HandleCommands(HandlerSettings options, string commandPath = "/commands") { Guard.EnsureNotNull(options, "options"); return next => env => { // PUT "/{guid}" with a Json body. var context = new OwinContext(env); if (!context.Request.Method.Equals("PUT", StringComparison.OrdinalIgnoreCase)) { // Not a PUT, pass through. return next(env); } var path = context.Request.Path; if (!path.StartsWithSegments(new PathString(commandPath), out path)) { // not routed to us return next(env); } var commandIdString = path.Value.Substring(1); Guid commandId; if (!Guid.TryParse(commandIdString, out commandId)) { // Resource is not a GUID, pass through return next(env); } return BuildHandlerCall(commandId).ExecuteWithExceptionHandling(context, options); }; }
protected override void TerminateEvents(HandlerSettings settings) { MemberTypeService.Saved -= EventSavedItem; MemberTypeService.Deleted -= EventDeletedItem; MemberTypeService.Moved -= EventMovedItem; MemberTypeService.SavedContainer -= EventContainerSaved; }
protected override void InitializeEvents(HandlerSettings settings) { ContentService.Saved += EventSavedItem; ContentService.Deleted += EventDeletedItem; ContentService.Moved += EventMovedItem; ContentService.Trashed += EventMovedItem; }
protected virtual IEnumerable <uSyncAction> ImportFolder(string folder, HandlerSettings config, Dictionary <string, TObject> updates, bool force, SyncUpdateCallback callback) { List <uSyncAction> actions = new List <uSyncAction>(); var files = syncFileService.GetFiles(folder, "*.config"); var flags = SerializerFlags.None; if (force) { flags |= SerializerFlags.Force; } if (config.BatchSave) { flags |= SerializerFlags.DoNotSave; } int count = 0; int total = files.Count(); foreach (string file in files) { count++; callback?.Invoke($"Importing {Path.GetFileNameWithoutExtension(file)}", count, total); var attempt = Import(file, config, flags); if (attempt.Success && attempt.Item != null) { updates.Add(file, attempt.Item); } var action = uSyncActionHelper <TObject> .SetAction(attempt, file, IsTwoPass); if (attempt.Details != null && attempt.Details.Any()) { action.Details = attempt.Details; } actions.Add(action); } // bulk save .. if (flags.HasFlag(SerializerFlags.DoNotSave) && updates.Any()) { callback?.Invoke($"Saving {updates.Count()} changes", 1, 1); serializer.Save(updates.Select(x => x.Value)); } var folders = syncFileService.GetDirectories(folder); foreach (var children in folders) { actions.AddRange(ImportFolder(children, config, updates, force, callback)); } callback?.Invoke("", 1, 1); return(actions); }
private bool GetConfigValue(HandlerSettings config, string setting, bool defaultValue) { if (!config.Settings.ContainsKey(setting)) { return(defaultValue); } return(config.Settings[setting].InvariantEquals("true")); }
public IEnumerable <uSyncAction> Report(string folder, HandlerSettings config, SyncUpdateCallback callback) { var actions = new List <uSyncAction>(); callback?.Invoke("Checking Actions", 0, 1); actions.AddRange(ReportFolder(folder, config, callback)); callback?.Invoke("Done", 1, 1); return(actions); }
public DscHandlerHelper(ILogger <DscHandlerHelper> logger, IOptions <HandlerSettings> settings, DscHandlerManager dscManager) { _logger = logger; _settings = settings.Value; _dscManager = dscManager; Init(); }
/// <summary> /// Workout if we are excluding this relationType from export/import /// </summary> protected override bool ShouldExport(XElement node, HandlerSettings config) { var exclude = config.GetSetting <string>("Exclude", defaultRelations); if (!string.IsNullOrWhiteSpace(exclude) && exclude.Contains(node.GetAlias())) { return(false); } return(true); }
public void Setup() { configuration = new Mock<IApiConfiguration>(); configuration.SetupGet(f => f.ConfigurationRotationInterval).Returns(new TimeSpan(0, 0, 2)); configuration.Setup(f => f.ChaosSettings) .Returns(new Collection<ChaosSettings> { new ChaosSettings { Name = "Test1" }, new ChaosSettings { Name = "Test2" }, new ChaosSettings { Name = "Test3" } }); rotationTimer = new Mock<IConfigurationRotatationTimer>(); handlerSettings = new HandlerSettings(rotationTimer.Object, configuration.Object); }
virtual public IEnumerable <uSyncAction> ExportAll(string folder, HandlerSettings config, SyncUpdateCallback callback) { // we dont clean the folder out on an export all. // because the actions (renames/deletes) live in the folder // // there will have to be a diffrent clean option /// // syncFileService.CleanFolder(folder); return(ExportAll(-1, folder, config, callback)); }
/// <summary> /// Import an item that is inthe trached state in the XML file. /// </summary> /// <remarks> /// Trashed items are only imported when the "ImportTrashed" setting is true on the handler /// </remarks> private bool ImportTrashedItem(XElement node, HandlerSettings config) { // unless the setting is explicit we don't import trashed items. var trashed = node.Element("Info")?.Element("Trashed").ValueOrDefault(false); if (trashed.GetValueOrDefault(false) && !config.GetSetting("ImportTrashed", true)) { return(false); } return(true); }
/// <summary> /// Update (PUT) an artifact to a URL - usually the URL for an existing OSLC artifact /// </summary> /// <param name="url"></param> /// <param name="artifact"></param> /// <param name="mediaType"></param> /// <param name="acceptType"></param> /// <returns></returns> public HttpResponseMessage UpdateResource( string url, object artifact, string mediaType, string acceptType, Dictionary <String, String> headers) { client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptType)); client.DefaultRequestHeaders.Add(OSLCConstants.OSLC_CORE_VERSION, "2.0"); AddAuthorizationHeader(); foreach (KeyValuePair <string, string> entry in headers) { client.DefaultRequestHeaders.Add(entry.Key, entry.Value); } MediaTypeHeaderValue mediaTypeValue = new MediaTypeHeaderValue(mediaType); MediaTypeFormatter formatter = new MediaTypeFormatterCollection(formatters).FindWriter(artifact.GetType(), mediaTypeValue); HttpResponseMessage response = null; bool redirect = false; do { ObjectContent content = new ObjectContent(artifact.GetType(), artifact, formatter); content.Headers.ContentType = mediaTypeValue; // Write the content of the object to the log. The writing of the object is an async operation, so // will get compiler warning if not await execution. OK to proceed here, so avoid warning by assigning // variable. See also http://msdn.microsoft.com/en-us/library/hh873131.aspx. Task t = HandlerSettings.LogMessage("Object PUT: ", content, url, HandlerSettings.LoggingLevel.INFO); response = client.PutAsync(url, content).Result; if ((response.StatusCode == HttpStatusCode.MovedPermanently) || (response.StatusCode == HttpStatusCode.Moved)) { url = response.Headers.Location.AbsoluteUri; response.ConsumeContent(); redirect = true; } else { redirect = false; } } while (redirect); return(response); }
private string GetConfigValue(HandlerSettings config, string setting, string defaultValue) { if (!config.Settings.ContainsKey(setting)) { return(defaultValue); } if (string.IsNullOrWhiteSpace(config.Settings[setting])) { return(defaultValue); } return(config.Settings[setting]); }
/// <summary> /// Initializes the instance from all necessary information /// </summary> public bool InitializeFromJobScript(JobScriptFile jobScriptFile, string outputAssembly, HandlerSettings handlerSettings) { // Check if any data changed at all if (JobScript != null && JobScript.Hash == jobScriptFile.Hash) { return(false); } // Set the new values JobScript = jobScriptFile; JobScriptAssembly = outputAssembly; HandlerSettings = handlerSettings; // Notify the running handler of the new value if (_handlerProxy != null) { _handlerProxy.ReplaceJobScriptHash(JobScript.Hash); } // Initialize cron scheduler _cronSchedule = null; NextStartTime = null; if (!String.IsNullOrWhiteSpace(HandlerSettings.Schedule)) { try { _cronSchedule = CrontabSchedule.Parse(HandlerSettings.Schedule); NextStartTime = _cronSchedule.GetNextOccurrence(DateTime.Now); } catch (Exception ex) { _logger.Warn("Handler: Failed to parse Crontab: '{0}' - Ex: {1}", HandlerSettings.Schedule, ex.Message); } } // Initialize idle time _idleInfo = null; if (!String.IsNullOrWhiteSpace(HandlerSettings.IdleTime) && HandlerSettings.IdleTime.Contains("-")) { TimeSpan idleTimeStart; TimeSpan idleTimeEnd; var idleTimeParts = HandlerSettings.IdleTime.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries); var validFrom = TimeSpan.TryParse(idleTimeParts[0], out idleTimeStart); var validTo = TimeSpan.TryParse(idleTimeParts[1], out idleTimeEnd); if (validFrom && validTo) { _idleInfo = new IdleInformation { Start = idleTimeStart, End = idleTimeEnd }; } } return(true); }
protected override void InitializeEvents(HandlerSettings settings) { if (Configuration.StoreUmbracoFormsInDb) { dataSourceService.Saved += DataSource_Saved; dataSourceService.Deleted += DataSource_Deleted; } else { dataSourceStorage.Saved += DataSource_Saved; dataSourceStorage.Deleted += DataSource_Deleted; } }
protected override void InitializeEvents(HandlerSettings settings) { if (Configuration.StoreUmbracoFormsInDb) { formService.Saved += Form_Saved; formService.Deleted += Form_Deleted; } else { formStorage.Saved += Form_Saved; formStorage.Deleted += Form_Deleted; } }
public static Func<IDictionary<string, object>, Type> OutputTypeFromAcceptHeader(HandlerSettings options) { return env => { var context = new OwinContext(env); var type = options.ContentTypeMapper.FindBest(context); if (type == null) { throw new HttpStatusException("The requested media type is not acceptable.", HttpStatusCode.UnsupportedMediaType, new NotSupportedException()); } return type; }; }
public static Func<IDictionary<string, object>, Type> InputTypeFromPathSegment(HandlerSettings options, string queryPath) { return env => { var context = new OwinContext(env); var type = options.ContentTypeMapper.GetFromContentType(context.Request.Path.Value.Remove(0, queryPath.Length + 1)); if (type == null) { // because we are using the path here and the type is not registered, the resource "doesn't exist" throw new HttpStatusException("The requested resource was not found.", HttpStatusCode.NotFound, new NotSupportedException()); } if (!context.Request.ContentType.EndsWith("+json", StringComparison.OrdinalIgnoreCase)) { // Not a json entity throw new HttpStatusException("The specified media type is not supported.", HttpStatusCode.UnsupportedMediaType, new NotSupportedException()); } return type; }; }
private static async Task HandleCommand(IOwinContext context, Guid commandId, HandlerSettings options) { string contentType = context.Request.ContentType; Type commandType = options.ContentTypeMapper.GetFromContentType(contentType); if (!contentType.EndsWith("+json", StringComparison.OrdinalIgnoreCase) || commandType == null) { // Not a json entity, bad request throw new HttpStatusException("The specified media type is not supported.", HttpStatusCode.UnsupportedMediaType, new NotSupportedException()); } object command; using (var streamReader = new StreamReader(context.Request.Body)) { command = options.Serializer.Deserialize(streamReader, commandType); } var user = (context.Request.User as ClaimsPrincipal) ?? new ClaimsPrincipal(new ClaimsIdentity()); var dispatchCommand = DispatchCommandMethodInfo.MakeGenericMethod(command.GetType()); await (Task) dispatchCommand.Invoke(null, new[] { options.HandlerModules, commandId, user, command, context.Request.CallCancelled }); context.Response.StatusCode = 202; context.Response.ReasonPhrase = "Accepted"; }