ae1612a3fde9cd911d3b6cad48713f2789ed6b29
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / LiteResource.cs
1  /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 namespace Tizen.Network.IoTConnectivity
19 {
20     /// <summary>
21     /// This class represents a lite resource.
22     /// It provides APIs to encapsulate resources.
23     /// This class is accessed by using a constructor to create a new instance of this object.
24     /// </summary>
25     public class LiteResource : Resource
26     {
27         /// <summary>
28         /// The LiteResource constructor
29         /// </summary>
30         /// <remarks>
31         /// Creates a lite resource which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource()"/>.\n
32         /// When client requests some operations, it send a response to client, automatically.\n
33         /// @a uri length must be less than 128.
34         /// </remarks>
35         /// <privilege>
36         /// http://tizen.org/privilege/internet
37         /// </privilege>
38         /// <param name="uri">The uri path of the lite resource</param>
39         /// <param name="types">The type of the resource</param>
40         /// <param name="policy">Policy of the resource</param>
41         /// <param name="attribs">Optional attributes of the resource</param>
42         /// <pre>
43         /// IoTConnectivityServerManager.Initialize() should be called to initialize
44         /// </pre>
45         /// <seealso cref="ResourceTypes"/>
46         /// <seealso cref="ResourcePolicy"/>
47         /// <seealso cref="Attributes"/>
48         /// <code>
49         /// List<string> list = new List<string>() { "org.tizen.light" };
50         /// Attributes attributes = new Attributes() {
51         ///     { "state", "ON" }
52         /// };
53         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
54         /// </code>
55         public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
56             : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
57         {
58             Attributes = attribs;
59         }
60
61         /// <summary>
62         /// Gets or sets the attributes of the lite resource
63         /// </summary>
64         /// <code>
65         /// List<string> list = new List<string>() { "org.tizen.light" };
66         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
67         /// Attributes attributes = new Attributes() {
68         ///     { "state", "ON" }
69         /// };
70         /// res.Attributes = newAttributes;
71         /// foreach (KeyValuePair<string, object> pair in res.Attributes)
72         /// {
73         ///     Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
74         /// }
75         /// </code>
76         public Attributes Attributes { get; set; }
77
78         /// <summary>
79         /// Decides whether to accept or reject a post request.
80         /// </summary>
81         /// <remarks>
82         /// Child classes of this class can override this method to accept or reject post request.
83         /// </remarks>
84         /// <param name="attribs">The new attributes of the lite resource</param>
85         /// <returns>true to accept post request, false to reject it</returns>
86         /// <code>
87         /// public class MyLightResource : LiteResource
88         /// {
89         ///     protected override bool OnPost(Attributes attributes)
90         ///     {
91         ///         object newAttributes;
92         ///         attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
93         ///         if((int)newAttributes == 1)
94         ///             return true;
95         ///         return false;
96         ///     }
97         /// }
98         /// </code>
99         protected virtual bool OnPost(Attributes attribs)
100         {
101             return true;
102         }
103
104         // The code block untill @endcond should not appear in doxygen spec.
105         /// @cond
106         protected sealed override Response OnGet(Request request)
107         {
108             Representation representation = new Representation()
109             {
110                 UriPath = UriPath,
111                 Interface = Interfaces,
112                 Type = Types,
113                 Attributes = Attributes
114             };
115
116             Response response = new Response()
117             {
118                 Representation = representation,
119                 Result = ResponseCode.Ok
120             };
121
122             return response;
123         }
124
125         protected sealed override Response OnPut(Request request)
126         {
127             Response response = new Response();
128             response.Result = ResponseCode.Forbidden;
129             return response;
130         }
131
132         protected sealed override Response OnPost(Request request)
133         {
134             if (OnPost(request.Representation.Attributes))
135             {
136                 Attributes = request.Representation.Attributes;
137                 Representation representation = new Representation() {
138                     UriPath = UriPath,
139                     Interface = Interfaces,
140                     Type = Types,
141                     Attributes = Attributes
142                 };
143
144                 Response response = new Response() {
145                     Representation = representation,
146                     Result = ResponseCode.Ok
147                 };
148
149                 Notify(representation, QualityOfService.High);
150                 return response;
151             }
152
153             return new Response()
154             {
155                 Result = ResponseCode.Error
156             };
157         }
158
159         protected sealed override Response OnDelete(Request request)
160         {
161             Response response = new Response();
162             response.Result = ResponseCode.Forbidden;
163             return response;
164         }
165
166         protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
167         {
168             return true;
169         }
170         /// @endcond
171     }
172 }