using putcondition methods
2007-09-17 @ 23:08#
the last post was a bit long (and vague), so i figured i'd post a follow up here. with the new CheckPut... utility methods, i can clean up my resource code that handles PUTs. below is an example from my FileHandler class":
Requestor req = new Requestor();
ExyusPrincipal ep = (ExyusPrincipal)this.Context.User;
req.Credentials = new NetworkCredential(((ExyusIdentity)ep.Identity).Name, ((ExyusIdentity)ep.Identity).Password);
// load headers for request
PutHeaders ph = new PutHeaders(this.Context);
if (ph.IfMatch != string.Empty)
req.RequestHeaders.Add(Constants.hdr_if_none_match, ph.IfMatch);
if (ph.IfUnmodifiedSince != string.Empty)
req.RequestHeaders.Add(Constants.hdr_if_modified_since, ph.IfUnmodifiedSince);
// make request for existing resource
try
{
out_text = req.Execute(
string.Format("{0}://{1}{2}",
this.Context.Request.Url.Scheme,
this.Context.Request.Url.DnsSafeHost,
this.Context.Request.RawUrl),
"head", "text/xml");
// record exists, this must be an update
etag = util.GetHttpHeader(Constants.hdr_etag, (NameValueCollection)req.ResponseHeaders);
last_mod = util.GetHttpHeader(Constants.hdr_last_modified, (NameValueCollection)req.ResponseHeaders);
// sort out update conditions
util.CheckPutUpdateCondition(ph, etag, last_mod, ref put_error, ref save_item);
}
catch (HttpException hex2)
{
// record not there or some other error
int code = hex2.GetHttpCode();
switch (code)
{
// record exists w/o changes, we can update!
case (int)HttpStatusCode.NotModified:
save_item = true;
break;
// see if it's ok to save
case (int)HttpStatusCode.NotFound:
// sort out create conditions
util.CheckPutCreateCondition(ph, this.AllowCreateOnPut, etag, ref put_error, ref save_item);
break;
// some other error, omgz!
default:
put_error = hex2.Message + " Unable to create.";
save_item = false;
break;
}
}