source: trunk/zoo-project/zoo-kernel/service_json.c @ 985

Last change on this file since 985 was 985, checked in by djay, 3 years ago

Move jobs management and execution endpoint from /processes/{procssId}/jobs to /jobs

  • Property svn:keywords set to Id
File size: 70.3 KB
Line 
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
36extern "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,"mediaType",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    struct dirent *dp;
1049    DIR *dirp = opendir (tmpPath->value);
1050    if(dirp!=NULL){
1051      while ((dp = readdir (dirp)) != NULL){
1052        char* extn = strstr(dp->d_name, "_status.json");
1053        if(extn!=NULL){
1054          char* tmpStr=zStrdup(dp->d_name);
1055          tmpStr[strlen(dp->d_name)-12]=0;
1056          json_object* cjob=printJobStatus(conf,tmpStr);
1057          json_object_array_add(res,cjob);
1058        }
1059      }
1060      closedir (dirp);
1061    }
1062    return res;
1063  }
1064 
1065  /**
1066   * Print the result of an execution
1067   *
1068   * @param conf the maps containing the settings of the main.cfg file
1069   * @param s service pointer to metadata
1070   * @param result outputs of the service
1071   * @param res the status of execution SERVICE_FAILED/SERVICE_SUCCEEDED
1072   * @return the JSON object pointer to the result
1073   */
1074  json_object* printJResult(maps* conf,service* s,maps* result,int res){
1075    if(res==SERVICE_FAILED){
1076      char* pacTmp=produceErrorMessage(conf);
1077      map* pamTmp=createMap("message",pacTmp);
1078      free(pacTmp);
1079      map* pmTmp=getMapFromMaps(conf,"lenv","code");
1080      if(pmTmp!=NULL)
1081        addToMap(pamTmp,"code",pmTmp->value);
1082      printExceptionReportResponseJ(conf,pamTmp);
1083      freeMap(&pamTmp);
1084      free(pamTmp);
1085      return NULL;
1086    }
1087    if(getMapFromMaps(conf,"lenv","no-headers")==NULL)
1088      printHeaders(conf);
1089    map* pmMode=getMapFromMaps(conf,"request","response");
1090    if(pmMode!=NULL && strncasecmp(pmMode->value,"raw",3)==0){
1091      maps* resu=result;
1092      printRawdataOutput(conf,resu);
1093      map* pmError=getMapFromMaps(conf,"lenv","error");
1094      if(pmError!=NULL && strncasecmp(pmError->value,"true",4)==0){
1095        printExceptionReportResponseJ(conf,pmError);
1096      }
1097      return NULL;
1098    }else{
1099      json_object* eres1=json_object_new_object();
1100      map* pmIOAsArray=getMapFromMaps(conf,"openapi","io_as_array");
1101      json_object* eres;
1102      if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0)
1103        eres=json_object_new_array();
1104      else
1105        eres=json_object_new_object();
1106      maps* resu=result;
1107      int itn=0;
1108      while(resu!=NULL){
1109        json_object* res1=json_object_new_object();
1110        if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0)
1111          json_object_object_add(res1,"id",json_object_new_string(resu->name));
1112        map* tmpMap=getMap(resu->content,"mimeType");
1113        json_object* res3=json_object_new_object();
1114        if(tmpMap!=NULL)
1115          json_object_object_add(res3,"mediaType",json_object_new_string(tmpMap->value));
1116        else{
1117          json_object_object_add(res3,"mediaType",json_object_new_string("text/plain"));
1118          map* pmDataType=getMap(resu->content,"dataType");
1119          if(pmDataType!=NULL){
1120            json_object* pjoTmp3=json_object_new_object();
1121            json_object_object_add(pjoTmp3,"name",json_object_new_string(pmDataType->value));
1122            json_object_object_add(res1,"dataType",pjoTmp3);
1123          }
1124        }
1125        if((tmpMap=getMap(resu->content,"value"))!=NULL ||
1126           (getMap(resu->content,"generated_file"))!=NULL){
1127          //json_object* res2=json_object_new_object();
1128          map* tmpMap1=NULL;
1129          if((tmpMap1=getMap(resu->content,"transmissionMode"))!=NULL) {
1130            if(strcmp(tmpMap1->value,"value")==0) {
1131              map* tmpMap2=getMap(resu->content,"mimeType");
1132              if(tmpMap2!=NULL && strstr(tmpMap2->value,"json")!=NULL){
1133                json_object *jobj = NULL;
1134                int slen = 0;
1135                enum json_tokener_error jerr;
1136                struct json_tokener* tok=json_tokener_new();
1137                do {
1138                  slen = strlen(tmpMap->value);
1139                  jobj = json_tokener_parse_ex(tok, tmpMap->value, slen);
1140                } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
1141                if (jerr != json_tokener_success) {
1142                  fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
1143                  return eres1;
1144                }
1145                if (tok->char_offset < slen){
1146                  return eres1;         
1147                }
1148                json_object_object_add(res3,"encoding",json_object_new_string("utf-8"));
1149                json_object_object_add(res1,"value",jobj);
1150              }else{
1151                map* tmp1=getMapFromMaps(conf,"main","tmpPath");
1152                map *gfile=getMap(resu->content,"generated_file");
1153                if(gfile!=NULL){
1154                  gfile=getMap(resu->content,"expected_generated_file");
1155                  if(gfile==NULL){
1156                    gfile=getMap(resu->content,"generated_file");
1157                  }
1158                  FILE* pfData=fopen(gfile->value,"rb");
1159                  if(pfData!=NULL){
1160                    zStatStruct f_status;
1161                    int s=zStat(gfile->value, &f_status);
1162                    char* pcaTmp=(char*)malloc((f_status.st_size+1)*sizeof(char));
1163                    fread(pcaTmp,1,f_status.st_size,pfData);
1164                    pcaTmp[f_status.st_size]=0;     
1165                    fclose(pfData);
1166                    json_object_object_add(res1,"value",json_object_new_string(base64(pcaTmp,f_status.st_size)));
1167                    json_object_object_add(res3,"encoding",json_object_new_string("base64"));
1168                    free(pcaTmp);
1169                  }
1170                }else{
1171                  json_object_object_add(res3,"encoding",json_object_new_string("utf-8"));
1172                  json_object_object_add(res1,"value",json_object_new_string(tmpMap->value));
1173                }
1174              }
1175            }
1176            else{
1177              char *pcaFileUrl=produceFileUrl(s,conf,resu,NULL,itn);
1178              itn++;
1179              if(pcaFileUrl!=NULL){
1180                json_object_object_add(res1,"href",
1181                                       json_object_new_string(pcaFileUrl));
1182                free(pcaFileUrl);
1183              }
1184            }
1185          }
1186          //json_object_object_add(res1,"value",res2);
1187          json_object_object_add(res1,"format",res3);
1188          if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0){
1189            json_object_array_add(eres,res1);
1190          }else
1191            json_object_object_add(eres,resu->name,res1);
1192        }
1193        resu=resu->next;
1194      }
1195      json_object_object_add(eres1,"outputs",eres);
1196      const char* jsonStr =
1197        json_object_to_json_string_ext(eres1,JSON_C_TO_STRING_PLAIN);
1198      map *tmpPath = getMapFromMaps (conf, "main", "tmpPath");
1199      map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier");
1200      map *sessId = getMapFromMaps (conf, "lenv", "usid");
1201      char *pacTmp=(char*)malloc((strlen(tmpPath->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char));
1202      sprintf(pacTmp,"%s/%s_%s.json",
1203              tmpPath->value,cIdentifier->value,sessId->value);
1204      zStatStruct zsFStatus;
1205      int iS=zStat(pacTmp, &zsFStatus);
1206      if(iS==0 && zsFStatus.st_size>0){
1207        map* tmpPath1 = getMapFromMaps (conf, "main", "tmpUrl");
1208        char* pacTmpUrl=(char*)malloc((strlen(tmpPath1->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char));;
1209        sprintf(pacTmpUrl,"%s/%s_%s.json",tmpPath1->value,
1210                cIdentifier->value,sessId->value);
1211        if(getMapFromMaps(conf,"lenv","gs_location")==NULL)
1212          setMapInMaps(conf,"headers","Location",pacTmpUrl);
1213        free(pacTmpUrl);
1214      }
1215      free(pacTmp);
1216      if(res==3){
1217        map* mode=getMapFromMaps(conf,"request","mode");
1218        if(mode!=NULL && strncasecmp(mode->value,"async",5)==0)
1219          setMapInMaps(conf,"headers","Status","201 Created");
1220        else
1221          setMapInMaps(conf,"headers","Status","200 Ok");
1222      }
1223      else{
1224        setMapInMaps(conf,"headers","Status","500 Issue running your service");
1225      }
1226      return eres1;
1227    }
1228  }
1229
1230
1231  /**
1232   * Create the status links
1233   *
1234   * @param conf the maps containing the settings of the main.cfg file
1235   * @param result an integer (>0 for adding the /result link)
1236   * @param obj the JSON object pointer to add the links to
1237   * @return 0
1238   */
1239  int createStatusLinks(maps* conf,int result,json_object* obj){
1240    json_object* res=json_object_new_array();
1241    map *tmpPath = getMapFromMaps (conf, "openapi", "rootUrl");
1242    map *sessId = getMapFromMaps (conf, "lenv", "usid");
1243    if(sessId==NULL){
1244      sessId = getMapFromMaps (conf, "lenv", "gs_usid");
1245    }
1246    char *Url0=(char*) malloc((strlen(tmpPath->value)+
1247                               strlen(sessId->value)+18)*sizeof(char));
1248    int needResult=-1;
1249    char *message, *status;
1250    sprintf(Url0,"%s/jobs/%s",
1251            tmpPath->value,
1252            sessId->value);
1253    if(getMapFromMaps(conf,"lenv","gs_location")==NULL)
1254      setMapInMaps(conf,"headers","Location",Url0);
1255    json_object* val=json_object_new_object();
1256    json_object_object_add(val,"title",
1257                           json_object_new_string(_("Status location")));
1258    json_object_object_add(val,"rel",
1259                           json_object_new_string(_("status")));
1260    json_object_object_add(val,"type",
1261                           json_object_new_string(_("application/json")));
1262    json_object_object_add(val,"href",json_object_new_string(Url0));
1263    json_object_array_add(res,val);
1264    if(result>0){
1265      free(Url0);
1266      Url0=(char*) malloc((strlen(tmpPath->value)+
1267                           strlen(sessId->value)+
1268                           25)*sizeof(char));
1269      sprintf(Url0,"%s/jobs/%s/results",
1270              tmpPath->value,
1271              sessId->value);
1272      json_object* val1=json_object_new_object();
1273      json_object_object_add(val1,"title",
1274                             json_object_new_string(_("Result location")));
1275      json_object_object_add(val1,"rel",
1276                             json_object_new_string(_("results")));
1277      json_object_object_add(val1,"type",
1278                             json_object_new_string(_("application/json")));
1279      json_object_object_add(val1,"href",json_object_new_string(Url0));
1280      json_object_array_add(res,val1);
1281    }
1282    json_object_object_add(obj,"links",res);
1283    free(Url0);
1284    return 0;
1285  }
1286
1287  /**
1288   * Get the status file path
1289   *
1290   * @param conf the maps containing the settings of the main.cfg file
1291   * @return a char* containing the full status file path
1292   */
1293  char* json_getStatusFilePath(maps* conf){
1294    map *tmpPath = getMapFromMaps (conf, "main", "tmpPath");
1295    map *sessId = getMapFromMaps (conf, "lenv", "usid");
1296    if(sessId!=NULL){
1297      sessId = getMapFromMaps (conf, "lenv", "gs_usid");
1298      if(sessId==NULL)
1299        sessId = getMapFromMaps (conf, "lenv", "usid");
1300    }else
1301      sessId = getMapFromMaps (conf, "lenv", "gs_usid");
1302   
1303    char *tmp1=(char*) malloc((strlen(tmpPath->value)+
1304                               strlen(sessId->value)+14)*sizeof(char));
1305    sprintf(tmp1,"%s/%s_status.json",
1306            tmpPath->value,
1307            sessId->value);
1308    return tmp1;
1309  }
1310
1311  char* getResultPath(maps* conf,char* jobId){
1312    map *tmpPath = getMapFromMaps (conf, "main", "tmpPath");
1313    char *pacUrl=(char*) malloc((strlen(tmpPath->value)+
1314                                 strlen(jobId)+8)*sizeof(char));
1315    sprintf(pacUrl,"%s/%s.json",tmpPath->value,jobId);
1316    return pacUrl;
1317  }
1318
1319  json_object* parseJson(maps* conf,char* myString){
1320    json_object *pajObj = NULL;
1321    enum json_tokener_error jerr;
1322    struct json_tokener* tok=json_tokener_new();
1323    int slen = 0;
1324    do {
1325      slen = strlen(myString);
1326      pajObj = json_tokener_parse_ex(tok, myString, slen);
1327    } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
1328    if (jerr != json_tokener_success) {
1329      setMapInMaps(conf,"lenv","message",json_tokener_error_desc(jerr));
1330      fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
1331      return NULL;
1332    }
1333    if (tok->char_offset < slen){
1334      fprintf(stderr, "Error parsing json\n");
1335      return NULL;
1336    }
1337    return pajObj;
1338  }
1339 
1340  json_object* json_readFile(maps* conf,char* filePath){
1341    json_object *pajObj = NULL;
1342    zStatStruct zsFStatus;
1343    int iS=zStat(filePath, &zsFStatus);
1344    if(iS==0 && zsFStatus.st_size>0){
1345      FILE* cdat=fopen(filePath,"rb");
1346      if(cdat!=NULL){
1347        char* pacMyString=(char*)malloc((zsFStatus.st_size+1)*sizeof(char));
1348        fread(pacMyString,1,zsFStatus.st_size,cdat);
1349        pacMyString[zsFStatus.st_size]=0;
1350        fclose(cdat);
1351        pajObj=parseJson(conf,pacMyString);
1352        free(pacMyString);
1353      }
1354      else
1355        return NULL;
1356    }else
1357      return NULL;
1358    return pajObj;
1359  }
1360 
1361  json_object* createStatus(maps* conf,int status){
1362    int needResult=0;
1363    const char *rstatus;
1364    char *message;
1365    // Create statusInfo JSON object
1366    // cf. https://github.com/opengeospatial/wps-rest-binding/blob/master/core/
1367    //     openapi/schemas/statusInfo.yaml
1368    json_object* res=json_object_new_object();
1369    switch(status){
1370    case SERVICE_ACCEPTED:
1371      {
1372        message=_("ZOO-Kernel accepted to run your service!");
1373        rstatus="accepted";
1374        break;
1375      }
1376    case SERVICE_STARTED:
1377      {
1378        message=_("ZOO-Kernel is currently running your service!");
1379        map* pmStatus=getMapFromMaps(conf,"lenv","status");
1380        if(pmStatus!=NULL)
1381          setMapInMaps(conf,"lenv","PercentCompleted",pmStatus->value);
1382        pmStatus=getMapFromMaps(conf,"lenv","message");
1383        if(pmStatus!=NULL)
1384          setMapInMaps(conf,"lenv","gs_message",pmStatus->value);
1385        rstatus="running";
1386        break;
1387      }
1388    case SERVICE_PAUSED:
1389      {
1390        message=_("ZOO-Kernel pause your service!");
1391        rstatus="paused";
1392        break;
1393      }
1394    case SERVICE_SUCCEEDED:
1395      {
1396        message=_("ZOO-Kernel successfully run your service!");
1397        rstatus="successful";
1398        setMapInMaps(conf,"lenv","PercentCompleted","100");
1399        needResult=1;
1400        break;
1401      }
1402    case SERVICE_DISMISSED:
1403      {
1404        message=_("ZOO-Kernel successfully dismissed your service!");
1405        rstatus="dismissed";
1406        needResult=1;
1407        break;
1408      }
1409    default:
1410      {
1411        map* pmTmp=getMapFromMaps(conf,"lenv","force");
1412        if(pmTmp==NULL || strncasecmp(pmTmp->value,"false",5)==0){
1413          char* pacTmp=json_getStatusFilePath(conf);
1414          json_object* pjoStatus=json_readFile(conf,pacTmp);
1415          free(pacTmp);
1416          if(pjoStatus!=NULL){
1417            json_object* pjoMessage=NULL;
1418            if(json_object_object_get_ex(pjoStatus,"message",&pjoMessage)!=FALSE){
1419              message=(char*)json_object_get_string(pjoMessage);
1420            }
1421          }
1422          // TODO: Error
1423        }else{
1424          map* mMap=getMapFromMaps(conf,"lenv","gs_message");
1425          if(mMap!=NULL)
1426            setMapInMaps(conf,"lenv","message",mMap->value);
1427          message=produceErrorMessage(conf);
1428          needResult=-1;
1429        }
1430        rstatus="failed";
1431        break;
1432      }
1433    }
1434    setMapInMaps(conf,"lenv","message",message);
1435    setMapInMaps(conf,"lenv","status",rstatus);
1436
1437    map *sessId = getMapFromMaps (conf, "lenv", "usid");
1438    if(sessId!=NULL){
1439      sessId = getMapFromMaps (conf, "lenv", "gs_usid");
1440      if(sessId==NULL)
1441        sessId = getMapFromMaps (conf, "lenv", "usid");
1442    }else
1443      sessId = getMapFromMaps (conf, "lenv", "gs_usid");
1444    if(sessId!=NULL)
1445      json_object_object_add(res,"jobID",json_object_new_string(sessId->value));
1446
1447    json_object_object_add(res,"status",json_object_new_string(rstatus));
1448    map* mMap=getMapFromMaps(conf,"lenv","gs_message");
1449    if(mMap==NULL)
1450      json_object_object_add(res,"message",json_object_new_string(message));
1451    else{
1452      json_object_object_add(res,"message",json_object_new_string(mMap->value));
1453      if((mMap=getMapFromMaps(conf,"lenv","PercentCompleted"))!=NULL)
1454        json_object_object_add(res,"progress",json_object_new_int(atoi(mMap->value)));
1455    }
1456    if(status!=SERVICE_DISMISSED)
1457      createStatusLinks(conf,needResult,res);
1458    else{
1459      json_object* res1=json_object_new_array();
1460      map *tmpPath = getMapFromMaps (conf, "openapi", "rootUrl");
1461      char *Url0=(char*) malloc((strlen(tmpPath->value)+17)*sizeof(char));
1462      sprintf(Url0,"%s/jobs",tmpPath->value);
1463      json_object* val=json_object_new_object();
1464      json_object_object_add(val,"title",
1465                             json_object_new_string(_("The job list for the current process")));
1466      json_object_object_add(val,"rel",
1467                             json_object_new_string(_("parent")));
1468      json_object_object_add(val,"type",
1469                             json_object_new_string(_("application/json")));
1470      json_object_object_add(val,"href",json_object_new_string(Url0));
1471      json_object_array_add(res1,val);
1472      free(Url0);
1473      json_object_object_add(res,"links",res1);
1474    }
1475    if(needResult<0)
1476      free(message);
1477    return res;
1478  }
1479 
1480  /**
1481   * Create the status file
1482   *
1483   * @param conf the maps containing the settings of the main.cfg file
1484   * @param status an integer (SERVICE_ACCEPTED,SERVICE_STARTED...)
1485   * @return an integer (0 in case of success, 1 in case of failure)
1486   */
1487  int createStatusFile(maps* conf,int status){
1488    json_object* res=createStatus(conf,status);
1489    char* tmp1=json_getStatusFilePath(conf);
1490    FILE* foutput1=fopen(tmp1,"w+");
1491    if(foutput1!=NULL){
1492      const char* jsonStr1=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN);
1493      fprintf(foutput1,"%s",jsonStr1);
1494      fclose(foutput1);
1495    }else{
1496      // Failure
1497      setMapInMaps(conf,"lenv","message",_("Unable to store the statusInfo!"));
1498      free(tmp1);
1499      return 1;
1500    }
1501    free(tmp1);
1502    return 0;
1503  }
1504
1505  /**
1506   * Create the status file
1507   *
1508   * @param conf the maps containing the settings of the main.cfg file
1509   * @return an integer (0 in case of success, 1 in case of failure)
1510   */
1511  int json_getStatusFile(maps* conf){
1512    char* tmp1=json_getStatusFilePath(conf);
1513    FILE* statusFile=fopen(tmp1,"rb");
1514
1515    map* tmpMap=getMapFromMaps(conf,"lenv","gs_usid");
1516    if(tmpMap!=NULL){
1517      char* tmpStr=_getStatus(conf,tmpMap->value);
1518      if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){
1519        char *tmpStr1=zStrdup(tmpStr);
1520        char *tmpStr0=zStrdup(strstr(tmpStr,"|")+1);
1521        free(tmpStr);
1522        tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0';
1523        setMapInMaps(conf,"lenv","PercentCompleted",tmpStr1);
1524        setMapInMaps(conf,"lenv","gs_message",tmpStr0);
1525        free(tmpStr0);
1526        free(tmpStr1);
1527        return 0;
1528      }else{
1529         
1530      }
1531    }
1532  }
1533
1534  /**
1535   * Produce the JSON object for api info object
1536   *
1537   * @param conf the maps containing the settings of the main.cfg file
1538   * @param res the JSON object for the api info
1539   */
1540  void produceApiInfo(maps* conf,json_object* res,json_object* res5){
1541    json_object *res1=json_object_new_object();
1542    map* tmpMap=getMapFromMaps(conf,"provider","providerName");
1543    if(tmpMap!=NULL){
1544      json_object *res2=json_object_new_object();
1545      json_object_object_add(res2,"name",json_object_new_string(tmpMap->value));
1546      tmpMap=getMapFromMaps(conf,"provider","addressElectronicMailAddress");
1547      if(tmpMap!=NULL)
1548        json_object_object_add(res2,"email",json_object_new_string(tmpMap->value));
1549      tmpMap=getMapFromMaps(conf,"provider","providerSite");
1550      if(tmpMap!=NULL)
1551        json_object_object_add(res2,"url",json_object_new_string(tmpMap->value));
1552      json_object_object_add(res1,"contact",res2);
1553    }
1554    tmpMap=getMapFromMaps(conf,"identification","abstract");
1555    if(tmpMap!=NULL){
1556      json_object_object_add(res1,"description",json_object_new_string(tmpMap->value));
1557      json_object_object_add(res5,"description",json_object_new_string(tmpMap->value));
1558      tmpMap=getMapFromMaps(conf,"openapi","rootUrl");
1559      json_object_object_add(res5,"url",json_object_new_string(tmpMap->value));
1560    }
1561    tmpMap=getMapFromMaps(conf,"identification","title");
1562    if(tmpMap!=NULL)
1563      json_object_object_add(res1,"title",json_object_new_string(tmpMap->value));
1564    json_object_object_add(res1,"version",json_object_new_string(ZOO_VERSION));
1565    tmpMap=getMapFromMaps(conf,"identification","keywords");
1566    if(tmpMap!=NULL){
1567      char *saveptr;
1568      char *tmps = strtok_r (tmpMap->value, ",", &saveptr);
1569      json_object *res3=json_object_new_array();
1570      while (tmps != NULL){
1571        json_object_array_add(res3,json_object_new_string(tmps));
1572        tmps = strtok_r (NULL, ",", &saveptr);
1573      }
1574      json_object_object_add(res1,"x-keywords",res3);
1575    }
1576    maps* tmpMaps=getMaps(conf,"provider");
1577    if(tmpMaps!=NULL){
1578      json_object *res4=json_object_new_object();
1579      map* cItem=tmpMaps->content;
1580      while(cItem!=NULL){
1581        json_object_object_add(res4,cItem->name,json_object_new_string(cItem->value));
1582        cItem=cItem->next;
1583      }
1584      json_object_object_add(res1,"x-ows-servicecontact",res4);
1585    }
1586
1587    json_object *res4=json_object_new_object();
1588    tmpMap=getMapFromMaps(conf,"openapi","license_name");
1589    if(tmpMap!=NULL){
1590      json_object_object_add(res4,"name",json_object_new_string(tmpMap->value));
1591      tmpMap=getMapFromMaps(conf,"openapi","license_url");
1592      if(tmpMap!=NULL){
1593        json_object_object_add(res4,"url",json_object_new_string(tmpMap->value));     
1594      }
1595    }
1596    json_object_object_add(res1,"license",res4);
1597
1598    json_object_object_add(res,"info",res1);   
1599  }
1600
1601  // addResponse(pmUseContent,cc3,vMap,tMap,"200","successful operation");
1602  void addResponse(const map* useContent,json_object* res,const map* pmSchema,const map* pmType,const char* code,const char* msg){
1603    json_object *cc=json_object_new_object();
1604    if(pmSchema!=NULL)
1605      json_object_object_add(cc,"$ref",json_object_new_string(pmSchema->value));
1606    if(useContent!=NULL && strncasecmp(useContent->value,"true",4)!=0){
1607      json_object_object_add(res,code,cc);
1608    }else{
1609      json_object *cc0=json_object_new_object();
1610      if(pmSchema!=NULL)
1611        json_object_object_add(cc0,"schema",cc);
1612      json_object *cc1=json_object_new_object();
1613      if(pmType!=NULL)
1614        json_object_object_add(cc1,pmType->value,cc0);
1615      else
1616        json_object_object_add(cc1,"application/json",cc0);
1617      json_object *cc2=json_object_new_object();
1618      json_object_object_add(cc2,"content",cc1);
1619      json_object_object_add(cc2,"description",json_object_new_string(msg));
1620      json_object_object_add(res,code,cc2);
1621    }
1622  }
1623
1624  void addParameter(maps* conf,const char* oName,const char* fName,const char* in,json_object* res){
1625    maps* tmpMaps1=getMaps(conf,oName);
1626    json_object *res8=json_object_new_object();
1627    if(tmpMaps1!=NULL){
1628      map* tmpMap=getMap(tmpMaps1->content,"title");
1629      if(tmpMap!=NULL)
1630        json_object_object_add(res8,"x-internal-summary",json_object_new_string(tmpMap->value));
1631      tmpMap=getMap(tmpMaps1->content,"abstract");
1632      if(tmpMap!=NULL)
1633        json_object_object_add(res8,"description",json_object_new_string(tmpMap->value));
1634      tmpMap=getMap(tmpMaps1->content,"example");
1635      if(tmpMap!=NULL)
1636        json_object_object_add(res8,"example",json_object_new_string(tmpMap->value));
1637      tmpMap=getMap(tmpMaps1->content,"required");
1638      if(tmpMap!=NULL){
1639        if(strcmp(tmpMap->value,"true")==0)
1640          json_object_object_add(res8,"required",json_object_new_boolean(TRUE));
1641        else
1642          json_object_object_add(res8,"required",json_object_new_boolean(FALSE));
1643      }
1644      else
1645        json_object_object_add(res8,"required",json_object_new_boolean(FALSE));
1646      json_object_object_add(res8,"in",json_object_new_string(in));
1647      json_object_object_add(res8,"name",json_object_new_string(fName));
1648      json_object *res6=json_object_new_object();
1649      tmpMap=getMap(tmpMaps1->content,"type");
1650      if(tmpMap!=NULL)
1651        json_object_object_add(res6,"type",json_object_new_string(tmpMap->value));
1652      else
1653        json_object_object_add(res6,"type",json_object_new_string("string"));
1654       
1655      json_object_object_add(res8,"schema",res6);
1656       
1657    }   
1658    json_object_object_add(res,fName,res8);   
1659  }
1660 
1661  /**
1662   * Produce the JSON object for api parameter
1663   *
1664   * @param conf the maps containing the settings of the main.cfg file
1665   * @param res the JSON object to populate with the parameters
1666   */
1667  void produceApiParameters(maps* conf,json_object* res){
1668    json_object *res9=json_object_new_object();
1669    maps* tmpMaps1=getMaps(conf,"{id}");
1670    map* tmpMap2=getMapFromMaps(conf,"openapi","parameters");
1671    char *saveptr12;
1672    char *tmps12 = strtok_r (tmpMap2->value, ",", &saveptr12);
1673    while(tmps12!=NULL){
1674      char* pacId=(char*) malloc((strlen(tmps12)+3)*sizeof(char));
1675      sprintf(pacId,"{%s}",tmps12);
1676      addParameter(conf,pacId,tmps12,"path",res9);
1677      free(pacId);
1678      tmps12 = strtok_r (NULL, ",", &saveptr12);
1679    }   
1680    tmpMap2=getMapFromMaps(conf,"openapi","header_parameters");
1681    if(tmpMap2!=NULL){
1682      char *saveptr13;
1683      char *tmps13 = strtok_r (tmpMap2->value, ",", &saveptr13);
1684      while(tmps13!=NULL){
1685        char* pacId=zStrdup(tmps13);
1686        addParameter(conf,pacId,pacId,"header",res9);
1687        free(pacId);
1688        tmps13 = strtok_r (NULL, ",", &saveptr13);
1689      }
1690    }
1691    json_object_object_add(res,"parameters",res9);
1692    maps* pmResponses=getMaps(conf,"responses");
1693    if(pmResponses!=NULL){
1694      json_object *cc=json_object_new_object();
1695      map* pmLen=getMap(pmResponses->content,"length");
1696      int iLen=atoi(pmLen->value);
1697      map* pmUseContent=getMapFromMaps(conf,"openapi","use_content");
1698      for(int i=0;i<iLen;i++){
1699        map* cMap=getMapArray(pmResponses->content,"code",i);
1700        map* vMap=getMapArray(pmResponses->content,"schema",i);
1701        map* tMap=getMapArray(pmResponses->content,"type",i);
1702        map* tMap0=getMapArray(pmResponses->content,"title",i);
1703        if(vMap!=NULL)
1704          addResponse(pmUseContent,cc,vMap,tMap,cMap->value,(tMap0==NULL)?"successful operation":tMap0->value);
1705      }
1706      json_object_object_add(res,"responses",cc);
1707    }
1708  }
1709
1710  void produceApiComponents(maps*conf,json_object* res){
1711    json_object* res1=json_object_new_object();
1712    produceApiParameters(conf,res1);
1713    json_object_object_add(res,"components",res1);
1714  }
1715 
1716  /**
1717   * Produce the JSON object for /api
1718   *
1719   * @param conf the maps containing the settings of the main.cfg file
1720   * @param res the JSON object to populate
1721   */
1722  void produceApi(maps* conf,json_object* res){
1723    // TODO: add 401 Gone for dismiss request
1724    json_object *res9=json_object_new_object();
1725    json_object *res10=json_object_new_object();
1726    maps* tmpMaps1=getMaps(conf,"{id}");
1727    produceApiComponents(conf,res);
1728    json_object *res4=json_object_new_object();
1729    setMapInMaps(conf,"headers","Content-Type","application/openapi+json; version=3.0;charset=UTF-8");
1730
1731    produceApiInfo(conf,res,res4);
1732    map* tmpMap=getMapFromMaps(conf,"provider","providerName");
1733
1734    tmpMap=getMapFromMaps(conf,"openapi","version");
1735    if(tmpMap!=NULL)
1736      json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value));
1737    else
1738      json_object_object_add(res,"openapi",json_object_new_string("3.0.2"));
1739
1740    tmpMap=getMapFromMaps(conf,"openapi","paths");
1741    if(tmpMap!=NULL){
1742      json_object *res5=json_object_new_object();
1743      char *saveptr;
1744      char *tmps = strtok_r (tmpMap->value, ",", &saveptr);
1745      int cnt=0;
1746      maps* tmpMaps;
1747      json_object *paths=json_object_new_object();
1748      while (tmps != NULL){
1749        tmpMaps=getMaps(conf,tmps+1);
1750        json_object *method=json_object_new_object();
1751        if(tmpMaps!=NULL){
1752          if(getMap(tmpMaps->content,"length")==NULL)
1753            addToMap(tmpMaps->content,"length","1");
1754          map* len=getMap(tmpMaps->content,"length");
1755           
1756          for(int i=0;i<atoi(len->value);i++){
1757            json_object *methodc=json_object_new_object();
1758            map* cMap=getMapArray(tmpMaps->content,"method",i);
1759            map* vMap=getMapArray(tmpMaps->content,"title",i);
1760            if(vMap!=NULL)
1761              json_object_object_add(methodc,"summary",json_object_new_string(vMap->value));
1762            vMap=getMapArray(tmpMaps->content,"abstract",i);
1763            if(vMap!=NULL)
1764              json_object_object_add(methodc,"description",json_object_new_string(vMap->value));
1765            vMap=getMapArray(tmpMaps->content,"tags",i);
1766            if(vMap!=NULL){
1767              json_object *cc=json_object_new_array();
1768              json_object_array_add(cc,json_object_new_string(vMap->value));
1769              json_object_object_add(methodc,"tags",cc);
1770              json_object_object_add(methodc,"operationId",json_object_new_string(vMap->value));
1771
1772            }
1773            json_object *responses=json_object_new_object();
1774            json_object *cc3=json_object_new_object();
1775            map* pmUseContent=getMapFromMaps(conf,"openapi","use_content");
1776            vMap=getMapArray(tmpMaps->content,"schema",i);
1777            if(vMap!=NULL){
1778              map* tMap=getMapArray(tmpMaps->content,"type",i);
1779              addResponse(pmUseContent,cc3,vMap,tMap,"200","successful operation");
1780              vMap=getMapArray(tmpMaps->content,"eschema",i);
1781              if(vMap!=NULL && cMap!=NULL && strncasecmp(cMap->value,"post",4)==0)
1782                addResponse(pmUseContent,cc3,vMap,tMap,"201","successful operation");
1783            }else{
1784              map* tMap=getMapFromMaps(conf,tmps,"type");
1785              map* pMap=createMap("ok","true");
1786              addResponse(pMap,cc3,vMap,tMap,"200","successful operation");
1787              if(cMap!=NULL && strncasecmp(cMap->value,"post",4)==0)
1788                addResponse(pmUseContent,cc3,vMap,tMap,"201","successful operation");
1789            }
1790            vMap=getMapArray(tmpMaps->content,"ecode",i);
1791            if(vMap!=NULL){
1792              char *saveptr0;
1793              char *tmps1 = strtok_r (vMap->value, ",", &saveptr0);
1794              while(tmps1!=NULL){
1795                char* tmpStr=(char*)malloc((strlen(tmps1)+24)*sizeof(char));
1796                sprintf(tmpStr,"#/components/responses/%s",tmps1);
1797                vMap=createMap("ok",tmpStr);
1798                map* pMap=createMap("ok","false");
1799                //TODO: fix successful operation with correct value
1800                addResponse(pMap,cc3,vMap,NULL,tmps1,"successful operation");
1801                tmps1 = strtok_r (NULL, ",", &saveptr0);
1802              }
1803            }else{
1804              if(strstr(tmps,"{id}")!=NULL){
1805                vMap=getMapFromMaps(conf,"exception","schema");
1806                map* tMap=getMapFromMaps(conf,"exception","type");
1807                if(vMap!=NULL)
1808                  addResponse(pmUseContent,cc3,vMap,tMap,"404",
1809                              (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.");
1810              }
1811            }
1812            json_object_object_add(methodc,"responses",cc3);
1813            vMap=getMapArray(tmpMaps->content,"parameters",i);
1814            if(vMap!=NULL){
1815              char *saveptr0;
1816              char *tmps1 = strtok_r (vMap->value, ",", &saveptr0);
1817              json_object *cc2=json_object_new_array();
1818              int cnt=0;
1819              while(tmps1!=NULL){
1820                char* tmpStr=(char*)malloc((strlen(tmps1)+2)*sizeof(char));
1821                sprintf(tmpStr,"#%s",tmps1);
1822                json_object *cc1=json_object_new_object();
1823                json_object_object_add(cc1,"$ref",json_object_new_string(tmpStr));
1824                json_object_array_add(cc2,cc1);
1825                free(tmpStr);
1826                cnt++;
1827                tmps1 = strtok_r (NULL, ",", &saveptr0);
1828              }
1829              json_object_object_add(methodc,"parameters",cc2);
1830            }
1831            if(i==1 && cMap!=NULL && strncasecmp(cMap->value,"post",4)==0){
1832              maps* tmpMaps1=getMaps(conf,"requestBody");
1833              if(tmpMaps1!=NULL){
1834                vMap=getMap(tmpMaps1->content,"schema");
1835                if(vMap!=NULL){
1836                  json_object *cc=json_object_new_object();
1837                  json_object_object_add(cc,"$ref",json_object_new_string(vMap->value));
1838                  json_object *cc0=json_object_new_object();
1839                  json_object_object_add(cc0,"schema",cc);
1840                  json_object *cc1=json_object_new_object();
1841                  map* tmpMap3=getMap(tmpMaps->content,"type");
1842                  if(tmpMap3!=NULL)
1843                    json_object_object_add(cc1,tmpMap3->value,cc0);
1844                  else
1845                    json_object_object_add(cc1,"application/json",cc0);
1846                  json_object *cc2=json_object_new_object();
1847                  json_object_object_add(cc2,"content",cc1);
1848                  vMap=getMap(tmpMaps1->content,"abstract");
1849                  if(vMap!=NULL)
1850                    json_object_object_add(cc2,"description",json_object_new_string(vMap->value));
1851                  json_object_object_add(cc2,"required",json_object_new_boolean(true));
1852                  json_object_object_add(methodc,"requestBody",cc2);
1853
1854                }               
1855              }
1856              // TODO: callbacks
1857              tmpMaps1=getMaps(conf,"callbacks");
1858              if(tmpMaps1!=NULL){
1859                map* pmTmp2=getMap(tmpMaps1->content,"length");
1860                int iLen=atoi(pmTmp2->value);
1861                json_object *pajRes=json_object_new_object();
1862                for(int i=0;i<iLen;i++){
1863                  map* pmState=getMapArray(tmpMaps1->content,"state",i);
1864                  map* pmUri=getMapArray(tmpMaps1->content,"uri",i);
1865                  map* pmSchema=getMapArray(tmpMaps1->content,"schema",i);
1866                  map* pmType=getMapArray(tmpMaps1->content,"type",i);
1867                  map* pmTitle=getMapArray(tmpMaps1->content,"title",i);
1868                  json_object *pajSchema=json_object_new_object();
1869                  if(pmSchema!=NULL)
1870                    json_object_object_add(pajSchema,"$ref",json_object_new_string(pmSchema->value));
1871                  json_object *pajType=json_object_new_object();
1872                  json_object_object_add(pajType,"schema",pajSchema);
1873                  json_object *pajContent=json_object_new_object();
1874                  if(pmType!=NULL)
1875                    json_object_object_add(pajContent,pmType->value,pajType);
1876                  else           
1877                    json_object_object_add(pajContent,"application/json",pajType);
1878                  json_object *pajRBody=json_object_new_object();
1879                  json_object_object_add(pajRBody,"content",pajContent);
1880                 
1881                  json_object *pajDescription=json_object_new_object();
1882                  json_object *pajPost=json_object_new_object();
1883                  if(pmTitle!=NULL){
1884                    json_object_object_add(pajDescription,"description",json_object_new_string(pmTitle->value));
1885                    json_object_object_add(pajPost,"summary",json_object_new_string(pmTitle->value));
1886                  }
1887                  json_object *pajResponse=json_object_new_object();
1888                  json_object_object_add(pajResponse,"200",pajDescription);
1889
1890                  json_object_object_add(pajPost,"requestBody",pajRBody);
1891                  json_object_object_add(pajPost,"responses",pajResponse);
1892                  json_object_object_add(pajPost,"operationId",json_object_new_string(pmState->value));
1893
1894                  json_object *pajMethod=json_object_new_object();
1895                  json_object_object_add(pajMethod,"post",pajPost);
1896
1897                 
1898                  char* pacUri=(char*) malloc((strlen(pmUri->value)+29)*sizeof(char));
1899                  sprintf(pacUri,"{$request.body#/subscriber/%s}",pmUri->value);
1900
1901                  json_object *pajFinal=json_object_new_object();
1902                  json_object_object_add(pajFinal,pacUri,pajMethod);
1903                  json_object_object_add(pajRes,pmState->value,pajFinal);
1904
1905                }
1906                json_object_object_add(methodc,"callbacks",pajRes);
1907              }
1908            }
1909            map* mMap=getMapArray(tmpMaps->content,"method",i);
1910            if(mMap!=NULL)
1911              json_object_object_add(method,mMap->value,methodc);
1912            else
1913              json_object_object_add(method,"get",methodc);
1914
1915          }
1916
1917          tmpMap=getMapFromMaps(conf,"openapi","version");
1918          if(tmpMap!=NULL)
1919            json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value));
1920          else
1921            json_object_object_add(res,"openapi",json_object_new_string("3.0.2"));
1922          if(strstr(tmps,"/root")!=NULL)
1923            json_object_object_add(paths,"/",method);
1924          else
1925            json_object_object_add(paths,tmps,method);
1926        }
1927        tmps = strtok_r (NULL, ",", &saveptr);
1928        cnt++;
1929      }
1930      json_object_object_add(res,"paths",paths);
1931    }
1932     
1933    tmpMap=getMapFromMaps(conf,"openapi","links");
1934    if(tmpMap!=NULL){
1935       
1936    }
1937   
1938    json_object *res3=json_object_new_array();
1939    json_object_array_add(res3,res4);
1940    json_object_object_add(res,"servers",res3);
1941  }
1942 
1943#ifdef __cplusplus
1944}
1945#endif
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png