Release 4.0.0-preview1-00051
[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     /// <since_tizen> 3 </since_tizen>
26     public class LiteResource : Resource
27     {
28         /// <summary>
29         /// The LiteResource constructor.
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <remarks>
33         /// Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource()"/>.\n
34         /// When client requests some operations, it sends a response to client automatically.\n
35         /// @a uri length must be less than 128.
36         /// </remarks>
37         /// <privilege>
38         /// http://tizen.org/privilege/internet
39         /// </privilege>
40         /// <privlevel>public</privlevel>
41         /// <param name="uri">The uri path of the lite resource.</param>
42         /// <param name="types">The type of the resource.</param>
43         /// <param name="policy">Policy of the resource.</param>
44         /// <param name="attribs">Optional attributes of the resource.</param>
45         /// <feature>http://tizen.org/feature/iot.ocf</feature>
46         /// <pre>
47         /// IoTConnectivityServerManager.Initialize() should be called to initialize.
48         /// </pre>
49         /// <seealso cref="ResourceTypes"/>
50         /// <seealso cref="ResourcePolicy"/>
51         /// <seealso cref="Attributes"/>
52         /// <code>
53         /// List<string> list = new List<string>() { "org.tizen.light" };
54         /// Attributes attributes = new Attributes() {
55         ///     { "state", "ON" }
56         /// };
57         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
58         /// </code>
59         public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
60             : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
61         {
62             Attributes = attribs;
63         }
64
65         /// <summary>
66         /// Gets or sets the attributes of the lite resource.
67         /// </summary>
68         /// <since_tizen> 3 </since_tizen>
69         /// <value>The attributes of the lite resource.</value>
70         /// <code>
71         /// List<string> list = new List<string>() { "org.tizen.light" };
72         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
73         /// Attributes attributes = new Attributes() {
74         ///     { "state", "ON" }
75         /// };
76         /// res.Attributes = newAttributes;
77         /// foreach (KeyValuePair<string, object> pair in res.Attributes)
78         /// {
79         ///     Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
80         /// }
81         /// </code>
82         public Attributes Attributes { get; set; }
83
84         /// <summary>
85         /// Decides whether to accept or reject a post request.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88         /// <remarks>
89         /// Child classes of this class can override this method to accept or reject post request.
90         /// </remarks>
91         /// <param name="attribs">The new attributes of the lite resource.</param>
92         /// <returns>true to accept post request, false to reject it.</returns>
93         /// <code>
94         /// public class MyLightResource : LiteResource
95         /// {
96         ///     protected override bool OnPost(Attributes attributes)
97         ///     {
98         ///         object newAttributes;
99         ///         attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
100         ///         if((int)newAttributes == 1)
101         ///             return true;
102         ///         return false;
103         ///     }
104         /// }
105         /// </code>
106         protected virtual bool OnPost(Attributes attribs)
107         {
108             return true;
109         }
110
111         // The code block untill @endcond should not appear in doxygen spec.
112         /// @cond
113         protected sealed override Response OnGet(Request request)
114         {
115             Representation representation = new Representation()
116             {
117                 UriPath = UriPath,
118                 Interface = Interfaces,
119                 Type = Types,
120                 Attributes = Attributes
121             };
122
123             Response response = new Response()
124             {
125                 Representation = representation,
126                 Result = ResponseCode.Ok
127             };
128
129             return response;
130         }
131
132         protected sealed override Response OnPut(Request request)
133         {
134             Response response = new Response();
135             response.Result = ResponseCode.Forbidden;
136             return response;
137         }
138
139         protected sealed override Response OnPost(Request request)
140         {
141             if (OnPost(request.Representation.Attributes))
142             {
143                 Attributes = request.Representation.Attributes;
144                 Representation representation = new Representation() {
145                     UriPath = UriPath,
146                     Interface = Interfaces,
147                     Type = Types,
148                     Attributes = Attributes
149                 };
150
151                 Response response = new Response() {
152                     Representation = representation,
153                     Result = ResponseCode.Ok
154                 };
155
156                 Notify(representation, QualityOfService.High);
157                 return response;
158             }
159
160             return new Response()
161             {
162                 Result = ResponseCode.Error
163             };
164         }
165
166         protected sealed override Response OnDelete(Request request)
167         {
168             Response response = new Response();
169             response.Result = ResponseCode.Forbidden;
170             return response;
171         }
172
173         protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
174         {
175             return true;
176         }
177         /// @endcond
178     }
179 }