/// <summary> /// Writes an entity reference link. /// </summary> /// <param name="binding">The link descriptor.</param> /// <param name="requestMessage">The request message used for writing the payload.</param> internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage) #endif { using (ODataMessageWriter messageWriter = Serializer.CreateMessageWriter(requestMessage, this.requestInfo, false /*isParameterPayload*/)) { EntityDescriptor targetResource = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target); Uri targetResourceEditLink; if (null != targetResource.GetLatestIdentity()) { // When we write the uri in the payload, we need to make sure that we write the edit // link in the payload, since the request uri is the edit link of the parent entity. // Think of a read/write service - since the uri is the target link to the parent entity // its better that we write the edit link of the child entity in the payload. targetResourceEditLink = targetResource.GetResourceUri(this.requestInfo.BaseUriResolver, false /*queryLink*/); } else { #if DEBUG Debug.Assert(isBatch, "we should be cross-referencing entities only in batch scenarios"); #endif targetResourceEditLink = UriUtil.CreateUri("$" + targetResource.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative); } ODataEntityReferenceLink referenceLink = new ODataEntityReferenceLink(); referenceLink.Url = targetResourceEditLink; messageWriter.WriteEntityReferenceLink(referenceLink); } }
/// <summary>uri to edit the entity</summary> /// <param name="baseUriResolver">retrieves the baseUri to use for a given entity set.</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(UriResolver baseUriResolver, bool queryLink) { // If the entity was inserted using the AddRelatedObject API if (this.ParentForInsert != 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.ParentForInsert.Identity == null) { Uri relativeReferenceUri = UriUtil.CreateUri("$" + this.ParentForInsert.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative); Uri absoluteReferenceUri = baseUriResolver.GetOrCreateAbsoluteUri(relativeReferenceUri); Uri requestUri = UriUtil.CreateUri(this.ParentPropertyForInsert, UriKind.Relative); return(UriUtil.CreateUri(absoluteReferenceUri, requestUri)); } else { Debug.Assert(this.ParentForInsert.ParentForInsert == null, "This code assumes that parentChild relationships will only ever be one level deep"); Debug.Assert(this.ParentPropertyForInsert != null, "parentProperty != null"); LinkInfo linkInfo; if (this.ParentForInsert.TryGetLinkInfo(this.ParentPropertyForInsert, out linkInfo)) { if (linkInfo.NavigationLink != null) { return(linkInfo.NavigationLink); } } return(UriUtil.CreateUri(this.ParentForInsert.GetLink(queryLink), this.GetLink(queryLink))); } } else { return(this.GetLink(queryLink)); } }
/// <summary> /// Create message reader settings for consuming responses. /// </summary> /// <param name="entryXmlCustomizer">Optional XML entry customization callback to be used.</param> /// <returns>Newly created message reader settings.</returns> internal ODataMessageReaderSettings CreateSettings(Func <ODataEntry, XmlReader, Uri, XmlReader> entryXmlCustomizer) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); if (!this.responseInfo.ResponsePipeline.HasAtomReadingEntityHandlers) { entryXmlCustomizer = null; } Func <IEdmType, string, IEdmType> resolveWireTypeName = this.responseInfo.TypeResolver.ResolveWireTypeName; if (this.responseInfo.Context.Format.ServiceModel != null) { resolveWireTypeName = null; } settings.EnableWcfDataServicesClientBehavior( resolveWireTypeName, this.responseInfo.DataNamespace, UriUtil.UriToString(this.responseInfo.TypeScheme), entryXmlCustomizer); settings.BaseUri = this.responseInfo.BaseUriResolver.BaseUriOrNull; settings.MaxProtocolVersion = CommonUtil.ConvertToODataVersion(this.responseInfo.MaxProtocolVersion); settings.UndeclaredPropertyBehaviorKinds = this.responseInfo.UndeclaredPropertyBehaviorKinds | ODataUndeclaredPropertyBehaviorKinds.ReportUndeclaredLinkProperty; CommonUtil.SetDefaultMessageQuotas(settings.MessageQuotas); this.responseInfo.ResponsePipeline.ExecuteReaderSettingsConfiguration(settings); return(settings); }
/// <summary> /// Translates resource bound expression tree to a URI. /// </summary> /// <param name='context'>Data context used to generate type names for types.</param> /// <param name="addTrailingParens">flag to indicate whether generated URI should include () if leaf is ResourceSet</param> /// <param name="e">The expression to translate</param> /// <param name="uri">uri</param> /// <param name="version">version for query</param> 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 = UriUtil.CreateUri(writer.uriBuilder.ToString(), UriKind.Absolute); version = writer.uriVersion; }
/// <summary> /// If necessary will create an absolute uri by combining the BaseUri and requestUri /// </summary> /// <param name="requestUri">The uri specified by the user</param> /// <returns>An absolute Uri based on the requestUri and if nessesary the BaseUri</returns> internal Uri GetOrCreateAbsoluteUri(Uri requestUri) { Util.CheckArgumentNull(requestUri, "requestUri"); if (!requestUri.IsAbsoluteUri) { return(UriUtil.CreateUri(this.GetBaseUriWithSlash(() => Strings.Context_RequestUriIsRelativeBaseUriRequired), requestUri)); } return(requestUri); }
/// <summary> /// Appends to create the entity instance URI for the specified <paramref name="entityInstance"/>. /// </summary> /// <param name="baseUri">The URI to append to</param> /// <param name="entityInstance">The entity instance to use.</param> /// <returns> /// The entity instance URI. /// </returns> internal override Uri BuildEntityInstanceUri(Uri baseUri, IEdmStructuredValue entityInstance) { var builder = new StringBuilder(); if (baseUri != null) { builder.Append(UriUtil.UriToString(baseUri)); } this.conventions.AppendKeyExpression(entityInstance, builder); return(UriUtil.CreateUri(builder.ToString(), UriKind.RelativeOrAbsolute)); }
/// <summary> /// Validates the value of the identity, the atom:id or DataServiceId /// </summary> /// <param name="identity">The value to validate</param> internal static void ValidateIdentityValue(string identity) { // here we could just assign idText to Identity // however we used to check for AbsoluteUri, thus we need to // convert string to Uri and check for absoluteness Uri idUri = UriUtil.CreateUri(identity, UriKind.RelativeOrAbsolute); if (!idUri.IsAbsoluteUri) { throw Error.InvalidOperation(Strings.Context_TrackingExpectsAbsoluteUri); } }
/// <summary> /// Validates the value of the 'Location' response header. /// </summary> /// <param name="location">the value as seen on the wire.</param> /// <returns>an absolute uri or </returns> internal static Uri ValidateLocationHeader(string location) { // We used to call the Uri constructor with the kind set to Absolute. // Hence now checking for the absoluteness. Uri locationUri = UriUtil.CreateUri(location, UriKind.RelativeOrAbsolute); if (!locationUri.IsAbsoluteUri) { throw Error.InvalidOperation(Strings.Context_LocationHeaderExpectsAbsoluteUri); } return(locationUri); }
/// <summary> /// Returns a slash terminated Uri. /// /// Will be the passed in one if it is already slash terminated, or a new one /// if the passed in one is not slash terminated. /// </summary> /// <param name="uri">The Uri to be slash terminated</param> /// <returns>A slash terminated version of the passed in Uri.</returns> private static Uri ForceSlashTerminatedUri(Uri uri) { Debug.Assert(uri.IsAbsoluteUri, "the uri must be an absolute uri"); Debug.Assert(String.IsNullOrEmpty(uri.Query), "the uri must not have any query"); Debug.Assert(String.IsNullOrEmpty(uri.Fragment), "the uri must not have any fragment"); string uriString = UriUtil.UriToString(uri); if (uriString[uriString.Length - 1] != '/') { return(UriUtil.CreateUri(uriString + "/", UriKind.Absolute)); } return(uri); }
/// <summary>base uri with no trailing slash</summary> /// <param name="entitySetName">the name of the entitSet whose Uri will be retrieved.</param> /// <returns>the baseUri ended with a slash for the entitySetName passed in.</returns> internal Uri GetEntitySetUri(string entitySetName) { Uri resolved = this.GetEntitySetUriFromResolver(entitySetName); if (resolved != null) { return(ForceNonSlashTerminatedUri(resolved)); } if (this.baseUriWithSlash != null) { return(UriUtil.CreateUri(this.baseUriWithSlash, UriUtil.CreateUri(entitySetName, UriKind.Relative))); } throw Error.InvalidOperation(Strings.Context_ResolveEntitySetOrBaseUriRequired(entitySetName)); }
/// <summary> /// Enumerates through the list of URI operation parameters and creates a new Uri with the uri operation parameters written as query string of the new Uri. /// </summary> /// <param name="requestUri">The Uri used to construct the new Uri.</param> /// <param name="operationParameters">The non-empty list of uri parameters which will be converted to query string.</param> /// <returns>Uri containing the uri parameters as query string.</returns> internal Uri WriteUriOperationParametersToUri(Uri requestUri, List <UriOperationParameter> operationParameters) { Debug.Assert(operationParameters != null && operationParameters.Any(), "OperationParameters was null or empty"); Debug.Assert(requestUri != null, "request_uri != null"); UriBuilder uriBuilder = new UriBuilder(requestUri); StringBuilder sb = new StringBuilder(); String uriString = UriUtil.UriToString(uriBuilder.Uri); if (!string.IsNullOrEmpty(uriBuilder.Query)) { Debug.Assert(uriBuilder.Query[0] == UriHelper.QUESTIONMARK, "uriBuilder.Query[0] == UriHelper.QUESTIONMARK"); // Don't append the '?', as later when we call setter on the Query, the '?' will be automatically added. sb.Append(uriBuilder.Query.Substring(1)); sb.Append(UriHelper.AMPERSAND); } foreach (UriOperationParameter op in operationParameters) { Debug.Assert(op != null, "op != null"); Debug.Assert(!string.IsNullOrEmpty(op.Name), "!string.IsNullOrEmpty(op.ParameterName)"); string paramName = op.Name.Trim(); // if the parameter name is an alias, make sure that the URI contains it. if (paramName.StartsWith(Char.ToString(UriHelper.ATSIGN), StringComparison.OrdinalIgnoreCase) && !uriString.Contains(paramName)) { throw new DataServiceRequestException(Strings.Serializer_UriDoesNotContainParameterAlias(op.Name)); } // name=value& sb.Append(paramName); sb.Append(UriHelper.EQUALSSIGN); sb.Append(this.ConvertToEscapedUriValue(paramName, op.Value)); sb.Append(UriHelper.AMPERSAND); } // remove the last extra ampersand. Debug.Assert(sb.ToString().EndsWith(Char.ToString(UriHelper.AMPERSAND), StringComparison.OrdinalIgnoreCase), "Uri was expected to end with an ampersand."); sb.Remove(sb.Length - 1, 1); uriBuilder.Query = sb.ToString(); return(uriBuilder.Uri); }
/// <summary> /// Validates that the passed in BaseUri /// </summary> /// <param name="baseUri">the baseUri that needs to be validated</param> /// <returns>True if the baseUri is valid, otherwise false</returns> private static bool IsValidBaseUri(Uri baseUri) { if (baseUri == null) { return(true); } if (!baseUri.IsAbsoluteUri || !Uri.IsWellFormedUriString(UriUtil.UriToString(baseUri), UriKind.Absolute) || !String.IsNullOrEmpty(baseUri.Query) || !String.IsNullOrEmpty(baseUri.Fragment) || ((baseUri.Scheme != "http") && (baseUri.Scheme != "https"))) { return(false); } return(true); }
/// <summary> /// Try and get the navigation link. If the navigation link is not specified, then its used the self link of the entity and /// appends the property name. /// </summary> /// <param name="baseUriResolver">retrieves the appropriate baseUri for a given entitySet.</param> /// <param name="property">ClientProperty instance representing the navigation property.</param> /// <returns>returns the uri for the given link. If the link is not present, its uses the self link of the current entity and appends the navigation property name.</returns> internal Uri GetNavigationLink(UriResolver baseUriResolver, ClientPropertyAnnotation property) { LinkInfo linkInfo = null; Uri uri = null; if (this.TryGetLinkInfo(property.PropertyName, out linkInfo)) { uri = linkInfo.NavigationLink; } if (uri == null) { Uri relativeUri = UriUtil.CreateUri(property.PropertyName + (property.IsEntityCollection ? "()" : String.Empty), UriKind.Relative); uri = UriUtil.CreateUri(this.GetResourceUri(baseUriResolver, true /*queryLink*/), relativeUri); } return(uri); }
/// <summary> /// In V1, we used to not support self links. Hence we used to use edit links as self links. /// IN V2, we are adding support for self links. But if there are not specified, we need to /// fall back on the edit link. /// </summary> /// <param name="queryLink">whether to get query link or the edit link.</param> /// <returns>the query or the edit link, as specified in the <paramref name="queryLink"/> parameter.</returns> private Uri GetLink(bool queryLink) { // If asked for a self link and self-link is present, return self link Uri link; if (queryLink && this.SelfLink != null) { return(this.SelfLink); } // otherwise return edit link if present. if ((link = this.GetLatestEditLink()) != null) { return(link); } if (this.State != EntityStates.Added) { throw new ArgumentNullException(Strings.EntityDescriptor_MissingSelfEditLink(this.identity)); } else { Debug.Assert(this.TransientEntityDescriptor == null, "The transient entity container must be null, when the entity is in added state"); // If the entity is in added state, and either the parent property or the addToUri must be non-null Debug.Assert(this.addToUri != null || !String.IsNullOrEmpty(this.ParentPropertyForInsert), "For entities in added state, parentProperty or addToUri must be specified"); if (this.addToUri != null) { return(this.addToUri); } else { return(UriUtil.CreateUri(this.ParentPropertyForInsert, UriKind.Relative)); } } }
/// <summary> /// Create an instance of primitive type from a string representation /// </summary> /// <param name="text">The string representation</param> /// <returns>An instance of primitive type</returns> internal override object Parse(String text) { return(UriUtil.CreateUri(text, UriKind.RelativeOrAbsolute)); }
/// <summary> /// Writes a navigation link. /// </summary> /// <param name="entityDescriptor">The entity</param> /// <param name="relatedLinks">The links related to the entity</param> /// <param name="odataWriter">The ODataWriter used to write the navigation link.</param> internal void WriteNavigationLink(EntityDescriptor entityDescriptor, IEnumerable <LinkDescriptor> relatedLinks, ODataWriterWrapper odataWriter) { // TODO:: create instance of odatawriter. // TODO:: send clientType once, so that we dont need entity descriptor Debug.Assert(EntityStates.Added == entityDescriptor.State, "entity not added state"); Dictionary <string, List <LinkDescriptor> > groupRelatedLinks = new Dictionary <string, List <LinkDescriptor> >(EqualityComparer <string> .Default); foreach (LinkDescriptor end in relatedLinks) { List <LinkDescriptor> linkDescriptorsList = null; if (!groupRelatedLinks.TryGetValue(end.SourceProperty, out linkDescriptorsList)) { linkDescriptorsList = new List <LinkDescriptor>(); groupRelatedLinks.Add(end.SourceProperty, linkDescriptorsList); } linkDescriptorsList.Add(end); } ClientTypeAnnotation clientType = null; foreach (var grlinks in groupRelatedLinks) { if (null == clientType) { ClientEdmModel model = this.requestInfo.Model; clientType = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType())); } bool isCollection = clientType.GetProperty(grlinks.Key, false).IsEntityCollection; bool started = false; foreach (LinkDescriptor end in grlinks.Value) { Debug.Assert(!end.ContentGeneratedForSave, "already saved link"); end.ContentGeneratedForSave = true; Debug.Assert(null != end.Target, "null is DELETE"); ODataNavigationLink navigationLink = new ODataNavigationLink(); navigationLink.Url = this.requestInfo.EntityTracker.GetEntityDescriptor(end.Target).GetLatestEditLink(); Debug.Assert(Uri.IsWellFormedUriString(UriUtil.UriToString(navigationLink.Url), UriKind.Absolute), "Uri.IsWellFormedUriString(targetEditLink, UriKind.Absolute)"); navigationLink.IsCollection = isCollection; navigationLink.Name = grlinks.Key; if (!started) { odataWriter.WriteNavigationLinksStart(navigationLink); started = true; } odataWriter.WriteNavigationLinkStart(navigationLink, end.Source, end.Target); odataWriter.WriteEntityReferenceLink(new ODataEntityReferenceLink() { Url = navigationLink.Url }, end.Source, end.Target); odataWriter.WriteNavigationLinkEnd(navigationLink, end.Source, end.Target); } odataWriter.WriteNavigationLinksEnd(); } }
private void ParseCurrentLink(AtomEntry targetEntry) { Debug.Assert(targetEntry != null, "targetEntry != null"); Debug.Assert( this.reader.NodeType == XmlNodeType.Element, "this.reader.NodeType == XmlNodeType.Element -- otherwise we shouldn't try to parse a link"); Debug.Assert( this.reader.LocalName == "link", "this.reader.LocalName == 'link' -- otherwise we shouldn't try to parse a link"); string relation = this.reader.GetAttribute(XmlConstants.AtomLinkRelationAttributeName); if (relation == null) { return; } if (relation == XmlConstants.AtomEditRelationAttributeValue && targetEntry.EditLink == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (String.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingEditLinkInResponseBody); } targetEntry.EditLink = this.ConvertHRefAttributeValueIntoURI(href); } else if (relation == XmlConstants.AtomSelfRelationAttributeValue && targetEntry.QueryLink == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (String.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingSelfLinkInResponseBody); } targetEntry.QueryLink = this.ConvertHRefAttributeValueIntoURI(href); } else if (relation == XmlConstants.AtomEditMediaRelationAttributeValue && targetEntry.MediaEditUri == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (String.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingEditMediaLinkInResponseBody); } targetEntry.MediaEditUri = this.ConvertHRefAttributeValueIntoURI(href); targetEntry.StreamETagText = this.reader.GetAttribute(XmlConstants.AtomETagAttributeName, XmlConstants.DataWebMetadataNamespace); } if (!this.reader.IsEmptyElement) { string propertyName = UriUtil.GetNameFromAtomLinkRelationAttribute(relation); if (propertyName == null) { return; } string propertyValueText = this.reader.GetAttribute(XmlConstants.AtomTypeAttributeName); bool isFeed; if (!IsAllowedLinkType(propertyValueText, out isFeed)) { return; } if (!ReadChildElement(this.reader, XmlConstants.AtomInlineElementName, XmlConstants.DataWebMetadataNamespace)) { return; } bool emptyInlineCollection = this.reader.IsEmptyElement; object propertyValue = null; if (!emptyInlineCollection) { AtomFeed nestedFeed = null; AtomEntry nestedEntry = null; List <AtomEntry> feedEntries = null; Debug.Assert(this.reader is Xml.XmlWrappingReader, "reader must be a instance of XmlWrappingReader"); string readerBaseUri = this.reader.BaseURI; XmlReader nestedReader = Xml.XmlWrappingReader.CreateReader(readerBaseUri, this.reader.ReadSubtree()); nestedReader.Read(); Debug.Assert(nestedReader.LocalName == "inline", "nestedReader.LocalName == 'inline'"); AtomParser nested = new AtomParser(nestedReader, this.entryCallback, this.typeScheme, this.currentDataNamespace); while (nested.Read()) { switch (nested.DataKind) { case AtomDataKind.Feed: feedEntries = new List <AtomEntry>(); nestedFeed = nested.CurrentFeed; propertyValue = nestedFeed; break; case AtomDataKind.Entry: nestedEntry = nested.CurrentEntry; if (feedEntries != null) { feedEntries.Add(nestedEntry); } else { propertyValue = nestedEntry; } break; case AtomDataKind.PagingLinks: break; default: throw new InvalidOperationException(Strings.AtomParser_UnexpectedContentUnderExpandedLink); } } if (nestedFeed != null) { Debug.Assert( nestedFeed.Entries == null, "nestedFeed.Entries == null -- otherwise someone initialized this for us"); nestedFeed.Entries = feedEntries; } } AtomContentProperty property = new AtomContentProperty(); property.Name = propertyName; if (emptyInlineCollection || propertyValue == null) { property.IsNull = true; if (isFeed) { property.Feed = new AtomFeed(); property.Feed.Entries = Enumerable.Empty <AtomEntry>(); } else { property.Entry = new AtomEntry(); property.Entry.IsNull = true; } } else { property.Feed = propertyValue as AtomFeed; property.Entry = propertyValue as AtomEntry; } targetEntry.DataValues.Add(property); } }
/// <summary> /// Convert an instance of primitive type to string /// </summary> /// <param name="instance">The instance</param> /// <returns>The string representation of the instance</returns> internal override string ToString(object instance) { return(UriUtil.UriToString((Uri)instance)); }