summaryrefslogtreecommitdiffstats
path: root/responsemessage.cpp
blob: 9f8e6f2cc19ddc22659d506b9ea4bd9258fdf6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2018-2020 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

#include "responsemessage.h"


ResponseMessage::ResponseMessage(QJsonDocument content)
{
	m_jdoc = content;
	QJsonArray msg = content.array();
	if (!msg[2].isObject()) {
		qWarning("Invalid appfw payload: no JSON object");
		return;
	}

	//deserialize:
	auto callid = msg[1].toString().toInt();
	QJsonObject payload = msg[2].toObject();

	auto request_iter = payload.find("request");
	auto request = request_iter.value().toObject();
	if (request.empty()) {
		qWarning("Invalid appfw reply message: empty request data");
		return;
	}

	auto status_iter = request.find("status");
	auto info_iter = request.find("info");
	auto response_iter = payload.find("response");
	auto response = response_iter.value().toObject();
	m_reply_status = status_iter.value().toString();
	m_reply_info = info_iter.value().toString();
	m_reply_data = response;
	m_reply_callid = callid;
	m_init = false; //not complete yet, missing matching request data
}

bool ResponseMessage::setAdditionalData(QByteArray data)
{
	QJsonDocument jdoc(QJsonDocument::fromJson(data));
	if (!jdoc.isArray()) {
		qWarning("Invalid data: not an array");
		return false;
	}

	QJsonArray content = jdoc.array();
	if (content.size() != 4) {
		qWarning("Invalid data: invalid array size");
	return false;
	}

	QStringList api_str_list = content[2].toString().split(QRegExp("/"));
	m_request["msgid"] = content.at(0);
	m_request["callid"] = content.at(1);
	m_request["api"] = api_str_list[0];
	m_request["verb"] = api_str_list[1];
	m_request["parameter"] = content.at(3);
	m_init = true;
	return true;
}

bool ResponseMessage::copyCallId(unsigned int *id)
{
	*id = m_reply_callid;
	return true;
}

QByteArray ResponseMessage::serialize(QJsonDocument::JsonFormat format)
{
	QJsonArray array;
	(m_reply_status == "failed")?
		array.append(static_cast<int>(MessageId::RetErr)) :
		array.append(static_cast<int>(MessageId::RetOk));
	array.append(static_cast<int>(m_reply_callid));
	array.append(m_request["api"].toString() + "/" + m_request["verb"].toString());
	array.append(m_reply_data);

	QJsonDocument jdoc;
	jdoc.setArray(array);

	return jdoc.toJson(format).data();
}