Beispiel #1
0
 /// <summary>
 /// Add a contained resource
 /// </summary>
 public void AddContainedResource(DomainResourceBase resource)
 {
     resource.MakeIdRef();
     this.Contained.Add(new ContainedResource()
     {
         Item = resource
     });
 }
Beispiel #2
0
        /// <summary>
        /// Create resource refernece for local
        /// </summary>
        public static Reference CreateLocalResourceReference(DomainResourceBase instance)
        {
            IdRef idRef = instance.MakeIdRef();

            return(new Reference()
            {
                ReferenceUrl = idRef.Value,
                Display = instance.ToString()
            });
        }
Beispiel #3
0
 /// <summary>
 /// Create resource refernece (friendly method)
 /// </summary>
 public static Reference CreateResourceReference(DomainResourceBase instance, Uri baseUri)
 {
     return(new Reference()
     {
         Display = instance.ToString(),
         //Type = new PrimitiveCode<string>(instance.GetType().GetCustomAttribute<XmlRootAttribute>() != null ? instance.GetType().GetCustomAttribute<XmlRootAttribute>().ElementName : instance.GetType().Name),
         ReferenceUrl = String.IsNullOrEmpty(instance.VersionId) ?
                        baseUri.ToString() + String.Format("/{0}/{1}", instance.GetType().Name, instance.Id) :
                        baseUri.ToString() + String.Format("/{0}/{1}/_history/{2}", instance.GetType().Name, instance.Id, instance.VersionId)
     });
 }
Beispiel #4
0
        /// <summary>
        /// Fetch a resource from the specified uri with the specified credentials
        /// </summary>
        public DomainResourceBase FetchResource(Uri baseUri, ICredentials credentials, DomainResourceBase context, Type resourceType)
        {
            // Request uri
            Uri requestUri = null;

            // Contained?
            if (this.ReferenceUrl.Value.StartsWith("#") && context.Contained != null)
            {
                var res = context.Contained.Find(o => o.Item.Id == this.ReferenceUrl.Value.ToString().Substring(1));
                if (res != null)
                {
                    return(res.Item);
                }
            }
            else if (Uri.TryCreate(this.ReferenceUrl.Value, UriKind.RelativeOrAbsolute, out requestUri))
            {
                requestUri = new Uri(baseUri, this.ReferenceUrl.Value.ToString());
            }

            // Make request to URI
            Trace.TraceInformation("Fetching from {0}...", requestUri);
            var webReq = HttpWebRequest.Create(requestUri);

            webReq.Method      = "GET";
            webReq.Credentials = credentials;

            // Fetch
            try
            {
                using (var response = webReq.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.Accepted)
                    {
                        throw new WebException(String.Format("Server responded with {0}", response.StatusCode));
                    }

                    XmlSerializer xsz = XmlModelSerializerFactory.Current.CreateSerializer(resourceType);
                    return(xsz.Deserialize(response.GetResponseStream()) as DomainResourceBase);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());
                throw;
            }
        }