Skip to content
Browse files

fixed up the URIs generated by the data service; this is *evil* code

  • Loading branch information...
1 parent ee58a12 commit ba00b147917074a3cc2136786fc2990246bab98c @half-ogre half-ogre committed Mar 20, 2012
Showing with 57 additions and 6 deletions.
  1. +57 −6 Website/DataServices/V2CuratedFeed.svc.cs
View
63 Website/DataServices/V2CuratedFeed.svc.cs
@@ -2,12 +2,11 @@
using System.Data.Entity;
using System.Data.Services;
using System.Linq;
+using System.Reflection;
using System.ServiceModel.Web;
-using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
-using Microsoft.WindowsAzure.StorageClient;
namespace NuGetGallery
{
@@ -26,7 +25,7 @@ public V2CuratedFeed(IEntitiesContext entities, IEntityRepository<Package> repo,
}
protected override FeedContext<V2FeedPackage> CreateDataSource()
- {
+ {
var packages = GetPackages();
return new FeedContext<V2FeedPackage>
@@ -43,19 +42,66 @@ public IQueryable<V2FeedPackage> FindPackagesById(string id)
.ToV2FeedPackageQuery(Configuration.SiteRoot);
}
- public IQueryable<Package> GetPackages()
+ private static void FixUpDataServiceUrisForCuratedFeedName(
+ DataServiceOperationContext operationContext,
+ string curatedFeedName)
{
- string curatedFeedName = HttpContext.Current.Request.QueryString["name"];
+ // AVERT YOUR EYES!
+
+ // This is an *evil* hack to get proper URIs into the data servive's output, e.g. /api/v2/curated-feeds/{name}.
+ // Without this, the URIs in the data service will be wrong, and won't work if a client tried to use them.
+
+ var fixedUpSeriveUri = operationContext.AbsoluteServiceUri.AbsoluteUri.Replace("/api/v2/curated-feed/", "/api/v2/curated-feeds/" + curatedFeedName + "/");
+ var fixedUpRequestUri = operationContext.AbsoluteRequestUri.AbsoluteUri.Replace("/api/v2/curated-feed/", "/api/v2/curated-feeds/" + curatedFeedName + "/");
+
+ // The URI needs to be fixed up both on the actual IDataService host (hostInterface) and the service host wrapper (hostWrapper)
+ // Null checks aren't really worth much here. If it does break, it'll result in a 500 to the client.
+ var hostInterfaceField = operationContext.GetType().GetField("hostInterface",BindingFlags.NonPublic | BindingFlags.Instance);
+ var hostInterface = hostInterfaceField.GetValue(operationContext);
+ var hostWrapperField = operationContext.GetType().GetField("hostWrapper", BindingFlags.NonPublic | BindingFlags.Instance);
+ var hostWrapper = hostWrapperField.GetValue(operationContext);
+
+ // Fix up the service URIs
+ var interfaceServiceUriField = hostInterface.GetType().GetField("absoluteServiceUri", BindingFlags.NonPublic | BindingFlags.Instance);
+ interfaceServiceUriField.SetValue(hostInterface, new Uri(fixedUpSeriveUri));
+ var wrapperServiceUriField = hostWrapper.GetType().GetField("absoluteServiceUri", BindingFlags.NonPublic | BindingFlags.Instance);
+ wrapperServiceUriField.SetValue(hostWrapper, new Uri(fixedUpSeriveUri));
+
+ // Fix up the request URIs
+ var interfaceRequestUriField = hostInterface.GetType().GetField("absoluteRequestUri",BindingFlags.NonPublic | BindingFlags.Instance);
+ interfaceRequestUriField.SetValue(hostInterface, new Uri(fixedUpRequestUri));
+ var wrapperRequestUriField = hostWrapper.GetType().GetField("absoluteRequestUri", BindingFlags.NonPublic | BindingFlags.Instance);
+ wrapperRequestUriField.SetValue(hostWrapper, new Uri(fixedUpRequestUri));
+
+ // Take a shower.
+ }
+
+ private string GetCuratedFeedName()
+ {
+ var curatedFeedName = HttpContext.Current.Request.QueryString["name"];
+
var curatedFeed = Entities.CuratedFeeds.SingleOrDefault(cf => cf.Name == curatedFeedName);
if (curatedFeed == null)
- throw new DataServiceException(404, "Not Found");
+ throw new DataServiceException(404, "Not Found");
+
+ return curatedFeedName;
+ }
+
+ private IQueryable<Package> GetPackages()
+ {
+ var curatedFeedName = GetCuratedFeedName();
return Entities.CuratedFeeds
.Where(cf => cf.Name == curatedFeedName)
.Include(cf => cf.Packages.Select(cp => cp.PackageRegistration.Packages))
.SelectMany(cf => cf.Packages.SelectMany(cp => cp.PackageRegistration.Packages.Select(p => p)));
}
+ protected override void OnStartProcessingRequest(ProcessRequestArgs args)
+ {
+ FixUpDataServiceUrisForCuratedFeedName(args.OperationContext, GetCuratedFeedName());
+ }
+
[WebGet]
public IQueryable<V2FeedPackage> Search(string searchTerm, string targetFramework, bool includePrerelease)
{
@@ -81,5 +127,10 @@ public IQueryable<V2FeedPackage> Search(string searchTerm, string targetFramewor
return new Uri(url, UriKind.Absolute);
}
+
@half-ogre
half-ogre added a note Mar 20, 2012

Oops, need to remove this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ public bool IsReusable
+ {
+ get { throw new NotImplementedException(); }
+ }
}
}

0 comments on commit ba00b14

Please sign in to comment.
Something went wrong with that request. Please try again.