Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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);
        }