Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / src / OCResourceRequest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 <OCResourceRequest.h>
22
23 #include <vector>
24 #include <map>
25 #include <cereal/cereal.hpp>
26 #include <OicJsonSerializer.hpp>
27
28 using namespace OC;
29 using namespace std;
30
31 void OCResourceRequest::setPayload(const std::string& requestPayload)
32 {
33     MessageContainer info;
34
35     if(requestPayload.empty())
36     {
37         return;
38     }
39
40     try
41     {
42         info.setJSONRepresentation(requestPayload);
43     }
44     catch(cereal::RapidJSONException& ex)
45     {
46         oclog() << "RapidJSON Exception in setPayload: "<<ex.what()<<std::endl<<
47             "Data was:"<<requestPayload<<std::flush;
48         return;
49     }
50     catch(cereal::Exception& ex)
51     {
52         oclog() << "Cereal Exception in setPayload: "<<ex.what()<<std::endl<<
53             "Data was:"<<requestPayload<<std::flush;
54         return;
55     }
56
57     const std::vector<OCRepresentation>& reps = info.representations();
58     if(reps.size() >0)
59     {
60         std::vector<OCRepresentation>::const_iterator itr = reps.begin();
61         std::vector<OCRepresentation>::const_iterator back = reps.end();
62         m_representation = *itr;
63         ++itr;
64
65         for(;itr != back; ++itr)
66         {
67             m_representation.addChild(*itr);
68         }
69     }
70     else
71     {
72         oclog() << "setPayload Error: "<<OC::Exception::INVALID_REPRESENTATION<< std::flush;
73     }
74 }