a69e2aed3b670aa2e1e0a45d544375686ebfd0d8
[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         /// Creates a lite resource, which can then be registered in server using <see cref="IoTConnectivityServerManager.RegisterResource(Resource)"/>.\n
36         /// When client requests some operations, it sends a response to client automatically.\n
37         /// @a uri length must be less than 128.
38         /// </remarks>
39         /// <privilege>
40         /// http://tizen.org/privilege/internet
41         /// </privilege>
42         /// <privlevel>public</privlevel>
43         /// <param name="uri">The uri path of the lite resource.</param>
44         /// <param name="types">The type of the resource.</param>
45         /// <param name="policy">Policy of the resource.</param>
46         /// <param name="attribs">Optional attributes of the resource.</param>
47         /// <feature>http://tizen.org/feature/iot.ocf</feature>
48         /// <pre>
49         /// IoTConnectivityServerManager.Initialize() should be called to initialize.
50         /// </pre>
51         /// <seealso cref="ResourceTypes"/>
52         /// <seealso cref="ResourcePolicy"/>
53         /// <seealso cref="Attributes"/>
54         /// <code><![CDATA[
55         /// List<string> list = new List<string>() { "org.tizen.light" };
56         /// Attributes attributes = new Attributes() {
57         ///     { "state", "ON" }
58         /// };
59         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable, attributes);
60         /// ]]></code>
61         public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, Attributes attribs = null)
62             : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy)
63         {
64             Attributes = attribs;
65         }
66
67         /// <summary>
68         /// Gets or sets the attributes of the lite resource.
69         /// </summary>
70         /// <since_tizen> 3 </since_tizen>
71         /// <value>The attributes of the lite resource.</value>
72         /// <code><![CDATA[
73         /// List<string> list = new List<string>() { "org.tizen.light" };
74         /// LiteResource res = new LiteResource("/light/1", new ResourceTypes(list), ResourcePolicy.Discoverable);
75         /// Attributes attributes = new Attributes() {
76         ///     { "state", "ON" }
77         /// };
78         /// res.Attributes = newAttributes;
79         /// foreach (KeyValuePair<string, object> pair in res.Attributes)
80         /// {
81         ///     Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
82         /// }
83         /// ]]></code>
84         public Attributes Attributes { get; set; }
85
86         /// <summary>
87         /// Decides whether to accept or reject a post request.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         /// <remarks>
91         /// Child classes of this class can override this method to accept or reject post request.
92         /// </remarks>
93         /// <param name="attribs">The new attributes of the lite resource.</param>
94         /// <returns>true to accept post request, false to reject it.</returns>
95         /// <code>
96         /// public class MyLightResource : LiteResource
97         /// {
98         ///     protected override bool OnPost(Attributes attributes)
99         ///     {
100         ///         object newAttributes;
101         ///         attributes.TryGetValue("LIGHT_ATTRIBUTE", out newAttributes);
102         ///         if((int)newAttributes == 1)
103         ///             return true;
104         ///         return false;
105         ///     }
106         /// }
107         /// </code>
108         protected virtual bool OnPost(Attributes attribs)
109         {
110             return true;
111         }
112
113         /// <summary>
114         /// This is called when the client performs get operation on this resource.
115         /// </summary>
116         /// <since_tizen> 3 </since_tizen>
117         /// <param name="request">A request from client.</param>
118         /// <returns>A response having the representation and the result.</returns>
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         protected sealed override Response OnGet(Request request)
121         {
122             Representation representation = new Representation()
123             {
124                 UriPath = UriPath,
125                 Interface = Interfaces,
126                 Type = Types,
127                 Attributes = Attributes
128             };
129
130             Response response = new Response()
131             {
132                 Representation = representation,
133                 Result = ResponseCode.Ok
134             };
135
136             return response;
137         }
138
139         /// <summary>
140         /// This is called when the client performs put operation on this resource.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         /// <param name="request">A request from client.</param>
144         /// <returns>A response.</returns>
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         protected sealed override Response OnPut(Request request)
147         {
148             Response response = new Response();
149             response.Result = ResponseCode.Forbidden;
150             return response;
151         }
152
153         /// <summary>
154         /// This is called when the client performs post operation on this resource.
155         /// </summary>
156         /// <since_tizen> 3 </since_tizen>
157         /// <param name="request">A request from client.</param>
158         /// <returns>A response having the representation and the result.</returns>
159         [EditorBrowsable(EditorBrowsableState.Never)]
160         protected sealed override Response OnPost(Request request)
161         {
162             if (OnPost(request.Representation.Attributes))
163             {
164                 Attributes = request.Representation.Attributes;
165                 Representation representation = new Representation() {
166                     UriPath = UriPath,
167                     Interface = Interfaces,
168                     Type = Types,
169                     Attributes = Attributes
170                 };
171
172                 Response response = new Response() {
173                     Representation = representation,
174                     Result = ResponseCode.Ok
175                 };
176
177                 Notify(representation, QualityOfService.High);
178                 return response;
179             }
180
181             return new Response()
182             {
183                 Result = ResponseCode.Error
184             };
185         }
186
187         /// <summary>
188         /// This is called when the client performs delete operation on this resource.
189         /// </summary>
190         /// <since_tizen> 3 </since_tizen>
191         /// <param name="request">A request from client.</param>
192         /// <returns>A response.</returns>
193         [EditorBrowsable(EditorBrowsableState.Never)]
194         protected sealed override Response OnDelete(Request request)
195         {
196             Response response = new Response();
197             response.Result = ResponseCode.Forbidden;
198             return response;
199         }
200
201         /// <summary>
202         /// Called on the observing event.
203         /// </summary>
204         /// <since_tizen> 3 </since_tizen>
205         /// <param name="request">A request from client.</param>
206         /// <param name="observeType">Observer type.</param>
207         /// <param name="observeId">Observe identifier.</param>
208         /// <returns>Returns true.</returns>
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         protected sealed override bool OnObserving(Request request, ObserveType observeType, int observeId)
211         {
212             return true;
213         }
214     }
215 }