| 1 | using System; | 
|---|
| 2 | using System.IO; | 
|---|
| 3 | using System.Net; | 
|---|
| 4 | using AllocsFixes.JSON; | 
|---|
| 5 |  | 
|---|
| 6 | namespace AllocsFixes.NetConnections.Servers.Web.API { | 
|---|
| 7 | public abstract class AbsRestApi : AbsWebAPI { | 
|---|
| 8 | public sealed override void HandleRequest (RequestContext _context) { | 
|---|
| 9 | JSONNode jsonBody = null; | 
|---|
| 10 |  | 
|---|
| 11 | if (_context.Request.HasEntityBody) { | 
|---|
| 12 | string body = new StreamReader (_context.Request.InputStream).ReadToEnd (); | 
|---|
| 13 |  | 
|---|
| 14 | if (!string.IsNullOrEmpty (body)) { | 
|---|
| 15 | try { | 
|---|
| 16 | jsonBody = Parser.Parse (body); | 
|---|
| 17 | } catch (Exception e) { | 
|---|
| 18 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "INVALID_BODY", e); | 
|---|
| 19 | return; | 
|---|
| 20 | } | 
|---|
| 21 | } | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | try { | 
|---|
| 25 | switch (_context.Request.HttpMethod) { | 
|---|
| 26 | case "GET": | 
|---|
| 27 | if (jsonBody != null) { | 
|---|
| 28 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "GET_WITH_BODY"); | 
|---|
| 29 | return; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | HandleRestGet (_context); | 
|---|
| 33 | return; | 
|---|
| 34 | case "POST": | 
|---|
| 35 | if (!string.IsNullOrEmpty (_context.RequestPath)) { | 
|---|
| 36 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "POST_WITH_ID"); | 
|---|
| 37 | return; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | if (jsonBody == null) { | 
|---|
| 41 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "POST_WITHOUT_BODY"); | 
|---|
| 42 | return; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | HandleRestPost (_context, jsonBody); | 
|---|
| 46 | return; | 
|---|
| 47 | case "PUT": | 
|---|
| 48 | if (string.IsNullOrEmpty (_context.RequestPath)) { | 
|---|
| 49 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "PUT_WITHOUT_ID"); | 
|---|
| 50 | return; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | if (jsonBody == null) { | 
|---|
| 54 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "PUT_WITHOUT_BODY"); | 
|---|
| 55 | return; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | HandleRestPut (_context, jsonBody); | 
|---|
| 59 | return; | 
|---|
| 60 | case "DELETE": | 
|---|
| 61 | if (string.IsNullOrEmpty (_context.RequestPath)) { | 
|---|
| 62 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, jsonBody, "DELETE_WITHOUT_ID"); | 
|---|
| 63 | return; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | if (jsonBody != null) { | 
|---|
| 67 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "DELETE_WITH_BODY"); | 
|---|
| 68 | return; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | HandleRestDelete (_context); | 
|---|
| 72 | return; | 
|---|
| 73 | default: | 
|---|
| 74 | SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, null, "INVALID_METHOD"); | 
|---|
| 75 | return; | 
|---|
| 76 | } | 
|---|
| 77 | } catch (Exception e) { | 
|---|
| 78 | SendEnvelopedResult (_context, null, HttpStatusCode.InternalServerError, jsonBody, "ERROR_PROCESSING", _exception: e); | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | private static readonly JSONArray emptyData = new JSONArray (); | 
|---|
| 83 |  | 
|---|
| 84 | protected void SendEnvelopedResult (RequestContext _context, JSONNode _resultData, HttpStatusCode _statusCode = HttpStatusCode.OK, | 
|---|
| 85 | JSONNode _jsonInputBody = null, string _errorCode = null, Exception _exception = null) { | 
|---|
| 86 | JSONObject meta = new JSONObject (); | 
|---|
| 87 |  | 
|---|
| 88 | meta.Add ("serverTime", new JSONString (DateTime.Now.ToString ("o"))); | 
|---|
| 89 | if (!string.IsNullOrEmpty (_errorCode)) { | 
|---|
| 90 | meta.Add ("requestMethod", new JSONString (_context.Request.HttpMethod)); | 
|---|
| 91 | meta.Add ("requestSubpath", new JSONString (_context.RequestPath)); | 
|---|
| 92 | meta.Add ("requestBody", new JSONString (_jsonInputBody?.ToString () ?? "-empty-")); | 
|---|
| 93 | meta.Add ("errorCode", new JSONString (_errorCode)); | 
|---|
| 94 | if (_exception != null) { | 
|---|
| 95 | meta.Add ("exceptionMessage", new JSONString (_exception.Message)); | 
|---|
| 96 | meta.Add ("exceptionTrace", new JSONString (_exception.StackTrace)); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | JSONObject envelope = new JSONObject (); | 
|---|
| 101 | envelope.Add ("meta", meta); | 
|---|
| 102 | envelope.Add ("data", _resultData ?? emptyData); | 
|---|
| 103 |  | 
|---|
| 104 | WebUtils.WriteJson (_context.Response, envelope, _statusCode); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | protected abstract void HandleRestGet (RequestContext _context); | 
|---|
| 108 |  | 
|---|
| 109 | protected abstract void HandleRestPost (RequestContext _context, JSONNode _jsonBody); | 
|---|
| 110 |  | 
|---|
| 111 | protected abstract void HandleRestPut (RequestContext _context, JSONNode _jsonBody); | 
|---|
| 112 |  | 
|---|
| 113 | protected abstract void HandleRestDelete (RequestContext _context); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|