|
using System;
using System.Net;
using Raven.Database;
using Raven.Database.Data;
namespace Raven.Server.Responders
{
public class Document : RequestResponder
{
public override string UrlPattern
{
get { return @"/docs/(.+)"; }
}
public override string[] SupportedVerbs
{
get { return new[] {"GET", "DELETE", "PUT", "PATCH"}; }
}
public override void Respond(HttpListenerContext context)
{
var match = urlMatcher.Match(context.Request.Url.LocalPath);
var docId = match.Groups[1].Value;
switch (context.Request.HttpMethod)
{
case "GET":
context.Response.Headers["Content-Type"] = "application/json; charset=utf-8";
var doc = Database.Get(docId,GetRequestTransaction(context));
if (doc == null)
{
context.SetStatusToNotFound();
return;
}
if (context.MatchEtag(doc.Etag))
{
context.SetStatusToNotModified();
return;
}
context.WriteData(doc.Data, doc.Metadata, doc.Etag);
break;
case "DELETE":
Database.Delete(docId, context.GetEtag(), GetRequestTransaction(context));
context.SetStatusToDeleted();
break;
case "PUT":
Put(context, docId);
break;
case "PATCH":
var patchDoc = context.ReadJsonArray();
var patchResult = Database.ApplyPatch(docId, context.GetEtag(), patchDoc, GetRequestTransaction(context));
switch (patchResult)
{
case PatchResult.DocumentDoesNotExists:
context.SetStatusToNotFound();
break;
case PatchResult.Patched:
context.WriteJson(new {patched = true});
break;
default:
throw new ArgumentOutOfRangeException("Value " + patchResult + " is not understood");
}
break;
}
}
private void Put(HttpListenerContext context, string docId)
{
var json = context.ReadJson();
context.SetStatusToCreated("/docs/" + docId);
var putResult = Database.Put(docId, context.GetEtag(), json, context.Request.Headers.FilterHeaders(), GetRequestTransaction(context));
context.WriteJson(putResult);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.