Merge test folder of test branch
[iotivity.git] / test / src / common / commonutil / c_cpp / MntResource.cpp
1 /******************************************************************
2 *
3 * Copyright 2018 Open Connectivity Foundation All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include "MntResource.h"
22
23 MntResource::MntResource(void)
24 {
25     numberOf503ErrorResponse = 0;
26     expect503ErrorRespond = false;
27 }
28
29 MntResource::MntResource(std::string resourceUri,
30         std::string resourceTypeName,
31         std::string resourceInterface,
32         uint8_t resourceProperty)
33          : SampleResource(resourceUri, resourceTypeName, resourceInterface, resourceProperty)
34 {
35     numberOf503ErrorResponse = 0;
36     expect503ErrorRespond = false;
37 }
38
39 MntResource::MntResource(std::string resourceUri,
40         std::string resourceTypeName,
41         std::string resourceInterface,
42         uint8_t resourceProperty,
43         OCRepresentation resourceRepresentation)
44          : SampleResource(resourceUri, resourceTypeName, resourceInterface, resourceProperty, resourceRepresentation)
45 {
46     numberOf503ErrorResponse = 0;
47     expect503ErrorRespond = false;
48 }
49
50 MntResource::~MntResource(void)
51 {
52 }
53
54 void MntResource::handleGetRequest(QueryParamsMap &queryParamsMap,
55         std::shared_ptr< OCResourceRequest > request,
56         std::shared_ptr< OCResourceResponse > response)
57 {
58     OCRepresentation rep = m_representation;
59     string rbValue = rep.getValueToString(string(REBOOT_KEY));
60     if (rbValue.compare("true") == 0)
61     {
62         cout << "resseting property 'rb' : " << rbValue << endl;
63         rep.setValue(REBOOT_KEY, false);
64     }
65
66     if (expect503ErrorRespond && numberOf503ErrorResponse < 2)
67     {
68         numberOf503ErrorResponse++;
69         rep.setValue(HTML_ERR_KEY, 503);
70     }
71     else if (numberOf503ErrorResponse >= 2)
72     {
73         numberOf503ErrorResponse = 0;
74         expect503ErrorRespond = false;
75     }
76
77     SampleResource::handleGetRequest(queryParamsMap, request, response, rep);
78 }
79
80 void MntResource::handlePostRequest(QueryParamsMap &queryParamsMap,
81         OCRepresentation incomingRepresentation, std::shared_ptr< OCResourceRequest > request,
82         std::shared_ptr< OCResourceResponse > response)
83 {
84     OCStackResult result = OC_STACK_ERROR;
85     bool shouldChange = true;
86     cout << "Inside handlePostRequest... " << endl;
87     cout << "THe POST request comprises of the following representation:" << endl;
88     p_resourceHelper->printRepresentation(incomingRepresentation);
89
90     // handle POST request
91     string targetValue = "";
92     string uriValue = "";
93     OCRepresentation rep = getRepresentation();
94     m_pResponse = response;
95     response->setResponseResult(OCEntityHandlerResult::OC_EH_OK);
96
97     if (queryParamsMap.size() > 0)
98     {
99         for (const auto &eachQuery : queryParamsMap)
100         {
101             string key = eachQuery.first;
102             if (key.compare(INTERFACE_KEY) == 0)
103             {
104                 string value = eachQuery.second;
105                 if ((value.compare("oic.if.r") == 0) || (value.compare("oic.if.s") == 0))
106                 {
107                     cout << "Update request received via interface: " << value
108                             << " . This interface is not authorized to update resource!!" << endl;
109                     response->setResponseResult(OCEntityHandlerResult::OC_EH_FORBIDDEN);
110                     shouldChange = false;
111                     break;
112                 }
113             }
114
115         }
116     }
117
118     if (shouldChange)
119     {
120         try
121         {
122             if (incomingRepresentation.hasAttribute(INTERFACE_KEY))
123             {
124                 response->setResponseResult(OCEntityHandlerResult::OC_EH_ERROR);
125                 cout << "\t\t" << "property 'if' is readOnly "<< endl;
126             }
127         }
128         catch (exception& e)
129         {
130             cout << e.what() << endl;
131         }
132
133         try
134         {
135             if (incomingRepresentation.hasAttribute(RESOURCE_TYPE_KEY))
136             {
137                 response->setResponseResult(OCEntityHandlerResult::OC_EH_ERROR);
138                 cout << "\t\t" << "property 'rt' is readOnly "<< endl;
139             }
140         }
141         catch (exception& e)
142         {
143             cout << e.what() << endl;
144         }
145
146         try
147         {
148             if (incomingRepresentation.hasAttribute(NAME_KEY))
149             {
150                 response->setResponseResult(OCEntityHandlerResult::OC_EH_ERROR);
151                 cout << "\t\t" << "property 'n' is readOnly "<< endl;
152             }
153         }
154         catch (exception& e)
155         {
156             cout << e.what() << endl;
157         }
158
159         try
160         {
161             if (incomingRepresentation.hasAttribute(HTML_ERR_KEY))
162             {
163                 response->setResponseResult(OCEntityHandlerResult::OC_EH_ERROR);
164                 cout << "\t\t" << "property 'err' is readOnly "<< endl;
165             }
166         }
167         catch (exception& e)
168         {
169             cout << e.what() << endl;
170         }
171
172         try
173         {
174             if (incomingRepresentation.hasAttribute(FACTORY_KEY))
175             {
176                 expect503ErrorRespond = true;
177             }
178         }
179         catch (exception& e)
180         {
181             cout << e.what() << endl;
182         }
183
184         try
185         {
186             if (incomingRepresentation.hasAttribute(REBOOT_KEY))
187             {
188                 expect503ErrorRespond = true;
189                 updateRepresentation(REBOOT_KEY, incomingRepresentation, response);
190                 notifyObservers(this);
191             }
192         }
193         catch (exception& e)
194         {
195             cout << e.what() << endl;
196         }
197     }
198
199     try
200     {
201         result = OCPlatform::sendResponse(response);
202     }
203     catch (exception& e)
204     {
205         cout << "Exception occurred while sending response. Exception is: " << e.what() << endl;
206     }
207     if (result != OC_STACK_OK)
208     {
209         cerr << "Unable to send response for POST Request" << endl;
210     }
211 }