2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "sw_collector_rest_api.h"
18 #include <rest/rest.h>
19 #include <utils/debug.h>
21 typedef struct private_sw_collector_rest_api_t private_sw_collector_rest_api_t
;
24 * Private data of an sw_collector_rest_api_t object.
26 struct private_sw_collector_rest_api_t
{
29 * Public members of sw_collector_rest_api_state_t
31 sw_collector_rest_api_t
public;
34 * Software collector database
36 sw_collector_db_t
*db
;
39 * REST API of central collector database
46 * Put all locally retrieved software identifiers into a json object
48 static json_object
* create_rest_request(private_sw_collector_rest_api_t
*this,
49 sw_collector_db_query_t type
)
51 json_object
*jrequest
, *jarray
, *jstring
;
52 char *name
, *package
, *version
;
56 jrequest
= json_object_new_object();
57 jarray
= json_object_new_array();
58 json_object_object_add(jrequest
, "data", jarray
);
60 e
= this->db
->create_sw_enumerator(this->db
, type
, NULL
);
65 while (e
->enumerate(e
, &sw_id
, &name
, &package
, &version
, &i
))
67 jstring
= json_object_new_string(name
);
68 json_object_array_add(jarray
, jstring
);
76 /** public enumerator interface */
78 /** enumerated json array */
80 /** current index +1, initialized at 0 */
82 } json_array_enumerator_t
;
84 METHOD(enumerator_t
, enumerate
, bool,
85 json_array_enumerator_t
*this, va_list args
)
90 VA_ARGS_VGET(args
, out
);
92 if (this->idx
>= json_object_array_length(this->jarray
))
97 jvalue
= json_object_array_get_idx(this->jarray
, this->idx
++);
98 if (json_object_get_type(jvalue
) != json_type_string
)
100 DBG1(DBG_IMC
, "json_string element expected in json_array");
103 *out
= (char*)json_object_get_string(jvalue
);
108 METHOD(enumerator_t
, enumerator_destroy
, void,
109 json_array_enumerator_t
*this)
111 json_object_put(this->jarray
);
115 METHOD(sw_collector_rest_api_t
, create_sw_enumerator
, enumerator_t
*,
116 private_sw_collector_rest_api_t
*this, sw_collector_db_query_t type
)
118 json_array_enumerator_t
*enumerator
;
119 json_object
*jrequest
, *jresponse
;
123 jrequest
= create_rest_request(this, type
);
128 snprintf(cmd
, BUF_LEN
, "sessions/0/swid-measurement/");
130 status
= this->rest_api
->post(this->rest_api
, cmd
, jrequest
, &jresponse
);
131 json_object_put(jrequest
);
137 jresponse
= json_object_new_array();
140 if (json_object_get_type(jresponse
) != json_type_array
)
142 DBG1(DBG_IMC
, "REST response was not a json_array");
143 json_object_put(jresponse
);
154 .enumerate
= enumerator_enumerate_default
,
155 .venumerate
= _enumerate
,
156 .destroy
= _enumerator_destroy
,
161 return &enumerator
->public;
164 METHOD(sw_collector_rest_api_t
, destroy
, void,
165 private_sw_collector_rest_api_t
*this)
167 this->rest_api
->destroy(this->rest_api
);
172 * Described in header.
174 sw_collector_rest_api_t
*sw_collector_rest_api_create(sw_collector_db_t
*db
)
176 private_sw_collector_rest_api_t
*this;
180 uri
= lib
->settings
->get_str(lib
->settings
, "%s.rest_api.uri", NULL
,
182 timeout
= lib
->settings
->get_int(lib
->settings
, "%s.rest_api.timeout", 120,
186 DBG1(DBG_IMC
, "REST URI to central collector database not set");
192 .create_sw_enumerator
= _create_sw_enumerator
,
196 .rest_api
= rest_create(uri
, timeout
),
199 return &this->public;