Skip to content

Commit 3520a67

Browse files
committed
Changes from Dmitry
1 parent 6da9e89 commit 3520a67

4 files changed

Lines changed: 133 additions & 111 deletions

File tree

Saleslogix.SData.Client/Framework/IAuthenticator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ namespace Saleslogix.SData.Client.Framework
77
public interface IAuthenticator
88
{
99
void Authenticate(WebRequest request);
10+
///invoked in response to 401
1011
UnauthorizedAction Unauthorized(WebResponse response);
1112
}
1213

14+
public interface IAuthenticator2 : IAuthenticator
15+
{
16+
///invoked in response to 403
17+
UnauthorizedAction Forbidden(WebResponse response);
18+
}
19+
1320
public enum UnauthorizedAction
1421
{
1522
Throw,
1623
Retry
1724
}
18-
}
25+
}

Saleslogix.SData.Client/Framework/SDataRequest.cs

Lines changed: 89 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,22 @@ public virtual SDataResponse GetResponse()
215215
attempts--;
216216
continue;
217217
}
218-
if (ex.Status == WebExceptionStatus.ProtocolError &&
219-
((HttpWebResponse) ex.Response).StatusCode == HttpStatusCode.Unauthorized &&
220-
Authenticator != null && Authenticator.Unauthorized(ex.Response) == UnauthorizedAction.Retry)
218+
if (ex.Status == WebExceptionStatus.ProtocolError && Authenticator != null)
221219
{
222-
continue;
220+
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Unauthorized)
221+
{
222+
//401
223+
if (Authenticator.Unauthorized(ex.Response) == UnauthorizedAction.Retry) continue;
224+
}
225+
else if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Forbidden)
226+
{
227+
IAuthenticator2 vAuthenticator2 = Authenticator as IAuthenticator2;
228+
if (vAuthenticator2 != null)
229+
{
230+
//403
231+
if (vAuthenticator2.Forbidden(ex.Response) == UnauthorizedAction.Retry) continue;
232+
}
233+
}
223234
}
224235
throw new SDataException(ex);
225236
}
@@ -257,87 +268,87 @@ public virtual IAsyncResult BeginGetResponse(AsyncCallback callback, object stat
257268
Action getResponse = null;
258269
Action loop =
259270
() =>
271+
{
272+
MediaType? contentType;
273+
object content;
274+
_request = CreateRequest(uri, out contentType, out content);
275+
if (content != null || Form.Count > 0 || Files.Count > 0)
260276
{
261-
MediaType? contentType;
262-
object content;
263-
_request = CreateRequest(uri, out contentType, out content);
264-
if (content != null || Form.Count > 0 || Files.Count > 0)
265-
{
266-
_request.BeginGetRequestStream(
267-
async =>
277+
_request.BeginGetRequestStream(
278+
async =>
279+
{
280+
try
281+
{
282+
using (var stream = _request.EndGetRequestStream(async))
268283
{
269-
try
270-
{
271-
using (var stream = _request.EndGetRequestStream(async))
272-
{
273-
WriteRequestContent(contentType, content, stream);
274-
}
275-
TraceRequest(_request);
276-
getResponse();
277-
}
278-
catch (Exception ex)
279-
{
280-
result.Failure(ex, async.CompletedSynchronously);
281-
_request = null;
282-
Interlocked.Exchange(ref _state, 0);
283-
}
284-
}, null);
285-
}
286-
else
287-
{
288-
TraceRequest(_request);
289-
getResponse();
290-
}
291-
};
284+
WriteRequestContent(contentType, content, stream);
285+
}
286+
TraceRequest(_request);
287+
getResponse();
288+
}
289+
catch (Exception ex)
290+
{
291+
result.Failure(ex, async.CompletedSynchronously);
292+
_request = null;
293+
Interlocked.Exchange(ref _state, 0);
294+
}
295+
}, null);
296+
}
297+
else
298+
{
299+
TraceRequest(_request);
300+
getResponse();
301+
}
302+
};
292303
getResponse =
293304
() => _request.BeginGetResponse(
294305
async =>
306+
{
307+
try
295308
{
309+
WebResponse response;
296310
try
297311
{
298-
WebResponse response;
299-
try
300-
{
301-
response = _request.EndGetResponse(async);
302-
TraceResponse(response);
303-
}
304-
catch (WebException webEx)
305-
{
312+
response = _request.EndGetResponse(async);
313+
TraceResponse(response);
314+
}
315+
catch (WebException webEx)
316+
{
306317
#if PCL || NETFX_CORE || SILVERLIGHT
307318
if (attempts > 0)
308319
#else
309-
if (webEx.Status == WebExceptionStatus.Timeout && attempts > 0)
320+
if (webEx.Status == WebExceptionStatus.Timeout && attempts > 0)
310321
#endif
311-
{
312-
attempts--;
313-
loop();
314-
return;
315-
}
316-
throw new SDataException(webEx);
317-
}
318-
319-
var httpResponse = response as HttpWebResponse;
320-
var statusCode = httpResponse != null ? httpResponse.StatusCode : 0;
321-
322-
if (statusCode != HttpStatusCode.Found)
323322
{
324-
result.Success(new SDataResponse(response, location), async.CompletedSynchronously);
325-
_request = null;
326-
Interlocked.Exchange(ref _state, 0);
327-
}
328-
else
329-
{
330-
uri = location = response.Headers["Location"];
323+
attempts--;
331324
loop();
325+
return;
332326
}
327+
throw new SDataException(webEx);
333328
}
334-
catch (Exception ex)
329+
330+
var httpResponse = response as HttpWebResponse;
331+
var statusCode = httpResponse != null ? httpResponse.StatusCode : 0;
332+
333+
if (statusCode != HttpStatusCode.Found)
335334
{
336-
result.Failure(ex, async.CompletedSynchronously);
335+
result.Success(new SDataResponse(response, location), async.CompletedSynchronously);
337336
_request = null;
338337
Interlocked.Exchange(ref _state, 0);
339338
}
340-
}, null);
339+
else
340+
{
341+
uri = location = response.Headers["Location"];
342+
loop();
343+
}
344+
}
345+
catch (Exception ex)
346+
{
347+
result.Failure(ex, async.CompletedSynchronously);
348+
_request = null;
349+
Interlocked.Exchange(ref _state, 0);
350+
}
351+
}, null);
341352
try
342353
{
343354
loop();
@@ -356,7 +367,7 @@ public virtual SDataResponse EndGetResponse(IAsyncResult asyncResult)
356367
Guard.ArgumentIsType<AsyncResult<SDataResponse>>(asyncResult, "asyncResult");
357368
try
358369
{
359-
return ((AsyncResult<SDataResponse>) asyncResult).End();
370+
return ((AsyncResult<SDataResponse>)asyncResult).End();
360371
}
361372
finally
362373
{
@@ -381,7 +392,7 @@ private WebRequest CreateRequest(string uri, out MediaType? contentType, out obj
381392
methodOverride = true;
382393
contentType = MediaType.Text;
383394
content = uri;
384-
uri = new SDataUri(uri) {Query = null}.ToString();
395+
uri = new SDataUri(uri) { Query = null }.ToString();
385396
}
386397
else
387398
{
@@ -531,18 +542,18 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
531542
{
532543
if (contentType != null)
533544
{
534-
var part = new MimePart(requestStream) {ContentType = MediaTypeNames.GetMediaType(contentType.Value)};
545+
var part = new MimePart(requestStream) { ContentType = MediaTypeNames.GetMediaType(contentType.Value) };
535546
multipart.Add(part);
536547
}
537548

538549
foreach (var data in Form)
539550
{
540551
var part = new MimePart(new MemoryStream(Encoding.UTF8.GetBytes(data.Value)))
541-
{
542-
ContentType = MediaTypeNames.TextMediaType,
543-
ContentTransferEncoding = "binary",
544-
ContentDisposition = "inline; name=" + data.Key
545-
};
552+
{
553+
ContentType = MediaTypeNames.TextMediaType,
554+
ContentTransferEncoding = "binary",
555+
ContentDisposition = "inline; name=" + data.Key
556+
};
546557
multipart.Add(part);
547558
}
548559

@@ -557,11 +568,11 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
557568
}
558569

559570
var part = new MimePart(file.Stream)
560-
{
561-
ContentType = file.ContentType ?? "application/octet-stream",
562-
ContentTransferEncoding = "binary",
563-
ContentDisposition = contentDisposition
564-
};
571+
{
572+
ContentType = file.ContentType ?? "application/octet-stream",
573+
ContentTransferEncoding = "binary",
574+
ContentDisposition = contentDisposition
575+
};
565576
multipart.Add(part);
566577
}
567578

Saleslogix.SData.Client/ISDataClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ public interface ISDataClient
4242
Task<ISDataResults<T>> ExecuteAsync<T>(SDataParameters parms, CancellationToken cancel = default(CancellationToken));
4343
Task<ISDataResults<IList<T>>> ExecuteBatchAsync<T>(IList<SDataParameters> items, CancellationToken cancel = default(CancellationToken));
4444
#endif
45+
CookieContainer Cookies { get; set; }
4546
}
4647
}

Saleslogix.SData.Client/SDataClient.cs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class SDataClient : ISDataClient
2929
#endif
3030

3131
private readonly Func<string, SDataRequest> _requestFactory;
32-
private readonly CookieContainer _cookies = new CookieContainer();
3332

3433
public SDataClient(string uri)
3534
: this(uri, targetUri => new SDataRequest(targetUri))
@@ -45,6 +44,10 @@ internal SDataClient(string uri, Func<string, SDataRequest> requestFactory)
4544
MaxGetUriLength = 2000;
4645
}
4746

47+
//private readonly CookieContainer _cookies = new CookieContainer();
48+
private CookieContainer _cookies = new CookieContainer();
49+
public CookieContainer Cookies { get { return _cookies; } set { _cookies = value; } }
50+
4851
public string Uri { get; set; }
4952
public string UserName { get; set; }
5053
public string Password { get; set; }
@@ -141,22 +144,22 @@ private SDataRequest CreateRequest(SDataParameters parms, bool responseContentIg
141144
Guard.ArgumentNotNull(parms, "parms");
142145

143146
var uri = new SDataUri(Uri)
144-
{
145-
StartIndex = parms.StartIndex,
146-
Count = parms.Count,
147-
Where = parms.Where,
148-
OrderBy = parms.OrderBy,
149-
Search = parms.Search,
150-
Include = parms.Include,
151-
Select = parms.Select,
152-
Precedence = parms.Precedence,
153-
IncludeSchema = parms.IncludeSchema,
154-
ReturnDelta = parms.ReturnDelta,
155-
TrackingId = parms.TrackingId,
156-
Format = parms.Format,
157-
Language = parms.Language,
158-
Version = parms.Version
159-
};
147+
{
148+
StartIndex = parms.StartIndex,
149+
Count = parms.Count,
150+
Where = parms.Where,
151+
OrderBy = parms.OrderBy,
152+
Search = parms.Search,
153+
Include = parms.Include,
154+
Select = parms.Select,
155+
Precedence = parms.Precedence,
156+
IncludeSchema = parms.IncludeSchema,
157+
ReturnDelta = parms.ReturnDelta,
158+
TrackingId = parms.TrackingId,
159+
Format = parms.Format,
160+
Language = parms.Language,
161+
Version = parms.Version
162+
};
160163
if (parms.Method != HttpMethod.Delete)
161164
{
162165
if (uri.Precedence == null && responseContentIgnored)
@@ -371,11 +374,11 @@ private SDataRequest CreateBatchRequest(ICollection<SDataParameters> items)
371374
}
372375

373376
var itemUri = new SDataUri(Uri)
374-
{
375-
Include = parms.Include,
376-
Select = parms.Select,
377-
Precedence = parms.Precedence
378-
};
377+
{
378+
Include = parms.Include,
379+
Select = parms.Select,
380+
Precedence = parms.Precedence
381+
};
379382
if (path != null)
380383
{
381384
itemUri.AppendPath(path);
@@ -394,12 +397,12 @@ private SDataRequest CreateBatchRequest(ICollection<SDataParameters> items)
394397
}
395398

396399
var uri = new SDataUri(Uri)
397-
{
398-
Precedence = precedence,
399-
Format = format ?? Format,
400-
Language = language ?? Language,
401-
Version = version ?? Version
402-
};
400+
{
401+
Precedence = precedence,
402+
Format = format ?? Format,
403+
Language = language ?? Language,
404+
Version = version ?? Version
405+
};
403406
if (path != null)
404407
{
405408
uri.AppendPath(path);
@@ -477,15 +480,15 @@ private ISDataResults<T> CreateResults<T>(SDataResponse response)
477480
TraceResponse(response);
478481

479482
object obj = null;
480-
if (typeof (T) == typeof (byte[]))
483+
if (typeof(T) == typeof(byte[]))
481484
{
482485
var str = response.Content as string;
483486
if (str != null)
484487
{
485488
obj = Encoding.UTF8.GetBytes(str);
486489
}
487490
}
488-
else if (typeof (T) == typeof (string))
491+
else if (typeof(T) == typeof(string))
489492
{
490493
var data = response.Content as byte[];
491494
if (data != null)
@@ -494,7 +497,7 @@ private ISDataResults<T> CreateResults<T>(SDataResponse response)
494497
}
495498
}
496499
#if !PCL && !NETFX_CORE && !SILVERLIGHT
497-
else if (typeof (T) == typeof (SDataSchema))
500+
else if (typeof(T) == typeof(SDataSchema))
498501
{
499502
var str = response.Content as string;
500503
if (str != null)
@@ -509,7 +512,7 @@ private ISDataResults<T> CreateResults<T>(SDataResponse response)
509512
T content;
510513
if (obj != null)
511514
{
512-
content = (T) obj;
515+
content = (T)obj;
513516
}
514517
else
515518
{

0 commit comments

Comments
 (0)