예제 #1
0
        public static void ITEM_EXISTS <TId, TItem>(this HTTPServer HTTPServer,
                                                    HTTPPath UriTemplate,
                                                    ParseIdDelegate <TId> ParseIdDelegate,
                                                    Func <String, String> ParseIdError,
                                                    TryGetItemDelegate <TId, TItem> TryGetItemDelegate,
                                                    ItemFilterDelegate <TItem> ItemFilterDelegate,
                                                    Func <TId, String> TryGetItemError,
                                                    String HTTPServerName = HTTPServer.DefaultHTTPServerName)
        {
            HTTPServer.AddMethodCallback(HTTPHostname.Any,
                                         HTTPMethod.EXISTS,
                                         UriTemplate,
                                         HTTPContentType.JSON_UTF8,
                                         HTTPDelegate: async Request => {
                if (!ParseIdDelegate(Request.ParsedURLParameters[0], out TId Id))
                {
                    return new HTTPResponse.Builder(Request)
                    {
                        HTTPStatusCode            = HTTPStatusCode.BadRequest,
                        Server                    = HTTPServerName,
                        Date                      = Timestamp.Now,
                        AccessControlAllowOrigin  = "*",
                        AccessControlAllowMethods = "GET, EXISTS, COUNT",
                        AccessControlAllowHeaders = "Content-Type, Accept, Authorization",
                        ETag                      = "1",
                        ContentType               = HTTPContentType.JSON_UTF8,
                        Content                   = ErrorMessage(ParseIdError(Request.ParsedURLParameters[0])).ToUTF8Bytes(),
                        CacheControl              = "no-cache",
                        Connection                = "close"
                    }
                }
                ;

                if (!TryGetItemDelegate(Id, out TItem Item) || !ItemFilterDelegate(Item))
                {
                    return new HTTPResponse.Builder(Request)
                    {
                        HTTPStatusCode = HTTPStatusCode.NotFound,
                        Server         = HTTPServerName,
                        Date           = Timestamp.Now,
                        ContentType    = HTTPContentType.JSON_UTF8,
                        Content        = ErrorMessage(TryGetItemError(Id)).ToUTF8Bytes(),
                        CacheControl   = "no-cache",
                        Connection     = "close"
                    }
                }
                ;

                return(new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server = HTTPServerName,
                    Date = Timestamp.Now,
                    ETag = "1",
                    CacheControl = "public",
                    //Expires         = "Mon, 25 Jun 2015 21:31:12 GMT",
                    Connection = "close"
                });
            });
        }
예제 #2
0
        public static void ITEM_GET <TId, TItem>(this HTTPServer HTTPServer,
                                                 HTTPPath UriTemplate,
                                                 ParseIdDelegate <TId> ParseIdDelegate,
                                                 Func <String, String> ParseIdError,
                                                 TryGetItemDelegate <TId, TItem> TryGetItemDelegate,
                                                 ItemFilterDelegate <TItem> ItemFilterDelegate,
                                                 Func <TId, String> TryGetItemError,
                                                 ItemToJSONDelegate <TItem> ToJSONDelegate)
        {
            HTTPServer.AddMethodCallback(HTTPHostname.Any,
                                         HTTPMethod.GET,
                                         UriTemplate,
                                         HTTPContentType.JSON_UTF8,
                                         HTTPDelegate: async Request => {
                TId Id;
                TItem Item;

                if (!ParseIdDelegate(Request.ParsedURLParameters[0], out Id))
                {
                    return new HTTPResponse.Builder(Request)
                    {
                        HTTPStatusCode = HTTPStatusCode.BadRequest,
                        Server         = HTTPServer.DefaultServerName,
                        ContentType    = HTTPContentType.JSON_UTF8,
                        Content        = ErrorMessage(ParseIdError(Request.ParsedURLParameters[0])).ToUTF8Bytes(),
                        CacheControl   = "no-cache",
                        Connection     = "close"
                    }
                }
                ;

                if (!TryGetItemDelegate(Id, out Item) || !ItemFilterDelegate(Item))
                {
                    return new HTTPResponse.Builder(Request)
                    {
                        HTTPStatusCode = HTTPStatusCode.NotFound,
                        Server         = HTTPServer.DefaultServerName,
                        ContentType    = HTTPContentType.JSON_UTF8,
                        Content        = ErrorMessage(TryGetItemError(Id)).ToUTF8Bytes(),
                        CacheControl   = "no-cache",
                        Connection     = "close"
                    }
                }
                ;

                return(new HTTPResponse.Builder(Request)
                {
                    HTTPStatusCode = HTTPStatusCode.OK,
                    Server = HTTPServer.DefaultServerName,
                    ContentType = HTTPContentType.JSON_UTF8,
                    Content = ToJSONDelegate(Item).ToUTF8Bytes(),
                    ETag = "1",
                    CacheControl = "public",
                    //Expires         = "Mon, 25 Jun 2015 21:31:12 GMT",
                    Connection = "close"
                });
            });
        }