Exemple #1
0
        internal static void Translate(DataServiceContext context, bool addTrailingParens, Expression e, out Uri uri, out Version version)
        {
            var writer = new UriWriter(context);

            writer.leafResourceSet = addTrailingParens ? (e as ResourceSetExpression) : null;
            writer.Visit(e);
            uri     = Util.CreateUri(context.BaseUriWithSlash, Util.CreateUri(writer.uriBuilder.ToString(), UriKind.Relative));
            version = writer.uriVersion;
        }
Exemple #2
0
 protected Uri CreateRequestRelativeUri(LinkDescriptor binding)
 {
     if (binding.IsSourcePropertyCollection && (EntityStates.Added != binding.State))
     {
         EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target);
         Uri uri = DataServiceContext.GenerateEditLinkRelativeUri(binding.SourceProperty, entityDescriptor.Entity, this.RequestInfo.MaxProtocolVersion);
         return(Util.CreateUri("$links" + '/' + CommonUtil.UriToString(uri), UriKind.Relative));
     }
     return(Util.CreateUri("$links" + '/' + binding.SourceProperty, UriKind.Relative));
 }
Exemple #3
0
        internal static Uri ValidateLocationHeader(string location)
        {
            Uri uri = Util.CreateUri(location, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_LocationHeaderExpectsAbsoluteUri);
            }
            return(uri);
        }
Exemple #4
0
        internal static void Translate(DataServiceContext context, bool addTrailingParens, Expression e, out Uri uri, out Version version)
        {
            UriWriter writer = new UriWriter(context)
            {
                leafResourceSet = addTrailingParens ? (e as ResourceSetExpression) : null
            };

            writer.Visit(e);
            uri     = Util.CreateUri(writer.uriBuilder.ToString(), UriKind.Absolute);
            version = writer.uriVersion;
        }
Exemple #5
0
        private Uri ConvertHRefAttributeValueIntoURI(string href)
        {
            Uri uri = Util.CreateUri(href, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri && !String.IsNullOrEmpty(this.reader.BaseURI))
            {
                Uri baseUri = Util.CreateUri(this.reader.BaseURI, UriKind.RelativeOrAbsolute);

                uri = new Uri(baseUri, uri);
            }

            return(uri);
        }
Exemple #6
0
        internal Uri GetEntitySetUri(string entitySetName)
        {
            Uri entitySetUriFromResolver = this.GetEntitySetUriFromResolver(entitySetName);

            if (entitySetUriFromResolver != null)
            {
                return(Util.ForceNonSlashTerminatedUri(entitySetUriFromResolver));
            }
            if (this.baseUriWithSlash == null)
            {
                throw Error.InvalidOperation(Strings.Context_ResolveEntitySetOrBaseUriRequired(entitySetName));
            }
            return(Util.CreateUri(this.baseUriWithSlash, new Uri(entitySetName, UriKind.Relative)));
        }
Exemple #7
0
        protected Uri CreateRequestUri(EntityDescriptor sourceResource, LinkDescriptor binding)
        {
            Uri      uri;
            LinkInfo linkInfo = null;

            if (sourceResource.TryGetLinkInfo(binding.SourceProperty, out linkInfo) && ((uri = linkInfo.AssociationLink) != null))
            {
                if (binding.IsSourcePropertyCollection && (EntityStates.Deleted == binding.State))
                {
                    EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target);
                    uri = DataServiceContext.AppendKeysToUri(uri.AbsoluteUri, entityDescriptor.Entity, UriKind.Absolute, this.RequestInfo.MaxProtocolVersion);
                }
                return(uri);
            }
            return(Util.CreateUri(sourceResource.GetResourceUri(this.RequestInfo.BaseUriResolver, false), this.CreateRequestRelativeUri(binding)));
        }
Exemple #8
0
        internal Uri GetNavigationLink(UriResolver baseUriResolver, ClientPropertyAnnotation property)
        {
            LinkInfo linkInfo       = null;
            Uri      navigationLink = null;

            if (this.TryGetLinkInfo(property.PropertyName, out linkInfo))
            {
                navigationLink = linkInfo.NavigationLink;
            }
            if (navigationLink == null)
            {
                Uri requestUri = Util.CreateUri(property.PropertyName + (property.IsEntityCollection ? "()" : string.Empty), UriKind.Relative);
                navigationLink = Util.CreateUri(this.GetResourceUri(baseUriResolver, true), requestUri);
            }
            return(navigationLink);
        }
Exemple #9
0
        /// <summary>
        /// creates a new uri instance which takes into account the base uri of the reader.
        /// </summary>
        /// <param name="href">href attribute value.</param>
        /// <returns>a new instance of uri as refered by the <paramref name="href"/></returns>
        private Uri ConvertHRefAttributeValueIntoURI(string href)
        {
            Uri uri = Util.CreateUri(href, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri && !String.IsNullOrEmpty(this.reader.BaseURI))
            {
                Uri baseUri = Util.CreateUri(this.reader.BaseURI, UriKind.RelativeOrAbsolute);

                // The reason why we can't use Util.CreateUri function here, is that the util method
                // checks for trailing slashes in the baseuri and starting forward slashes in the request uri
                // and does some tricks which is not consistent with the uri class behaviour. Hence using the
                // uri class directly here.
                uri = new Uri(baseUri, uri);
            }

            return(uri);
        }
Exemple #10
0
        internal Uri GetResourceUri(UriResolver baseUriResolver, bool queryLink)
        {
            LinkInfo info;

            if (this.parentDescriptor == null)
            {
                return(this.GetLink(queryLink));
            }
            if (this.parentDescriptor.Identity == null)
            {
                return(Util.CreateUri(Util.CreateUri(baseUriResolver.GetBaseUriWithSlash(), new Uri("$" + this.parentDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative)), Util.CreateUri(this.parentProperty, UriKind.Relative)));
            }
            if (this.parentDescriptor.TryGetLinkInfo(this.parentProperty, out info) && (info.NavigationLink != null))
            {
                return(info.NavigationLink);
            }
            return(Util.CreateUri(this.parentDescriptor.GetLink(queryLink), this.GetLink(queryLink)));
        }
Exemple #11
0
        protected ODataRequestMessageWrapper CreateRequest(LinkDescriptor binding)
        {
            if (binding.ContentGeneratedForSave)
            {
                return(null);
            }
            EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Source);
            EntityDescriptor descriptor2      = (binding.Target != null) ? this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target) : null;
            Uri requestUri = null;

            if (entityDescriptor.GetLatestIdentity() == null)
            {
                if (!this.IsBatch)
                {
                    binding.ContentGeneratedForSave = true;
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_LinkResourceInsertFailure, entityDescriptor.SaveError);
                }
                Uri uri = this.CreateRequestRelativeUri(binding);
                requestUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture) + "/" + CommonUtil.UriToString(uri), UriKind.Relative);
            }
            else
            {
                if ((!this.IsBatch && (descriptor2 != null)) && (descriptor2.GetLatestIdentity() == null))
                {
                    binding.ContentGeneratedForSave = true;
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_LinkResourceInsertFailure, descriptor2.SaveError);
                }
                requestUri = this.CreateRequestUri(entityDescriptor, binding);
            }
            ODataRequestMessageWrapper requestMessage = this.CreateRequestMessage(requestUri, GetLinkHttpMethod(binding));

            if (this.IsBatch)
            {
                requestMessage.SetHeader("Content-ID", binding.ChangeOrder.ToString(CultureInfo.InvariantCulture));
            }
            if ((EntityStates.Added == binding.State) || ((EntityStates.Modified == binding.State) && (binding.Target != null)))
            {
                requestMessage.SetHeader("Content-Type", "application/xml");
            }
            WebUtil.SetOperationVersionHeaders(requestMessage, Util.DataServiceVersion1, this.RequestInfo.MaxProtocolVersionAsVersion);
            return(requestMessage);
        }
 internal Uri GetResourceUri(Uri baseUriWithSlash, bool queryLink)
 {
     if (this.parentDescriptor != null)
     {
         if (this.parentDescriptor.Identity == null)
         {
             return(Util.CreateUri(
                        Util.CreateUri(baseUriWithSlash, new Uri("$" + this.parentDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative)),
                        Util.CreateUri(this.parentProperty, UriKind.Relative)));
         }
         else
         {
             return(Util.CreateUri(Util.CreateUri(baseUriWithSlash, this.parentDescriptor.GetLink(queryLink)), this.GetLink(queryLink)));
         }
     }
     else
     {
         return(Util.CreateUri(baseUriWithSlash, this.GetLink(queryLink)));
     }
 }
Exemple #13
0
 internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage)
 {
     using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
     {
         Uri resourceUri;
         EntityDescriptor entityDescriptor = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target);
         if (entityDescriptor.GetLatestIdentity() != null)
         {
             resourceUri = entityDescriptor.GetResourceUri(this.requestInfo.BaseUriResolver, false);
         }
         else
         {
             resourceUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative);
         }
         ODataEntityReferenceLink link = new ODataEntityReferenceLink {
             Url = resourceUri
         };
         writer.WriteEntityReferenceLink(link);
     }
 }
Exemple #14
0
        private Uri GetLink(bool queryLink)
        {
            if (queryLink && (this.SelfLink != null))
            {
                return(this.SelfLink);
            }
            Uri latestEditLink = this.GetLatestEditLink();

            if (latestEditLink != null)
            {
                return(latestEditLink);
            }
            if (base.State != EntityStates.Added)
            {
                throw new ArgumentNullException(System.Data.Services.Client.Strings.EntityDescriptor_MissingSelfEditLink(this.identity));
            }
            if (this.addToUri != null)
            {
                return(this.addToUri);
            }
            return(Util.CreateUri(this.parentProperty, UriKind.Relative));
        }
        private Uri GetLink(bool queryLink)
        {
            if (queryLink && this.SelfLink != null)
            {
                return(this.SelfLink);
            }

            if (this.EditLink != null)
            {
                return(this.EditLink);
            }

            Debug.Assert(this.State == EntityStates.Added, "the entity must be in added state");
            if (!String.IsNullOrEmpty(this.entitySetName))
            {
                return(Util.CreateUri(this.entitySetName, UriKind.Relative));
            }
            else
            {
                return(Util.CreateUri(this.parentProperty, UriKind.Relative));
            }
        }
 /// <summary>uri to edit the entity</summary>
 /// <param name="baseUriWithSlash">baseUriWithSlash</param>
 /// <param name="queryLink">whether to return the query link or edit link</param>
 /// <returns>absolute uri which can be used to edit the entity</returns>
 internal Uri GetResourceUri(Uri baseUriWithSlash, bool queryLink)
 {
     // If the entity was inserted using the AddRelatedObject API
     if (this.parentDescriptor != null)
     {
         // This is the batch scenario, where the entity might not have been saved yet, and there is another operation
         // (for e.g. PUT $1/links/BestFriend or something). Hence we need to generate a Uri with the changeorder number.
         if (this.parentDescriptor.Identity == null)
         {
             return(Util.CreateUri(
                        Util.CreateUri(baseUriWithSlash, new Uri("$" + this.parentDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative)),
                        Util.CreateUri(this.parentProperty, UriKind.Relative)));
         }
         else
         {
             return(Util.CreateUri(Util.CreateUri(baseUriWithSlash, this.parentDescriptor.GetLink(queryLink)), this.GetLink(queryLink)));
         }
     }
     else
     {
         return(Util.CreateUri(baseUriWithSlash, this.GetLink(queryLink)));
     }
 }
Exemple #17
0
        private ODataRequestMessageWrapper CreateBatchRequest()
        {
            Uri requestUrl = Util.CreateUri(base.RequestInfo.BaseUriResolver.GetBaseUriWithSlash(), Util.CreateUri("$batch", UriKind.Relative));
            ODataRequestMessageWrapper requestMessage = new ODataRequestMessageWrapper("POST", requestUrl, base.RequestInfo);

            WebUtil.SetOperationVersionHeaders(requestMessage, Util.DataServiceVersion1, base.RequestInfo.MaxProtocolVersionAsVersion);
            return(requestMessage);
        }
Exemple #18
0
        private void ParseCurrentEntry(out AtomEntry targetEntry)
        {
            Debug.Assert(this.reader.NodeType == XmlNodeType.Element, "this.reader.NodeType == XmlNodeType.Element");

            var callbackResult = this.entryCallback(this.reader);

            Debug.Assert(callbackResult.Key != null, "callbackResult.Key != null");
            this.readers.Push(this.reader);
            this.reader = callbackResult.Key;

            this.reader.Read();
            Debug.Assert(this.reader.LocalName == "entry", "this.reader.LocalName == 'entry' - otherwise we're not reading the subtree");

            bool hasContent = false;

            targetEntry            = new AtomEntry();
            targetEntry.DataValues = new List <AtomContentProperty>();
            targetEntry.Tag        = callbackResult.Value;
            targetEntry.ETagText   = this.reader.GetAttribute(XmlConstants.AtomETagAttributeName, XmlConstants.DataWebMetadataNamespace);

            while (this.reader.Read())
            {
                if (ShouldIgnoreNode(this.reader))
                {
                    continue;
                }

                if (this.reader.NodeType == XmlNodeType.Element)
                {
                    int    depth        = this.reader.Depth;
                    string elementName  = this.reader.LocalName;
                    string namespaceURI = this.reader.NamespaceURI;
                    if (namespaceURI == XmlConstants.AtomNamespace)
                    {
                        if (elementName == XmlConstants.AtomCategoryElementName && targetEntry.TypeName == null)
                        {
                            string text = this.reader.GetAttributeEx(XmlConstants.AtomCategorySchemeAttributeName, XmlConstants.AtomNamespace);
                            if (text == this.typeScheme)
                            {
                                targetEntry.TypeName = this.reader.GetAttributeEx(XmlConstants.AtomCategoryTermAttributeName, XmlConstants.AtomNamespace);
                            }
                        }
                        else if (elementName == XmlConstants.AtomContentElementName)
                        {
                            hasContent = true;
                            this.ParseCurrentContent(targetEntry);
                        }
                        else if (elementName == XmlConstants.AtomIdElementName && targetEntry.Identity == null)
                        {
                            string idText = ReadElementStringForText(this.reader);
                            idText = Util.ReferenceIdentity(idText);

                            Uri idUri = Util.CreateUri(idText, UriKind.RelativeOrAbsolute);
                            if (!idUri.IsAbsoluteUri)
                            {
                                throw Error.InvalidOperation(Strings.Context_TrackingExpectsAbsoluteUri);
                            }

                            targetEntry.Identity = idText;
                        }
                        else if (elementName == XmlConstants.AtomLinkElementName)
                        {
                            this.ParseCurrentLink(targetEntry);
                        }
                    }
                    else if (namespaceURI == XmlConstants.DataWebMetadataNamespace)
                    {
                        if (elementName == XmlConstants.AtomPropertiesElementName)
                        {
                            if (targetEntry.MediaLinkEntry.HasValue && !targetEntry.MediaLinkEntry.Value)
                            {
                                throw Error.InvalidOperation(Strings.Deserialize_ContentPlusPropertiesNotAllowed);
                            }

                            targetEntry.MediaLinkEntry = true;

                            if (!this.reader.IsEmptyElement)
                            {
                                this.ReadCurrentProperties(targetEntry.DataValues);
                            }
                        }
                    }

                    SkipToEndAtDepth(this.reader, depth);
                }
            }

            if (targetEntry.Identity == null)
            {
                throw Error.InvalidOperation(Strings.Deserialize_MissingIdElement);
            }

            if (!hasContent)
            {
                throw Error.BatchStreamContentExpected(BatchStreamState.GetResponse);
            }

            this.reader = this.readers.Pop();
        }
 internal Uri GetEditMediaResourceUri(Uri serviceBaseUri)
 {
     return(this.EditStreamUri == null ? null : Util.CreateUri(serviceBaseUri, this.EditStreamUri));
 }
Exemple #20
0
        internal static object ChangeType(string propertyValue, Type propertyType)
        {
            Debug.Assert(null != propertyValue, "should never be passed null");
            try
            {
                switch ((StorageType)IndexOfStorage(propertyType))
                {
                case StorageType.Boolean:
                    return(XmlConvert.ToBoolean(propertyValue));

                case StorageType.Byte:
                    return(XmlConvert.ToByte(propertyValue));

                case StorageType.ByteArray:
                    return(Convert.FromBase64String(propertyValue));

                case StorageType.Char:
                    return(XmlConvert.ToChar(propertyValue));

                case StorageType.CharArray:
                    return(propertyValue.ToCharArray());

                case StorageType.DateTime:
                    return(XmlConvert.ToDateTime(propertyValue, XmlDateTimeSerializationMode.RoundtripKind));

                case StorageType.DateTimeOffset:
                    return(XmlConvert.ToDateTimeOffset(propertyValue));

                case StorageType.Decimal:
                    return(XmlConvert.ToDecimal(propertyValue));

                case StorageType.Double:
                    return(XmlConvert.ToDouble(propertyValue));

                case StorageType.Guid:
                    return(new Guid(propertyValue));

                case StorageType.Int16:
                    return(XmlConvert.ToInt16(propertyValue));

                case StorageType.Int32:
                    return(XmlConvert.ToInt32(propertyValue));

                case StorageType.Int64:
                    return(XmlConvert.ToInt64(propertyValue));

                case StorageType.Single:
                    return(XmlConvert.ToSingle(propertyValue));

                case StorageType.String:
                    return(propertyValue);

                case StorageType.SByte:
                    return(XmlConvert.ToSByte(propertyValue));

                case StorageType.TimeSpan:
                    return(XmlConvert.ToTimeSpan(propertyValue));

                case StorageType.Type:
                    return(Type.GetType(propertyValue, true));

                case StorageType.UInt16:
                    return(XmlConvert.ToUInt16(propertyValue));

                case StorageType.UInt32:
                    return(XmlConvert.ToUInt32(propertyValue));

                case StorageType.UInt64:
                    return(XmlConvert.ToUInt64(propertyValue));

                case StorageType.Uri:
                    return(Util.CreateUri(propertyValue, UriKind.RelativeOrAbsolute));

                case StorageType.XDocument:
                    return(0 < propertyValue.Length ? System.Xml.Linq.XDocument.Parse(propertyValue) : new System.Xml.Linq.XDocument());

                case StorageType.XElement:
                    return(System.Xml.Linq.XElement.Parse(propertyValue));

#if !ASTORIA_LIGHT
                case StorageType.Binary:
                    Debug.Assert(null != knownTypes[(int)StorageType.Binary], "null typeof(System.Data.Linq.Binary)");
                    return(Activator.CreateInstance(knownTypes[(int)StorageType.Binary], Convert.FromBase64String(propertyValue)));
#endif
                default:
                    Debug.Assert(false, "new StorageType without update to knownTypes");
                    return(propertyValue);
                }
            }
            catch (FormatException ex)
            {
                propertyValue = (0 == propertyValue.Length ? "String.Empty" : "String");
                throw Error.InvalidOperation(Strings.Deserialize_Current(propertyType.ToString(), propertyValue), ex);
            }
            catch (OverflowException ex)
            {
                propertyValue = (0 == propertyValue.Length ? "String.Empty" : "String");
                throw Error.InvalidOperation(Strings.Deserialize_Current(propertyType.ToString(), propertyValue), ex);
            }
        }
Exemple #21
0
 internal override object Parse(string text)
 {
     return(Util.CreateUri(text, UriKind.RelativeOrAbsolute));
 }