Exemple #1
0
        /// <summary>
        /// Hydrates an object using the string response from the specified URL resource.
        /// </summary>
        /// <typeparam name="T">The type of object to hydrate.</typeparam>
        /// <param name="url">The web service URL.</param>
        /// <returns>
        /// The hydrated object.
        /// </returns>
        /// <exception cref="T:System.Web.HttpException">If the web service call to the resource fails,
        /// the exception will contain the status code and reason.</exception>
        /// <exception cref="T:System.ArgumentNullException">url</exception>
        public virtual T Hydrate <T>(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                string json = string.Empty;
                Uri    uri  = null;
                try
                {
                    uri  = new Uri(ForceJsonFormat(url));
                    json = this.WebHelper.DownloadString(uri);
                    if (url.ToLower().Contains("arcgis/rest/services"))
                    {
                        ESRIException error = JsonConvert.DeserializeObject <ESRIException>(json);
                        if (error.Error != null)
                        {
                            throw new HttpException((int)error.Error.Code,
                                                    string.Format("The underlying ArcGIS service located at " +
                                                                  "{0} returned the following error: {1}: {2}",
                                                                  url, error.Error.Message, string.Join("; ", error.Error.Details)));
                        }
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Response is HttpWebResponse)
                    {
                        switch ((ex.Response as HttpWebResponse).StatusCode)
                        {
                        case HttpStatusCode.NotFound:
                            throw new HttpException((int)HttpStatusCode.NotFound,
                                                    string.Format("The underlying web service located at {0} could not be found.",
                                                                  uri.GetLeftPart(UriPartial.Path)));

                        case HttpStatusCode.BadGateway:
                            throw new HttpException((int)HttpStatusCode.BadGateway,
                                                    string.Format("The underlying web service domain could not be resolved: {0}.",
                                                                  uri.Host));
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
                if (string.IsNullOrEmpty(json))
                {
                    throw new HttpException((int)HttpStatusCode.NoContent,
                                            string.Format("The underlying web service response from {0} was null.", url));
                }
                return(JsonConvert.DeserializeObject <T>(json, new StringEnumConverter()));
            }
            else
            {
                throw new ArgumentNullException("url", "Url parameter is required!");
            }
        }
Exemple #2
0
        private void UpdateJobs(string result, string jobId)
        {
            if (!this.jobs.ContainsKey(jobId))
            {
                return;
            }
            try
            {
                JobParams jobParams = this.jobs[jobId];
                if (jobParams.cancelled)
                {
                    this.jobs.Remove(jobId);
                    jobParams.timer.Stop();
                    jobParams.timer = null;
                }
                else
                {
                    ESRIException error = RestHelper.HydrateFromJson <ESRIException>(result);
                    if (error != null && error.Error != null)
                    {
                        this.jobs.Remove(jobId);
                        jobParams.timer.Stop();
                        jobParams.timer.Dispose();
                        jobParams.timer = null;
                        OnFailed(new TaskFailedEventArgs(new HttpException((int)error.Error.Code,
                                                                           error.Error.Message)));
                        return;
                    }
                    JobInfo jobInfo            = RestHelper.HydrateFromJson <JobInfo>(result);
                    JobCompletedEventArgs args = new JobCompletedEventArgs(jobInfo);
                    switch (jobInfo.JobStatus)
                    {
                    case esriJobStatus.esriJobSucceeded:
                    case esriJobStatus.esriJobFailed:
                    case esriJobStatus.esriJobTimedOut:
                    case esriJobStatus.esriJobCancelled:
                    case esriJobStatus.esriJobDeleted:
                        this.jobs.Remove(jobId);
                        jobParams.timer.Stop();
                        jobParams.timer.Dispose();
                        jobParams.timer = null;
                        this.OnJobCompleted(args);
                        break;

                    default:
                        jobParams.timer.Start();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                OnFailed(new TaskFailedEventArgs(ex));
            }
        }
Exemple #3
0
        /// <summary>
        /// Hydrates an object using the string response from the specified URL resource.
        /// </summary>
        /// <typeparam name="T">The type of object to hydrate.</typeparam>
        /// <param name="url">The web service URL.</param>
        /// <param name="callback">A callback function that receives the result.</param>
        /// <param name="userState">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param>
        /// <exception cref="System.InvalidOperationException">If the web service call to the resource fails.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">url</exception>
        public void HydrateAsync <T>(string url, Action <T, object> callback, object userState)
        {
            if (!string.IsNullOrEmpty(url))
            {
                string json = string.Empty;
                Uri    uri  = null;
                try
                {
                    uri = new Uri(ForceJsonFormat(url), UriKind.RelativeOrAbsolute);
                    DownloadStringAsync(uri, delegate(string cb, object token)
                    {
                        json = cb;
                        if (string.IsNullOrEmpty(json))
                        {
                            callback(default(T), token);
                            return;
                            //throw new InvalidProgramException(
                            //    string.Format("The underlying web service response from {0} was null.", url));
                        }
                        else if (url.ToLower().Contains("arcgis/rest/services"))
                        {
                            ESRIException error = JsonConvert.DeserializeObject <ESRIException>(json);
                            if (error.Error != null)
                            {
                                throw new InvalidOperationException(
                                    string.Format("The underlying ArcGIS service located at " +
                                                  "{0} returned the following error: {1}: {2}",
                                                  url, error.Error.Message, string.Join("; ", error.Error.Details)));
                            }
                        }
                        callback((T)JsonConvert.DeserializeObject <T>(json, new StringEnumConverter()), token);
                    }, userState);
                }
                catch (WebException ex)
                {
                    switch ((ex.Response as HttpWebResponse).StatusCode)
                    {
                    case HttpStatusCode.NotFound:
                        throw new InvalidOperationException(
                                  string.Format("The underlying web service located at {0} could not be found.",
                                                url));

                    case HttpStatusCode.BadGateway:
                        throw new InvalidOperationException(
                                  string.Format("The underlying web service domain could not be resolved: {0}.",
                                                uri.Host));
                    }
                }
            }
            else
            {
                throw new ArgumentNullException("url", "Url parameter is required!");
            }
        }
Exemple #4
0
        private void ValidateStringResult <TModel>(object sender, WrappedAsyncCompletedEventArgs e, string result)
        {
            // check for error
            if (e.Error != null)
            {
                OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel), e.Error,
                                                                                   e.Cancelled, e.UserState, e.OriginalUrl, result));
            }

            // check response type
            var headers = ((IWebHelper)sender).ResponseHeaders != null ?
                          ((IWebHelper)sender).ResponseHeaders.GetValues("Content-Type") : null;

            if (headers != null && headers.Any(x => x.Contains("text/html")))
            {
                OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel),
                                                                                   new HttpException((int)HttpStatusCode.UnsupportedMediaType, string.Format(
                                                                                                         "The underlying web service located at {0} returned HTML and only JSON or XML is supported.",
                                                                                                         e.OriginalUrl)), e.Cancelled, e.UserState, e.OriginalUrl, result));
            }

            // check for empty response
            string json = result;

            if (string.IsNullOrEmpty(json))
            {
                OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel),
                                                                                   new HttpException((int)HttpStatusCode.NoContent, string.Format(
                                                                                                         "The underlying web service response from {0} was null.",
                                                                                                         e.OriginalUrl)), e.Cancelled, e.UserState, e.OriginalUrl, result));
            }
            else if (e.OriginalUrl != null && e.OriginalUrl.AbsoluteUri.ToString().ToLower().Contains("arcgis/rest/services"))
            {
                ESRIException error = JsonConvert.DeserializeObject <ESRIException>(json);
                if (error.Error != null)
                {
                    OnHydrateCompleted <TModel>(new HydrateCompletedEventArgs <TModel>(default(TModel),
                                                                                       new HttpException((int)error.Error.Code, string.Format(
                                                                                                             "The underlying ArcGIS service located at {0} returned the following error: {1}: {2}",
                                                                                                             e.OriginalUrl, error.Error.Message, string.Join("; ", error.Error.Details))),
                                                                                       e.Cancelled, e.UserState, e.OriginalUrl, result));
                }
                else
                {
                    Success <TModel>(json, e);
                }
            }
            else
            {
                Success <TModel>(json, e);
            }
        }