public void DoWork(IRequest request) { // If an asyncState object already exists, an exception is thrown as the performer only accepts // one call after each other. The Request receiver has to manage a queued execution. // Before calling the performers execution implementation method a new AsyncState object is created // and set to an initial tracking state. lock (_asyncStateObj) { if (null != _asyncStateObj.Tracking) { throw new InvalidOperationException("The performer cannot be executed because it is already running."); } SyncTracking tracking = new SyncTracking(); tracking.ElapsedSeconds = 1; tracking.Phase = TrackingPhase.INIT; tracking.PhaseDetail = "Tracking Id was: " + _requestContext.TrackingId.ToString(); tracking.PollingMillis = 100; tracking.RemainingSeconds = 100; _asyncStateObj.Tracking = tracking; } // *** Initialization for the async execution *** // - Read Feed from request stream. // - Read trackingId from request URL // convert tracking ID from request to type Guid string strTrackingId = request.Uri.TrackingID; if (String.IsNullOrEmpty(strTrackingId)) { throw new RequestException("TrackingId is missing"); } GuidConverter converter = new GuidConverter(); this.TrackingId = (Guid)converter.ConvertFrom(strTrackingId); //read feed SyncFeed feed = new SyncFeed(); XmlReader reader = XmlReader.Create(request.Stream); feed.ReadXml(reader, ResourceKindHelpers.GetPayloadType(_requestContext.ResourceKind)); // *** Do work asynchronously *** _asyncPerformer = new InternalAsyncPerformer(this); _asyncPerformer.DoWork(_requestContext.Config, feed); // *** set the tracking to the request response *** this.GetTrackingState(request); }
public void DoWork(IRequest request) { if (request.ContentType != Sage.Common.Syndication.MediaType.AtomEntry) { throw new RequestException("Atom entry content type expected"); } if (!String.IsNullOrEmpty(_requestContext.ResourceKey)) { throw new RequestException("Please use resource url"); } // read entry from stream SyncFeedEntry entry = new SyncFeedEntry(); XmlReader reader = XmlReader.Create(request.Stream); reader.MoveToContent(); entry.ReadXml(reader, ResourceKindHelpers.GetPayloadType(_requestContext.ResourceKind)); IEntityWrapper wrapper = EntityWrapperFactory.Create(_requestContext.ResourceKind, _requestContext); Document document = wrapper.GetTransformedDocument(entry.Payload, entry.SyncLinks); if (!String.IsNullOrEmpty(document.Id)) { throw new RequestException("Entity alredy exists"); } // Add Document SdataTransactionResult sdTrResult = wrapper.Add(entry.Payload, entry.SyncLinks); if (sdTrResult == null) { throw new RequestException("Entity does not exists"); //????? } if ((sdTrResult.HttpStatus == System.Net.HttpStatusCode.OK) || (sdTrResult.HttpStatus == System.Net.HttpStatusCode.Created)) { // store correlation SyncFeedEntry responseEntry = wrapper.GetFeedEntry(sdTrResult.LocalId); SyncFeed feed = new SyncFeed(); feed.FeedType = FeedType.ResourceEntry; feed.Entries.Add(responseEntry); request.Response.StatusCode = System.Net.HttpStatusCode.Created; request.Response.Serializer = new SyncFeedSerializer(); request.Response.Feed = feed; request.Response.Protocol.SendUnknownResponseHeader("Location", responseEntry.Id); request.Response.ContentType = Sage.Common.Syndication.MediaType.AtomEntry; } else { throw new RequestException(sdTrResult.HttpMessage); } }
public void DoWork(IRequest request) { Feed <ResourceFeedEntry> feed = new Feed <ResourceFeedEntry>(); feed.Title = "Available Resources"; Dictionary <SupportedResourceKinds, Type> resourcePayloadTypes = ResourceKindHelpers.GetAllResourcePayloadTypes(); string resourceName; string resourceDescription; string resourceLink; foreach (SupportedResourceKinds resKind in resourcePayloadTypes.Keys) { Type resourceType = resourcePayloadTypes[resKind]; ResourceDescriptionAttribute resourceDescriptionAttr; if (ReflectionHelpers.TryGetSingleCustomAttribute <ResourceDescriptionAttribute>(resourceType, out resourceDescriptionAttr)) { if (resourceDescriptionAttr.CanGet) { resourceName = resourceDescriptionAttr.Name; resourceDescription = resourceDescriptionAttr.Description; resourceLink = string.Format("{0}{1}/", _requestContext.DatasetLink, resKind.ToString()); ResourceFeedEntry entry = new ResourceFeedEntry(); entry.Id = resourceLink; entry.Name = resourceName; entry.Description = resourceDescription; entry.Link = resourceLink; entry.Updated = DateTime.Now; entry.Summary = resourceDescription; entry.Source = resourceDescription; FeedLink feedentryLink = new FeedLink(resourceLink, LinkType.Self, MediaType.Atom, entry.Description); entry.Links.Add(feedentryLink); feedentryLink = new FeedLink(resourceLink, LinkType.Related, MediaType.Atom, entry.Description); entry.Links.Add(feedentryLink); feed.Entries.Add(entry); } } else { // only in debug mode! //this.ThrowResourceDescriptionAttributeMissing(resourceType); } } request.Response.Feed = feed; }
public void DoWork(IRequest request) { if (request.ContentType != Sage.Common.Syndication.MediaType.Atom) { throw new RequestException("Atom content type expected"); } //read feed string resourceKindName = _requestContext.ResourceKind.ToString(); SyncFeed feed = new SyncFeed(); XmlReader reader = XmlReader.Create(request.Stream); feed.ReadXml(reader, ResourceKindHelpers.GetPayloadType(_requestContext.ResourceKind)); /* iterate through all entries and store result information */ ISyncResultInfoStore syncResultInfoStore = RequestReceiver.NorthwindAdapter.StoreLocator.GetSyncResultStore(_requestContext.SdataContext); int noOfEntries = feed.Entries.Count; string endpoint = string.Empty; try { endpoint = new RequestContext(new Sage.Common.Syndication.SDataUri(feed.Id)).OriginEndPoint; } catch { } SyncResultEntryInfo[] syncResultEntries = new SyncResultEntryInfo[noOfEntries]; for (int i = 0; i < noOfEntries; i++) { SyncFeedEntry entry = (SyncFeedEntry)feed.Entries[i]; string httpMethod = entry.HttpMethod; int httpStatus = -1; if (Enum.IsDefined(typeof(HttpStatusCode), entry.HttpStatusCode.ToString())) { httpStatus = (int)Enum.Parse(typeof(HttpStatusCode), entry.HttpStatusCode.ToString(), true); } string httpLocation = entry.HttpLocation; string httpMessage = entry.HttpMessage; string diagnosisXml = XmlSerializationHelpers.SerializeObjectToXml(entry.Diagnoses); string payloadXml = XmlSerializationHelpers.SerializeObjectToXml(entry.Payload); syncResultEntries[i] = new SyncResultEntryInfo(httpMethod, httpStatus, httpLocation, httpMessage, diagnosisXml, payloadXml, DateTime.Now, endpoint); } syncResultInfoStore.Add(resourceKindName, syncResultEntries); }
private List <SyncFeedEntryLink> GetLinks(Dictionary <string, string> foreignIds) { List <SyncFeedEntryLink> result = new List <SyncFeedEntryLink>(); foreach (string key in foreignIds.Keys) { string value; if (foreignIds.TryGetValue(key, out value)) { SupportedResourceKinds tmpResKind = ResourceKindHelpers.GetResourceKind(key); Guid guid = GetUuid(value, "", tmpResKind); SyncFeedEntryLink link = SyncFeedEntryLink.CreateRelatedLink( Common.ResourceKindHelpers.GetSingleResourceUrl(_context.DatasetLink, key, value), tmpResKind.ToString(), key, guid.ToString()); result.Add(link); } } return(result); }
public void DoWork(IRequest request) { if (request.ContentType != Sage.Common.Syndication.MediaType.AtomEntry) { throw new RequestException("Atom entry content type expected"); } if (String.IsNullOrEmpty(_requestContext.ResourceKey)) { throw new RequestException("Please use single resource url"); } //SupportedResourceKinds resKind = _requestContext.ResourceKind; //switch (resKind) //{ // case SupportedResourceKinds.tradingAccounts: // case SupportedResourceKinds.contacts: // case SupportedResourceKinds.phoneNumbers: // case SupportedResourceKinds.postalAddresses: // break; // default: // throw new RequestException("Put is not Supported for requested resource"); //} // read entry from stream SyncFeedEntry entry = new SyncFeedEntry(); XmlReader reader = XmlReader.Create(request.Stream); reader.MoveToContent(); entry.ReadXml(reader, ResourceKindHelpers.GetPayloadType(_requestContext.ResourceKind)); IEntityWrapper wrapper = EntityWrapperFactory.Create(_requestContext.ResourceKind, _requestContext); Token emptyToken = new Token(); Identity identity = wrapper.GetIdentity(_requestContext.ResourceKey); Document originalDocument = wrapper.Entity.GetDocument(identity, emptyToken, _requestContext.Config); if (originalDocument.LogState == LogState.Deleted) { throw new RequestException("Entity does not exists"); } entry.Payload.LocalID = _requestContext.ResourceKey; SdataTransactionResult sdTrResult = wrapper.Update(entry.Payload, entry.SyncLinks); if (sdTrResult == null) { SyncFeedEntry responseEntry = wrapper.GetFeedEntry(_requestContext.ResourceKey); SyncFeed feed = new SyncFeed(); feed.FeedType = FeedType.ResourceEntry; feed.Entries.Add(responseEntry); request.Response.Serializer = new SyncFeedSerializer(); request.Response.Feed = feed; request.Response.ContentType = Sage.Common.Syndication.MediaType.AtomEntry; } else if (sdTrResult.HttpStatus == System.Net.HttpStatusCode.OK) { SyncFeedEntry responseEntry = wrapper.GetFeedEntry(_requestContext.ResourceKey); SyncFeed feed = new SyncFeed(); feed.FeedType = FeedType.ResourceEntry; feed.Entries.Add(responseEntry); request.Response.Serializer = new SyncFeedSerializer(); request.Response.Feed = feed; request.Response.ContentType = Sage.Common.Syndication.MediaType.AtomEntry; } else { throw new RequestException(sdTrResult.HttpMessage); } }