34d041800855e97d4b8ccdc8fb87407c963fbfbe
[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 using System;
18 using System.ComponentModel;
19
20 namespace Tizen.Network.IoTConnectivity
21 {
22     /// <summary>
23     /// This class represents a lite resource.
24     /// It provides APIs to encapsulate resources.
25     /// This class is accessed by using a constructor to create a new instance of this object.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class LiteResource : Resource
29     {
30         /// <summary>
31         /// The LiteResource constructor.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         /// <remarks>
35         /// <para>Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource(Resource)"/>.</para>
36         /// <para>When client requests some operations, it sends a response to client automatically.</para>
37         /// <para><paramref name="uri" /> length must be less than 128.</para>
38         /// </remarks>
39         /// <privilege>http://tizen.org/privilege/internet</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><![CDATA[
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><![CDATA[
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         /// <summary>
112         /// This is called when the client performs get operation on this resource.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         /// <param name="request">A request from client.</param>
116         /// <returns>A response having the representation and the result.</returns>
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         protected sealed override Response OnGet(Request request)
119         {
120             Representation representation = new Representation()
121             {
122                 UriPath = UriPath,
123                 Interface = Interfaces,
124                 Type = Types,
125                 Attributes = Attributes
126             };
127
128             Response response = new Response()
129             {
130                 Representation = representation,
131                 Result = ResponseCode.Ok
132             };
133
134             return response;
135         }
136
137         /// <summary>
138         /// This is called when the client performs put operation on this resource.
139         /// </summary>
140         /// <since_tizen> 3 </since_tizen>
141         /// <param name="request">A request from client.</param>
142         /// <returns>A response.</returns>
143         [EditorBrowsable(EditorBrowsableState.Never)]
144         protected sealed override Response OnPut(Request request)
145         {
146             Response response = new Response();
147             response.Result = ResponseCode.Forbidden;
148             return response;
149         }
150
151         /// <summary>
152         /// This is called when the client performs post operation on this resource.
153         /// </summary>
154         /// <since_tizen> 3 </since_tizen>
155         /// <param name="request">A request from client.</param>
156         /// <returns>A response having the representation and the result.</returns>
157         [EditorBrowsable(EditorBrowsableState.Never)]
158         protected sealed override Response OnPost(Request request)
159         {
160             if (OnPost(request.Representation.Attributes))
161             {
162                 Attributes = request.Representation.Attributes;
163                 Representation representation = new Representation() {
164                     UriPath = UriPath,
165                     Interface = Interfaces,
166                     Type = Types,
167                     Attributes = Attributes
168                 };
169
170                 Response response = new Response() {
171                     Representation = representation,
172                     Result = ResponseCode.Ok
173                 };
174
175                 Notify(representation, QualityOfService.High);
176                 return response;
177             }
178
179             return new Response()
180             {
181                 Result = ResponseCode.Error
182             };
183         }
184
185         /// <summary>
186         /// This is called when the client performs delete operation on this resource.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         /// <param name="request">A request from client.</param>
190         /// <returns>A response.</returns>
191         [EditorBrowsable(EditorBrowsableState.Never)]
192         protected sealed override Response OnDelete(Request request)
193         {
194             Response response = new Response();
195             response.Result = ResponseCode.Forbidden;
196             return response;
197         }
198
199         /// <summary>
200         /// Called on the observing event.
201         /// </summary>
202         /// <since_tizen> 3 </since_tizen>
203         /// <param name="request">A request from client.</param>
204         /// <param name="observeType">Observer type.</param>
205         /// <param name="observeId">Observe identifier.</param>
206         /// <returns>Returns true.</returns>
207         [EditorBrowsable(EditorBrowsableState.Never)]
208         protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
209         {
210             return true;
211         }
212     }
213 }