Skip to content

Commit 7bef717

Browse files
author
Jon Nicholson
committed
Fixed a binding naming conventions and order of arguments to reflect the Java interface. Added support for brokered and direct environments for binding based on sourceName and applicationKey/sessionToken respectively.
1 parent ead45d1 commit 7bef717

8 files changed

Lines changed: 114 additions & 63 deletions

File tree

Code/Sif3Framework/Sif.Framework/Model/Infrastructure/SifObjectBinding.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ namespace Sif.Framework.Model.Infrastructure
66
{
77
class SifObjectBinding : IPersistable<long>, ISifRefId<Guid>
88
{
9+
/// <summary>
10+
/// Internal identifier used by hibernate. Do not use/alter this.
11+
/// </summary>
912
public virtual long Id { get; set; }
1013

14+
/// <summary>
15+
/// The ref id of the object to be bound
16+
/// </summary>
1117
public virtual Guid RefId { get; set; }
1218

13-
public virtual string SessionToken { get; set; }
19+
/// <summary>
20+
/// The id of the owner of the object (application key, source id, etc.)
21+
/// </summary>
22+
public virtual string OwnerId { get; set; }
1423
}
1524
}

Code/Sif3Framework/Sif.Framework/Persistence/NHibernate/Mapping/SifObjectBinding.hbm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<id name="Id" column="SIF_OBJECT_BINDING_ID">
55
<generator class="native"/>
66
</id>
7-
<property name="RefId" column="SIF_ID" length="64"/>
8-
<property name="SessionToken" column="SESSION_TOKEN" length="64"/>
7+
<property name="RefId" column="REF_ID" length="200"/>
8+
<property name="OwnerId" column="OWNER_ID" length="200"/>
99
</class>
1010
</hibernate-mapping>

Code/Sif3Framework/Sif.Framework/Providers/FunctionalServiceProvider.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromUri]
106106

107107
if (SettingsManager.ProviderSettings.JobBinding)
108108
{
109-
service.Bind(getOwnerId(sessionToken), id);
109+
service.Bind(id, getOwnerId(sessionToken));
110110
}
111111

112112
jobType job = service.Retrieve(id, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]));
@@ -170,7 +170,7 @@ public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromBody]
170170

171171
if (SettingsManager.ProviderSettings.JobBinding)
172172
{
173-
service.Bind(getOwnerId(sessionToken), id);
173+
service.Bind(id, getOwnerId(sessionToken));
174174
}
175175

176176
creates.Add(ProviderUtils.CreateCreate(HttpStatusCode.Created, id.ToString(), job.id));
@@ -245,7 +245,7 @@ public virtual ICollection<jobType> Get([FromUri] string serviceName, [MatrixPar
245245
foreach (jobType job in jobs)
246246
{
247247
if (!SettingsManager.ProviderSettings.JobBinding
248-
|| service.IsBound(getOwnerId(sessionToken), Guid.Parse(job.id)))
248+
|| service.IsBound(Guid.Parse(job.id), getOwnerId(sessionToken)))
249249
{
250250
items.Add(job);
251251
}
@@ -284,7 +284,7 @@ public virtual HttpResponseMessage Get([FromUri] string serviceName, [FromUri] G
284284
item = service.Retrieve(id, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]));
285285

286286
if (SettingsManager.ProviderSettings.JobBinding
287-
&& !service.IsBound(getOwnerId(sessionToken), Guid.Parse(item.id)))
287+
&& !service.IsBound(Guid.Parse(item.id), getOwnerId(sessionToken)))
288288
{
289289
throw new InvalidSessionException("Request failed as one or more jobs referred to in this request do not belong to this consumer.");
290290
}
@@ -344,7 +344,7 @@ public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromUri
344344
IFunctionalService service = getService(serviceName);
345345

346346
if (SettingsManager.ProviderSettings.JobBinding
347-
&& !service.IsBound(getOwnerId(sessionToken), id))
347+
&& !service.IsBound(id, getOwnerId(sessionToken)))
348348
{
349349
throw new InvalidSessionException("Request failed as one or more jobs referred to in this request do not belong to this consumer.");
350350
}
@@ -384,9 +384,9 @@ public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromBod
384384
try
385385
{
386386
if (SettingsManager.ProviderSettings.JobBinding
387-
&& !service.IsBound(getOwnerId(sessionToken), Guid.Parse(deleteId.id)))
387+
&& !service.IsBound(Guid.Parse(deleteId.id), getOwnerId(sessionToken)))
388388
{
389-
throw new InvalidSessionException("Request failed as one or more jobs referred to in this request do not belong to this consumer.");
389+
throw new InvalidSessionException("Request failed as job does not belong to this consumer.");
390390
}
391391

392392
service.Delete(Guid.Parse(deleteId.id), zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]));
@@ -412,7 +412,7 @@ public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromBod
412412
}
413413
catch (InvalidSessionException e)
414414
{
415-
statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.BadRequest, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Request failed for object " + serviceName + " with ID of " + deleteId.id + ".\n " + e.Message)));
415+
statuses.Add(ProviderUtils.CreateDelete(HttpStatusCode.BadRequest, deleteId.id, ProviderUtils.CreateError(HttpStatusCode.BadRequest, serviceName, "Request failed for object " + serviceName + " with ID of " + deleteId.id + ", job doesn't belong to this consumer.\n " + e.Message)));
416416
}
417417
catch (Exception e)
418418
{
@@ -444,11 +444,11 @@ public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromUri]
444444
{
445445
IFunctionalService service = getService(serviceName);
446446
if (SettingsManager.ProviderSettings.JobBinding
447-
&& !service.IsBound(getOwnerId(sessionToken), id))
447+
&& !service.IsBound(id, getOwnerId(sessionToken)))
448448
{
449449
throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer.");
450450
}
451-
return OKResult(service.CreateToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.getContentType(Request), accept: HttpUtils.GetAccept(Request)));
451+
return OKResult(service.CreateToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.GetContentType(Request), accept: HttpUtils.GetAccept(Request)));
452452
}
453453
catch (ArgumentException e)
454454
{
@@ -485,11 +485,11 @@ public virtual HttpResponseMessage Get([FromUri] string serviceName, [FromUri] G
485485
{
486486
IFunctionalService service = getService(serviceName);
487487
if (SettingsManager.ProviderSettings.JobBinding
488-
&& !service.IsBound(getOwnerId(sessionToken), id))
488+
&& !service.IsBound(id, getOwnerId(sessionToken)))
489489
{
490490
throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer.");
491491
}
492-
return OKResult(service.RetrieveToPhase(id, phaseName, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.getContentType(Request), accept: HttpUtils.GetAccept(Request)));
492+
return OKResult(service.RetrieveToPhase(id, phaseName, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.GetContentType(Request), accept: HttpUtils.GetAccept(Request)));
493493
}
494494
catch (ArgumentException e)
495495
{
@@ -524,11 +524,11 @@ public virtual HttpResponseMessage Put([FromUri] string serviceName, [FromUri] G
524524
{
525525
IFunctionalService service = getService(serviceName);
526526
if (SettingsManager.ProviderSettings.JobBinding
527-
&& !service.IsBound(getOwnerId(sessionToken), id))
527+
&& !service.IsBound(id, getOwnerId(sessionToken)))
528528
{
529529
throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer.");
530530
}
531-
return OKResult(service.UpdateToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.getContentType(Request), accept: HttpUtils.GetAccept(Request)));
531+
return OKResult(service.UpdateToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.GetContentType(Request), accept: HttpUtils.GetAccept(Request)));
532532
}
533533
catch (ArgumentException e)
534534
{
@@ -565,11 +565,11 @@ public virtual HttpResponseMessage Delete([FromUri] string serviceName, [FromUri
565565
{
566566
IFunctionalService service = getService(serviceName);
567567
if (SettingsManager.ProviderSettings.JobBinding
568-
&& !service.IsBound(getOwnerId(sessionToken), id))
568+
&& !service.IsBound(id, getOwnerId(sessionToken)))
569569
{
570570
throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer.");
571571
}
572-
return OKResult(service.DeleteToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.getContentType(Request), accept: HttpUtils.GetAccept(Request)));
572+
return OKResult(service.DeleteToPhase(id, phaseName, body, zone: (zone == null ? null : zone[0]), context: (context == null ? null : context[0]), contentType: HttpUtils.GetContentType(Request), accept: HttpUtils.GetAccept(Request)));
573573
}
574574
catch (ArgumentException e)
575575
{
@@ -609,7 +609,7 @@ public virtual HttpResponseMessage Post([FromUri] string serviceName, [FromUri]
609609
{
610610
IFunctionalService service = getService(serviceName);
611611
if (SettingsManager.ProviderSettings.JobBinding
612-
&& !service.IsBound(getOwnerId(sessionToken), id))
612+
&& !service.IsBound(id, getOwnerId(sessionToken)))
613613
{
614614
throw new InvalidSessionException("Request failed as the job referred to in this request does not belong to this consumer.");
615615
}
@@ -787,7 +787,25 @@ protected HttpResponseMessage OKResult(string payload = null)
787787

788788
private String getOwnerId(String sessionToken)
789789
{
790-
return sessionToken;
790+
String ownerId = null;
791+
792+
switch (SettingsManager.ProviderSettings.EnvironmentType)
793+
{
794+
case EnvironmentType.DIRECT:
795+
// Application key is either in header or in session token
796+
ownerId = HttpUtils.GetApplicationKey(Request.Headers) ?? sessionToken;
797+
break;
798+
case EnvironmentType.BROKERED:
799+
// Application key must have been moved into sourceName property
800+
ownerId = HttpUtils.GetSourceName(Request.Headers);
801+
break;
802+
}
803+
804+
if (ownerId == null)
805+
{
806+
throw new NotFoundException("Could not identify consumer.");
807+
}
808+
return ownerId;
791809
}
792810
}
793811

Code/Sif3Framework/Sif.Framework/Service/Functional/FunctionalService.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,28 @@ public virtual void Shutdown()
7979
}
8080

8181
/// <summary>
82-
/// <see cref="IFunctionalService.Bind(string, Guid)"/>
82+
/// <see cref="IFunctionalService.Bind(Guid, string)"/>
8383
/// </summary>
84-
public virtual void Bind(string sessionToken, Guid objectId)
84+
public virtual void Bind(Guid objectId, string ownerId)
8585
{
8686
long id = bindings.Save(new SifObjectBinding()
8787
{
88-
SessionToken = sessionToken,
88+
OwnerId = ownerId,
8989
RefId = objectId
9090
});
91-
log.Info("Bound object " + objectId + " with session token " + sessionToken + ". ID = " + id);
91+
log.Info("Bound object " + objectId + " with session token " + ownerId + ". ID = " + id);
9292
}
9393

9494
/// <summary>
9595
/// <see cref="IFunctionalService.Unbind(string)"/>
9696
/// </summary>
97-
public virtual void Unbind(string sessionToken)
97+
public virtual void Unbind(string ownerId)
9898
{
9999
bindings.Delete(bindings.Retrieve(new SifObjectBinding()
100100
{
101-
SessionToken = sessionToken
101+
OwnerId = ownerId
102102
}));
103-
log.Info("Unbound all objects from the session token " + sessionToken);
103+
log.Info("Unbound all objects from the session token " + ownerId);
104104
}
105105

106106
/// <summary>
@@ -129,29 +129,29 @@ public virtual string GetBinding(Guid objectId)
129129
log.Debug("Could not find any bindings for the object " + objectId + ".");
130130
return null;
131131
}
132-
string sessionToken = candidates.FirstOrDefault().SessionToken;
132+
string sessionToken = candidates.FirstOrDefault().OwnerId;
133133
log.Info("Binding for object " + objectId + " is session token " + sessionToken + ".");
134134
return sessionToken;
135135
}
136136

137137
/// <summary>
138-
/// <see cref="IFunctionalService.IsBound(string, Guid)"/>
138+
/// <see cref="IFunctionalService.IsBound(Guid, string)"/>
139139
/// </summary>
140-
public virtual Boolean IsBound(string sessionToken, Guid objectId)
140+
public virtual Boolean IsBound(Guid objectId, string ownerId)
141141
{
142142
ICollection<SifObjectBinding> candidates = bindings.Retrieve(new SifObjectBinding()
143143
{
144-
SessionToken = sessionToken,
144+
OwnerId = ownerId,
145145
RefId = objectId
146146
});
147147
Boolean bound = candidates != null && candidates.Count > 0;
148148
if (bound)
149149
{
150-
log.Info("Found binding for object " + objectId + " with session token " + sessionToken + ".");
150+
log.Info("Found binding for object " + objectId + " with session token " + ownerId + ".");
151151
}
152152
else
153153
{
154-
log.Debug("Cound not find binding for object " + objectId + " with session token " + sessionToken + ".");
154+
log.Debug("Cound not find binding for object " + objectId + " with session token " + ownerId + ".");
155155
}
156156
return bound;
157157
}

Code/Sif3Framework/Sif.Framework/Service/Functional/IFunctionalService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ public interface IFunctionalService : ISifService<jobType, Job>
108108
/// <summary>
109109
/// Binds a (job) object's refid to a session token
110110
/// </summary>
111-
/// <param name="sessionToken">The session token the (job) object was created in</param>
111+
/// <param name="ownerId">The session token the (job) object was created in</param>
112112
/// <param name="objectId">The refid of the created object</param>
113-
void Bind(string sessionToken, Guid objectId);
113+
void Bind(Guid objectId, string ownerId);
114114

115115
/// <summary>
116116
/// Unbinds all (job) object refid from this session token
117117
/// </summary>
118-
/// <param name="sessionToken">The session token to unbind all objects from</param>
119-
void Unbind(string sessionToken);
118+
/// <param name="ownerId">The session token to unbind all objects from</param>
119+
void Unbind(string ownerId);
120120

121121
/// <summary>
122122
/// Unbinds the (job) object's refid from its associated session token
@@ -134,9 +134,9 @@ public interface IFunctionalService : ISifService<jobType, Job>
134134
/// <summary>
135135
/// Returns true if the (job) object refid is associated with the session token.
136136
/// </summary>
137-
/// <param name="sessionToken">The session token to look for</param>
137+
/// <param name="ownerId">The session token to look for</param>
138138
/// <param name="objectId">The refid of the (job) object to look for</param>
139139
/// <returns>See summary</returns>
140-
Boolean IsBound(string sessionToken, Guid objectId);
140+
Boolean IsBound(Guid objectId, string ownerId);
141141
}
142142
}

Code/Sif3Framework/Sif.Framework/Utils/HttpUtils.cs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ enum RequestHeader
4444
methodOverrideSif,
4545
mustUseAdvisory,
4646
navigationPage,
47-
navigationPageSize
47+
navigationPageSize,
48+
applicationKey,
49+
sourceName
4850
}
4951

50-
/*
51-
public static readonly string JobIdHeader = "jobId";
52-
*/
53-
5452
/// <summary>
5553
///
5654
/// </summary>
@@ -198,7 +196,7 @@ public static string DeleteRequest(string url, string authorisationToken, string
198196

199197
public static string DeleteRequest(string url, string authorisationToken, string body, string contentTypeOverride = null, string acceptOverride = null)
200198
{
201-
return RequestWithPayload(RequestMethod.DELETE, url, authorisationToken, body, contentTypeOverride, acceptOverride);
199+
return RequestWithPayload(RequestMethod.DELETE, url, authorisationToken, body, contentTypeOverride, acceptOverride);
202200
}
203201

204202
/// <summary>
@@ -480,6 +478,49 @@ internal static bool ValidatePagingParameters(HttpHeaders headers, out string er
480478
return string.IsNullOrWhiteSpace(errorMessage);
481479
}
482480

481+
/// <summary>
482+
/// Retrieve the applicationKey property from the header.
483+
/// </summary>
484+
/// <param name="headers">Request headers.</param>
485+
/// <returns>applicationKey value if set; null otherwise.</returns>
486+
internal static string GetApplicationKey(HttpHeaders headers)
487+
{
488+
return GetHeaderValue(headers, RequestHeader.applicationKey.ToDescription());
489+
}
490+
491+
/// <summary>
492+
/// Retrieve the sourceName property from the header.
493+
/// </summary>
494+
/// <param name="headers">Request headers.</param>
495+
/// <returns>sourceName value if set; null otherwise.</returns>
496+
internal static string GetSourceName(HttpHeaders headers)
497+
{
498+
return GetHeaderValue(headers, RequestHeader.sourceName.ToDescription());
499+
}
500+
501+
/// <summary>
502+
/// Gets the content type from the request headers.
503+
/// </summary>
504+
/// <param name="Request">HTTP Request</param>
505+
public static string GetContentType(HttpRequestMessage Request)
506+
{
507+
return Request.Content.Headers.ContentType.MediaType;
508+
}
509+
510+
/// <summary>
511+
/// Gets the accept type from the request headers.
512+
/// </summary>
513+
/// <param name="Request">HTTP Request</param>
514+
public static string GetAccept(HttpRequestMessage Request)
515+
{
516+
string[] values = (from a in Request.Headers.Accept select a.MediaType).ToArray();
517+
if (values == null || values.Length == 0)
518+
{
519+
return "plain/text";
520+
}
521+
return values[0];
522+
}
523+
483524
/// <summary>
484525
/// Build up a string of Matrix Parameters based upon the passed parameters.
485526
/// </summary>
@@ -502,22 +543,5 @@ public static string MatrixParameters(string zone = null, string context = null)
502543

503544
return matrixParameters;
504545
}
505-
506-
public static string GetAccept(HttpRequestMessage Request)
507-
{
508-
string[] values = (from a in Request.Headers.Accept select a.MediaType).ToArray();
509-
if(values == null || values.Length == 0)
510-
{
511-
return "plain/text";
512-
}
513-
return values[0];
514-
}
515-
516-
public static string getContentType(HttpRequestMessage Request)
517-
{
518-
return Request.Content.Headers.ContentType.MediaType;
519-
}
520-
521546
}
522-
523547
}
0 Bytes
Binary file not shown.
1 KB
Binary file not shown.

0 commit comments

Comments
 (0)