public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromUri] Guid id, [FromUri] string phaseName, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.UPDATE, RightValue.APPROVED)); preventPagingHeaders(); string body = Request.Content.ReadAsStringAsync().Result; try { IFunctionalService service = getService(serviceName); if (SettingsManager.ProviderSettings.JobBinding && !service.IsBound(id, getOwnerId(sessionToken))) { throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer."); } return(OKResult(service.DeleteToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.GetContentType(Request), accept: HttpUtils.GetAccept(Request)))); } catch (ArgumentException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid argument: id=" + id + ", phaseName=" + phaseName + ".\n" + e.Message)); } catch (DeleteException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Problem deleting data for " + phaseName + "@" + id + ".\n", e)); } catch (RejectedException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Problem deleting data for " + phaseName + "@" + id + ".\n", e)); } catch (Exception e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Request failed for phase " + phaseName + " in job " + id + ".\n " + e.Message)); } }
public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromUri] Guid id, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.DELETE, RightValue.APPROVED)); try { IFunctionalService service = getService(serviceName); if (SettingsManager.ProviderSettings.JobBinding && !service.IsBound(id, getOwnerId(sessionToken))) { throw new InvalidSessionException("Request failed as one or more jobs referred to in this request do not belong to this consumer."); } service.Delete(id, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); if (SettingsManager.ProviderSettings.JobBinding) { service.Unbind(id); } } catch (Exception e) { string errorMessage = "The DELETE request failed for a " + serviceName + " job with an ID of " + id + " due to the following error:\n " + e.Message; throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage)); } HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.NoContent); return(result); }
public virtual ICollection <jobType> Get([FromUri] string serviceName, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.QUERY, RightValue.APPROVED)); ICollection <jobType> items = new List <jobType>(); try { IFunctionalService service = getService(serviceName); ICollection <jobType> jobs = service.Retrieve(zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); foreach (jobType job in jobs) { if (!SettingsManager.ProviderSettings.JobBinding || service.IsBound(Guid.Parse(job.id), getOwnerId(sessionToken))) { items.Add(job); } } } catch (Exception e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The GET request failed for functional services due to the following error:\n " + e.Message)); } if (items == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(items); }
public virtual HttpResponseMessage Get([FromUri] string serviceName, [FromUri] Guid id, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.QUERY, RightValue.APPROVED)); // Check that we support that provider // if not then throw new HttpResponseException(HttpStatusCode.NotFound); jobType item; try { IFunctionalService service = getService(serviceName); item = service.Retrieve(id, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); if (SettingsManager.ProviderSettings.JobBinding && !service.IsBound(Guid.Parse(item.id), getOwnerId(sessionToken))) { throw new InvalidSessionException("Request failed as one or more jobs referred to in this request do not belong to this consumer."); } } catch (Exception e) { string errorMessage = "The GET request failed for a " + serviceName + " job with an ID of " + id + " due to the following error:\n " + e.Message; throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage)); } if (item == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } HttpResponseMessage result = Request.CreateResponse <jobType>(HttpStatusCode.OK, item); return(result); }
public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromBody] deleteRequestType deleteRequest, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.DELETE, RightValue.APPROVED)); IFunctionalService service = getService(serviceName); ICollection <deleteStatus> statuses = new List <deleteStatus>(); foreach (deleteIdType deleteId in deleteRequest.deletes) { try { if (SettingsManager.ProviderSettings.JobBinding && !service.IsBound(Guid.Parse(deleteId.id), getOwnerId(sessionToken))) { throw new InvalidSessionException("Request failed as job does not belong to this consumer."); } service.Delete(Guid.Parse(deleteId.id), zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); if (SettingsManager.ProviderSettings.JobBinding) { service.Unbind(Guid.Parse(deleteId.id)); } statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.OK, deleteId.id)); } catch (ArgumentException e) { statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.BadRequest, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Invalid argument: id=" + deleteId.id + ".\n" + e.Message))); } catch (DeleteException e) { statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.BadRequest, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Request failed for object " + serviceName + " with ID of " + deleteId.id + ".\n " + e.Message))); } catch (NotFoundException e) { statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.NotFound, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Object " + serviceName + " with ID of " + deleteId.id + " not found.\n" + e.Message))); } catch (InvalidSessionException e) { statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.BadRequest, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Request failed for object " + serviceName + " with ID of " + deleteId.id + ", job doesn't belong to this consumer.\n " + e.Message))); } catch (Exception e) { statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.InternalServerError, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Request failed for object " + serviceName + " with ID of " + deleteId.id + ".\n " + e.Message))); } } return(Request.CreateResponse <deleteResponseType>(HttpStatusCode.OK, ProviderUtils.CreateDeleteResponse(statuses.ToArray()))); }
public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromUri] Guid id, [FromUri] string phaseName, [FromBody] stateType item, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.UPDATE, RightValue.APPROVED)); preventPagingHeaders(); HttpResponseMessage result; try { IFunctionalService service = getService(serviceName); if (SettingsManager.ProviderSettings.JobBinding && !service.IsBound(id, getOwnerId(sessionToken))) { throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer."); } stateType state = service.CreateToState(id, phaseName, item, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); string uri = Url.Link("ServiceStatesRoute", new { controller = serviceName, id = id, phaseName = phaseName, stateId = state.id }); result = Request.CreateResponse <stateType>(HttpStatusCode.Created, state); result.Headers.Location = new Uri(uri); } catch (ArgumentException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid argument: id=" + id + ", phaseName=" + phaseName + ".\n" + e.Message)); } catch (CreateException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Problem creating state data for " + phaseName + "@" + id + ".\n", e)); } catch (RejectedException e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Problem creating state data for " + phaseName + "@" + id + ".\n", e)); } catch (Exception e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Request failed for creating state for phase " + phaseName + " in job " + id + ".\n " + e.Message)); } return(result); }
public static async Task Initialize(DataProtectionKeysContext dataProtectionKeysContext, ApplicationDbContext applicationDBContext, IFunctionalService functionalService) { // Check, if db DataProtectionKeysContext is created // Check, if db ApplicationDbContext is created await dataProtectionKeysContext.Database.EnsureCreatedAsync(); await applicationDBContext.Database.EnsureCreatedAsync(); // Check, if db contains any users. If db is not empty, then db has been already seeded if (applicationDBContext.ApplicationUsers.Any()) { return; } // If empty create Admin User and App User await functionalService.CreateDefaultAdminUser(); await functionalService.CreateDefaultUser(); }
private void InitialiseProviders(ProviderSettings settings) { log.Debug("Initialising ProviderFactory (currently only supports Functional Services)"); // settings.Classes only returns functional services at the moment, but can easily be extended to other types of services. foreach (Type type in Classes) { log.Debug("Provider class to initialse: " + type.FullName); try { ServiceClassInfo providerClassInfo = new ServiceClassInfo(type, Type.EmptyTypes); if (!providerClassInfo.HasConstructor()) { log.Error("The provider class " + type.FullName + " does not have a valid constructor. Must have a public constructor that takes no arguments."); continue; } IFunctionalService provider = providerClassInfo.GetClassInstance() as IFunctionalService; string providerName = provider.GetServiceName(); log.Info("Adding provider for '" + providerName + "', using provider class '" + provider.GetType().FullName + "'."); // First add it to the standard request/response dictionary providerClasses[providerName] = providerClassInfo; // Add it to dictionary of providers providers[providerName] = provider; // Add it to dictionary of background threads providerThreads[providerName] = new Thread(new ThreadStart(provider.Startup)); // Each thread is essentially a global instance of a service whose responsibility is to maintain the services using timed tasks etc. - it never recieves any REST calls. } catch (Exception ex) { log.Error("Cannot create Provider Class " + type.FullName + ": " + ex.Message, ex); } } }
public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromUri] string jobName, [FromBody] jobType item, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.CREATE, RightValue.APPROVED)); HttpResponseMessage result; try { bool hasAdvisoryId = ProviderUtils.IsAdvisoryId(item.id); bool?_mustUseAdvisory = HttpUtils.GetMustUseAdvisory(Request.Headers); bool mustUseAdvisory = _mustUseAdvisory.HasValue && _mustUseAdvisory.Value; IFunctionalService service = getService(serviceName); if (!service.AcceptJob(serviceName, jobName)) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Service " + serviceName + " does not handle jobs named " + jobName)); } if (hasAdvisoryId && !mustUseAdvisory) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request failed for creating job for " + serviceName + " as object ID provided (" + item.id + "), but mustUseAdvisory is not specified or is false.\n" + !Guid.Empty.ToString().Equals(item.id))); } if (!hasAdvisoryId && mustUseAdvisory) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request requires use of advisory id, but none has been supplied.")); } Guid id = service.Create(item, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); if (SettingsManager.ProviderSettings.JobBinding) { service.Bind(id, getOwnerId(sessionToken)); } jobType job = service.Retrieve(id, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); string uri = Url.Link("ServicesRoute", new { controller = serviceName, id = id }); result = Request.CreateResponse <jobType>(HttpStatusCode.Created, job); result.Headers.Location = new Uri(uri); } catch (AlreadyExistsException e) { result = Request.CreateErrorResponse(HttpStatusCode.Conflict, e); } catch (ArgumentException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Object to create Job is invalid.\n ", e); } catch (CreateException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request failed for Job.\n ", e); } catch (RejectedException e) { result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "Create request rejected for Job" + (item.id == null ? "" : " with advisory id " + item.id) + ".\n", e); } catch (QueryException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request failed for Job.\n", e); } catch (Exception e) { result = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return(result); }
public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromBody] jobCollectionType items, [MatrixParameter] string[] zone = null, [MatrixParameter] string[] context = null) { string sessionToken = CheckAuthorisation(serviceName, zone, context, new Right(RightType.CREATE, RightValue.APPROVED)); HttpResponseMessage result; try { IFunctionalService service = getService(serviceName); List <createType> creates = new List <createType>(); foreach (jobType job in items.job) { try { if (!service.AcceptJob(serviceName, job.name)) { throw new ArgumentException("Service " + serviceName + " does not handle jobs named " + job.name); } Guid id = service.Create(job, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0])); if (SettingsManager.ProviderSettings.JobBinding) { service.Bind(id, getOwnerId(sessionToken)); } creates.Add(ProviderUtils.CreateCreate(HttpStatusCode.Created, id.ToString(), job.id)); } catch (CreateException e) { ProviderUtils.CreateCreate(HttpStatusCode.Conflict, job.id, error: ProviderUtils.CreateError(HttpStatusCode.Conflict, HttpStatusCode.Conflict.ToString(), e.Message)); } } createResponseType createResponse = ProviderUtils.CreateCreateResponse(creates.ToArray()); result = Request.CreateResponse <createResponseType>(HttpStatusCode.Created, createResponse); string uri = Url.Link("ServicesRoute", new { controller = serviceName }); result.Headers.Location = new Uri(uri); } catch (AlreadyExistsException e) { result = Request.CreateErrorResponse(HttpStatusCode.Conflict, e); } catch (ArgumentException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Object to create Jobs is invalid.\n ", e); } catch (CreateException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request failed for creating Jobs.\n ", e); } catch (RejectedException e) { result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "Create request rejected for creating Jobs.\n", e); } catch (QueryException e) { result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request failed for Job.\n", e); } catch (Exception e) { result = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } return(result); }
public static async Task Initialize(ApplicationDbContext applicationDbContext, IFunctionalService functionalSvc) { // Check, if db ApplicationDbContext is created await applicationDbContext.Database.EnsureCreatedAsync(); // Check, if db contains any users. If db is not empty, then db has been already seeded if (applicationDbContext.ApplicationUsers.Any()) { return; } // If empty create Admin User await functionalSvc.CreateDefaultUser("Administrator"); }
public virtual HttpResponseMessage Put( [FromUri] string serviceName, [FromUri] Guid id, [FromUri] string phaseName, [MatrixParameter] string[] zoneId = null, [MatrixParameter] string[] contextId = null) { string sessionToken = CheckAuthorisation(serviceName, zoneId, contextId, new Right(RightType.UPDATE, RightValue.APPROVED)); string body = Request.Content.ReadAsStringAsync().Result; try { IFunctionalService service = GetService(serviceName); if (ProviderSettings.JobBinding && !service.IsBound(id, GetOwnerId(sessionToken))) { throw new InvalidSessionException( "Request failed as the job referred to in this request does not belong to this consumer."); } return(OkResult( service.UpdateToPhase( id, phaseName, body, zoneId?[0], contextId?[0], HttpUtils.GetContentType(Request), HttpUtils.GetAccept(Request)))); } catch (ArgumentException e) { throw new HttpResponseException( Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Invalid argument for Phase " + phaseName + " in job " + id + ".\n" + e.Message)); } catch (UpdateException e) { throw new HttpResponseException( Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Problem updating data for " + phaseName + "@" + id + ".\n", e)); } catch (RejectedException e) { throw new HttpResponseException( Request.CreateErrorResponse( HttpStatusCode.Unauthorized, "Problem updating data for " + phaseName + "@" + id + ".\n", e)); } catch (Exception e) { throw new HttpResponseException( Request.CreateErrorResponse( HttpStatusCode.InternalServerError, "Request failed for phase " + phaseName + " in job " + id + ".\n " + e.Message, e)); } }
public static async Task Initialize(ApplicationDbContext context, QuartzDbContext quartzDbContext, IFunctionalService functional, ILogger logger) { await context.Database.EnsureCreatedAsync(); await quartzDbContext.Database.EnsureCreatedAsync(); // Here we will check, if db contains any users. If db is not empty, then db has been seeded if (context.ApplicationUsers.Any()) { return; } // We want to now create a Super Admin user if Users are not yet created. So they can be managed await functional.CreateDefaultAdminUser(logger); // We want to now create a App user if Users are not yet created. So they can be managed await functional.CreateDefaultAppUser(logger); // Populate Plants table await functional.CreateDefaultPlants(logger); }