/
V2CuratedFeed.svc.cs
138 lines (113 loc) · 6.3 KB
/
V2CuratedFeed.svc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Data.Entity;
using System.Data.Services;
using System.Linq;
using System.Reflection;
using System.ServiceModel.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace NuGetGallery
{
// TODO : Have V2CuratedFeed derive from V2Feed
public class V2CuratedFeed : FeedServiceBase<V2FeedPackage>
{
private const int FeedVersion = 2;
public V2CuratedFeed()
{
}
public V2CuratedFeed(IEntitiesContext entities, IEntityRepository<Package> repo, IConfiguration configuration, ISearchService searchService)
: base(entities, repo, configuration, searchService)
{
}
protected override FeedContext<V2FeedPackage> CreateDataSource()
{
var packages = GetPackages();
return new FeedContext<V2FeedPackage>
{
Packages = packages.ToV2FeedPackageQuery(Configuration.GetSiteRoot(UseHttps()))
};
}
public static void InitializeService(DataServiceConfiguration config)
{
InitializeServiceBase(config);
}
[WebGet]
public IQueryable<V2FeedPackage> FindPackagesById(string id)
{
return GetPackages()
.Where(p => p.PackageRegistration.Id.Equals(id, StringComparison.OrdinalIgnoreCase))
.ToV2FeedPackageQuery(Configuration.GetSiteRoot(UseHttps()));
}
private static void FixUpDataServiceUrisForCuratedFeedName(
DataServiceOperationContext operationContext,
string curatedFeedName)
{
// 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.Request.QueryString["name"];
var curatedFeed = Entities.CuratedFeeds.SingleOrDefault(cf => cf.Name == curatedFeedName);
if (curatedFeed == null)
{
throw new DataServiceException(404, "Not Found");
}
return curatedFeedName;
}
private IQueryable<Package> GetPackages()
{
var curatedFeedName = GetCuratedFeedName();
var packages = 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)));
// The curated feeds table has duplicate entries for feed, package registration pairs. Consequently
// we have to apply a distinct on the results.
return packages.Distinct();
}
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
FixUpDataServiceUrisForCuratedFeedName(args.OperationContext, GetCuratedFeedName());
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "targetFramework", Justification = "We can't change it because it's now part of the contract of this service method.")]
[WebGet]
public IQueryable<V2FeedPackage> Search(string searchTerm, string targetFramework, bool includePrerelease)
{
IQueryable<Package> curatedPackages = GetPackages();
return SearchAdaptor.SearchCore(SearchService, HttpContext.Request, SiteRoot, curatedPackages, searchTerm, targetFramework, includePrerelease, filterToPackageSet: curatedPackages)
.ToV2FeedPackageQuery(Configuration.GetSiteRoot(UseHttps()));
}
public override Uri GetReadStreamUri(
object entity,
DataServiceOperationContext operationContext)
{
var package = (V2FeedPackage)entity;
var urlHelper = new UrlHelper(new RequestContext(HttpContext, new RouteData()));
string url = urlHelper.PackageDownload(FeedVersion, package.Id, package.Version);
return new Uri(url, UriKind.Absolute);
}
}
}