Skip to content

Commit 17eea32

Browse files
committed
Merge pull request #21 from latticework/kenbrubaker/HandleDeepPaths
Added support for services running in a deep path.
2 parents e30a50b + db340b5 commit 17eea32

File tree

16 files changed

+157
-77
lines changed

16 files changed

+157
-77
lines changed

Jali.Pcl/samples/HelloJali.Web/App_Start/WebApiConfig.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Security.Policy;
24
using System.Security.Principal;
35
using System.Threading;
46
using System.Threading.Tasks;
7+
using System.Web;
8+
using System.Web.Hosting;
59
using System.Web.Http;
610
using Jali;
711
using Jali.Core;
@@ -18,18 +22,12 @@ public static void Register(HttpConfiguration config)
1822
// Web API configuration and services
1923

2024
// Web API routes
25+
var context = new AspNetExecutionContext();
2126

22-
var claims = WindowsIdentity.GetCurrent()?.Claims.Select(c => new Jali.Secure.Claim(c.Type, c.Value));
27+
var absolutePath = VirtualPathUtility.ToAbsolute("~");
28+
var baseUri = new Uri(absolutePath, UriKind.Relative);
2329

24-
if (claims == null)
25-
{
26-
throw new InternalErrorException("The AppPool identity is not assigned. Cannot create Jali Service");
27-
}
28-
29-
var identity = new SecurityIdentity(claims);
30-
var context = new DefaultExecutionContext(identity);
31-
32-
config.UseJaliService(context, async ctx => await Task.FromResult(new HelloService(ctx)), null);
30+
config.UseJaliService(context, async ctx => await Task.FromResult(new HelloService(ctx, baseUri)), null);
3331

3432
config.MapHttpAttributeRoutes();
3533

Jali.Pcl/src/Jali.Serve.Pcl/Jali.Serve.Pcl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
<Link>TenantIdentity.cs</Link>
289289
</Compile>
290290
<Compile Include="Definition\RestMethodVerbs.cs" />
291+
<Compile Include="JaliUriExtensions.cs" />
291292
<Compile Include="Properties\AssemblyInfo.cs" />
292293
</ItemGroup>
293294
<ItemGroup>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Text.RegularExpressions;
2+
using Jali.Core;
3+
using Jali.Note;
4+
using Jali.Serve;
5+
6+
// ReSharper disable once CheckNamespace
7+
namespace System
8+
{
9+
/// <summary>
10+
/// Provides Jali-specific extensions to the <see cref="Uri"/> class.
11+
/// </summary>
12+
public static class JaliUriExtensions
13+
{
14+
/// <summary>
15+
/// Parses the request for Jali Message information
16+
/// </summary>
17+
/// <param name="uri">
18+
/// The request URL.
19+
/// </param>
20+
/// <param name="rootUrl">
21+
/// A relative URL that represents the service root.
22+
/// </param>
23+
/// <returns>
24+
/// The parse result.
25+
/// </returns>
26+
public static HttpRequestParseResult JaliParse(this Uri uri, string rootUrl = null)
27+
{
28+
if (uri == null) throw new ArgumentNullException(nameof(uri));
29+
30+
rootUrl = rootUrl ?? string.Empty;
31+
32+
if (rootUrl.Length > 0 && rootUrl[0] == '/')
33+
{
34+
rootUrl = rootUrl.Substring(1);
35+
}
36+
37+
var messages = new NotificationMessageCollection();
38+
39+
var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
40+
41+
if (!path.StartsWith(rootUrl, StringComparison.OrdinalIgnoreCase))
42+
{
43+
// TODO: JaliHttpRequestMessageExtensions.Parse: Change to Domain error.
44+
var message =
45+
$"Route '{uri}' is not rooted with the Jali Server url, which is '{rootUrl}'. The request should not have been forwarded to it.";
46+
messages.Append(new InternalErrorException(message).Messages);
47+
return new HttpRequestParseResult(messages);
48+
}
49+
else
50+
{
51+
path = path.Substring(rootUrl.Length);
52+
53+
if (path.Length > 0 && path[0] == '/')
54+
{
55+
path = path.Substring(1);
56+
}
57+
}
58+
59+
var resourceMatch = Regex.Match(
60+
path,
61+
@"^resources/(?<resourceName>[_a-zA-Z][_a-zA-Z0-9]*)(/(?<resourceKey>[_a-zA-Z0-9]+))?(/routines/(?<routineName>[_a-zA-Z][_a-zA-Z0-9]*)(?<messageAction>)[_a-zA-Z][_a-zA-Z0-9]*)?$");
62+
63+
if (!resourceMatch.Success)
64+
{
65+
// TODO: JaliHttpRequestMessageExtensions.Parse: Change to Domain error.
66+
var message =
67+
$"Jali Server cannot handle route '{uri}'. The request should not have been forwarded to it.";
68+
messages.Append(new InternalErrorException(message).Messages);
69+
return new HttpRequestParseResult(messages);
70+
}
71+
72+
var resourceName = GetCaptureValue(resourceMatch, "resourceName");
73+
var resourceKey = GetCaptureValue(resourceMatch, "resourceKey");
74+
var routineName = GetCaptureValue(resourceMatch, "routineName");
75+
var messageAction = GetCaptureValue(resourceMatch, "messageAction");
76+
77+
return new HttpRequestParseResult(
78+
method: null,
79+
resourceName: resourceName,
80+
payload: null,
81+
resourceKey: resourceKey,
82+
routineName: routineName,
83+
messageAction: messageAction,
84+
messages: messages);
85+
}
86+
87+
private static string GetCaptureValue(Match match, string name)
88+
{
89+
var value = match.Groups[name].Value;
90+
91+
return (value == string.Empty) ? null : value;
92+
}
93+
}
94+
}

psakefile.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ properties {
33
$zipFileName = "Jali00r1.zip"
44
$majorVersion = "0.0"
55
$majorWithReleaseVersion = "0.0.1"
6-
$nugetPrelease = "prealpha603"
6+
$nugetPrelease = "prealpha609"
77
$version = GetVersion $majorWithReleaseVersion
88
$signAssemblies = $false
99
# $signKeyPath = "C:\Development\Releases\newtonsoft.snk"

samples/Jali.Serve.Samples.HelloServices/GreetingData/GetGreetingDataRoutine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override async Task ExecuteProcedureCore(
4343

4444
public static Routine GetDefinition(Uri resourceUrl)
4545
{
46-
var url = new Uri(resourceUrl, $"routines/{Name}");
46+
var url = resourceUrl.Combine($"routines/{Name}");
4747
return new Routine
4848
{
4949
Name = Name,

samples/Jali.Serve.Samples.HelloServices/GreetingData/GreetingDataResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public GreetingDataResource(ServiceBase service, Resource definition, IResourceC
1919

2020
public static Resource GetDefinition(Uri helloServiceUrl)
2121
{
22-
var url = new Uri(helloServiceUrl, "resources/greetingdata/v1.0.0");
22+
var url = helloServiceUrl.Combine("resources/greetingdata/v1.0.0");
2323
var schema = GetSchema();
2424
var keySchema = GetKeySchema();
2525

samples/Jali.Serve.Samples.HelloServices/GreetingData/NewGreetingDataRoutine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override async Task ExecuteProcedureCore(
3333

3434
public static Routine GetDefinition(Uri resourceUrl)
3535
{
36-
var url = new Uri(resourceUrl, $"routines/{Name}");
36+
var url = resourceUrl.Combine($"routines/{Name}");
3737
return new Routine
3838
{
3939
Name = Name,

samples/Jali.Serve.Samples.HelloServices/GreetingData/RemoveGreetingDataRoutine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected override async Task ExecuteProcedureCore(
4747

4848
public static Routine GetDefinition(Uri resourceUrl)
4949
{
50-
var url = new Uri(resourceUrl, $"routines/{Name}");
50+
var url = resourceUrl.Combine($"routines/{Name}");
5151
return new Routine
5252
{
5353
Name = Name,

samples/Jali.Serve.Samples.HelloServices/GreetingData/SetGreetingDataRoutine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected override async Task ExecuteProcedureCore(
9696

9797
public static Routine GetDefinition(Uri resourceUrl)
9898
{
99-
var url = new Uri(resourceUrl, $"routines/{Name}");
99+
var url = resourceUrl.Combine($"routines/{Name}");
100100
return new Routine
101101
{
102102
Name = Name,

samples/Jali.Serve.Samples.HelloServices/Hello/HelloResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public HelloResource(ServiceBase service, Resource definition, IResourceContext
1919

2020
public static Resource GetDefinition(Uri helloServiceUrl)
2121
{
22-
var helloResourceUrl = new Uri(helloServiceUrl, "resources/hello/v1.0.0");
22+
var helloResourceUrl = helloServiceUrl.Combine("resources/hello/v1.0.0");
2323

2424
var resourceSchema = JSchema.Parse(@"{
2525
""$schema"": ""http://json-schema.org/draft-04/schema#"",
@@ -52,7 +52,7 @@ public static Resource GetDefinition(Uri helloServiceUrl)
5252
["get-hello"] = new Routine
5353
{
5454
Name = "get-hello",
55-
Url = new Uri(helloResourceUrl, "routines/get-hello"),
55+
Url = helloResourceUrl.Combine("routines/get-hello"),
5656
Description = "Retrieves the hello message.",
5757
Messages =
5858
{

0 commit comments

Comments
 (0)