1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2017-2020 GeoLabs SARL. All rights reserved. |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_json.h" |
---|
26 | #include "json.h" |
---|
27 | #include <errno.h> |
---|
28 | #include "json_tokener.h" |
---|
29 | #include "stdlib.h" |
---|
30 | #include "mimetypes.h" |
---|
31 | #include "server_internal.h" |
---|
32 | #include "service_internal.h" |
---|
33 | #include <dirent.h> |
---|
34 | |
---|
35 | #ifdef __cplusplus |
---|
36 | extern "C" { |
---|
37 | #endif |
---|
38 | /** |
---|
39 | * Equivalent range keywords for WPS version 1 and 2 |
---|
40 | */ |
---|
41 | const char* rangeCorrespondances[4][2]={ |
---|
42 | { "rangeMin", "minimumValue" }, |
---|
43 | { "rangeMax", "maximumValue"}, |
---|
44 | { "rangeSpacing", "spacing" }, |
---|
45 | { "rangeClosure", "rangeClosure" } |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * Convert a map to a json object |
---|
51 | * @param myMap the map to be converted into json object |
---|
52 | * @return a json_object pointer to the created json_object |
---|
53 | */ |
---|
54 | json_object* mapToJson(map* myMap){ |
---|
55 | json_object *res=json_object_new_object(); |
---|
56 | map* cursor=myMap; |
---|
57 | map* sizeMap=getMap(myMap,"size"); |
---|
58 | map* length=getMap(myMap,"length"); |
---|
59 | while(cursor!=NULL){ |
---|
60 | json_object *val=NULL; |
---|
61 | if(length==NULL && sizeMap==NULL) |
---|
62 | val=json_object_new_string(cursor->value); |
---|
63 | else{ |
---|
64 | if(length==NULL && sizeMap!=NULL){ |
---|
65 | if(strncasecmp(cursor->name,"value",5)==0) |
---|
66 | val=json_object_new_string_len(cursor->value,atoi(sizeMap->value)); |
---|
67 | else |
---|
68 | val=json_object_new_string(cursor->value); |
---|
69 | } |
---|
70 | else{ |
---|
71 | // In other case we should consider array with potential sizes |
---|
72 | int limit=atoi(length->value); |
---|
73 | int i=0; |
---|
74 | val=json_object_new_array(); |
---|
75 | for(i=0;i<limit;i++){ |
---|
76 | map* lsizeMap=getMapArray(myMap,"size",i); |
---|
77 | if(lsizeMap!=NULL && strncasecmp(cursor->name,"value",5)==0){ |
---|
78 | json_object_array_add(val,json_object_new_string_len(cursor->value,atoi(sizeMap->value))); |
---|
79 | }else{ |
---|
80 | json_object_array_add(val,json_object_new_string(cursor->value)); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | if(val!=NULL) |
---|
86 | json_object_object_add(res,cursor->name,val); |
---|
87 | cursor=cursor->next; |
---|
88 | } |
---|
89 | return res; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Convert a maps to a json object |
---|
94 | * @param myMap the maps to be converted into json object |
---|
95 | * @return a json_object pointer to the created json_object |
---|
96 | */ |
---|
97 | json_object* mapsToJson(maps* myMap){ |
---|
98 | json_object *res=json_object_new_object(); |
---|
99 | maps* cursor=myMap; |
---|
100 | while(cursor!=NULL){ |
---|
101 | json_object *obj=NULL; |
---|
102 | if(cursor->content!=NULL){ |
---|
103 | obj=mapToJson(cursor->content); |
---|
104 | }else |
---|
105 | obj=json_object_new_object(); |
---|
106 | if(cursor->child!=NULL){ |
---|
107 | json_object *child=NULL; |
---|
108 | child=mapsToJson(cursor->child); |
---|
109 | json_object_object_add(obj,"child",child); |
---|
110 | } |
---|
111 | json_object_object_add(res,cursor->name,obj); |
---|
112 | cursor=cursor->next; |
---|
113 | } |
---|
114 | return res; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Convert an elements to a json object |
---|
119 | * @param myElements the elements pointer to be converted into a json object |
---|
120 | * @return a json_object pointer to the created json_object |
---|
121 | */ |
---|
122 | json_object* elementsToJson(elements* myElements){ |
---|
123 | json_object *res=json_object_new_object(); |
---|
124 | elements* cur=myElements; |
---|
125 | while(cur!=NULL){ |
---|
126 | json_object *cres=json_object_new_object(); |
---|
127 | json_object_object_add(cres,"content",mapToJson(cur->content)); |
---|
128 | json_object_object_add(cres,"metadata",mapToJson(cur->metadata)); |
---|
129 | json_object_object_add(cres,"additional_parameters",mapToJson(cur->additional_parameters)); |
---|
130 | if(cur->format!=NULL){ |
---|
131 | json_object_object_add(cres,"format",json_object_new_string(cur->format)); |
---|
132 | } |
---|
133 | if(cur->child==NULL){ |
---|
134 | if(cur->defaults!=NULL) |
---|
135 | json_object_object_add(cres,"defaults",mapToJson(cur->defaults->content)); |
---|
136 | else |
---|
137 | json_object_object_add(cres,"defaults",mapToJson(NULL)); |
---|
138 | iotype* scur=cur->supported; |
---|
139 | json_object *resi=json_object_new_array(); |
---|
140 | while(scur!=NULL){ |
---|
141 | json_object_array_add(resi,mapToJson(scur->content)); |
---|
142 | scur=scur->next; |
---|
143 | } |
---|
144 | json_object_object_add(cres,"supported",resi); |
---|
145 | } |
---|
146 | |
---|
147 | json_object_object_add(cres,"child",elementsToJson(cur->child)); |
---|
148 | |
---|
149 | json_object_object_add(res,cur->name,cres); |
---|
150 | cur=cur->next; |
---|
151 | } |
---|
152 | return res; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Convert an service to a json object |
---|
157 | * @param myService the service pointer to be converted into a json object |
---|
158 | * @return a json_object pointer to the created json_object |
---|
159 | */ |
---|
160 | json_object* serviceToJson(service* myService){ |
---|
161 | json_object *res=json_object_new_object(); |
---|
162 | json_object_object_add(res,"name",json_object_new_string(myService->name)); |
---|
163 | json_object_object_add(res,"content",mapToJson(myService->content)); |
---|
164 | json_object_object_add(res,"metadata",mapToJson(myService->metadata)); |
---|
165 | json_object_object_add(res,"additional_parameters",mapToJson(myService->additional_parameters)); |
---|
166 | json_object_object_add(res,"inputs",elementsToJson(myService->inputs)); |
---|
167 | json_object_object_add(res,"outputs",elementsToJson(myService->outputs)); |
---|
168 | return res; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Add Allowed Range properties to a json_object |
---|
173 | * @param m the main configuration maps pointer |
---|
174 | * @param iot the iotype pointer |
---|
175 | * @param prop the json_object pointer to add the allowed range properties |
---|
176 | * @return a json_object pointer to the created json_object |
---|
177 | */ |
---|
178 | void printAllowedRangesJ(maps* m,iotype* iot,json_object* prop){ |
---|
179 | map* tmpMap1; |
---|
180 | json_object* prop4=json_object_new_object(); |
---|
181 | for(int i=0;i<4;i++){ |
---|
182 | tmpMap1=getMap(iot->content,rangeCorrespondances[i][0]); |
---|
183 | if(tmpMap1!=NULL){ |
---|
184 | if(i<3) |
---|
185 | json_object_object_add(prop4,rangeCorrespondances[i][1],json_object_new_string(tmpMap1->value)); |
---|
186 | else{ |
---|
187 | char* currentValue=NULL; |
---|
188 | int limit=strlen(tmpMap1->value); |
---|
189 | for(int j=0;j<limit;j++){ |
---|
190 | const char* tmpS="closed"; |
---|
191 | if(tmpMap1->value[j]=='o'){ |
---|
192 | tmpS="open"; |
---|
193 | } |
---|
194 | if(currentValue==NULL){ |
---|
195 | currentValue=(char*)malloc((strlen(tmpS)+1)*sizeof(char)); |
---|
196 | sprintf(currentValue,"%s",tmpS); |
---|
197 | }else{ |
---|
198 | char* tmpS1=zStrdup(currentValue); |
---|
199 | currentValue=(char*)realloc(currentValue,(strlen(tmpS1)+strlen(tmpS)+2)*sizeof(char)); |
---|
200 | sprintf(currentValue,"%s-%s",tmpS1,tmpS); |
---|
201 | } |
---|
202 | } |
---|
203 | json_object_object_add(prop4,rangeCorrespondances[i][1],json_object_new_string(currentValue)); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | json_object_array_add(prop,prop4); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Add literalDataDomains property to a json_object |
---|
212 | * @param m the main configuration maps pointer |
---|
213 | * @param in the elements pointer |
---|
214 | * @param input the json_object pointer to add the literalDataDomains property |
---|
215 | * @return a json_object pointer to the created json_object |
---|
216 | */ |
---|
217 | void printLiteralDataJ(maps* m,elements* in,json_object* input){ |
---|
218 | json_object* prop0=json_object_new_array(); |
---|
219 | json_object* prop1=json_object_new_object(); |
---|
220 | json_object* prop2=json_object_new_object(); |
---|
221 | if(in->defaults!=NULL){ |
---|
222 | map* tmpMap1=getMap(in->defaults->content,"DataType"); |
---|
223 | if(tmpMap1!=NULL){ |
---|
224 | json_object_object_add(prop2,"name",json_object_new_string(tmpMap1->value)); |
---|
225 | char *tmp=(char*)malloc((strlen(tmpMap1->value)+35)*sizeof(char)); |
---|
226 | sprintf(tmp,"http://www.w3.org/TR/xmlschema-2/#%s",tmpMap1->value); |
---|
227 | json_object_object_add(prop2,"reference",json_object_new_string(tmp)); |
---|
228 | free(tmp); |
---|
229 | json_object_object_add(prop1,"dataType",prop2); |
---|
230 | } |
---|
231 | tmpMap1=getMap(in->defaults->content,"value"); |
---|
232 | if(tmpMap1!=NULL) |
---|
233 | json_object_object_add(prop1,"defaultValue",json_object_new_string(tmpMap1->value)); |
---|
234 | json_object* prop3=json_object_new_object(); |
---|
235 | tmpMap1=getMap(in->defaults->content,"rangeMin"); |
---|
236 | if(tmpMap1!=NULL){ |
---|
237 | json_object* prop5=json_object_new_array(); |
---|
238 | printAllowedRangesJ(m,in->defaults,prop5); |
---|
239 | if(in->supported!=NULL){ |
---|
240 | iotype* iot=in->supported; |
---|
241 | while(iot!=NULL){ |
---|
242 | printAllowedRangesJ(m,iot,prop5); |
---|
243 | iot=iot->next; |
---|
244 | } |
---|
245 | } |
---|
246 | json_object_object_add(prop3,"allowedRanges",prop5); |
---|
247 | } |
---|
248 | else{ |
---|
249 | tmpMap1=getMap(in->defaults->content,"range"); |
---|
250 | if(tmpMap1!=NULL){ |
---|
251 | // TODO: parse range = [rangeMin,rangeMax] |
---|
252 | }else{ |
---|
253 | // AllowedValues |
---|
254 | tmpMap1=getMap(in->defaults->content,"AllowedValues"); |
---|
255 | if(tmpMap1!=NULL){ |
---|
256 | char* saveptr; |
---|
257 | json_object* prop5=json_object_new_array(); |
---|
258 | char *tmps = strtok_r (tmpMap1->value, ",", &saveptr); |
---|
259 | while(tmps!=NULL){ |
---|
260 | json_object_array_add(prop5,json_object_new_string(tmps)); |
---|
261 | tmps = strtok_r (NULL, ",", &saveptr); |
---|
262 | } |
---|
263 | json_object_object_add(prop3,"allowedValues",prop5); |
---|
264 | |
---|
265 | }else{ |
---|
266 | json_object_object_add(prop3,"anyValue",json_object_new_boolean(true)); |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | json_object_object_add(prop1,"valueDefinition",prop3); |
---|
271 | json_object_array_add(prop0,prop1); |
---|
272 | } |
---|
273 | json_object_object_add(input,"literalDataDomains",prop0); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Add Format properties to a json_object |
---|
278 | * @param m the main configuration maps pointer |
---|
279 | * @param iot the current iotype pointer |
---|
280 | * @param res the json_object pointer to add the properties to |
---|
281 | * @param isDefault boolean specifying if the currrent iotype is default |
---|
282 | * @param maxSize a map pointer to the maximumMegabytes param defined in the zcfg file for this input/output |
---|
283 | */ |
---|
284 | void printFormatJ(maps* m,iotype* iot,json_object* res,bool isDefault,map* maxSize){ |
---|
285 | if(iot!=NULL){ |
---|
286 | map* tmpMap1=getMap(iot->content,"mimeType"); |
---|
287 | json_object* prop1=json_object_new_object(); |
---|
288 | json_object_object_add(prop1,"default",json_object_new_boolean(isDefault)); |
---|
289 | json_object_object_add(prop1,"mimeType",json_object_new_string(tmpMap1->value)); |
---|
290 | tmpMap1=getMap(iot->content,"encoding"); |
---|
291 | if(tmpMap1!=NULL) |
---|
292 | json_object_object_add(prop1,"encoding",json_object_new_string(tmpMap1->value)); |
---|
293 | tmpMap1=getMap(iot->content,"schema"); |
---|
294 | if(tmpMap1!=NULL) |
---|
295 | json_object_object_add(prop1,"schema",json_object_new_string(tmpMap1->value)); |
---|
296 | if(maxSize!=NULL) |
---|
297 | json_object_object_add(prop1,"maximumMegabytes",json_object_new_int64(atoll(maxSize->value))); |
---|
298 | json_object_array_add(res,prop1); |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Add additionalParameters property to a json_object |
---|
304 | * @param conf the main configuration maps pointer |
---|
305 | * @param meta a map pointer to the current metadata informations |
---|
306 | * @param doc the json_object pointer to add the property to |
---|
307 | */ |
---|
308 | void printJAdditionalParameters(maps* conf,map* meta,json_object* doc){ |
---|
309 | map* cmeta=meta; |
---|
310 | json_object* carr=json_object_new_array(); |
---|
311 | int hasElement=-1; |
---|
312 | json_object* jcaps=json_object_new_object(); |
---|
313 | while(cmeta!=NULL){ |
---|
314 | json_object* jcmeta=json_object_new_object(); |
---|
315 | if(strcasecmp(cmeta->name,"role")!=0 && |
---|
316 | strcasecmp(cmeta->name,"href")!=0 && |
---|
317 | strcasecmp(cmeta->name,"title")!=0 && |
---|
318 | strcasecmp(cmeta->name,"length")!=0 ){ |
---|
319 | json_object_object_add(jcmeta,"name",json_object_new_string(cmeta->name)); |
---|
320 | json_object_object_add(jcmeta,"value",json_object_new_string(cmeta->value)); |
---|
321 | json_object_array_add(carr,jcmeta); |
---|
322 | hasElement++; |
---|
323 | }else{ |
---|
324 | if(strcasecmp(cmeta->name,"length")!=0) |
---|
325 | json_object_object_add(jcaps,cmeta->name,json_object_new_string(cmeta->value)); |
---|
326 | } |
---|
327 | cmeta=cmeta->next; |
---|
328 | } |
---|
329 | if(hasElement>=0){ |
---|
330 | json_object_object_add(jcaps,"additionalParameter",carr); |
---|
331 | json_object_object_add(doc,"additionalParameters",jcaps); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * Add metadata property to a json_object |
---|
337 | * @param conf the main configuration maps pointer |
---|
338 | * @param meta a map pointer to the current metadata informations |
---|
339 | * @param doc the json_object pointer to add the property to |
---|
340 | */ |
---|
341 | void printJMetadata(maps* conf,map* meta,json_object* doc){ |
---|
342 | map* cmeta=meta; |
---|
343 | json_object* carr=json_object_new_array(); |
---|
344 | int hasElement=-1; |
---|
345 | while(cmeta!=NULL){ |
---|
346 | json_object* jcmeta=json_object_new_object(); |
---|
347 | if(strcasecmp(cmeta->name,"role")==0 || |
---|
348 | strcasecmp(cmeta->name,"href")==0 || |
---|
349 | strcasecmp(cmeta->name,"title")==0 ){ |
---|
350 | json_object_object_add(jcmeta,cmeta->name,json_object_new_string(cmeta->value)); |
---|
351 | hasElement++; |
---|
352 | } |
---|
353 | json_object_array_add(carr,jcmeta); |
---|
354 | cmeta=cmeta->next; |
---|
355 | } |
---|
356 | if(hasElement>=0) |
---|
357 | json_object_object_add(doc,"metadata",carr); |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * Add metadata properties to a json_object |
---|
362 | * @param m the main configuration maps pointer |
---|
363 | * @param io a string |
---|
364 | * @param in an elements pointer to the current input/output |
---|
365 | * @param inputs the json_object pointer to add the property to |
---|
366 | * @param serv the service pointer to extract the metadata from |
---|
367 | */ |
---|
368 | void printIOTypeJ(maps* m, const char *io, elements* in,json_object* inputs,service* serv){ |
---|
369 | while(in!=NULL){ |
---|
370 | json_object* input=json_object_new_object(); |
---|
371 | json_object_object_add(input,"id",json_object_new_string(in->name)); |
---|
372 | map* tmpMap=getMap(in->content,"title"); |
---|
373 | if(tmpMap!=NULL) |
---|
374 | json_object_object_add(input,"title",json_object_new_string(tmpMap->value)); |
---|
375 | tmpMap=getMap(in->content,"abstract"); |
---|
376 | if(tmpMap!=NULL) |
---|
377 | json_object_object_add(input,"description",json_object_new_string(tmpMap->value)); |
---|
378 | if(strcmp(io,"input")==0){ |
---|
379 | tmpMap=getMap(in->content,"minOccurs"); |
---|
380 | if(tmpMap!=NULL) |
---|
381 | json_object_object_add(input,"minOccurs",json_object_new_int(atoi(tmpMap->value))); |
---|
382 | tmpMap=getMap(in->content,"maxOccurs"); |
---|
383 | if(tmpMap!=NULL){ |
---|
384 | if(strncasecmp(tmpMap->value,"unbounded",9)==0) |
---|
385 | json_object_object_add(input,"maxOccurs",json_object_new_string(tmpMap->value)); |
---|
386 | else |
---|
387 | json_object_object_add(input,"maxOccurs",json_object_new_int(atoi(tmpMap->value))); |
---|
388 | } |
---|
389 | } |
---|
390 | if(in->format!=NULL){ |
---|
391 | json_object* input1=json_object_new_object(); |
---|
392 | json_object* prop0=json_object_new_array(); |
---|
393 | if(strcasecmp(in->format,"LiteralData")==0 || |
---|
394 | strcasecmp(in->format,"LiteralOutput")==0){ |
---|
395 | printLiteralDataJ(m,in,input1); |
---|
396 | }else{ |
---|
397 | if(strcasecmp(in->format,"ComplexData")==0 || |
---|
398 | strcasecmp(in->format,"ComplexOutput")==0){ |
---|
399 | map* sizeMap=getMap(in->content,"maximumMegabytes"); |
---|
400 | printFormatJ(m,in->defaults,prop0,true,sizeMap); |
---|
401 | iotype* sup=in->supported; |
---|
402 | while(sup!=NULL){ |
---|
403 | printFormatJ(m,sup,prop0,false,sizeMap); |
---|
404 | sup=sup->next; |
---|
405 | } |
---|
406 | json_object_object_add(input1,"formats",prop0); |
---|
407 | } |
---|
408 | else{ |
---|
409 | json_object* prop1=json_object_new_object(); |
---|
410 | json_object_object_add(prop1,"default",json_object_new_boolean(true)); |
---|
411 | map* tmpMap1=getMap(in->defaults->content,"crs"); |
---|
412 | if(tmpMap1==NULL) |
---|
413 | return; |
---|
414 | json_object_object_add(prop1,"crs",json_object_new_string(tmpMap1->value)); |
---|
415 | json_object_array_add(prop0,prop1); |
---|
416 | iotype* sup=in->supported; |
---|
417 | while(sup!=NULL){ |
---|
418 | json_object* prop1=json_object_new_object(); |
---|
419 | json_object_object_add(prop1,"default",json_object_new_boolean(false)); |
---|
420 | tmpMap1=getMap(sup->content,"crs"); |
---|
421 | json_object_object_add(prop1,"crs",json_object_new_string(tmpMap1->value)); |
---|
422 | json_object_array_add(prop0,prop1); |
---|
423 | sup=sup->next; |
---|
424 | } |
---|
425 | json_object_object_add(input1,"supportedCRS",prop0); |
---|
426 | } |
---|
427 | } |
---|
428 | json_object_object_add(input,io,input1); |
---|
429 | } |
---|
430 | printJMetadata(m,in->metadata,input); |
---|
431 | printJAdditionalParameters(m,in->additional_parameters,input); |
---|
432 | json_object_array_add(inputs,input); |
---|
433 | in=in->next; |
---|
434 | } |
---|
435 | |
---|
436 | } |
---|
437 | |
---|
438 | /** |
---|
439 | * Add all the capabilities properties to a json_object |
---|
440 | * @param ref the registry pointer |
---|
441 | * @param m the main configuration maps pointer |
---|
442 | * @param doc0 the void (json_object) pointer to add the property to |
---|
443 | * @param nc0 the void (json_object) pointer to add the property to |
---|
444 | * @param serv the service pointer to extract the metadata from |
---|
445 | */ |
---|
446 | void printGetCapabilitiesForProcessJ(registry *reg, maps* m,void* doc0,void* nc0,service* serv){ |
---|
447 | json_object* doc=(json_object*) doc0; |
---|
448 | json_object* nc=(json_object*) nc0; |
---|
449 | json_object *res; |
---|
450 | if(doc!=NULL) |
---|
451 | res=json_object_new_object(); |
---|
452 | else |
---|
453 | res=(json_object*) nc0; |
---|
454 | |
---|
455 | map* tmpMap0=getMapFromMaps(m,"lenv","level"); |
---|
456 | char* rUrl=serv->name; |
---|
457 | if(tmpMap0!=NULL && atoi(tmpMap0->value)>0){ |
---|
458 | int i=0; |
---|
459 | maps* tmpMaps=getMaps(m,"lenv"); |
---|
460 | char* tmpName=NULL; |
---|
461 | for(i=0;i<atoi(tmpMap0->value);i++){ |
---|
462 | char* key=(char*)malloc(15*sizeof(char)); |
---|
463 | sprintf(key,"sprefix_%d",i); |
---|
464 | map* tmpMap1=getMap(tmpMaps->content,key); |
---|
465 | if(i+1==atoi(tmpMap0->value)) |
---|
466 | if(tmpName==NULL){ |
---|
467 | tmpName=(char*) malloc((strlen(serv->name)+strlen(tmpMap1->value)+1)*sizeof(char)); |
---|
468 | sprintf(tmpName,"%s%s",tmpMap1->value,serv->name); |
---|
469 | }else{ |
---|
470 | char* tmpStr=zStrdup(tmpName); |
---|
471 | tmpName=(char*) realloc(tmpName,(strlen(tmpStr)+strlen(tmpMap1->value)+strlen(serv->name)+1)*sizeof(char)); |
---|
472 | sprintf(tmpName,"%s%s%s",tmpStr,tmpMap1->value,serv->name); |
---|
473 | free(tmpStr); |
---|
474 | } |
---|
475 | else |
---|
476 | if(tmpName==NULL){ |
---|
477 | tmpName=(char*) malloc((strlen(tmpMap1->value)+1)*sizeof(char)); |
---|
478 | sprintf(tmpName,"%s",tmpMap1->value); |
---|
479 | }else{ |
---|
480 | char* tmpStr=zStrdup(tmpName); |
---|
481 | tmpName=(char*) realloc(tmpName,(strlen(tmpStr)+strlen(tmpMap1->value)+1)*sizeof(char)); |
---|
482 | sprintf(tmpName,"%s%s",tmpStr,tmpMap1->value); |
---|
483 | free(tmpStr); |
---|
484 | } |
---|
485 | } |
---|
486 | json_object_object_add(res,"id",json_object_new_string(tmpName)); |
---|
487 | if(tmpName!=NULL){ |
---|
488 | rUrl=zStrdup(tmpName); |
---|
489 | free(tmpName); |
---|
490 | } |
---|
491 | } |
---|
492 | else |
---|
493 | json_object_object_add(res,"id",json_object_new_string(serv->name)); |
---|
494 | if(serv->content!=NULL){ |
---|
495 | map* tmpMap=getMap(serv->content,"title"); |
---|
496 | if(tmpMap!=NULL){ |
---|
497 | json_object_object_add(res,"title",json_object_new_string(tmpMap->value)); |
---|
498 | } |
---|
499 | tmpMap=getMap(serv->content,"abstract"); |
---|
500 | if(tmpMap!=NULL){ |
---|
501 | json_object_object_add(res,"description",json_object_new_string(tmpMap->value)); |
---|
502 | } |
---|
503 | tmpMap=getMap(serv->content,"processVersion"); |
---|
504 | if(tmpMap!=NULL){ |
---|
505 | if(strlen(tmpMap->value)<5){ |
---|
506 | char *val=(char*)malloc((strlen(tmpMap->value)+5)*sizeof(char)); |
---|
507 | sprintf(val,"%s.0.0",tmpMap->value); |
---|
508 | json_object_object_add(res,"version",json_object_new_string(val)); |
---|
509 | free(val); |
---|
510 | } |
---|
511 | else |
---|
512 | json_object_object_add(res,"version",json_object_new_string(tmpMap->value)); |
---|
513 | }else |
---|
514 | json_object_object_add(res,"version",json_object_new_string("1.0.0")); |
---|
515 | int limit=4; |
---|
516 | int i=0; |
---|
517 | map* sType=getMap(serv->content,"serviceType"); |
---|
518 | for(;i<limit;i+=2){ |
---|
519 | json_object *res1=json_object_new_object(); |
---|
520 | json_object *res2=json_object_new_array(); |
---|
521 | char *saveptr; |
---|
522 | char* dupStr=strdup(jcapabilities[i+1]); |
---|
523 | char *tmps = strtok_r (dupStr, " ", &saveptr); |
---|
524 | while(tmps!=NULL){ |
---|
525 | json_object_array_add(res2,json_object_new_string(tmps)); |
---|
526 | tmps = strtok_r (NULL, " ", &saveptr); |
---|
527 | } |
---|
528 | free(dupStr); |
---|
529 | json_object_object_add(res,jcapabilities[i],res2); |
---|
530 | } |
---|
531 | json_object *res1=json_object_new_array(); |
---|
532 | json_object *res2=json_object_new_object(); |
---|
533 | json_object *res3=json_object_new_object(); |
---|
534 | map* pmTmp=getMapFromMaps(m,"lenv","requestType"); |
---|
535 | if(pmTmp!=NULL && strncasecmp(pmTmp->value,"desc",4)==0) |
---|
536 | json_object_object_add(res2,"rel",json_object_new_string("process-desc")); |
---|
537 | else{ |
---|
538 | json_object_object_add(res2,"rel",json_object_new_string("execute")); |
---|
539 | json_object_object_add(res3,"rel",json_object_new_string("alternate")); |
---|
540 | json_object_object_add(res3,"type",json_object_new_string("text/html")); |
---|
541 | } |
---|
542 | json_object_object_add(res2,"type",json_object_new_string("application/json")); |
---|
543 | json_object_object_add(res2,"title",json_object_new_string("Process Description")); |
---|
544 | map* tmpUrl=getMapFromMaps(m,"openapi","rootUrl"); |
---|
545 | char* tmpStr=(char*) malloc((strlen(tmpUrl->value)+strlen(rUrl)+13)*sizeof(char)); |
---|
546 | sprintf(tmpStr,"%s/processes/%s/",tmpUrl->value,rUrl); |
---|
547 | if(doc==NULL){ |
---|
548 | json_object_object_add(res2,"title",json_object_new_string("Execute End Point")); |
---|
549 | json_object_object_add(res3,"title",json_object_new_string("Execute End Point")); |
---|
550 | char* tmpStr1=zStrdup(tmpStr); |
---|
551 | tmpStr=(char*) realloc(tmpStr,(strlen(tmpStr)+6)*sizeof(char)); |
---|
552 | sprintf(tmpStr,"%sjobs",tmpStr1); |
---|
553 | free(tmpStr1); |
---|
554 | char* tmpStr3=(char*) malloc((strlen(tmpStr)+6)*sizeof(char)); |
---|
555 | sprintf(tmpStr3,"%s.html",tmpStr); |
---|
556 | json_object_object_add(res3,"href",json_object_new_string(tmpStr3)); |
---|
557 | free(tmpStr3); |
---|
558 | } |
---|
559 | json_object_object_add(res2,"href",json_object_new_string(tmpStr)); |
---|
560 | free(tmpStr); |
---|
561 | json_object_array_add(res1,res2); |
---|
562 | tmpUrl=getMapFromMaps(m,"openapi","partial_html_support"); |
---|
563 | if(tmpUrl!=NULL && strncasecmp(tmpUrl->value,"true",4)==0) |
---|
564 | json_object_array_add(res1,res3); |
---|
565 | else |
---|
566 | json_object_put(res3); |
---|
567 | json_object_object_add(res,"links",res1); |
---|
568 | } |
---|
569 | if(doc==NULL){ |
---|
570 | elements* in=serv->inputs; |
---|
571 | json_object* inputs=json_object_new_array(); |
---|
572 | printIOTypeJ(m,"input",in,inputs,serv); |
---|
573 | json_object_object_add(res,"inputs",inputs); |
---|
574 | |
---|
575 | in=serv->outputs; |
---|
576 | json_object* outputs=json_object_new_array(); |
---|
577 | printIOTypeJ(m,"output",in,outputs,serv); |
---|
578 | json_object_object_add(res,"outputs",outputs); |
---|
579 | |
---|
580 | } |
---|
581 | if(strcmp(rUrl,serv->name)!=0) |
---|
582 | free(rUrl); |
---|
583 | if(doc!=NULL) |
---|
584 | json_object_array_add(doc,res); |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * Print an OWS ExceptionReport Document and HTTP headers (when required) |
---|
589 | * depending on the code. |
---|
590 | * Set hasPrinted value to true in the [lenv] section. |
---|
591 | * |
---|
592 | * @param m the maps containing the settings of the main.cfg file |
---|
593 | * @param s the map containing the text,code,locator keys (or a map array of the same keys) |
---|
594 | */ |
---|
595 | void printExceptionReportResponseJ(maps* m,map* s){ |
---|
596 | if(getMapFromMaps(m,"lenv","hasPrinted")!=NULL) |
---|
597 | return; |
---|
598 | int buffersize; |
---|
599 | json_object *res=json_object_new_object(); |
---|
600 | |
---|
601 | maps* tmpMap=getMaps(m,"main"); |
---|
602 | const char *exceptionCode; |
---|
603 | |
---|
604 | map* tmp=getMap(s,"code"); |
---|
605 | exceptionCode=produceStatusString(m,tmp); |
---|
606 | if(tmp!=NULL){ |
---|
607 | json_object_object_add(res,"code",json_object_new_string(tmp->value)); |
---|
608 | } |
---|
609 | else{ |
---|
610 | json_object_object_add(res,"code",json_object_new_string("NoApplicableCode")); |
---|
611 | } |
---|
612 | if(getMapFromMaps(m,"lenv","no-headers")==NULL) |
---|
613 | printHeaders(m); |
---|
614 | |
---|
615 | tmp=getMapFromMaps(m,"lenv","status_code"); |
---|
616 | if(tmp!=NULL) |
---|
617 | exceptionCode=tmp->value; |
---|
618 | if(getMapFromMaps(m,"lenv","no-headers")==NULL){ |
---|
619 | if(m!=NULL){ |
---|
620 | map *tmpSid=getMapFromMaps(m,"lenv","sid"); |
---|
621 | if(tmpSid!=NULL){ |
---|
622 | if( getpid()==atoi(tmpSid->value) ){ |
---|
623 | printf("Status: %s\r\n\r\n",exceptionCode); |
---|
624 | } |
---|
625 | } |
---|
626 | else{ |
---|
627 | printf("Status: %s\r\n\r\n",exceptionCode); |
---|
628 | } |
---|
629 | }else{ |
---|
630 | printf("Status: %s\r\n\r\n",exceptionCode); |
---|
631 | } |
---|
632 | } |
---|
633 | tmp=getMap(s,"text"); |
---|
634 | if(tmp==NULL) |
---|
635 | tmp=getMap(s,"message"); |
---|
636 | if(tmp==NULL) |
---|
637 | tmp=getMapFromMaps(m,"lenv","message"); |
---|
638 | if(tmp!=NULL) |
---|
639 | json_object_object_add(res,"description",json_object_new_string(tmp->value)); |
---|
640 | const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); |
---|
641 | if(getMapFromMaps(m,"lenv","jsonStr")==NULL) |
---|
642 | setMapInMaps(m,"lenv","jsonStr",jsonStr); |
---|
643 | maps* pmsTmp=getMaps(m,"lenv"); |
---|
644 | printf(jsonStr); |
---|
645 | if(m!=NULL) |
---|
646 | setMapInMaps(m,"lenv","hasPrinted","true"); |
---|
647 | } |
---|
648 | |
---|
649 | /** |
---|
650 | * Parse LiteralData value |
---|
651 | * @param conf the maps containing the settings of the main.cfg file |
---|
652 | * @param req json_object pointing to the input/output |
---|
653 | * @param element |
---|
654 | * @param output the maps to set current json structure |
---|
655 | */ |
---|
656 | void parseJLiteral(maps* conf,json_object* req,elements* element,maps* output){ |
---|
657 | json_object* json_cinput=NULL; |
---|
658 | if(json_object_object_get_ex(req,"value",&json_cinput)!=FALSE){ |
---|
659 | output->content=createMap("value",json_object_get_string(json_cinput)); |
---|
660 | } |
---|
661 | const char* tmpStrs[2]={ |
---|
662 | "dataType", |
---|
663 | "uom" |
---|
664 | }; |
---|
665 | for(int i=0;i<2;i++) |
---|
666 | if(json_object_object_get_ex(req,tmpStrs[i],&json_cinput)!=FALSE){ |
---|
667 | json_object* json_cinput1; |
---|
668 | if(json_object_object_get_ex(json_cinput,"name",&json_cinput1)!=FALSE){ |
---|
669 | if(output->content==NULL) |
---|
670 | output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput1)); |
---|
671 | else |
---|
672 | addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput1)); |
---|
673 | } |
---|
674 | if(json_object_object_get_ex(json_cinput,"reference",&json_cinput1)!=FALSE){ |
---|
675 | if(output->content==NULL) |
---|
676 | output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput1)); |
---|
677 | else |
---|
678 | addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput1)); |
---|
679 | } |
---|
680 | } |
---|
681 | if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ |
---|
682 | if(output->content==NULL) |
---|
683 | output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); |
---|
684 | else |
---|
685 | addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * Parse ComplexData value |
---|
691 | * @param conf the maps containing the settings of the main.cfg file |
---|
692 | * @param req json_object pointing to the input/output |
---|
693 | * @param element |
---|
694 | * @param output the maps to set current json structure |
---|
695 | * @param name the name of the request from http_requests |
---|
696 | */ |
---|
697 | void parseJComplex(maps* conf,json_object* req,elements* element,maps* output,const char* name){ |
---|
698 | json_object* json_cinput=NULL; |
---|
699 | if(json_object_object_get_ex(req,"value",&json_cinput)!=FALSE){ |
---|
700 | output->content=createMap("value",json_object_get_string(json_cinput)); |
---|
701 | } |
---|
702 | else{ |
---|
703 | json_object* json_value=NULL; |
---|
704 | if(json_object_object_get_ex(req,"href",&json_value)!=FALSE){ |
---|
705 | output->content=createMap("xlink:href",json_object_get_string(json_value)); |
---|
706 | int len=0; |
---|
707 | int createdStr=0; |
---|
708 | char *tmpStr="url"; |
---|
709 | char *tmpStr1="input"; |
---|
710 | if(getMaps(conf,"http_requests")==NULL){ |
---|
711 | maps* tmpMaps=createMaps("http_requests"); |
---|
712 | tmpMaps->content=createMap("length","1"); |
---|
713 | addMapsToMaps(&conf,tmpMaps); |
---|
714 | freeMaps(&tmpMaps); |
---|
715 | free(tmpMaps); |
---|
716 | }else{ |
---|
717 | map* tmpMap=getMapFromMaps(conf,"http_requests","length"); |
---|
718 | int len=atoi(tmpMap->value); |
---|
719 | createdStr=1; |
---|
720 | tmpStr=(char*) malloc((12)*sizeof(char)); |
---|
721 | sprintf(tmpStr,"%d",len+1); |
---|
722 | setMapInMaps(conf,"http_requests","length",tmpStr); |
---|
723 | sprintf(tmpStr,"url_%d",len); |
---|
724 | tmpStr1=(char*) malloc((14)*sizeof(char)); |
---|
725 | sprintf(tmpStr1,"input_%d",len); |
---|
726 | } |
---|
727 | setMapInMaps(conf,"http_requests",tmpStr,json_object_get_string(json_value)); |
---|
728 | setMapInMaps(conf,"http_requests",tmpStr1,name); |
---|
729 | if(createdStr>0){ |
---|
730 | free(tmpStr); |
---|
731 | free(tmpStr1); |
---|
732 | } |
---|
733 | } |
---|
734 | } |
---|
735 | if(json_object_object_get_ex(req,"format",&json_cinput)!=FALSE){ |
---|
736 | json_object_object_foreach(json_cinput, key, val) { |
---|
737 | if(output->content==NULL) |
---|
738 | output->content=createMap(key,json_object_get_string(val)); |
---|
739 | else |
---|
740 | addToMap(output->content,key,json_object_get_string(val)); |
---|
741 | |
---|
742 | } |
---|
743 | } |
---|
744 | if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ |
---|
745 | if(output->content==NULL) |
---|
746 | output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); |
---|
747 | else |
---|
748 | addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | /** |
---|
753 | * Parse BoundingBox value |
---|
754 | * @param conf the maps containing the settings of the main.cfg file |
---|
755 | * @param req json_object pointing to the input/output |
---|
756 | * @param element |
---|
757 | * @param output the maps to set current json structure |
---|
758 | */ |
---|
759 | void parseJBoundingBox(maps* conf,json_object* req,elements* element,maps* output){ |
---|
760 | json_object* json_cinput=NULL; |
---|
761 | if(json_object_object_get_ex(req,"bbox",&json_cinput)!=FALSE){ |
---|
762 | output->content=createMap("value",json_object_get_string(json_cinput)); |
---|
763 | } |
---|
764 | if(json_object_object_get_ex(req,"crs",&json_cinput)!=FALSE){ |
---|
765 | if(output->content==NULL) |
---|
766 | output->content=createMap("crs",json_object_get_string(json_cinput)); |
---|
767 | else |
---|
768 | addToMap(output->content,"crs","http://www.opengis.net/def/crs/OGC/1.3/CRS84"); |
---|
769 | } |
---|
770 | char* tmpStrs[2]={ |
---|
771 | "lowerCorner", |
---|
772 | "upperCorner" |
---|
773 | }; |
---|
774 | for(int i=0;i<2;i++) |
---|
775 | if(json_object_object_get_ex(req,tmpStrs[i],&json_cinput)!=FALSE){ |
---|
776 | if(output->content==NULL) |
---|
777 | output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput)); |
---|
778 | else |
---|
779 | addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput)); |
---|
780 | } |
---|
781 | if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ |
---|
782 | if(output->content==NULL) |
---|
783 | output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); |
---|
784 | else |
---|
785 | addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); |
---|
786 | } |
---|
787 | } |
---|
788 | |
---|
789 | /** |
---|
790 | * Parse a single input / output entity |
---|
791 | * |
---|
792 | * @param conf the maps containing the settings of the main.cfg file |
---|
793 | * @param ioElements the elements extracted from the zcfg |
---|
794 | * @param ioMaps the produced maps containing inputs (or outputs) |
---|
795 | * @param ioType the char* set to inputs or outputs |
---|
796 | * @param key char* the input/output name |
---|
797 | * @param val json_object pointing to the input/output |
---|
798 | * @param cMaps the outputed maps containing input (or output) metedata |
---|
799 | */ |
---|
800 | void _parseJIOSingle(maps* conf,elements* cio, maps** ioMaps,const char* ioType,const char* key,json_object* val,maps* cMaps){ |
---|
801 | json_object* json_cinput; |
---|
802 | if(json_object_object_get_ex(val,"dataType",&json_cinput)!=FALSE){ |
---|
803 | parseJLiteral(conf,val,cio,cMaps); |
---|
804 | } else if(json_object_object_get_ex(val,"format",&json_cinput)!=FALSE){ |
---|
805 | parseJComplex(conf,val,cio,cMaps,key); |
---|
806 | } else if(json_object_object_get_ex(val,"bbox",&json_cinput)!=FALSE){ |
---|
807 | parseJBoundingBox(conf,val,cio,cMaps); |
---|
808 | }// else error! |
---|
809 | else{ |
---|
810 | if(json_object_object_get_ex(val,"value",&json_cinput)!=FALSE){ |
---|
811 | map* error=createMap("code","BadRequest"); |
---|
812 | char tmpS[1024]; |
---|
813 | sprintf(tmpS,_("Missing input for %s"),cio->name); |
---|
814 | addToMap(error,"message",tmpS); |
---|
815 | setMapInMaps(conf,"lenv","status_code","400 Bad Request"); |
---|
816 | printExceptionReportResponseJ(conf,error); |
---|
817 | return; |
---|
818 | }else{ |
---|
819 | if(json_object_get_type(json_cinput)==json_type_string){ |
---|
820 | parseJLiteral(conf,val,cio,cMaps); |
---|
821 | }else if(json_object_get_type(json_cinput)==json_type_object){ |
---|
822 | json_object* json_ccinput=NULL; |
---|
823 | if(json_object_object_get_ex(json_cinput,"bbox",&json_ccinput)!=FALSE){ |
---|
824 | parseJComplex(conf,val,cio,cMaps,key); |
---|
825 | } |
---|
826 | else{ |
---|
827 | parseJBoundingBox(conf,val,cio,cMaps); |
---|
828 | } |
---|
829 | }else{ |
---|
830 | if(strcmp(ioType,"input")==0){ |
---|
831 | map* error=createMap("code","BadRequest"); |
---|
832 | char tmpS1[1024]; |
---|
833 | sprintf(tmpS1,_("Issue with input %s"),cio->name); |
---|
834 | addToMap(error,"message",tmpS1); |
---|
835 | setMapInMaps(conf,"lenv","status_code","400 Bad Request"); |
---|
836 | printExceptionReportResponseJ(conf,error); |
---|
837 | return; |
---|
838 | } |
---|
839 | } |
---|
840 | } |
---|
841 | } |
---|
842 | } |
---|
843 | |
---|
844 | /** |
---|
845 | * Parse a single input / output entity that can be an array |
---|
846 | * |
---|
847 | * @param conf the maps containing the settings of the main.cfg file |
---|
848 | * @param ioElements the elements extracted from the zcfg |
---|
849 | * @param ioMaps the produced maps containing inputs (or outputs) |
---|
850 | * @param ioType the char* set to inputs or outputs |
---|
851 | * @param key char* the input/output name |
---|
852 | * @param val json_object pointing to the input/output |
---|
853 | */ |
---|
854 | void parseJIOSingle(maps* conf,elements* ioElements, maps** ioMaps,const char* ioType,const char* key,json_object* val){ |
---|
855 | maps *cMaps=NULL; |
---|
856 | cMaps=createMaps(key); |
---|
857 | elements* cio=getElements(ioElements,key); |
---|
858 | if(json_object_is_type(val,json_type_array)){ |
---|
859 | int i=0; |
---|
860 | size_t len=json_object_array_length(val); |
---|
861 | for(i=0;i<len;i++){ |
---|
862 | json_object* json_current_io=json_object_array_get_idx(val,i); |
---|
863 | maps* pmsExtra=createMaps(key); |
---|
864 | _parseJIOSingle(conf,cio,ioMaps,ioType,key,json_current_io,pmsExtra); |
---|
865 | map* pmCtx=pmsExtra->content; |
---|
866 | while(pmCtx!=NULL){ |
---|
867 | if(cMaps->content==NULL) |
---|
868 | cMaps->content=createMap(pmCtx->name,pmCtx->value); |
---|
869 | else |
---|
870 | setMapArray(cMaps->content,pmCtx->name,i,pmCtx->value); |
---|
871 | pmCtx=pmCtx->next; |
---|
872 | } |
---|
873 | freeMaps(&pmsExtra); |
---|
874 | free(pmsExtra); |
---|
875 | } |
---|
876 | }else{ |
---|
877 | _parseJIOSingle(conf,cio,ioMaps,ioType,key,val,cMaps); |
---|
878 | } |
---|
879 | |
---|
880 | |
---|
881 | if(strcmp(ioType,"outputs")==0){ |
---|
882 | // Outputs |
---|
883 | json_object* json_cinput; |
---|
884 | if(json_object_object_get_ex(val,"transmissionMode",&json_cinput)!=FALSE){ |
---|
885 | if(cMaps->content==NULL) |
---|
886 | cMaps->content=createMap("transmissionMode",json_object_get_string(json_cinput)); |
---|
887 | else |
---|
888 | addToMap(cMaps->content,"transmissionMode",json_object_get_string(json_cinput)); |
---|
889 | } |
---|
890 | if(json_object_object_get_ex(val,"format",&json_cinput)!=FALSE){ |
---|
891 | json_object_object_foreach(json_cinput, key1, val1) { |
---|
892 | if(cMaps->content==NULL) |
---|
893 | cMaps->content=createMap(key1,json_object_get_string(val1)); |
---|
894 | else |
---|
895 | addToMap(cMaps->content,key1,json_object_get_string(val1)); |
---|
896 | } |
---|
897 | } |
---|
898 | } |
---|
899 | |
---|
900 | addToMap(cMaps->content,"inRequest","true"); |
---|
901 | if (ioMaps == NULL) |
---|
902 | *ioMaps = dupMaps(&cMaps); |
---|
903 | else |
---|
904 | addMapsToMaps (ioMaps, cMaps); |
---|
905 | |
---|
906 | freeMaps(&cMaps); |
---|
907 | free(cMaps); |
---|
908 | } |
---|
909 | |
---|
910 | /** |
---|
911 | * Parse all the inputs / outputs entities |
---|
912 | * |
---|
913 | * @param conf the maps containing the settings of the main.cfg file |
---|
914 | * @param req json_object pointing to the input/output |
---|
915 | * @param ioElements the elements extracted from the zcfg |
---|
916 | * @param ioMaps the produced maps containing inputs (or outputs) |
---|
917 | * @param ioType the char* set to inputs or outputs |
---|
918 | */ |
---|
919 | void parseJIO(maps* conf, json_object* req, elements* ioElements, maps** ioMaps,const char* ioType){ |
---|
920 | json_object* json_io=NULL; |
---|
921 | if(json_object_object_get_ex(req,ioType,&json_io)!=FALSE){ |
---|
922 | json_object* json_current_io=NULL; |
---|
923 | if(json_object_is_type(json_io,json_type_array)){ |
---|
924 | size_t len=json_object_array_length(json_io); |
---|
925 | for(int i=0;i<len;i++){ |
---|
926 | maps *cMaps=NULL; |
---|
927 | json_current_io=json_object_array_get_idx(json_io,i); |
---|
928 | json_object* cname=NULL; |
---|
929 | if(json_object_object_get_ex(json_current_io,"id",&cname)!=FALSE) { |
---|
930 | json_object* json_input=NULL; |
---|
931 | if(json_object_object_get_ex(json_current_io,"input",&json_input)!=FALSE) { |
---|
932 | parseJIOSingle(conf,ioElements,ioMaps,ioType,json_object_get_string(cname),json_input); |
---|
933 | }else |
---|
934 | parseJIOSingle(conf,ioElements,ioMaps,ioType,json_object_get_string(cname),json_current_io); |
---|
935 | } |
---|
936 | } |
---|
937 | }else{ |
---|
938 | json_object_object_foreach(json_io, key, val) { |
---|
939 | parseJIOSingle(conf,ioElements,ioMaps,ioType,key,val); |
---|
940 | } |
---|
941 | } |
---|
942 | } |
---|
943 | } |
---|
944 | |
---|
945 | |
---|
946 | |
---|
947 | /** |
---|
948 | * Parse Json Request |
---|
949 | * @param conf the maps containing the settings of the main.cfg file |
---|
950 | * @param s the current service metadata |
---|
951 | * @param req the JSON object of the request body |
---|
952 | * @param inputs the produced maps |
---|
953 | * @param outputs the produced maps |
---|
954 | */ |
---|
955 | void parseJRequest(maps* conf, service* s,json_object* req, map* request_inputs, maps** inputs,maps** outputs){ |
---|
956 | elements* io=s->inputs; |
---|
957 | |
---|
958 | json_object* json_io=NULL; |
---|
959 | int parsed=0; |
---|
960 | char* tmpS="input"; |
---|
961 | maps* in=*inputs; |
---|
962 | parseJIO(conf,req,s->inputs,inputs,"inputs"); |
---|
963 | parseJIO(conf,req,s->outputs,outputs,"outputs"); |
---|
964 | |
---|
965 | if(parsed==0){ |
---|
966 | json_io=NULL; |
---|
967 | if(json_object_object_get_ex(req,"mode",&json_io)!=FALSE){ |
---|
968 | addToMap(request_inputs,"mode",json_object_get_string(json_io)); |
---|
969 | setMapInMaps(conf,"request","mode",json_object_get_string(json_io)); |
---|
970 | } |
---|
971 | json_io=NULL; |
---|
972 | if(json_object_object_get_ex(req,"response",&json_io)!=FALSE){ |
---|
973 | addToMap(request_inputs,"response",json_object_get_string(json_io)); |
---|
974 | setMapInMaps(conf,"request","response",json_object_get_string(json_io)); |
---|
975 | } |
---|
976 | json_io=NULL; |
---|
977 | if(json_object_object_get_ex(req,"subscriber",&json_io)!=FALSE){ |
---|
978 | maps* subscribers=createMaps("subscriber"); |
---|
979 | json_object* json_subscriber=NULL; |
---|
980 | if(json_object_object_get_ex(json_io,"successUri",&json_subscriber)!=FALSE){ |
---|
981 | subscribers->content=createMap("successUri",json_object_get_string(json_subscriber)); |
---|
982 | } |
---|
983 | if(json_object_object_get_ex(json_io,"inProgressUri",&json_subscriber)!=FALSE){ |
---|
984 | if(subscribers->content==NULL) |
---|
985 | subscribers->content=createMap("inProgressUri",json_object_get_string(json_subscriber)); |
---|
986 | else |
---|
987 | addToMap(subscribers->content,"inProgressUri",json_object_get_string(json_subscriber)); |
---|
988 | } |
---|
989 | if(json_object_object_get_ex(json_io,"failedUri",&json_subscriber)!=FALSE){ |
---|
990 | if(subscribers->content==NULL) |
---|
991 | subscribers->content=createMap("failedUri",json_object_get_string(json_subscriber)); |
---|
992 | else |
---|
993 | addToMap(subscribers->content,"failedUri",json_object_get_string(json_subscriber)); |
---|
994 | } |
---|
995 | addMapsToMaps(&conf,subscribers); |
---|
996 | freeMaps(&subscribers); |
---|
997 | free(subscribers); |
---|
998 | } |
---|
999 | } |
---|
1000 | |
---|
1001 | } |
---|
1002 | |
---|
1003 | /** |
---|
1004 | * Print the jobs status info |
---|
1005 | * cf. |
---|
1006 | * |
---|
1007 | * @param conf the maps containing the settings of the main.cfg file |
---|
1008 | * @return the JSON object pointer to the jobs list |
---|
1009 | */ |
---|
1010 | json_object* printJobStatus(maps* pmsConf,char* pcJobId){ |
---|
1011 | json_object* pjoRes=NULL; |
---|
1012 | runGetStatus(pmsConf,pcJobId,"GetStatus"); |
---|
1013 | map* pmError=getMapFromMaps(pmsConf,"lenv","error"); |
---|
1014 | if(pmError!=NULL && strncasecmp(pmError->value,"true",4)==0){ |
---|
1015 | printExceptionReportResponseJ(pmsConf,getMapFromMaps(pmsConf,"lenv","code")); |
---|
1016 | return NULL; |
---|
1017 | }else{ |
---|
1018 | map* pmStatus=getMapFromMaps(pmsConf,"lenv","status"); |
---|
1019 | setMapInMaps(pmsConf,"lenv","gs_location","false"); |
---|
1020 | setMapInMaps(pmsConf,"lenv","gs_usid",pcJobId); |
---|
1021 | if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Failed",6)==0) |
---|
1022 | pjoRes=createStatus(pmsConf,SERVICE_FAILED); |
---|
1023 | else |
---|
1024 | if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Succeeded",9)==0) |
---|
1025 | pjoRes=createStatus(pmsConf,SERVICE_SUCCEEDED); |
---|
1026 | else |
---|
1027 | if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Running",7)==0){ |
---|
1028 | map* tmpMap=getMapFromMaps(pmsConf,"lenv","Message"); |
---|
1029 | if(tmpMap!=NULL) |
---|
1030 | setMapInMaps(pmsConf,"lenv","gs_message",tmpMap->value); |
---|
1031 | pjoRes=createStatus(pmsConf,SERVICE_STARTED); |
---|
1032 | } |
---|
1033 | else |
---|
1034 | pjoRes=createStatus(pmsConf,SERVICE_FAILED); |
---|
1035 | } |
---|
1036 | return pjoRes; |
---|
1037 | } |
---|
1038 | |
---|
1039 | /** |
---|
1040 | * Print the jobs list |
---|
1041 | * |
---|
1042 | * @param conf the maps containing the settings of the main.cfg file |
---|
1043 | * @return the JSON object pointer to the jobs list |
---|
1044 | */ |
---|
1045 | json_object* printJobList(maps* conf){ |
---|
1046 | json_object* res=json_object_new_array(); |
---|
1047 | map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); |
---|
1048 | map* oIdentifier=getMapFromMaps(conf,"lenv","oIdentifier"); |
---|
1049 | char* cpath=(char*)malloc((strlen(tmpPath->value)+strlen(oIdentifier->value)+14)*sizeof(char)); |
---|
1050 | sprintf(cpath,"%s/statusInfos/%s",tmpPath->value,oIdentifier->value); |
---|
1051 | struct dirent *dp; |
---|
1052 | DIR *dirp = opendir (cpath); |
---|
1053 | if(dirp!=NULL){ |
---|
1054 | while ((dp = readdir (dirp)) != NULL){ |
---|
1055 | char* extn = strstr(dp->d_name, ".json"); |
---|
1056 | if(extn!=NULL){ |
---|
1057 | char* tmpStr=zStrdup(dp->d_name); |
---|
1058 | tmpStr[strlen(dp->d_name)-5]=0; |
---|
1059 | json_object* cjob=json_object_new_object(); |
---|
1060 | json_object_object_add(cjob,"id",json_object_new_string(tmpStr)); |
---|
1061 | json_object *jobj=printJobStatus(conf,tmpStr); |
---|
1062 | if(jobj!=NULL) |
---|
1063 | json_object_object_add(cjob,"infos",jobj); |
---|
1064 | free(tmpStr); |
---|
1065 | json_object_array_add(res,cjob); |
---|
1066 | } |
---|
1067 | } |
---|
1068 | closedir (dirp); |
---|
1069 | } |
---|
1070 | return res; |
---|
1071 | } |
---|
1072 | |
---|
1073 | /** |
---|
1074 | * Print the result of an execution |
---|
1075 | * |
---|
1076 | * @param conf the maps containing the settings of the main.cfg file |
---|
1077 | * @param s service pointer to metadata |
---|
1078 | * @param result outputs of the service |
---|
1079 | * @param res the status of execution SERVICE_FAILED/SERVICE_SUCCEEDED |
---|
1080 | * @return the JSON object pointer to the result |
---|
1081 | */ |
---|
1082 | json_object* printJResult(maps* conf,service* s,maps* result,int res){ |
---|
1083 | if(res==SERVICE_FAILED){ |
---|
1084 | char* pacTmp=produceErrorMessage(conf); |
---|
1085 | map* pamTmp=createMap("message",pacTmp); |
---|
1086 | free(pacTmp); |
---|
1087 | map* pmTmp=getMapFromMaps(conf,"lenv","code"); |
---|
1088 | if(pmTmp!=NULL) |
---|
1089 | addToMap(pamTmp,"code",pmTmp->value); |
---|
1090 | printExceptionReportResponseJ(conf,pamTmp); |
---|
1091 | freeMap(&pamTmp); |
---|
1092 | free(pamTmp); |
---|
1093 | return NULL; |
---|
1094 | } |
---|
1095 | if(getMapFromMaps(conf,"lenv","no-headers")==NULL) |
---|
1096 | printHeaders(conf); |
---|
1097 | map* pmMode=getMapFromMaps(conf,"request","response"); |
---|
1098 | if(pmMode!=NULL && strncasecmp(pmMode->value,"raw",3)==0){ |
---|
1099 | maps* resu=result; |
---|
1100 | printRawdataOutput(conf,resu); |
---|
1101 | map* pmError=getMapFromMaps(conf,"lenv","error"); |
---|
1102 | if(pmError!=NULL && strncasecmp(pmError->value,"true",4)==0){ |
---|
1103 | printExceptionReportResponseJ(conf,pmError); |
---|
1104 | } |
---|
1105 | return NULL; |
---|
1106 | }else{ |
---|
1107 | json_object* eres1=json_object_new_object(); |
---|
1108 | map* pmIOAsArray=getMapFromMaps(conf,"openapi","io_as_array"); |
---|
1109 | json_object* eres; |
---|
1110 | if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0) |
---|
1111 | eres=json_object_new_array(); |
---|
1112 | else |
---|
1113 | eres=json_object_new_object(); |
---|
1114 | maps* resu=result; |
---|
1115 | int itn=0; |
---|
1116 | while(resu!=NULL){ |
---|
1117 | json_object* res1=json_object_new_object(); |
---|
1118 | if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0) |
---|
1119 | json_object_object_add(res1,"id",json_object_new_string(resu->name)); |
---|
1120 | map* tmpMap=getMap(resu->content,"mimeType"); |
---|
1121 | json_object* res3=json_object_new_object(); |
---|
1122 | if(tmpMap!=NULL) |
---|
1123 | json_object_object_add(res3,"mimeType",json_object_new_string(tmpMap->value)); |
---|
1124 | else{ |
---|
1125 | json_object_object_add(res3,"mimeType",json_object_new_string("text/plain")); |
---|
1126 | map* pmDataType=getMap(resu->content,"dataType"); |
---|
1127 | if(pmDataType!=NULL){ |
---|
1128 | json_object* pjoTmp3=json_object_new_object(); |
---|
1129 | json_object_object_add(pjoTmp3,"name",json_object_new_string(pmDataType->value)); |
---|
1130 | json_object_object_add(res1,"dataType",pjoTmp3); |
---|
1131 | } |
---|
1132 | } |
---|
1133 | if((tmpMap=getMap(resu->content,"value"))!=NULL || |
---|
1134 | (getMap(resu->content,"generated_file"))!=NULL){ |
---|
1135 | //json_object* res2=json_object_new_object(); |
---|
1136 | map* tmpMap1=NULL; |
---|
1137 | if((tmpMap1=getMap(resu->content,"transmissionMode"))!=NULL) { |
---|
1138 | if(strcmp(tmpMap1->value,"value")==0) { |
---|
1139 | map* tmpMap2=getMap(resu->content,"mimeType"); |
---|
1140 | if(tmpMap2!=NULL && strstr(tmpMap2->value,"json")!=NULL){ |
---|
1141 | json_object *jobj = NULL; |
---|
1142 | int slen = 0; |
---|
1143 | enum json_tokener_error jerr; |
---|
1144 | struct json_tokener* tok=json_tokener_new(); |
---|
1145 | do { |
---|
1146 | slen = strlen(tmpMap->value); |
---|
1147 | jobj = json_tokener_parse_ex(tok, tmpMap->value, slen); |
---|
1148 | } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); |
---|
1149 | if (jerr != json_tokener_success) { |
---|
1150 | fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); |
---|
1151 | return eres1; |
---|
1152 | } |
---|
1153 | if (tok->char_offset < slen){ |
---|
1154 | return eres1; |
---|
1155 | } |
---|
1156 | json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); |
---|
1157 | json_object_object_add(res1,"value",jobj); |
---|
1158 | }else{ |
---|
1159 | map* tmp1=getMapFromMaps(conf,"main","tmpPath"); |
---|
1160 | map *gfile=getMap(resu->content,"generated_file"); |
---|
1161 | if(gfile!=NULL){ |
---|
1162 | gfile=getMap(resu->content,"expected_generated_file"); |
---|
1163 | if(gfile==NULL){ |
---|
1164 | gfile=getMap(resu->content,"generated_file"); |
---|
1165 | } |
---|
1166 | FILE* pfData=fopen(gfile->value,"rb"); |
---|
1167 | if(pfData!=NULL){ |
---|
1168 | zStatStruct f_status; |
---|
1169 | int s=zStat(gfile->value, &f_status); |
---|
1170 | char* pcaTmp=(char*)malloc((f_status.st_size+1)*sizeof(char)); |
---|
1171 | fread(pcaTmp,1,f_status.st_size,pfData); |
---|
1172 | pcaTmp[f_status.st_size]=0; |
---|
1173 | fclose(pfData); |
---|
1174 | json_object_object_add(res1,"value",json_object_new_string(base64(pcaTmp,f_status.st_size))); |
---|
1175 | json_object_object_add(res3,"encoding",json_object_new_string("base64")); |
---|
1176 | free(pcaTmp); |
---|
1177 | } |
---|
1178 | }else{ |
---|
1179 | json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); |
---|
1180 | json_object_object_add(res1,"value",json_object_new_string(tmpMap->value)); |
---|
1181 | } |
---|
1182 | } |
---|
1183 | } |
---|
1184 | else{ |
---|
1185 | char *pcaFileUrl=produceFileUrl(s,conf,resu,NULL,itn); |
---|
1186 | itn++; |
---|
1187 | if(pcaFileUrl!=NULL){ |
---|
1188 | json_object_object_add(res1,"href", |
---|
1189 | json_object_new_string(pcaFileUrl)); |
---|
1190 | free(pcaFileUrl); |
---|
1191 | } |
---|
1192 | } |
---|
1193 | } |
---|
1194 | //json_object_object_add(res1,"value",res2); |
---|
1195 | json_object_object_add(res1,"format",res3); |
---|
1196 | if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0){ |
---|
1197 | json_object_array_add(eres,res1); |
---|
1198 | }else |
---|
1199 | json_object_object_add(eres,resu->name,res1); |
---|
1200 | } |
---|
1201 | resu=resu->next; |
---|
1202 | } |
---|
1203 | json_object_object_add(eres1,"outputs",eres); |
---|
1204 | const char* jsonStr = |
---|
1205 | json_object_to_json_string_ext(eres1,JSON_C_TO_STRING_PLAIN); |
---|
1206 | map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); |
---|
1207 | map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); |
---|
1208 | map *sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1209 | char *pacTmp=(char*)malloc((strlen(tmpPath->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char)); |
---|
1210 | sprintf(pacTmp,"%s/%s_%s.json", |
---|
1211 | tmpPath->value,cIdentifier->value,sessId->value); |
---|
1212 | zStatStruct zsFStatus; |
---|
1213 | int iS=zStat(pacTmp, &zsFStatus); |
---|
1214 | if(iS==0 && zsFStatus.st_size>0){ |
---|
1215 | map* tmpPath1 = getMapFromMaps (conf, "main", "tmpUrl"); |
---|
1216 | char* pacTmpUrl=(char*)malloc((strlen(tmpPath1->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char));; |
---|
1217 | sprintf(pacTmpUrl,"%s/%s_%s.json",tmpPath1->value, |
---|
1218 | cIdentifier->value,sessId->value); |
---|
1219 | if(getMapFromMaps(conf,"lenv","gs_location")==NULL) |
---|
1220 | setMapInMaps(conf,"headers","Location",pacTmpUrl); |
---|
1221 | free(pacTmpUrl); |
---|
1222 | } |
---|
1223 | free(pacTmp); |
---|
1224 | if(res==3){ |
---|
1225 | map* mode=getMapFromMaps(conf,"request","mode"); |
---|
1226 | if(mode!=NULL && strncasecmp(mode->value,"async",5)==0) |
---|
1227 | setMapInMaps(conf,"headers","Status","201 Created"); |
---|
1228 | else |
---|
1229 | setMapInMaps(conf,"headers","Status","200 Ok"); |
---|
1230 | } |
---|
1231 | else{ |
---|
1232 | setMapInMaps(conf,"headers","Status","500 Issue running your service"); |
---|
1233 | } |
---|
1234 | return eres1; |
---|
1235 | } |
---|
1236 | } |
---|
1237 | |
---|
1238 | |
---|
1239 | /** |
---|
1240 | * Create the status links |
---|
1241 | * |
---|
1242 | * @param conf the maps containing the settings of the main.cfg file |
---|
1243 | * @param result an integer (>0 for adding the /result link) |
---|
1244 | * @param obj the JSON object pointer to add the links to |
---|
1245 | * @return 0 |
---|
1246 | */ |
---|
1247 | int createStatusLinks(maps* conf,int result,json_object* obj){ |
---|
1248 | json_object* res=json_object_new_array(); |
---|
1249 | map *tmpPath = getMapFromMaps (conf, "openapi", "rootUrl"); |
---|
1250 | map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); |
---|
1251 | map *sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1252 | if(sessId==NULL){ |
---|
1253 | sessId = getMapFromMaps (conf, "lenv", "gs_usid"); |
---|
1254 | } |
---|
1255 | char *Url0=(char*) malloc((strlen(tmpPath->value)+ |
---|
1256 | strlen(cIdentifier->value)+ |
---|
1257 | strlen(sessId->value)+18)*sizeof(char)); |
---|
1258 | int needResult=-1; |
---|
1259 | char *message, *status; |
---|
1260 | sprintf(Url0,"%s/processes/%s/jobs/%s", |
---|
1261 | tmpPath->value, |
---|
1262 | cIdentifier->value, |
---|
1263 | sessId->value); |
---|
1264 | if(getMapFromMaps(conf,"lenv","gs_location")==NULL) |
---|
1265 | setMapInMaps(conf,"headers","Location",Url0); |
---|
1266 | json_object* val=json_object_new_object(); |
---|
1267 | json_object_object_add(val,"title", |
---|
1268 | json_object_new_string(_("Status location"))); |
---|
1269 | json_object_object_add(val,"rel", |
---|
1270 | json_object_new_string(_("status"))); |
---|
1271 | json_object_object_add(val,"type", |
---|
1272 | json_object_new_string(_("application/json"))); |
---|
1273 | json_object_object_add(val,"href",json_object_new_string(Url0)); |
---|
1274 | json_object_array_add(res,val); |
---|
1275 | if(result>0){ |
---|
1276 | free(Url0); |
---|
1277 | Url0=(char*) malloc((strlen(tmpPath->value)+ |
---|
1278 | strlen(cIdentifier->value)+strlen(sessId->value)+ |
---|
1279 | 25)*sizeof(char)); |
---|
1280 | sprintf(Url0,"%s/processes/%s/jobs/%s/results", |
---|
1281 | tmpPath->value, |
---|
1282 | cIdentifier->value, |
---|
1283 | sessId->value); |
---|
1284 | json_object* val1=json_object_new_object(); |
---|
1285 | json_object_object_add(val1,"title", |
---|
1286 | json_object_new_string(_("Result location"))); |
---|
1287 | json_object_object_add(val1,"rel", |
---|
1288 | json_object_new_string(_("results"))); |
---|
1289 | json_object_object_add(val1,"type", |
---|
1290 | json_object_new_string(_("application/json"))); |
---|
1291 | json_object_object_add(val1,"href",json_object_new_string(Url0)); |
---|
1292 | json_object_array_add(res,val1); |
---|
1293 | } |
---|
1294 | json_object_object_add(obj,"links",res); |
---|
1295 | free(Url0); |
---|
1296 | return 0; |
---|
1297 | } |
---|
1298 | |
---|
1299 | /** |
---|
1300 | * Get the status file path |
---|
1301 | * |
---|
1302 | * @param conf the maps containing the settings of the main.cfg file |
---|
1303 | * @return a char* containing the full status file path |
---|
1304 | */ |
---|
1305 | char* json_getStatusFilePath(maps* conf){ |
---|
1306 | map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); |
---|
1307 | map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); |
---|
1308 | map *sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1309 | if(sessId!=NULL){ |
---|
1310 | sessId = getMapFromMaps (conf, "lenv", "gs_usid"); |
---|
1311 | if(sessId==NULL) |
---|
1312 | sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1313 | }else |
---|
1314 | sessId = getMapFromMaps (conf, "lenv", "gs_usid"); |
---|
1315 | char *tmp1=(char*) malloc((strlen(tmpPath->value)+ |
---|
1316 | strlen(cIdentifier->value)+14)*sizeof(char)); |
---|
1317 | sprintf(tmp1,"%s/statusInfos/%s", |
---|
1318 | tmpPath->value, |
---|
1319 | cIdentifier->value); |
---|
1320 | if(zMkdir(tmp1) != 0 && errno != EEXIST){ |
---|
1321 | fprintf(stderr,"Issue creating directory %s\n",tmp1); |
---|
1322 | return NULL; |
---|
1323 | } |
---|
1324 | tmp1=(char*) realloc(tmp1,(strlen(tmpPath->value)+ |
---|
1325 | strlen(cIdentifier->value)+ |
---|
1326 | strlen(sessId->value)+21)*sizeof(char)); |
---|
1327 | int needResult=0; |
---|
1328 | char *message, *rstatus; |
---|
1329 | sprintf(tmp1,"%s/statusInfos/%s/%s.json", |
---|
1330 | tmpPath->value, |
---|
1331 | cIdentifier->value, |
---|
1332 | sessId->value); |
---|
1333 | return tmp1; |
---|
1334 | } |
---|
1335 | |
---|
1336 | char* getResultPath(maps* conf,char* jobId){ |
---|
1337 | map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); |
---|
1338 | map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); |
---|
1339 | char *pacUrl=(char*) malloc((strlen(tmpPath->value)+ |
---|
1340 | strlen(cIdentifier->value)+ |
---|
1341 | strlen(jobId)+8)*sizeof(char)); |
---|
1342 | sprintf(pacUrl,"%s/%s_%s.json",tmpPath->value, |
---|
1343 | cIdentifier->value,jobId); |
---|
1344 | return pacUrl; |
---|
1345 | } |
---|
1346 | |
---|
1347 | json_object* parseJson(maps* conf,char* myString){ |
---|
1348 | json_object *pajObj = NULL; |
---|
1349 | enum json_tokener_error jerr; |
---|
1350 | struct json_tokener* tok=json_tokener_new(); |
---|
1351 | int slen = 0; |
---|
1352 | do { |
---|
1353 | slen = strlen(myString); |
---|
1354 | pajObj = json_tokener_parse_ex(tok, myString, slen); |
---|
1355 | } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); |
---|
1356 | if (jerr != json_tokener_success) { |
---|
1357 | setMapInMaps(conf,"lenv","message",json_tokener_error_desc(jerr)); |
---|
1358 | fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); |
---|
1359 | return NULL; |
---|
1360 | } |
---|
1361 | if (tok->char_offset < slen){ |
---|
1362 | fprintf(stderr, "Error parsing json\n"); |
---|
1363 | return NULL; |
---|
1364 | } |
---|
1365 | return pajObj; |
---|
1366 | } |
---|
1367 | |
---|
1368 | json_object* json_readFile(maps* conf,char* filePath){ |
---|
1369 | json_object *pajObj = NULL; |
---|
1370 | zStatStruct zsFStatus; |
---|
1371 | int iS=zStat(filePath, &zsFStatus); |
---|
1372 | if(iS==0 && zsFStatus.st_size>0){ |
---|
1373 | FILE* cdat=fopen(filePath,"rb"); |
---|
1374 | if(cdat!=NULL){ |
---|
1375 | char* pacMyString=(char*)malloc((zsFStatus.st_size+1)*sizeof(char)); |
---|
1376 | fread(pacMyString,1,zsFStatus.st_size,cdat); |
---|
1377 | pacMyString[zsFStatus.st_size]=0; |
---|
1378 | fclose(cdat); |
---|
1379 | pajObj=parseJson(conf,pacMyString); |
---|
1380 | free(pacMyString); |
---|
1381 | } |
---|
1382 | else |
---|
1383 | return NULL; |
---|
1384 | }else |
---|
1385 | return NULL; |
---|
1386 | return pajObj; |
---|
1387 | } |
---|
1388 | |
---|
1389 | json_object* createStatus(maps* conf,int status){ |
---|
1390 | int needResult=0; |
---|
1391 | const char *rstatus; |
---|
1392 | char *message; |
---|
1393 | // Create statusInfo JSON object |
---|
1394 | // cf. https://github.com/opengeospatial/wps-rest-binding/blob/master/core/ |
---|
1395 | // openapi/schemas/statusInfo.yaml |
---|
1396 | json_object* res=json_object_new_object(); |
---|
1397 | switch(status){ |
---|
1398 | case SERVICE_ACCEPTED: |
---|
1399 | { |
---|
1400 | message=_("ZOO-Kernel accepted to run your service!"); |
---|
1401 | rstatus="accepted"; |
---|
1402 | break; |
---|
1403 | } |
---|
1404 | case SERVICE_STARTED: |
---|
1405 | { |
---|
1406 | message=_("ZOO-Kernel is currently running your service!"); |
---|
1407 | map* pmStatus=getMapFromMaps(conf,"lenv","status"); |
---|
1408 | if(pmStatus!=NULL) |
---|
1409 | setMapInMaps(conf,"lenv","PercentCompleted",pmStatus->value); |
---|
1410 | pmStatus=getMapFromMaps(conf,"lenv","message"); |
---|
1411 | if(pmStatus!=NULL) |
---|
1412 | setMapInMaps(conf,"lenv","gs_message",pmStatus->value); |
---|
1413 | rstatus="running"; |
---|
1414 | break; |
---|
1415 | } |
---|
1416 | case SERVICE_PAUSED: |
---|
1417 | { |
---|
1418 | message=_("ZOO-Kernel pause your service!"); |
---|
1419 | rstatus="paused"; |
---|
1420 | break; |
---|
1421 | } |
---|
1422 | case SERVICE_SUCCEEDED: |
---|
1423 | { |
---|
1424 | message=_("ZOO-Kernel successfully run your service!"); |
---|
1425 | rstatus="successful"; |
---|
1426 | setMapInMaps(conf,"lenv","PercentCompleted","100"); |
---|
1427 | needResult=1; |
---|
1428 | break; |
---|
1429 | } |
---|
1430 | case SERVICE_DISMISSED: |
---|
1431 | { |
---|
1432 | message=_("ZOO-Kernel successfully dismissed your service!"); |
---|
1433 | rstatus="dismissed"; |
---|
1434 | needResult=1; |
---|
1435 | break; |
---|
1436 | } |
---|
1437 | default: |
---|
1438 | { |
---|
1439 | map* pmTmp=getMapFromMaps(conf,"lenv","force"); |
---|
1440 | if(pmTmp==NULL || strncasecmp(pmTmp->value,"false",5)==0){ |
---|
1441 | char* pacTmp=json_getStatusFilePath(conf); |
---|
1442 | json_object* pjoStatus=json_readFile(conf,pacTmp); |
---|
1443 | free(pacTmp); |
---|
1444 | if(pjoStatus!=NULL){ |
---|
1445 | json_object* pjoMessage=NULL; |
---|
1446 | if(json_object_object_get_ex(pjoStatus,"message",&pjoMessage)!=FALSE){ |
---|
1447 | message=(char*)json_object_get_string(pjoMessage); |
---|
1448 | } |
---|
1449 | } |
---|
1450 | // TODO: Error |
---|
1451 | }else{ |
---|
1452 | map* mMap=getMapFromMaps(conf,"lenv","gs_message"); |
---|
1453 | if(mMap!=NULL) |
---|
1454 | setMapInMaps(conf,"lenv","message",mMap->value); |
---|
1455 | message=produceErrorMessage(conf); |
---|
1456 | needResult=-1; |
---|
1457 | } |
---|
1458 | rstatus="failed"; |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | setMapInMaps(conf,"lenv","message",message); |
---|
1463 | setMapInMaps(conf,"lenv","status",rstatus); |
---|
1464 | |
---|
1465 | map *sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1466 | if(sessId!=NULL){ |
---|
1467 | sessId = getMapFromMaps (conf, "lenv", "gs_usid"); |
---|
1468 | if(sessId==NULL) |
---|
1469 | sessId = getMapFromMaps (conf, "lenv", "usid"); |
---|
1470 | }else |
---|
1471 | sessId = getMapFromMaps (conf, "lenv", "gs_usid"); |
---|
1472 | if(sessId!=NULL) |
---|
1473 | json_object_object_add(res,"jobID",json_object_new_string(sessId->value)); |
---|
1474 | json_object_object_add(res,"status",json_object_new_string(rstatus)); |
---|
1475 | map* mMap=getMapFromMaps(conf,"lenv","gs_message"); |
---|
1476 | if(mMap==NULL) |
---|
1477 | json_object_object_add(res,"message",json_object_new_string(message)); |
---|
1478 | else{ |
---|
1479 | json_object_object_add(res,"message",json_object_new_string(mMap->value)); |
---|
1480 | if((mMap=getMapFromMaps(conf,"lenv","PercentCompleted"))!=NULL) |
---|
1481 | json_object_object_add(res,"progress",json_object_new_int(atoi(mMap->value))); |
---|
1482 | } |
---|
1483 | if(status!=SERVICE_DISMISSED) |
---|
1484 | createStatusLinks(conf,needResult,res); |
---|
1485 | else{ |
---|
1486 | json_object* res1=json_object_new_array(); |
---|
1487 | map *tmpPath = getMapFromMaps (conf, "openapi", "rootUrl"); |
---|
1488 | map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); |
---|
1489 | char *Url0=(char*) malloc((strlen(tmpPath->value)+ |
---|
1490 | strlen(cIdentifier->value)+ |
---|
1491 | 17)*sizeof(char)); |
---|
1492 | sprintf(Url0,"%s/processes/%s/jobs", |
---|
1493 | tmpPath->value, |
---|
1494 | cIdentifier->value); |
---|
1495 | json_object* val=json_object_new_object(); |
---|
1496 | json_object_object_add(val,"title", |
---|
1497 | json_object_new_string(_("The job list for the current process"))); |
---|
1498 | json_object_object_add(val,"rel", |
---|
1499 | json_object_new_string(_("parent"))); |
---|
1500 | json_object_object_add(val,"type", |
---|
1501 | json_object_new_string(_("application/json"))); |
---|
1502 | json_object_object_add(val,"href",json_object_new_string(Url0)); |
---|
1503 | json_object_array_add(res1,val); |
---|
1504 | free(Url0); |
---|
1505 | json_object_object_add(res,"links",res1); |
---|
1506 | } |
---|
1507 | if(needResult<0) |
---|
1508 | free(message); |
---|
1509 | return res; |
---|
1510 | } |
---|
1511 | |
---|
1512 | /** |
---|
1513 | * Create the status file |
---|
1514 | * |
---|
1515 | * @param conf the maps containing the settings of the main.cfg file |
---|
1516 | * @param status an integer (SERVICE_ACCEPTED,SERVICE_STARTED...) |
---|
1517 | * @return an integer (0 in case of success, 1 in case of failure) |
---|
1518 | */ |
---|
1519 | int createStatusFile(maps* conf,int status){ |
---|
1520 | json_object* res=createStatus(conf,status); |
---|
1521 | char* tmp1=json_getStatusFilePath(conf); |
---|
1522 | FILE* foutput1=fopen(tmp1,"w+"); |
---|
1523 | if(foutput1!=NULL){ |
---|
1524 | const char* jsonStr1=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); |
---|
1525 | fprintf(foutput1,"%s",jsonStr1); |
---|
1526 | fclose(foutput1); |
---|
1527 | }else{ |
---|
1528 | // Failure |
---|
1529 | setMapInMaps(conf,"lenv","message",_("Unable to store the statusInfo!")); |
---|
1530 | free(tmp1); |
---|
1531 | return 1; |
---|
1532 | } |
---|
1533 | free(tmp1); |
---|
1534 | return 0; |
---|
1535 | } |
---|
1536 | |
---|
1537 | /** |
---|
1538 | * Create the status file |
---|
1539 | * |
---|
1540 | * @param conf the maps containing the settings of the main.cfg file |
---|
1541 | * @return an integer (0 in case of success, 1 in case of failure) |
---|
1542 | */ |
---|
1543 | int json_getStatusFile(maps* conf){ |
---|
1544 | char* tmp1=json_getStatusFilePath(conf); |
---|
1545 | FILE* statusFile=fopen(tmp1,"rb"); |
---|
1546 | |
---|
1547 | map* tmpMap=getMapFromMaps(conf,"lenv","gs_usid"); |
---|
1548 | if(tmpMap!=NULL){ |
---|
1549 | char* tmpStr=_getStatus(conf,tmpMap->value); |
---|
1550 | if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ |
---|
1551 | char *tmpStr1=zStrdup(tmpStr); |
---|
1552 | char *tmpStr0=zStrdup(strstr(tmpStr,"|")+1); |
---|
1553 | free(tmpStr); |
---|
1554 | tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; |
---|
1555 | setMapInMaps(conf,"lenv","PercentCompleted",tmpStr1); |
---|
1556 | setMapInMaps(conf,"lenv","gs_message",tmpStr0); |
---|
1557 | free(tmpStr0); |
---|
1558 | free(tmpStr1); |
---|
1559 | return 0; |
---|
1560 | }else{ |
---|
1561 | |
---|
1562 | } |
---|
1563 | } |
---|
1564 | } |
---|
1565 | |
---|
1566 | /** |
---|
1567 | * Produce the JSON object for api info object |
---|
1568 | * |
---|
1569 | * @param conf the maps containing the settings of the main.cfg file |
---|
1570 | * @param res the JSON object for the api info |
---|
1571 | */ |
---|
1572 | void produceApiInfo(maps* conf,json_object* res,json_object* res5){ |
---|
1573 | json_object *res1=json_object_new_object(); |
---|
1574 | map* tmpMap=getMapFromMaps(conf,"provider","providerName"); |
---|
1575 | if(tmpMap!=NULL){ |
---|
1576 | json_object *res2=json_object_new_object(); |
---|
1577 | json_object_object_add(res2,"name",json_object_new_string(tmpMap->value)); |
---|
1578 | tmpMap=getMapFromMaps(conf,"provider","addressElectronicMailAddress"); |
---|
1579 | if(tmpMap!=NULL) |
---|
1580 | json_object_object_add(res2,"email",json_object_new_string(tmpMap->value)); |
---|
1581 | tmpMap=getMapFromMaps(conf,"provider","providerSite"); |
---|
1582 | if(tmpMap!=NULL) |
---|
1583 | json_object_object_add(res2,"url",json_object_new_string(tmpMap->value)); |
---|
1584 | json_object_object_add(res1,"contact",res2); |
---|
1585 | } |
---|
1586 | tmpMap=getMapFromMaps(conf,"identification","abstract"); |
---|
1587 | if(tmpMap!=NULL){ |
---|
1588 | json_object_object_add(res1,"description",json_object_new_string(tmpMap->value)); |
---|
1589 | json_object_object_add(res5,"description",json_object_new_string(tmpMap->value)); |
---|
1590 | tmpMap=getMapFromMaps(conf,"openapi","rootUrl"); |
---|
1591 | json_object_object_add(res5,"url",json_object_new_string(tmpMap->value)); |
---|
1592 | } |
---|
1593 | tmpMap=getMapFromMaps(conf,"identification","title"); |
---|
1594 | if(tmpMap!=NULL) |
---|
1595 | json_object_object_add(res1,"title",json_object_new_string(tmpMap->value)); |
---|
1596 | json_object_object_add(res1,"version",json_object_new_string(ZOO_VERSION)); |
---|
1597 | tmpMap=getMapFromMaps(conf,"identification","keywords"); |
---|
1598 | if(tmpMap!=NULL){ |
---|
1599 | char *saveptr; |
---|
1600 | char *tmps = strtok_r (tmpMap->value, ",", &saveptr); |
---|
1601 | json_object *res3=json_object_new_array(); |
---|
1602 | while (tmps != NULL){ |
---|
1603 | json_object_array_add(res3,json_object_new_string(tmps)); |
---|
1604 | tmps = strtok_r (NULL, ",", &saveptr); |
---|
1605 | } |
---|
1606 | json_object_object_add(res1,"x-keywords",res3); |
---|
1607 | } |
---|
1608 | maps* tmpMaps=getMaps(conf,"provider"); |
---|
1609 | if(tmpMaps!=NULL){ |
---|
1610 | json_object *res4=json_object_new_object(); |
---|
1611 | map* cItem=tmpMaps->content; |
---|
1612 | while(cItem!=NULL){ |
---|
1613 | json_object_object_add(res4,cItem->name,json_object_new_string(cItem->value)); |
---|
1614 | cItem=cItem->next; |
---|
1615 | } |
---|
1616 | json_object_object_add(res1,"x-ows-servicecontact",res4); |
---|
1617 | } |
---|
1618 | |
---|
1619 | json_object *res4=json_object_new_object(); |
---|
1620 | tmpMap=getMapFromMaps(conf,"openapi","license_name"); |
---|
1621 | if(tmpMap!=NULL){ |
---|
1622 | json_object_object_add(res4,"name",json_object_new_string(tmpMap->value)); |
---|
1623 | tmpMap=getMapFromMaps(conf,"openapi","license_url"); |
---|
1624 | if(tmpMap!=NULL){ |
---|
1625 | json_object_object_add(res4,"url",json_object_new_string(tmpMap->value)); |
---|
1626 | } |
---|
1627 | } |
---|
1628 | json_object_object_add(res1,"license",res4); |
---|
1629 | |
---|
1630 | json_object_object_add(res,"info",res1); |
---|
1631 | } |
---|
1632 | |
---|
1633 | // addResponse(pmUseContent,cc3,vMap,tMap,"200","successful operation"); |
---|
1634 | void addResponse(const map* useContent,json_object* res,const map* pmSchema,const map* pmType,const char* code,const char* msg){ |
---|
1635 | json_object *cc=json_object_new_object(); |
---|
1636 | if(pmSchema!=NULL) |
---|
1637 | json_object_object_add(cc,"$ref",json_object_new_string(pmSchema->value)); |
---|
1638 | if(useContent!=NULL && strncasecmp(useContent->value,"true",4)!=0){ |
---|
1639 | json_object_object_add(res,code,cc); |
---|
1640 | }else{ |
---|
1641 | json_object *cc0=json_object_new_object(); |
---|
1642 | if(pmSchema!=NULL) |
---|
1643 | json_object_object_add(cc0,"schema",cc); |
---|
1644 | json_object *cc1=json_object_new_object(); |
---|
1645 | if(pmType!=NULL) |
---|
1646 | json_object_object_add(cc1,pmType->value,cc0); |
---|
1647 | else |
---|
1648 | json_object_object_add(cc1,"application/json",cc0); |
---|
1649 | json_object *cc2=json_object_new_object(); |
---|
1650 | json_object_object_add(cc2,"content",cc1); |
---|
1651 | json_object_object_add(cc2,"description",json_object_new_string(msg)); |
---|
1652 | json_object_object_add(res,code,cc2); |
---|
1653 | } |
---|
1654 | } |
---|
1655 | |
---|
1656 | void addParameter(maps* conf,const char* oName,const char* fName,const char* in,json_object* res){ |
---|
1657 | maps* tmpMaps1=getMaps(conf,oName); |
---|
1658 | json_object *res8=json_object_new_object(); |
---|
1659 | if(tmpMaps1!=NULL){ |
---|
1660 | map* tmpMap=getMap(tmpMaps1->content,"title"); |
---|
1661 | if(tmpMap!=NULL) |
---|
1662 | json_object_object_add(res8,"x-internal-summary",json_object_new_string(tmpMap->value)); |
---|
1663 | tmpMap=getMap(tmpMaps1->content,"abstract"); |
---|
1664 | if(tmpMap!=NULL) |
---|
1665 | json_object_object_add(res8,"description",json_object_new_string(tmpMap->value)); |
---|
1666 | tmpMap=getMap(tmpMaps1->content,"example"); |
---|
1667 | if(tmpMap!=NULL) |
---|
1668 | json_object_object_add(res8,"example",json_object_new_string(tmpMap->value)); |
---|
1669 | tmpMap=getMap(tmpMaps1->content,"required"); |
---|
1670 | if(tmpMap!=NULL){ |
---|
1671 | if(strcmp(tmpMap->value,"true")==0) |
---|
1672 | json_object_object_add(res8,"required",json_object_new_boolean(TRUE)); |
---|
1673 | else |
---|
1674 | json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); |
---|
1675 | } |
---|
1676 | else |
---|
1677 | json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); |
---|
1678 | json_object_object_add(res8,"in",json_object_new_string(in)); |
---|
1679 | json_object_object_add(res8,"name",json_object_new_string(fName)); |
---|
1680 | json_object *res6=json_object_new_object(); |
---|
1681 | tmpMap=getMap(tmpMaps1->content,"type"); |
---|
1682 | if(tmpMap!=NULL) |
---|
1683 | json_object_object_add(res6,"type",json_object_new_string(tmpMap->value)); |
---|
1684 | else |
---|
1685 | json_object_object_add(res6,"type",json_object_new_string("string")); |
---|
1686 | |
---|
1687 | json_object_object_add(res8,"schema",res6); |
---|
1688 | |
---|
1689 | } |
---|
1690 | json_object_object_add(res,fName,res8); |
---|
1691 | } |
---|
1692 | |
---|
1693 | /** |
---|
1694 | * Produce the JSON object for api parameter |
---|
1695 | * |
---|
1696 | * @param conf the maps containing the settings of the main.cfg file |
---|
1697 | * @param res the JSON object to populate with the parameters |
---|
1698 | */ |
---|
1699 | void produceApiParameters(maps* conf,json_object* res){ |
---|
1700 | json_object *res9=json_object_new_object(); |
---|
1701 | maps* tmpMaps1=getMaps(conf,"{id}"); |
---|
1702 | map* tmpMap2=getMapFromMaps(conf,"openapi","parameters"); |
---|
1703 | char *saveptr12; |
---|
1704 | char *tmps12 = strtok_r (tmpMap2->value, ",", &saveptr12); |
---|
1705 | while(tmps12!=NULL){ |
---|
1706 | char* pacId=(char*) malloc((strlen(tmps12)+3)*sizeof(char)); |
---|
1707 | sprintf(pacId,"{%s}",tmps12); |
---|
1708 | addParameter(conf,pacId,tmps12,"path",res9); |
---|
1709 | free(pacId); |
---|
1710 | tmps12 = strtok_r (NULL, ",", &saveptr12); |
---|
1711 | } |
---|
1712 | tmpMap2=getMapFromMaps(conf,"openapi","header_parameters"); |
---|
1713 | if(tmpMap2!=NULL){ |
---|
1714 | char *saveptr13; |
---|
1715 | char *tmps13 = strtok_r (tmpMap2->value, ",", &saveptr13); |
---|
1716 | while(tmps13!=NULL){ |
---|
1717 | char* pacId=zStrdup(tmps13); |
---|
1718 | addParameter(conf,pacId,pacId,"header",res9); |
---|
1719 | free(pacId); |
---|
1720 | tmps13 = strtok_r (NULL, ",", &saveptr13); |
---|
1721 | } |
---|
1722 | } |
---|
1723 | json_object_object_add(res,"parameters",res9); |
---|
1724 | maps* pmResponses=getMaps(conf,"responses"); |
---|
1725 | if(pmResponses!=NULL){ |
---|
1726 | json_object *cc=json_object_new_object(); |
---|
1727 | map* pmLen=getMap(pmResponses->content,"length"); |
---|
1728 | int iLen=atoi(pmLen->value); |
---|
1729 | map* pmUseContent=getMapFromMaps(conf,"openapi","use_content"); |
---|
1730 | for(int i=0;i<iLen;i++){ |
---|
1731 | map* cMap=getMapArray(pmResponses->content,"code",i); |
---|
1732 | map* vMap=getMapArray(pmResponses->content,"schema",i); |
---|
1733 | map* tMap=getMapArray(pmResponses->content,"type",i); |
---|
1734 | map* tMap0=getMapArray(pmResponses->content,"title",i); |
---|
1735 | if(vMap!=NULL) |
---|
1736 | addResponse(pmUseContent,cc,vMap,tMap,cMap->value,(tMap0==NULL)?"successful operation":tMap0->value); |
---|
1737 | } |
---|
1738 | json_object_object_add(res,"responses",cc); |
---|
1739 | } |
---|
1740 | } |
---|
1741 | |
---|
1742 | void produceApiComponents(maps*conf,json_object* res){ |
---|
1743 | json_object* res1=json_object_new_object(); |
---|
1744 | produceApiParameters(conf,res1); |
---|
1745 | json_object_object_add(res,"components",res1); |
---|
1746 | } |
---|
1747 | |
---|
1748 | /** |
---|
1749 | * Produce the JSON object for /api |
---|
1750 | * |
---|
1751 | * @param conf the maps containing the settings of the main.cfg file |
---|
1752 | * @param res the JSON object to populate |
---|
1753 | */ |
---|
1754 | void produceApi(maps* conf,json_object* res){ |
---|
1755 | // TODO: add 401 Gone for dismiss request |
---|
1756 | json_object *res9=json_object_new_object(); |
---|
1757 | json_object *res10=json_object_new_object(); |
---|
1758 | maps* tmpMaps1=getMaps(conf,"{id}"); |
---|
1759 | produceApiComponents(conf,res); |
---|
1760 | json_object *res4=json_object_new_object(); |
---|
1761 | setMapInMaps(conf,"headers","Content-Type","application/openapi+json; version=3.0;charset=UTF-8"); |
---|
1762 | |
---|
1763 | produceApiInfo(conf,res,res4); |
---|
1764 | map* tmpMap=getMapFromMaps(conf,"provider","providerName"); |
---|
1765 | |
---|
1766 | tmpMap=getMapFromMaps(conf,"openapi","version"); |
---|
1767 | if(tmpMap!=NULL) |
---|
1768 | json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value)); |
---|
1769 | else |
---|
1770 | json_object_object_add(res,"openapi",json_object_new_string("3.0.2")); |
---|
1771 | |
---|
1772 | tmpMap=getMapFromMaps(conf,"openapi","paths"); |
---|
1773 | if(tmpMap!=NULL){ |
---|
1774 | json_object *res5=json_object_new_object(); |
---|
1775 | char *saveptr; |
---|
1776 | char *tmps = strtok_r (tmpMap->value, ",", &saveptr); |
---|
1777 | int cnt=0; |
---|
1778 | maps* tmpMaps; |
---|
1779 | json_object *paths=json_object_new_object(); |
---|
1780 | while (tmps != NULL){ |
---|
1781 | tmpMaps=getMaps(conf,tmps+1); |
---|
1782 | json_object *method=json_object_new_object(); |
---|
1783 | if(tmpMaps!=NULL){ |
---|
1784 | if(getMap(tmpMaps->content,"length")==NULL) |
---|
1785 | addToMap(tmpMaps->content,"length","1"); |
---|
1786 | map* len=getMap(tmpMaps->content,"length"); |
---|
1787 | |
---|
1788 | for(int i=0;i<atoi(len->value);i++){ |
---|
1789 | json_object *methodc=json_object_new_object(); |
---|
1790 | map* cMap=getMapArray(tmpMaps->content,"method",i); |
---|
1791 | map* vMap=getMapArray(tmpMaps->content,"title",i); |
---|
1792 | if(vMap!=NULL) |
---|
1793 | json_object_object_add(methodc,"summary",json_object_new_string(vMap->value)); |
---|
1794 | vMap=getMapArray(tmpMaps->content,"abstract",i); |
---|
1795 | if(vMap!=NULL) |
---|
1796 | json_object_object_add(methodc,"description",json_object_new_string(vMap->value)); |
---|
1797 | vMap=getMapArray(tmpMaps->content,"tags",i); |
---|
1798 | if(vMap!=NULL){ |
---|
1799 | json_object *cc=json_object_new_array(); |
---|
1800 | json_object_array_add(cc,json_object_new_string(vMap->value)); |
---|
1801 | json_object_object_add(methodc,"tags",cc); |
---|
1802 | json_object_object_add(methodc,"operationId",json_object_new_string(vMap->value)); |
---|
1803 | |
---|
1804 | } |
---|
1805 | json_object *responses=json_object_new_object(); |
---|
1806 | json_object *cc3=json_object_new_object(); |
---|
1807 | map* pmUseContent=getMapFromMaps(conf,"openapi","use_content"); |
---|
1808 | vMap=getMapArray(tmpMaps->content,"schema",i); |
---|
1809 | if(vMap!=NULL){ |
---|
1810 | map* tMap=getMapArray(tmpMaps->content,"type",i); |
---|
1811 | addResponse(pmUseContent,cc3,vMap,tMap,"200","successful operation"); |
---|
1812 | vMap=getMapArray(tmpMaps->content,"eschema",i); |
---|
1813 | if(vMap!=NULL && cMap!=NULL && strncasecmp(cMap->value,"post",4)==0) |
---|
1814 | addResponse(pmUseContent,cc3,vMap,tMap,"201","successful operation"); |
---|
1815 | }else{ |
---|
1816 | map* tMap=getMapFromMaps(conf,tmps,"type"); |
---|
1817 | map* pMap=createMap("ok","true"); |
---|
1818 | addResponse(pMap,cc3,vMap,tMap,"200","successful operation"); |
---|
1819 | if(cMap!=NULL && strncasecmp(cMap->value,"post",4)==0) |
---|
1820 | addResponse(pmUseContent,cc3,vMap,tMap,"201","successful operation"); |
---|
1821 | } |
---|
1822 | vMap=getMapArray(tmpMaps->content,"ecode",i); |
---|
1823 | if(vMap!=NULL){ |
---|
1824 | char *saveptr0; |
---|
1825 | char *tmps1 = strtok_r (vMap->value, ",", &saveptr0); |
---|
1826 | while(tmps1!=NULL){ |
---|
1827 | char* tmpStr=(char*)malloc((strlen(tmps1)+24)*sizeof(char)); |
---|
1828 | sprintf(tmpStr,"#/components/responses/%s",tmps1); |
---|
1829 | vMap=createMap("ok",tmpStr); |
---|
1830 | map* pMap=createMap("ok","false"); |
---|
1831 | //TODO: fix successful operation with correct value |
---|
1832 | addResponse(pMap,cc3,vMap,NULL,tmps1,"successful operation"); |
---|
1833 | tmps1 = strtok_r (NULL, ",", &saveptr0); |
---|
1834 | } |
---|
1835 | }else{ |
---|
1836 | if(strstr(tmps,"{id}")!=NULL){ |
---|
1837 | vMap=getMapFromMaps(conf,"exception","schema"); |
---|
1838 | map* tMap=getMapFromMaps(conf,"exception","type"); |
---|
1839 | if(vMap!=NULL) |
---|
1840 | addResponse(pmUseContent,cc3,vMap,tMap,"404", |
---|
1841 | (strstr(tmps,"{jobID}")==NULL)?"The process with id {id} does not exist.":"The process with id {id} or job with id {jobID} does not exist."); |
---|
1842 | } |
---|
1843 | } |
---|
1844 | json_object_object_add(methodc,"responses",cc3); |
---|
1845 | vMap=getMapArray(tmpMaps->content,"parameters",i); |
---|
1846 | if(vMap!=NULL){ |
---|
1847 | char *saveptr0; |
---|
1848 | char *tmps1 = strtok_r (vMap->value, ",", &saveptr0); |
---|
1849 | json_object *cc2=json_object_new_array(); |
---|
1850 | int cnt=0; |
---|
1851 | while(tmps1!=NULL){ |
---|
1852 | char* tmpStr=(char*)malloc((strlen(tmps1)+2)*sizeof(char)); |
---|
1853 | sprintf(tmpStr,"#%s",tmps1); |
---|
1854 | json_object *cc1=json_object_new_object(); |
---|
1855 | json_object_object_add(cc1,"$ref",json_object_new_string(tmpStr)); |
---|
1856 | json_object_array_add(cc2,cc1); |
---|
1857 | free(tmpStr); |
---|
1858 | cnt++; |
---|
1859 | tmps1 = strtok_r (NULL, ",", &saveptr0); |
---|
1860 | } |
---|
1861 | json_object_object_add(methodc,"parameters",cc2); |
---|
1862 | } |
---|
1863 | if(i==1 && cMap!=NULL && strncasecmp(cMap->value,"post",4)==0){ |
---|
1864 | maps* tmpMaps1=getMaps(conf,"requestBody"); |
---|
1865 | if(tmpMaps1!=NULL){ |
---|
1866 | vMap=getMap(tmpMaps1->content,"schema"); |
---|
1867 | if(vMap!=NULL){ |
---|
1868 | json_object *cc=json_object_new_object(); |
---|
1869 | json_object_object_add(cc,"$ref",json_object_new_string(vMap->value)); |
---|
1870 | json_object *cc0=json_object_new_object(); |
---|
1871 | json_object_object_add(cc0,"schema",cc); |
---|
1872 | json_object *cc1=json_object_new_object(); |
---|
1873 | map* tmpMap3=getMap(tmpMaps->content,"type"); |
---|
1874 | if(tmpMap3!=NULL) |
---|
1875 | json_object_object_add(cc1,tmpMap3->value,cc0); |
---|
1876 | else |
---|
1877 | json_object_object_add(cc1,"application/json",cc0); |
---|
1878 | json_object *cc2=json_object_new_object(); |
---|
1879 | json_object_object_add(cc2,"content",cc1); |
---|
1880 | vMap=getMap(tmpMaps1->content,"abstract"); |
---|
1881 | if(vMap!=NULL) |
---|
1882 | json_object_object_add(cc2,"description",json_object_new_string(vMap->value)); |
---|
1883 | json_object_object_add(cc2,"required",json_object_new_boolean(true)); |
---|
1884 | json_object_object_add(methodc,"requestBody",cc2); |
---|
1885 | |
---|
1886 | } |
---|
1887 | } |
---|
1888 | // TODO: callbacks |
---|
1889 | tmpMaps1=getMaps(conf,"callbacks"); |
---|
1890 | if(tmpMaps1!=NULL){ |
---|
1891 | map* pmTmp2=getMap(tmpMaps1->content,"length"); |
---|
1892 | int iLen=atoi(pmTmp2->value); |
---|
1893 | json_object *pajRes=json_object_new_object(); |
---|
1894 | for(int i=0;i<iLen;i++){ |
---|
1895 | map* pmState=getMapArray(tmpMaps1->content,"state",i); |
---|
1896 | map* pmUri=getMapArray(tmpMaps1->content,"uri",i); |
---|
1897 | map* pmSchema=getMapArray(tmpMaps1->content,"schema",i); |
---|
1898 | map* pmType=getMapArray(tmpMaps1->content,"type",i); |
---|
1899 | map* pmTitle=getMapArray(tmpMaps1->content,"title",i); |
---|
1900 | json_object *pajSchema=json_object_new_object(); |
---|
1901 | if(pmSchema!=NULL) |
---|
1902 | json_object_object_add(pajSchema,"$ref",json_object_new_string(pmSchema->value)); |
---|
1903 | json_object *pajType=json_object_new_object(); |
---|
1904 | json_object_object_add(pajType,"schema",pajSchema); |
---|
1905 | json_object *pajContent=json_object_new_object(); |
---|
1906 | if(pmType!=NULL) |
---|
1907 | json_object_object_add(pajContent,pmType->value,pajType); |
---|
1908 | else |
---|
1909 | json_object_object_add(pajContent,"application/json",pajType); |
---|
1910 | json_object *pajRBody=json_object_new_object(); |
---|
1911 | json_object_object_add(pajRBody,"content",pajContent); |
---|
1912 | |
---|
1913 | json_object *pajDescription=json_object_new_object(); |
---|
1914 | json_object *pajPost=json_object_new_object(); |
---|
1915 | if(pmTitle!=NULL){ |
---|
1916 | json_object_object_add(pajDescription,"description",json_object_new_string(pmTitle->value)); |
---|
1917 | json_object_object_add(pajPost,"summary",json_object_new_string(pmTitle->value)); |
---|
1918 | } |
---|
1919 | json_object *pajResponse=json_object_new_object(); |
---|
1920 | json_object_object_add(pajResponse,"200",pajDescription); |
---|
1921 | |
---|
1922 | json_object_object_add(pajPost,"requestBody",pajRBody); |
---|
1923 | json_object_object_add(pajPost,"responses",pajResponse); |
---|
1924 | json_object_object_add(pajPost,"operationId",json_object_new_string(pmState->value)); |
---|
1925 | |
---|
1926 | json_object *pajMethod=json_object_new_object(); |
---|
1927 | json_object_object_add(pajMethod,"post",pajPost); |
---|
1928 | |
---|
1929 | |
---|
1930 | char* pacUri=(char*) malloc((strlen(pmUri->value)+29)*sizeof(char)); |
---|
1931 | sprintf(pacUri,"{$request.body#/subscriber/%s}",pmUri->value); |
---|
1932 | |
---|
1933 | json_object *pajFinal=json_object_new_object(); |
---|
1934 | json_object_object_add(pajFinal,pacUri,pajMethod); |
---|
1935 | json_object_object_add(pajRes,pmState->value,pajFinal); |
---|
1936 | |
---|
1937 | } |
---|
1938 | json_object_object_add(methodc,"callbacks",pajRes); |
---|
1939 | } |
---|
1940 | } |
---|
1941 | map* mMap=getMapArray(tmpMaps->content,"method",i); |
---|
1942 | if(mMap!=NULL) |
---|
1943 | json_object_object_add(method,mMap->value,methodc); |
---|
1944 | else |
---|
1945 | json_object_object_add(method,"get",methodc); |
---|
1946 | |
---|
1947 | } |
---|
1948 | |
---|
1949 | tmpMap=getMapFromMaps(conf,"openapi","version"); |
---|
1950 | if(tmpMap!=NULL) |
---|
1951 | json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value)); |
---|
1952 | else |
---|
1953 | json_object_object_add(res,"openapi",json_object_new_string("3.0.2")); |
---|
1954 | if(strstr(tmps,"/root")!=NULL) |
---|
1955 | json_object_object_add(paths,"/",method); |
---|
1956 | else |
---|
1957 | json_object_object_add(paths,tmps,method); |
---|
1958 | } |
---|
1959 | tmps = strtok_r (NULL, ",", &saveptr); |
---|
1960 | cnt++; |
---|
1961 | } |
---|
1962 | json_object_object_add(res,"paths",paths); |
---|
1963 | } |
---|
1964 | |
---|
1965 | tmpMap=getMapFromMaps(conf,"openapi","links"); |
---|
1966 | if(tmpMap!=NULL){ |
---|
1967 | |
---|
1968 | } |
---|
1969 | |
---|
1970 | json_object *res3=json_object_new_array(); |
---|
1971 | json_object_array_add(res3,res4); |
---|
1972 | json_object_object_add(res,"servers",res3); |
---|
1973 | } |
---|
1974 | |
---|
1975 | #ifdef __cplusplus |
---|
1976 | } |
---|
1977 | #endif |
---|