source: trunk/zoo-kernel/service_internal.c @ 76

Last change on this file since 76 was 76, checked in by djay, 13 years ago

Add complete BoundingBoxData? support. Code cleanup for DescribeProcess? gesture. Small fixes: use Reference node when lineage is true and inputs was provided as a reference, fix error when RawDataOutput? was requested with a wrong Output name.

File size: 56.6 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2011 GeoLabs SARL
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_internal.h"
26
27void *addLangAttr(xmlNodePtr n,maps *m){
28  map *tmpLmap=getMapFromMaps(m,"main","language");
29  if(tmpLmap!=NULL)
30    xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST tmpLmap->value);
31  else
32    xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST "en-US");
33}
34
35/* Converts a hex character to its integer value */
36char from_hex(char ch) {
37  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
38}
39
40/* Converts an integer value to its hex character*/
41char to_hex(char code) {
42  static char hex[] = "0123456789abcdef";
43  return hex[code & 15];
44}
45
46void* unhandleStatus(maps *conf){
47  int shmid,i;
48  key_t key;
49  void *shm;
50  struct shmid_ds shmids;
51  char *s,*s1;
52  map *tmpMap=getMapFromMaps(conf,"lenv","sid");
53  if(tmpMap!=NULL){
54    key=atoi(tmpMap->value);
55    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
56#ifdef DEBUG
57      fprintf(stderr,"shmget failed to update value\n");
58#endif
59    }else{
60      if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
61#ifdef DEBUG
62        fprintf(stderr,"shmat failed to update value\n");
63#endif
64      }else{
65        shmdt(shm);
66        shmctl(shmid,IPC_RMID,&shmids);
67      }
68    }
69  }
70}
71
72#ifdef USE_JS
73
74JSBool
75JSUpdateStatus(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
76{
77  JS_MaybeGC(cx);
78  char *sid;
79  int istatus=0;
80  char *status=NULL;
81  maps *conf;
82  int i=0;
83  if(argc>2){
84#ifdef JS_DEBUG
85    fprintf(stderr,"Number of arguments used to call the function : %i",argc);
86#endif
87    return JS_FALSE;
88  }
89  conf=mapsFromJSObject(cx,argv[0]);
90  if(JS_ValueToInt32(cx,argv[1],&istatus)==JS_TRUE){
91    char tmpStatus[4];
92    sprintf(tmpStatus,"%i",istatus);
93    tmpStatus[3]=0;
94    status=strdup(tmpStatus);
95  }
96  if(getMapFromMaps(conf,"lenv","status")!=NULL){
97    if(status!=NULL)
98      setMapInMaps(conf,"lenv","status",status);
99    else
100      setMapInMaps(conf,"lenv","status","15");
101    updateStatus(conf);
102  }
103  freeMaps(&conf);
104  free(conf);
105  JS_MaybeGC(cx);
106  return JS_TRUE;
107}
108
109#endif
110
111void* updateStatus(maps *conf){
112  int shmid,i;
113  key_t key;
114  char *shm,*s,*s1;
115  map *tmpMap=NULL;
116  tmpMap=getMapFromMaps(conf,"lenv","sid");
117  if(tmpMap!=NULL){
118    key=atoi(tmpMap->value);
119    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
120#ifdef DEBUG
121      fprintf(stderr,"shmget failed to update value\n");
122#endif
123    }else{
124      if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) {
125#ifdef DEBUG
126        fprintf(stderr,"shmat failed to update value\n");
127#endif
128      }
129      else{
130        tmpMap=getMapFromMaps(conf,"lenv","status");
131        s1=shm;
132        for(s=tmpMap->value;*s!=NULL;s++)
133          *s1++=*s;
134        shmdt((void *)shm);
135      }
136    }
137  }
138}
139
140char* getStatus(int pid){
141  int shmid,i;
142  key_t key;
143  void *shm;
144  char *s;
145  key=pid;
146  if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
147#ifdef DEBUG
148    fprintf(stderr,"shmget failed in getStatus\n");
149#endif
150  }else{
151    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
152#ifdef DEBUG
153      fprintf(stderr,"shmat failed in getStatus\n");
154#endif
155    }else{
156      return (char*)shm;
157    }
158  }
159  return "-1";
160}
161
162
163/* Returns a url-encoded version of str */
164/* IMPORTANT: be sure to free() the returned string after use */
165char *url_encode(char *str) {
166  char *pstr = str, *buf = (char*) malloc(strlen(str) * 3 + 1), *pbuf = buf;
167  while (*pstr) {
168    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') 
169      *pbuf++ = *pstr;
170    else if (*pstr == ' ') 
171      *pbuf++ = '+';
172    else 
173      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
174    pstr++;
175  }
176  *pbuf = '\0';
177  return buf;
178}
179
180/* Returns a url-decoded version of str */
181/* IMPORTANT: be sure to free() the returned string after use */
182char *url_decode(char *str) {
183  char *pstr = str, *buf = (char*) malloc(strlen(str) + 1), *pbuf = buf;
184  while (*pstr) {
185    if (*pstr == '%') {
186      if (pstr[1] && pstr[2]) {
187        *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
188        pstr += 2;
189      }
190    } else if (*pstr == '+') { 
191      *pbuf++ = ' ';
192    } else {
193      *pbuf++ = *pstr;
194    }
195    pstr++;
196  }
197  *pbuf = '\0';
198  return buf;
199}
200
201char *zCapitalize1(char *tmp){
202        char *res=strdup(tmp);
203        if(res[0]>=97 && res[0]<=122)
204                res[0]-=32;
205        return res;
206}
207
208char *zCapitalize(char *tmp){
209  int i=0;
210  char *res=strdup(tmp);
211  for(i=0;i<strlen(res);i++)
212    if(res[i]>=97 && res[i]<=122)
213      res[i]-=32;
214  return res;
215}
216
217
218int zooXmlSearchForNs(char* name){
219  int i;
220  int res=-1;
221  for(i=0;i<nbNs;i++)
222    if(strncasecmp(name,nsName[i],strlen(nsName[i]))==0){
223      res=i;
224      break;
225    }
226  return res;
227}
228
229int zooXmlAddNs(xmlNodePtr nr,char* url,char* name){
230#ifdef DEBUG
231  fprintf(stderr,"zooXmlAddNs %d \n",nbNs);
232#endif
233  int currId=-1;
234  if(nbNs==0){
235    nbNs++;
236    currId=0;
237    nsName[currId]=strdup(name);
238    usedNs[currId]=xmlNewNs(nr,BAD_CAST url,BAD_CAST name);
239  }else{
240    currId=zooXmlSearchForNs(name);
241    if(currId<0){
242      nbNs++;
243      currId=nbNs-1;
244      nsName[currId]=strdup(name);
245      usedNs[currId]=xmlNewNs(nr,BAD_CAST url,BAD_CAST name);
246    }
247  }
248  return currId;
249}
250
251void zooXmlCleanupNs(){
252  int j;
253#ifdef DEBUG
254  fprintf(stderr,"zooXmlCleanup %d\n",nbNs);
255#endif
256  for(j=nbNs-1;j>=0;j--){
257#ifdef DEBUG
258    fprintf(stderr,"zooXmlCleanup %d\n",j);
259#endif
260    if(j==0)
261      xmlFreeNs(usedNs[j]);
262    free(nsName[j]);
263    nbNs--;
264  }
265  nbNs=0;
266}
267
268xmlNodePtr printGetCapabilitiesHeader(xmlDocPtr doc,char* service,maps* m){
269
270  xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi;
271  xmlNodePtr n,nc,nc1,nc2,nc3,nc4,nc5,nc6,pseudor;
272  xmlChar *xmlbuff;
273  int buffersize;
274  /**
275   * Create the document and its temporary root.
276   */
277  int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps");
278  ns=usedNs[wpsId];
279  maps* toto1=getMaps(m,"main");
280
281  n = xmlNewNode(ns, BAD_CAST "Capabilities");
282  int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows");
283  ns_ows=usedNs[owsId];
284  xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps");
285  int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi");
286  ns_xsi=usedNs[xsiId];
287  int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink");
288  ns_xlink=usedNs[xlinkId];
289  xmlNewNsProp(n,ns_xsi,BAD_CAST "schemaLocation",BAD_CAST "http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsGetCapabilities_response.xsd"); 
290  xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS");
291  addLangAttr(n,m);
292 
293  if(toto1!=NULL){
294    map* tmp=getMap(toto1->content,"version");
295    if(tmp!=NULL){
296      xmlNewProp(n,BAD_CAST "version",BAD_CAST tmp->value);
297    }
298    else
299      xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0");
300  }
301  else
302    xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0");
303
304  char tmp[256];
305 
306  nc = xmlNewNode(ns_ows, BAD_CAST "ServiceIdentification");
307  maps* tmp4=getMaps(m,"identification");
308  if(tmp4!=NULL){
309    map* tmp2=tmp4->content;
310    char *orderedFields[5];
311    orderedFields[0]="Title";
312    orderedFields[1]="Abstract";
313    orderedFields[2]="Keywords";
314    orderedFields[3]="Fees";
315    orderedFields[4]="AccessConstraints";
316    int oI=0;
317    for(oI=0;oI<5;oI++)
318      if((tmp2=getMap(tmp4->content,orderedFields[oI]))!=NULL){
319        if(strcasecmp(tmp2->name,"abstract")==0 ||
320           strcasecmp(tmp2->name,"title")==0 ||
321           strcasecmp(tmp2->name,"accessConstraints")==0 ||
322           strcasecmp(tmp2->name,"fees")==0){
323          tmp2->name[0]=toupper(tmp2->name[0]);
324          nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name);
325          xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value));
326          xmlAddChild(nc,nc1);
327        }
328        else
329          if(strcmp(tmp2->name,"keywords")==0){
330            nc1 = xmlNewNode(ns_ows, BAD_CAST "Keywords");
331            char *toto=tmp2->value;
332            char buff[256];
333            int i=0;
334            int j=0;
335            while(toto[i]){
336              if(toto[i]!=',' && toto[i]!=0){
337                buff[j]=toto[i];
338                buff[j+1]=0;
339                j++;
340              }
341              else{
342                nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword");
343                xmlAddChild(nc2,xmlNewText(BAD_CAST buff));           
344                xmlAddChild(nc1,nc2);
345                j=0;
346              }
347              i++;
348            }
349            if(strlen(buff)>0){
350              nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword");
351              xmlAddChild(nc2,xmlNewText(BAD_CAST buff));             
352              xmlAddChild(nc1,nc2);
353            }
354            xmlAddChild(nc,nc1);
355            nc2 = xmlNewNode(ns_ows, BAD_CAST "ServiceType");
356            xmlAddChild(nc2,xmlNewText(BAD_CAST "WPS"));
357            xmlAddChild(nc,nc2);
358            nc2 = xmlNewNode(ns_ows, BAD_CAST "ServiceTypeVersion");
359            xmlAddChild(nc2,xmlNewText(BAD_CAST "1.0.0"));
360            xmlAddChild(nc,nc2);         
361          }
362        tmp2=tmp2->next;
363      }
364  }
365  else{
366    fprintf(stderr,"TMP4 NOT FOUND !!");
367    return NULL;
368  }
369  xmlAddChild(n,nc);
370
371  nc = xmlNewNode(ns_ows, BAD_CAST "ServiceProvider");
372  nc3 = xmlNewNode(ns_ows, BAD_CAST "ServiceContact");
373  nc4 = xmlNewNode(ns_ows, BAD_CAST "ContactInfo");
374  nc5 = xmlNewNode(ns_ows, BAD_CAST "Phone");
375  nc6 = xmlNewNode(ns_ows, BAD_CAST "Address");
376  tmp4=getMaps(m,"provider");
377  if(tmp4!=NULL){
378    map* tmp2=tmp4->content;
379    char *tmpAddress[6];
380    tmpAddress[0]="addressDeliveryPoint";
381    tmpAddress[1]="addressCity";
382    tmpAddress[2]="addressAdministrativeArea";
383    tmpAddress[3]="addressPostalCode";
384    tmpAddress[4]="addressCountry";
385    tmpAddress[5]="addressElectronicMailAddress";
386    char *tmpPhone[2];
387    tmpPhone[0]="phoneVoice";
388    tmpPhone[1]="phoneFacsimile";
389    char *orderedFields[12];
390    orderedFields[0]="providerName";
391    orderedFields[1]="providerSite";
392    orderedFields[2]="individualName";
393    orderedFields[3]="positionName";
394    orderedFields[4]=tmpPhone[0];
395    orderedFields[5]=tmpPhone[1];
396    orderedFields[6]=tmpAddress[0];
397    orderedFields[7]=tmpAddress[1];
398    orderedFields[8]=tmpAddress[2];
399    orderedFields[9]=tmpAddress[3];
400    orderedFields[10]=tmpAddress[4];
401    orderedFields[11]=tmpAddress[5];
402    int oI=0;
403    for(oI=0;oI<12;oI++)
404      if((tmp2=getMap(tmp4->content,orderedFields[oI]))!=NULL){
405        if(strcmp(tmp2->name,"keywords")!=0 &&
406           strcmp(tmp2->name,"serverAddress")!=0 &&
407           strcmp(tmp2->name,"lang")!=0){
408          tmp2->name[0]=toupper(tmp2->name[0]);
409          if(strcmp(tmp2->name,"ProviderName")==0){
410            nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name);
411            xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value));
412            xmlAddChild(nc,nc1);
413          }
414          else{
415            if(strcmp(tmp2->name,"ProviderSite")==0){
416              nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name);
417              xmlNewNsProp(nc1,ns_xlink,BAD_CAST "href",BAD_CAST tmp2->value);
418              xmlAddChild(nc,nc1);
419            } 
420            else 
421              if(strcmp(tmp2->name,"IndividualName")==0 || 
422                 strcmp(tmp2->name,"PositionName")==0){
423                nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name);
424                xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value));
425                xmlAddChild(nc3,nc1);
426              } 
427              else 
428                if(strncmp(tmp2->name,"Phone",5)==0){
429                  int j;
430                  for(j=0;j<2;j++)
431                    if(strcasecmp(tmp2->name,tmpPhone[j])==0){
432                      char *toto=NULL;
433                      char *toto1=tmp2->name;
434                      toto=strstr(toto1,"Phone");
435                      nc1 = xmlNewNode(ns_ows, BAD_CAST toto1+5);
436                      xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value));
437                      xmlAddChild(nc5,nc1);
438                    }
439                }
440                else 
441                  if(strncmp(tmp2->name,"Address",7)==0){
442                    int j;
443                    for(j=0;j<6;j++)
444                      if(strcasecmp(tmp2->name,tmpAddress[j])==0){
445                        char *toto=NULL;
446                        char *toto1=tmp2->name;
447                        toto=strstr(toto1,"Address");
448                        nc1 = xmlNewNode(ns_ows, BAD_CAST toto1+7);
449                        xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value));
450                        xmlAddChild(nc6,nc1);
451                      }
452                  }
453          }
454        }
455        else
456          if(strcmp(tmp2->name,"keywords")==0){
457            nc1 = xmlNewNode(ns_ows, BAD_CAST "Keywords");
458            char *toto=tmp2->value;
459            char buff[256];
460            int i=0;
461            int j=0;
462            while(toto[i]){
463              if(toto[i]!=',' && toto[i]!=0){
464                buff[j]=toto[i];
465                buff[j+1]=0;
466                j++;
467              }
468              else{
469                nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword");
470                xmlAddChild(nc2,xmlNewText(BAD_CAST buff));           
471                xmlAddChild(nc1,nc2);
472                j=0;
473              }
474              i++;
475            }
476            if(strlen(buff)>0){
477              nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword");
478              xmlAddChild(nc2,xmlNewText(BAD_CAST buff));             
479              xmlAddChild(nc1,nc2);
480            }
481            xmlAddChild(nc,nc1);
482          }
483        tmp2=tmp2->next;
484      }
485  }
486  else{
487    fprintf(stderr,"TMP4 NOT FOUND !!");
488  }
489  xmlAddChild(nc4,nc5);
490  xmlAddChild(nc4,nc6);
491  xmlAddChild(nc3,nc4);
492  xmlAddChild(nc,nc3);
493  xmlAddChild(n,nc);
494
495
496  nc = xmlNewNode(ns_ows, BAD_CAST "OperationsMetadata");
497  char *tmp2[3];
498  tmp2[0]=strdup("GetCapabilities");
499  tmp2[1]=strdup("DescribeProcess");
500  tmp2[2]=strdup("Execute");
501  int j=0;
502
503  if(toto1!=NULL){
504    map* tmp=getMap(toto1->content,"serverAddress");
505    if(tmp!=NULL){
506      SERVICE_URL = strdup(tmp->value);
507    }
508    else
509      SERVICE_URL = strdup("not_found");
510  }
511  else
512    SERVICE_URL = strdup("not_found");
513
514  for(j=0;j<3;j++){
515    nc1 = xmlNewNode(ns_ows, BAD_CAST "Operation");
516    xmlNewProp(nc1,BAD_CAST "name",BAD_CAST tmp2[j]);
517    nc2 = xmlNewNode(ns_ows, BAD_CAST "DCP");
518    nc3 = xmlNewNode(ns_ows, BAD_CAST "HTTP");
519    nc4 = xmlNewNode(ns_ows, BAD_CAST "Get");
520    sprintf(tmp,"%s/%s",SERVICE_URL,service);
521    xmlNewNsProp(nc4,ns_xlink,BAD_CAST "href",BAD_CAST tmp);
522    xmlAddChild(nc3,nc4);
523    if(j>0){
524      nc4 = xmlNewNode(ns_ows, BAD_CAST "Post");
525      xmlNewNsProp(nc4,ns_xlink,BAD_CAST "href",BAD_CAST tmp);
526      xmlAddChild(nc3,nc4);
527    }
528    xmlAddChild(nc2,nc3);
529    xmlAddChild(nc1,nc2);   
530    xmlAddChild(nc,nc1);   
531  }
532  for(j=2;j>=0;j--)
533    free(tmp2[j]);
534  xmlAddChild(n,nc);
535
536  nc = xmlNewNode(ns, BAD_CAST "ProcessOfferings");
537  xmlAddChild(n,nc);
538
539  nc1 = xmlNewNode(ns, BAD_CAST "Languages");
540  nc2 = xmlNewNode(ns, BAD_CAST "Default");
541  nc3 = xmlNewNode(ns, BAD_CAST "Supported");
542 
543  toto1=getMaps(m,"main");
544  if(toto1!=NULL){
545    map* tmp1=getMap(toto1->content,"lang");
546    char *toto=tmp1->value;
547    char buff[256];
548    int i=0;
549    int j=0;
550    int dcount=0;
551    while(toto[i]){
552      if(toto[i]!=',' && toto[i]!=0){
553        buff[j]=toto[i];
554        buff[j+1]=0;
555        j++;
556      }
557      else{
558        nc4 = xmlNewNode(ns_ows, BAD_CAST "Language");
559        xmlAddChild(nc4,xmlNewText(BAD_CAST buff));
560        if(dcount==0){
561          xmlAddChild(nc2,nc4);
562          xmlAddChild(nc1,nc2);
563          dcount++;
564        }
565        nc4 = xmlNewNode(ns_ows, BAD_CAST "Language");
566        xmlAddChild(nc4,xmlNewText(BAD_CAST buff));
567        xmlAddChild(nc3,nc4);
568        j=0;
569        buff[j]=0;
570      }
571      i++;
572    }
573    if(strlen(buff)>0){
574      nc4 = xmlNewNode(ns_ows, BAD_CAST "Language");
575      xmlAddChild(nc4,xmlNewText(BAD_CAST buff));             
576      xmlAddChild(nc3,nc4);
577    }
578  }
579  xmlAddChild(nc1,nc3);
580  xmlAddChild(n,nc1);
581 
582  xmlDocSetRootElement(doc, n);
583  //xmlFreeNs(ns);
584  free(SERVICE_URL);
585  return nc;
586}
587
588void printGetCapabilitiesForProcess(maps* m,xmlNodePtr nc,service* serv){
589  xmlNsPtr ns,ns_ows,ns_xlink;
590  xmlNodePtr nr,n,nc1,nc2,nc3,nc4,nc5,nc6,pseudor;
591  /**
592   * Initialize or get existing namspaces
593   */
594  int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps");
595  ns=usedNs[wpsId];
596  int owsId=zooXmlAddNs(NULL,"http://www.opengis.net/ows/1.1","ows");
597  ns_ows=usedNs[owsId];
598  int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink");
599  ns_xlink=usedNs[xlinkId];
600
601  int cursor=0;
602  map* tmp1;
603  if(serv->content!=NULL){
604    nc1 = xmlNewNode(ns, BAD_CAST "Process");
605    tmp1=getMap(serv->content,"processVersion");
606    if(tmp1!=NULL)
607      xmlNewNsProp(nc1,ns,BAD_CAST "processVersion",BAD_CAST tmp1->value);
608    printDescription(nc1,ns_ows,serv->name,serv->content);
609    tmp1=serv->metadata;
610    while(tmp1!=NULL){
611      nc2 = xmlNewNode(ns_ows, BAD_CAST "Metadata");
612      xmlNewNsProp(nc2,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
613      xmlAddChild(nc1,nc2);
614      tmp1=tmp1->next;
615    }
616    xmlAddChild(nc,nc1);
617  }
618}
619
620xmlNodePtr printDescribeProcessHeader(xmlDocPtr doc,char* service,maps* m){
621
622  xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi;
623  xmlNodePtr n,nr;
624  xmlChar *xmlbuff;
625  int buffersize;
626
627  int wpsId=zooXmlAddNs(NULL,"http://schemas.opengis.net/wps/1.0.0","wps");
628  ns=usedNs[wpsId];
629  n = xmlNewNode(ns, BAD_CAST "ProcessDescriptions");
630  int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows");
631  ns_ows=usedNs[owsId];
632  xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps");
633  zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink");
634  int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi");
635  ns_xsi=usedNs[xsiId];
636 
637  xmlNewNsProp(n,ns_xsi,BAD_CAST "schemaLocation",BAD_CAST "http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsDescribeProcess_response.xsd");
638  xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS");
639  xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0");
640  addLangAttr(n,m);
641
642  xmlDocSetRootElement(doc, n);
643
644  return n;
645}
646
647void printDescribeProcessForProcess(maps* m,xmlNodePtr nc,service* serv,int sc){
648  xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi;
649  xmlNodePtr nr,n,nc1,nc2,nc3,nc4,nc5,nc6,pseudor;
650
651  char tmp[256];
652  n=nc;
653 
654  int wpsId=zooXmlAddNs(NULL,"http://schemas.opengis.net/wps/1.0.0","wps");
655  ns=usedNs[wpsId];
656  int owsId=zooXmlAddNs(NULL,"http://www.opengis.net/ows/1.1","ows");
657  ns_ows=usedNs[owsId];
658  int xlinkId=zooXmlAddNs(NULL,"http://www.w3.org/1999/xlink","xlink");
659  ns_xlink=usedNs[xlinkId];
660
661  nc = xmlNewNode(NULL, BAD_CAST "ProcessDescription");
662  char *tmp4[3];
663  tmp4[0]="processVersion";
664  tmp4[1]="storeSupported";
665  tmp4[2]="statusSupported";
666  int j=0;
667  map* tmp1=NULL;
668  for(j=0;j<3;j++){
669    tmp1=getMap(serv->content,tmp4[j]);
670    if(tmp1!=NULL){
671      if(j==0)
672        xmlNewNsProp(nc,ns,BAD_CAST "processVersion",BAD_CAST tmp1->value);     
673      else
674        xmlNewProp(nc,BAD_CAST tmp4[j],BAD_CAST tmp1->value);     
675    }
676    else{
677      if(j>0)
678        xmlNewProp(nc,BAD_CAST tmp4[j],BAD_CAST "false");     
679    }
680  }
681 
682  printDescription(nc,ns_ows,serv->name,serv->content);
683
684  tmp1=serv->metadata;
685  while(tmp1!=NULL){
686    nc1 = xmlNewNode(ns_ows, BAD_CAST "Metadata");
687    xmlNewNsProp(nc1,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
688    xmlAddChild(nc,nc1);
689    tmp1=tmp1->next;
690  }
691
692  tmp1=getMap(serv->content,"Profile");
693  if(tmp1!=NULL){
694    nc1 = xmlNewNode(ns, BAD_CAST "Profile");
695    xmlAddChild(nc1,xmlNewText(BAD_CAST tmp1->value));
696    xmlAddChild(nc,nc1);
697  }
698
699  nc1 = xmlNewNode(NULL, BAD_CAST "DataInputs");
700  elements* e=serv->inputs;
701  printFullDescription(e,"Input",ns_ows,nc1);
702  xmlAddChild(nc,nc1);
703
704  nc1 = xmlNewNode(NULL, BAD_CAST "ProcessOutputs");
705  e=serv->outputs;
706  printFullDescription(e,"Output",ns_ows,nc1);
707  xmlAddChild(nc,nc1);
708
709  xmlAddChild(n,nc);
710
711}
712
713void printFullDescription(elements *elem,char* type,xmlNsPtr ns_ows,xmlNodePtr nc1){
714  char *orderedFields[7];
715  orderedFields[0]="mimeType";
716  orderedFields[1]="encoding";
717  orderedFields[2]="schema";
718  orderedFields[3]="dataType";
719  orderedFields[4]="uom";
720  orderedFields[5]="CRS";
721  orderedFields[6]="value";
722
723  xmlNodePtr nc2,nc3,nc4,nc5,nc6,nc7;
724  elements* e=elem;
725  map* tmp1=NULL;
726  while(e!=NULL){
727    int default1=0;
728    int isAnyValue=1;
729    nc2 = xmlNewNode(NULL, BAD_CAST type);
730    tmp1=getMap(e->content,"minOccurs");
731    if(tmp1){
732      xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
733    }
734    tmp1=getMap(e->content,"maxOccurs");
735    if(tmp1){
736      xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
737    }
738
739    printDescription(nc2,ns_ows,e->name,e->content);
740
741    if(strncmp(type,"Output",6)==0){
742      if(strncasecmp(e->format,"LITERALDATA",strlen(e->format))==0)
743        nc3 = xmlNewNode(NULL, BAD_CAST "LiteralOutput");
744      else if(strncasecmp(e->format,"COMPLEXDATA",strlen(e->format))==0)
745        nc3 = xmlNewNode(NULL, BAD_CAST "ComplexOutput");
746      else if(strncasecmp(e->format,"BOUNDINGBOXDATA",strlen(e->format))==0)
747        nc3 = xmlNewNode(NULL, BAD_CAST "BoundingBoxOutput");
748      else
749        nc3 = xmlNewNode(NULL, BAD_CAST e->format);
750    }else{
751      if(strncasecmp(e->format,"LITERALDATA",strlen(e->format))==0)
752        nc3 = xmlNewNode(NULL, BAD_CAST "LiteralData");
753      else if(strncasecmp(e->format,"COMPLEXDATA",strlen(e->format))==0)
754        nc3 = xmlNewNode(NULL, BAD_CAST "ComplexData");
755      else if(strncasecmp(e->format,"BOUNDINGBOXDATA",strlen(e->format))==0)
756        nc3 = xmlNewNode(NULL, BAD_CAST "BoundingBoxData");
757      else
758        nc3 = xmlNewNode(NULL, BAD_CAST e->format);
759    }
760    iotype* _tmp=e->defaults;
761    int datatype=0;
762    if(_tmp!=NULL){
763     if(strcmp(e->format,"LiteralOutput")==0 ||
764        strcmp(e->format,"LiteralData")==0){
765        datatype=1;
766        nc4 = xmlNewNode(NULL, BAD_CAST "UOMs");
767        nc5 = xmlNewNode(NULL, BAD_CAST "Default");
768     }
769     else if(strcmp(e->format,"BoundingBoxOutput")==0 ||
770             strcmp(e->format,"BoundingBoxData")==0){
771       datatype=2;
772       //nc4 = xmlNewNode(NULL, BAD_CAST "BoundingBoxOutput");
773       nc5 = xmlNewNode(NULL, BAD_CAST "Default");
774     }
775     else{
776       nc4 = xmlNewNode(NULL, BAD_CAST "Default");
777       nc5 = xmlNewNode(NULL, BAD_CAST "Format");
778     }
779
780     tmp1=_tmp->content;
781     int avcnt=0;
782     int dcnt=0;
783     int oI=0;
784     for(oI=0;oI<7;oI++)
785       if((tmp1=getMap(_tmp->content,orderedFields[oI]))!=NULL){
786     //while(tmp1!=NULL){
787#ifdef DEBUG
788         printf("DATATYPE DEFAULT ? %s\n",tmp1->name);
789#endif
790         if(strncasecmp(tmp1->name,"DataType",8)==0){
791           nc6 = xmlNewNode(ns_ows, BAD_CAST "DataType");
792           xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value));
793           char tmp[1024];
794           sprintf(tmp,"http://www.w3.org/TR/xmlschema-2/#%s",tmp1->value);
795           xmlNewNsProp(nc6,ns_ows,BAD_CAST "reference",BAD_CAST tmp);
796           xmlAddChild(nc3,nc6);
797           tmp1=tmp1->next;
798           datatype=1;
799           continue;
800         }
801         if(strcmp(tmp1->name,"asReference")!=0 &&
802            strncasecmp(tmp1->name,"DataType",8)!=0 &&
803            strcasecmp(tmp1->name,"extension")!=0 &&
804            strcasecmp(tmp1->name,"value")!=0 &&
805            strncasecmp(tmp1->name,"AllowedValues",13)!=0){
806           if(datatype!=1){
807             char *tmp2=zCapitalize1(tmp1->name);
808             nc6 = xmlNewNode(NULL, BAD_CAST tmp2);
809             free(tmp2);
810           }
811           else{
812             char *tmp2=zCapitalize(tmp1->name);
813             nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2);
814             free(tmp2);
815           }
816           xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value));
817           xmlAddChild(nc5,nc6);
818         }else 
819           if(strncmp(type,"Input",5)==0){
820             if(strcmp(tmp1->name,"value")==0){
821               nc7 = xmlNewNode(NULL, BAD_CAST "DefaultValue");
822               xmlAddChild(nc7,xmlNewText(BAD_CAST tmp1->value));
823               default1=1;
824             }
825             if(strncasecmp(tmp1->name,"AllowedValues",13)==0){
826               nc6 = xmlNewNode(ns_ows, BAD_CAST "AllowedValues");
827               char *token,*saveptr1;
828               token=strtok_r(tmp1->value,",",&saveptr1);
829               while(token!=NULL){
830                 nc7 = xmlNewNode(ns_ows, BAD_CAST "Value");
831                 char *tmps=strdup(token);
832                 tmps[strlen(tmps)]=0;
833                 xmlAddChild(nc7,xmlNewText(BAD_CAST tmps));
834                 fprintf(stderr,"strgin : %s\n",tmps);
835                 xmlAddChild(nc6,nc7);
836                 token=strtok_r(NULL,",",&saveptr1);
837               }
838               xmlAddChild(nc3,nc6);
839               isAnyValue=-1;
840             }
841           }
842         tmp1=tmp1->next;
843         if(datatype!=2){
844           xmlAddChild(nc4,nc5);
845           xmlAddChild(nc3,nc4);
846         }else{
847           fprintf(stderr,"OK \n");
848           xmlAddChild(nc3,nc5);
849           fprintf(stderr,"OK \n");
850         }
851         
852         if(strncmp(type,"Input",5)==0){
853           if(datatype==1 && isAnyValue==1 && avcnt==0){
854             xmlAddChild(nc3,xmlNewNode(ns_ows, BAD_CAST "AnyValue"));
855             avcnt++;
856           }
857           if(datatype==1 && default1>0){
858             xmlAddChild(nc3,nc7);
859           }
860        }
861       }
862    }
863    _tmp=e->supported;
864    int hasSupported=-1;
865    while(_tmp!=NULL){
866      if(hasSupported<0){
867        if(datatype==0){
868          nc4 = xmlNewNode(NULL, BAD_CAST "Supported");
869          nc5 = xmlNewNode(NULL, BAD_CAST "Format");
870        }
871        else
872          nc5 = xmlNewNode(NULL, BAD_CAST "Supported");
873        hasSupported=0;
874      }else
875        if(datatype==0)
876          nc5 = xmlNewNode(NULL, BAD_CAST "Format");
877      tmp1=_tmp->content;
878      int oI=0;
879      for(oI=0;oI<6;oI++)
880        if((tmp1=getMap(_tmp->content,orderedFields[oI]))!=NULL){
881#ifdef DEBUG
882          printf("DATATYPE SUPPORTED ? %s\n",tmp1->name);
883#endif
884          if(strcmp(tmp1->name,"asReference")!=0 && 
885             strcmp(tmp1->name,"DataType")!=0 &&
886             strcasecmp(tmp1->name,"extension")!=0){
887            if(datatype!=1){
888              char *tmp2=zCapitalize1(tmp1->name);
889              nc6 = xmlNewNode(NULL, BAD_CAST tmp2);
890              free(tmp2);
891            }
892            else{
893              char *tmp2=zCapitalize(tmp1->name);
894              nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2);
895              free(tmp2);
896            }
897            if(datatype==2){
898              char *tmpv,*tmps;
899              tmps=strtok_r(tmp1->value,",",&tmpv);
900              while(tmps){
901                fprintf(stderr,"Element %s\n",tmps);
902                xmlAddChild(nc6,xmlNewText(BAD_CAST tmps));
903                xmlAddChild(nc5,nc6);
904                tmps=strtok_r(NULL,",",&tmpv);
905                if(tmps){
906                  char *tmp2=zCapitalize1(tmp1->name);
907                  nc6 = xmlNewNode(NULL, BAD_CAST tmp2);
908                  free(tmp2);
909                }
910              }
911              //free(tmpv);
912            }
913            else{
914              xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value));
915              xmlAddChild(nc5,nc6);
916            }
917          }
918          tmp1=tmp1->next;
919        }
920      if(hasSupported<=0){
921        if(datatype!=2){
922          xmlAddChild(nc4,nc5);
923          xmlAddChild(nc3,nc4);
924        }else
925          xmlAddChild(nc3,nc5);
926        fprintf(stderr,"OK \n");
927        hasSupported=1;
928      }
929      else
930        if(datatype!=2){
931          xmlAddChild(nc4,nc5);
932        }
933        else
934          xmlAddChild(nc3,nc5);
935      _tmp=_tmp->next;
936    }
937    xmlAddChild(nc2,nc3);
938   
939    if(datatype!=2){
940      xmlAddChild(nc3,nc4);
941      xmlAddChild(nc2,nc3);
942    }
943   
944    xmlAddChild(nc1,nc2);
945   
946    e=e->next;
947  }
948}
949
950void printProcessResponse(maps* m,map* request, int pid,service* serv,char* service,int status,maps* inputs,maps* outputs){
951  xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi;
952  xmlNodePtr nr,n,nc,nc1,nc2,nc3,pseudor;
953  xmlDocPtr doc;
954  xmlChar *xmlbuff;
955  int buffersize;
956  time_t time1; 
957  time(&time1);
958  /**
959   * Create the document and its temporary root.
960   */
961  doc = xmlNewDoc(BAD_CAST "1.0");
962  int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps");
963  ns=usedNs[wpsId];
964 
965  n = xmlNewNode(ns, BAD_CAST "ExecuteResponse");
966  int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows");
967  ns_ows=usedNs[owsId];
968  int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink");
969  ns_xlink=usedNs[xlinkId];
970  int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi");
971  ns_xsi=usedNs[xsiId];
972  xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps");
973
974  xmlNewNsProp(n,ns_xsi,BAD_CAST "schemaLocation",BAD_CAST "http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsExecute_response.xsd");
975 
976  xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS");
977  xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0");
978  addLangAttr(n,m);
979
980  char tmp[256];
981  char url[1024];
982  char stored_path[1024];
983  memset(tmp,0,256);
984  memset(url,0,1024);
985  memset(stored_path,0,1024);
986  maps* tmp_maps=getMaps(m,"main");
987  if(tmp_maps!=NULL){
988    map* tmpm1=getMap(tmp_maps->content,"serverAddress");
989    /**
990     * Check if the ZOO Service GetStatus is available in the local directory.
991     * If yes, then it uses a reference to an URL which the client can access
992     * to get information on the status of a running Service (using the
993     * percentCompleted attribute).
994     * Else fallback to the initial method using the xml file to write in ...
995     */
996    char ntmp[1024];
997#ifndef WIN32
998    getcwd(ntmp,1024);
999#else
1000    _getcwd(ntmp,1024);
1001#endif
1002    struct stat myFileInfo;
1003    int statRes;
1004    char file_path[1024];
1005    sprintf(file_path,"%s/GetStatus.zcfg",ntmp);
1006    statRes=stat(file_path,&myFileInfo);
1007    if(statRes==0){
1008      char currentSid[128];
1009      map* tmpm=getMap(tmp_maps->content,"rewriteUrl");
1010      map *tmp_lenv=NULL;
1011      tmp_lenv=getMapFromMaps(m,"lenv","sid");
1012      if(tmp_lenv==NULL)
1013        sprintf(currentSid,"%i",pid);
1014      else
1015        sprintf(currentSid,"%s",tmp_lenv->value);
1016      if(tmpm==NULL || strcasecmp(tmpm->value,"false")==0){
1017        sprintf(url,"%s/?request=Execute&service=WPS&version=1.0.0&Identifier=GetStatus&DataInputs=sid=%s&RawDataOutput=Result",tmpm1->value,currentSid);
1018      }else{
1019        if(strlen(tmpm->value)>0)
1020          if(strcasecmp(tmpm->value,"true")!=0)
1021            sprintf(url,"%s/%s/GetStatus/%s",tmpm1->value,tmpm->value,currentSid);
1022          else
1023            sprintf(url,"%s/GetStatus/%s",tmpm1->value,currentSid);
1024        else
1025          sprintf(url,"%s/?request=Execute&service=WPS&version=1.0.0&Identifier=GetStatus&DataInputs=sid=%s&RawDataOutput=Result",tmpm1->value,currentSid);
1026      }
1027    }else{
1028      map* tmpm2=getMap(tmp_maps->content,"tmpUrl");
1029      if(tmpm1!=NULL && tmpm2!=NULL){
1030        sprintf(url,"%s/%s/%s_%i.xml",tmpm1->value,tmpm2->value,service,pid);
1031      }
1032    }
1033    if(tmpm1!=NULL)
1034      sprintf(tmp,"%s/",tmpm1->value);
1035    tmpm1=getMapFromMaps(m,"main","TmpPath");
1036    sprintf(stored_path,"%s/%s_%i.xml",tmpm1->value,service,pid);
1037  }
1038
1039 
1040
1041  xmlNewProp(n,BAD_CAST "serviceInstance",BAD_CAST tmp);
1042  map* test=getMap(request,"storeExecuteResponse");
1043  bool hasStoredExecuteResponse=false;
1044  if(test!=NULL && strcasecmp(test->value,"true")==0){
1045    xmlNewProp(n,BAD_CAST "statusLocation",BAD_CAST url);
1046    hasStoredExecuteResponse=true;
1047  }
1048
1049  nc = xmlNewNode(ns, BAD_CAST "Process");
1050  map* tmp2=getMap(serv->content,"processVersion");
1051
1052  if(tmp2!=NULL)
1053    xmlNewNsProp(nc,ns,BAD_CAST "processVersion",BAD_CAST tmp2->value);
1054 
1055  printDescription(nc,ns_ows,serv->name,serv->content);
1056  fflush(stderr);
1057
1058  xmlAddChild(n,nc);
1059
1060  nc = xmlNewNode(ns, BAD_CAST "Status");
1061  const struct tm *tm;
1062  size_t len;
1063  time_t now;
1064  char *tmp1;
1065  map *tmpStatus;
1066 
1067  now = time ( NULL );
1068  tm = localtime ( &now );
1069
1070  tmp1 = (char*)malloc((TIME_SIZE+1)*sizeof(char));
1071
1072  len = strftime ( tmp1, TIME_SIZE, "%Y-%m-%dT%I:%M:%SZ", tm );
1073
1074  xmlNewProp(nc,BAD_CAST "creationTime",BAD_CAST tmp1);
1075
1076  char sMsg[2048];
1077  switch(status){
1078  case SERVICE_SUCCEEDED:
1079    nc1 = xmlNewNode(ns, BAD_CAST "ProcessSucceeded");
1080    sprintf(sMsg,_("Service \"%s\" run successfully."),serv->name);
1081    nc3=xmlNewText(BAD_CAST sMsg);
1082    xmlAddChild(nc1,nc3);
1083    break;
1084  case SERVICE_STARTED:
1085    nc1 = xmlNewNode(ns, BAD_CAST "ProcessStarted");
1086    tmpStatus=getMapFromMaps(m,"lenv","status");
1087    xmlNewProp(nc1,BAD_CAST "percentCompleted",BAD_CAST tmpStatus->value);
1088    sprintf(sMsg,_("ZOO Service \"%s\" is currently running. Please, reload this document to get the up-to-date status of the Service."),serv->name);
1089    nc3=xmlNewText(BAD_CAST sMsg);
1090    xmlAddChild(nc1,nc3);
1091    break;
1092  case SERVICE_ACCEPTED:
1093    nc1 = xmlNewNode(ns, BAD_CAST "ProcessAccepted");
1094    sprintf(sMsg,_("Service \"%s\" was accepted by the ZOO Kernel and it run as a background task. Please consult the statusLocation attribtue providen in this document to get the up-to-date document."),serv->name);
1095    nc3=xmlNewText(BAD_CAST sMsg);
1096    xmlAddChild(nc1,nc3);
1097    break;
1098  case SERVICE_FAILED:
1099    nc1 = xmlNewNode(ns, BAD_CAST "ProcessFailed");
1100    map *errorMap;
1101    map *te;
1102    te=getMapFromMaps(m,"lenv","code");
1103    if(te!=NULL)
1104      errorMap=createMap("code",te->value);
1105    else
1106      errorMap=createMap("code","NoApplicableCode");
1107    te=getMapFromMaps(m,"lenv","message");
1108    if(te!=NULL)
1109      addToMap(errorMap,"text",_ss(te->value));
1110    else
1111      addToMap(errorMap,"text",_("No more information available"));
1112    nc3=createExceptionReportNode(m,errorMap,0);
1113    freeMap(&errorMap);
1114    free(errorMap);
1115    xmlAddChild(nc1,nc3);
1116    break;
1117  default :
1118    printf(_("error code not know : %i\n"),status);
1119    //exit(1);
1120    break;
1121  }
1122  xmlAddChild(nc,nc1);
1123  xmlAddChild(n,nc);
1124  free(tmp1);
1125
1126#ifdef DEBUG
1127  fprintf(stderr,"printProcessResponse 1 161\n");
1128#endif
1129
1130  map* lineage=getMap(request,"lineage");
1131  if(lineage!=NULL){
1132    nc = xmlNewNode(ns, BAD_CAST "DataInputs");
1133    int i;
1134    maps* mcursor=inputs;
1135    elements* scursor=NULL;
1136    while(mcursor!=NULL /*&& scursor!=NULL*/){
1137      scursor=getElements(serv->inputs,mcursor->name);
1138      printIOType(doc,nc,ns,ns_ows,ns_xlink,scursor,mcursor,"Input");
1139      mcursor=mcursor->next;
1140    }
1141    xmlAddChild(n,nc);
1142   
1143#ifdef DEBUG
1144    fprintf(stderr,"printProcessResponse 1 177\n");
1145#endif
1146
1147    nc = xmlNewNode(ns, BAD_CAST "OutputDefinitions");
1148    mcursor=outputs;
1149    scursor=NULL;
1150    while(mcursor!=NULL){
1151      scursor=getElements(serv->outputs,mcursor->name);
1152      printOutputDefinitions1(doc,nc,ns,ns_ows,scursor,mcursor,"Output");
1153      mcursor=mcursor->next;
1154    }
1155    xmlAddChild(n,nc);
1156  }
1157#ifdef DEBUG
1158  fprintf(stderr,"printProcessResponse 1 190\n");
1159#endif
1160
1161  /**
1162   * Display the process output only when requested !
1163   */
1164  if(status==SERVICE_SUCCEEDED){
1165    nc = xmlNewNode(ns, BAD_CAST "ProcessOutputs");
1166    maps* mcursor=outputs;
1167    elements* scursor=serv->outputs;
1168    while(mcursor!=NULL){
1169      scursor=getElements(serv->outputs,mcursor->name);
1170      printIOType(doc,nc,ns,ns_ows,ns_xlink,scursor,mcursor,"Output");
1171      mcursor=mcursor->next;
1172    }
1173    xmlAddChild(n,nc);
1174  }
1175#ifdef DEBUG
1176  fprintf(stderr,"printProcessResponse 1 202\n");
1177#endif
1178  xmlDocSetRootElement(doc, n);
1179  if(hasStoredExecuteResponse==true){
1180    /* We need to write the ExecuteResponse Document somewhere */
1181    FILE* output=fopen(stored_path,"w");
1182    xmlChar *xmlbuff;
1183    int buffersize;
1184    xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8", 1);
1185    fwrite(xmlbuff,1,strlen(xmlbuff)*sizeof(char),output);
1186    xmlFree(xmlbuff);
1187    fclose(output);
1188  }
1189  printDocument(m,doc,pid);
1190
1191  xmlCleanupParser();
1192  zooXmlCleanupNs();
1193}
1194
1195
1196void printDocument(maps* m, xmlDocPtr doc,int pid){
1197  rewind(stdout);
1198  char *encoding=getEncoding(m);
1199  if(pid==getpid()){
1200    printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1201  }
1202  fflush(stdout);
1203  xmlChar *xmlbuff;
1204  int buffersize;
1205  /*
1206   * Dump the document to a buffer and print it on stdout
1207   * for demonstration purposes.
1208   */
1209  xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
1210  printf((char *) xmlbuff);
1211  //fflush(stdout);
1212  /*
1213   * Free associated memory.
1214   */
1215  xmlFree(xmlbuff);
1216  xmlFreeDoc(doc);
1217  xmlCleanupParser();
1218  zooXmlCleanupNs();
1219}
1220
1221void printOutputDefinitions1(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,elements* e,maps* m,char* type){
1222  xmlNodePtr nc1;
1223  nc1=xmlNewNode(ns_wps, BAD_CAST type);
1224  map *tmp=NULL; 
1225  if(e!=NULL && e->defaults!=NULL)
1226    tmp=e->defaults->content;
1227  else{
1228    /*
1229    dumpElements(e);
1230    */
1231    return;
1232  }
1233  while(tmp!=NULL){
1234    if(strncasecmp(tmp->name,"MIMETYPE",strlen(tmp->name))==0
1235       || strncasecmp(tmp->name,"ENCODING",strlen(tmp->name))==0
1236       || strncasecmp(tmp->name,"SCHEMA",strlen(tmp->name))==0
1237       || strncasecmp(tmp->name,"UOM",strlen(tmp->name))==0)
1238    xmlNewProp(nc1,BAD_CAST tmp->name,BAD_CAST tmp->value);
1239    tmp=tmp->next;
1240  }
1241  tmp=getMap(e->defaults->content,"asReference");
1242  if(tmp==NULL)
1243    xmlNewProp(nc1,BAD_CAST "asReference",BAD_CAST "false");
1244
1245  tmp=e->content;
1246
1247  printDescription(nc1,ns_ows,m->name,e->content);
1248
1249  xmlAddChild(nc,nc1);
1250
1251}
1252
1253void printOutputDefinitions(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,elements* e,map* m,char* type){
1254  xmlNodePtr nc1,nc2,nc3;
1255  nc1=xmlNewNode(ns_wps, BAD_CAST type);
1256  map *tmp=NULL; 
1257  if(e!=NULL && e->defaults!=NULL)
1258    tmp=e->defaults->content;
1259  else{
1260    /*
1261    dumpElements(e);
1262    */
1263    return;
1264  }
1265  while(tmp!=NULL){
1266    xmlNewProp(nc1,BAD_CAST tmp->name,BAD_CAST tmp->value);
1267    tmp=tmp->next;
1268  }
1269  tmp=getMap(e->defaults->content,"asReference");
1270  if(tmp==NULL)
1271    xmlNewProp(nc1,BAD_CAST "asReference",BAD_CAST "false");
1272
1273  tmp=e->content;
1274
1275  printDescription(nc1,ns_ows,m->name,e->content);
1276
1277  xmlAddChild(nc,nc1);
1278
1279}
1280
1281void printIOType(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,xmlNsPtr ns_xlink,elements* e,maps* m,char* type){
1282  xmlNodePtr nc1,nc2,nc3;
1283  nc1=xmlNewNode(ns_wps, BAD_CAST type);
1284  map *tmp=NULL;
1285  if(e!=NULL)
1286    tmp=e->content;
1287  else
1288    tmp=m->content;
1289#ifdef DEBUG
1290  dumpMap(tmp);
1291  dumpElements(e);
1292#endif
1293  nc2=xmlNewNode(ns_ows, BAD_CAST "Identifier");
1294  if(e!=NULL)
1295    nc3=xmlNewText(BAD_CAST e->name);
1296  else
1297    nc3=xmlNewText(BAD_CAST m->name);
1298  xmlAddChild(nc2,nc3);
1299  xmlAddChild(nc1,nc2);
1300  xmlAddChild(nc,nc1);
1301  // Extract Title required to be first element in the ZCFG file !
1302  bool isTitle=true;
1303  if(e!=NULL)
1304    tmp=getMap(e->content,"Title");
1305  else
1306    tmp=getMap(m->content,"Title");
1307 
1308  if(tmp!=NULL){
1309    nc2=xmlNewNode(ns_ows, BAD_CAST tmp->name);
1310    nc3=xmlNewText(BAD_CAST _ss(tmp->value));
1311    xmlAddChild(nc2,nc3); 
1312    xmlAddChild(nc1,nc2);
1313  }
1314
1315  if(e!=NULL)
1316    tmp=getMap(e->content,"Abstract");
1317  else
1318    tmp=getMap(m->content,"Abstract");
1319  if(tmp!=NULL){
1320    nc2=xmlNewNode(ns_ows, BAD_CAST tmp->name);
1321    nc3=xmlNewText(BAD_CAST _ss(tmp->value));
1322    xmlAddChild(nc2,nc3); 
1323    xmlAddChild(nc1,nc2);
1324    xmlAddChild(nc,nc1);
1325  }
1326
1327  /**
1328   * IO type Reference or full Data ?
1329   */
1330#ifdef DEBUG
1331  fprintf(stderr,"FORMAT %s %s\n",e->format,e->format);
1332#endif
1333  map *tmpMap=getMap(m->content,"Reference");
1334  if(tmpMap==NULL){
1335    nc2=xmlNewNode(ns_wps, BAD_CAST "Data");
1336    if(e!=NULL){
1337      if(strncasecmp(e->format,"LiteralOutput",strlen(e->format))==0)
1338        nc3=xmlNewNode(ns_wps, BAD_CAST "LiteralData");
1339      else
1340        if(strncasecmp(e->format,"ComplexOutput",strlen(e->format))==0)
1341          nc3=xmlNewNode(ns_wps, BAD_CAST "ComplexData");
1342        else if(strncasecmp(e->format,"BoundingBoxOutput",strlen(e->format))==0)
1343          nc3=xmlNewNode(ns_wps, BAD_CAST "BoundingBoxData");
1344        else
1345          nc3=xmlNewNode(ns_wps, BAD_CAST e->format);
1346    }
1347    else{
1348      map* tmpV=getMapFromMaps(m,"format","value");
1349      if(tmpV!=NULL)
1350        nc3=xmlNewNode(ns_wps, BAD_CAST tmpV->value);
1351      else
1352        nc3=xmlNewNode(ns_wps, BAD_CAST "LitteralData");
1353    } 
1354    tmp=m->content;
1355    while(tmp!=NULL){
1356      if(strcasecmp(tmp->name,"mimeType")==0 ||
1357         strcasecmp(tmp->name,"encoding")==0 ||
1358         strcasecmp(tmp->name,"schema")==0 ||
1359         strcasecmp(tmp->name,"datatype")==0 ||
1360         strcasecmp(tmp->name,"uom")==0)
1361        xmlNewProp(nc3,BAD_CAST tmp->name,BAD_CAST tmp->value);
1362      tmp=tmp->next;
1363      xmlAddChild(nc2,nc3);
1364    }
1365    if(e!=NULL && e->format!=NULL && strcasecmp(e->format,"BoundingBoxData")==0){
1366      map* bb=getMap(m->content,"value");
1367      if(bb!=NULL){
1368        map* tmpRes=parseBoundingBox(bb->value);
1369        printBoundingBox(ns_ows,nc3,tmpRes);
1370        freeMap(&tmpRes);
1371        free(tmpRes);
1372      }
1373    }else{
1374      if(e!=NULL)
1375        tmp=getMap(e->defaults->content,"mimeType");
1376      else
1377        tmp=NULL;
1378      map* tmp1=getMap(m->content,"encoding");
1379      map* tmp2=getMap(m->content,"mimeType");
1380      map* toto=getMap(m->content,"value");
1381      if((tmp1!=NULL && strncmp(tmp1->value,"base64",6)==0)
1382         || (tmp2!=NULL && (strncmp(tmp2->value,"image/",6)==0 ||
1383                            (strncmp(tmp2->value,"application/",12)==0) &&
1384                            strncmp(tmp2->value,"application/json",16)!=0))) {
1385        map* rs=getMap(m->content,"size");
1386        bool isSized=true;
1387        if(rs==NULL){
1388          char tmp1[1024];
1389          sprintf(tmp1,"%d",strlen(toto->value));
1390          rs=createMap("z",tmp1);
1391          isSized=false;
1392        }
1393        xmlAddChild(nc3,xmlNewText(BAD_CAST base64((const unsigned char*)toto->value,atoi(rs->value))));
1394        if(!isSized){
1395          freeMap(&rs);
1396          free(rs);
1397        }
1398      }
1399      else if(tmp!=NULL){
1400        if(strncmp(tmp->value,"text/js",4)==0 ||
1401           strncmp(tmp->value,"application/js",14)==0)
1402          xmlAddChild(nc3,xmlNewCDataBlock(doc,BAD_CAST toto->value,strlen(toto->value)));
1403        else
1404          xmlAddChild(nc3,xmlNewText(BAD_CAST toto->value));
1405        xmlAddChild(nc2,nc3);
1406      }
1407      else
1408        xmlAddChild(nc3,xmlNewText(BAD_CAST toto->value));
1409    }
1410  }
1411  else{
1412    nc3=nc2=xmlNewNode(ns_wps, BAD_CAST "Reference");
1413    if(strcasecmp(type,"Output")==0)
1414      xmlNewProp(nc3,BAD_CAST "href",BAD_CAST tmpMap->value);
1415    else
1416      xmlNewNsProp(nc3,ns_xlink,BAD_CAST "href",BAD_CAST tmpMap->value);
1417    tmp=m->content;
1418    while(tmp!=NULL){
1419      if(strcasecmp(tmp->name,"mimeType")==0 ||
1420         strcasecmp(tmp->name,"encoding")==0 ||
1421         strcasecmp(tmp->name,"schema")==0 ||
1422         strcasecmp(tmp->name,"datatype")==0 ||
1423         strcasecmp(tmp->name,"uom")==0)
1424        xmlNewProp(nc3,BAD_CAST tmp->name,BAD_CAST tmp->value);
1425      tmp=tmp->next;
1426      xmlAddChild(nc2,nc3);
1427    }
1428  }
1429
1430  xmlAddChild(nc1,nc2);
1431  xmlAddChild(nc,nc1);
1432
1433}
1434
1435void printDescription(xmlNodePtr root,xmlNsPtr ns_ows,char* identifier,map* amap){
1436  xmlNodePtr nc2 = xmlNewNode(ns_ows, BAD_CAST "Identifier");
1437  xmlAddChild(nc2,xmlNewText(BAD_CAST identifier));
1438  xmlAddChild(root,nc2);
1439  map* tmp=amap;
1440  char *tmp2[2];
1441  tmp2[0]="Title";
1442  tmp2[1]="Abstract";
1443  int j=0;
1444  for(j=0;j<2;j++){
1445    map* tmp1=getMap(tmp,tmp2[j]);
1446    if(tmp1!=NULL){
1447      nc2 = xmlNewNode(ns_ows, BAD_CAST tmp2[j]);
1448      xmlAddChild(nc2,xmlNewText(BAD_CAST _ss(tmp1->value)));
1449      xmlAddChild(root,nc2);
1450    }
1451  }
1452}
1453
1454char* getEncoding(maps* m){
1455  if(m!=NULL){
1456    map* tmp=getMap(m->content,"encoding");
1457    if(tmp!=NULL){
1458      return tmp->value;
1459    }
1460    else
1461      return "UTF-8";
1462  }
1463  else
1464    return "UTF-8"; 
1465}
1466
1467char* getVersion(maps* m){
1468  if(m!=NULL){
1469    map* tmp=getMap(m->content,"version");
1470    if(tmp!=NULL){
1471      return tmp->value;
1472    }
1473    else
1474      return "1.0.0";
1475  }
1476  else
1477    return "1.0.0";
1478}
1479
1480void printExceptionReportResponse(maps* m,map* s){
1481  int buffersize;
1482  xmlDocPtr doc;
1483  xmlChar *xmlbuff;
1484  xmlNodePtr n;
1485
1486  doc = xmlNewDoc(BAD_CAST "1.0");
1487  maps* tmpMap=getMaps(m,"main");
1488  char *encoding=getEncoding(tmpMap);
1489  if(m!=NULL){
1490    map *tmpSid=getMapFromMaps(m,"lenv","sid");
1491    if(tmpSid!=NULL){
1492      if( getpid()==atoi(tmpSid->value) )
1493        printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1494    }
1495    else
1496      printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1497  }else
1498    printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1499  n=createExceptionReportNode(m,s,1);
1500  xmlDocSetRootElement(doc, n);
1501  xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
1502  printf("%s",xmlbuff);
1503  fflush(stdout);
1504  xmlFreeDoc(doc);
1505  xmlFree(xmlbuff);
1506  xmlCleanupParser();
1507  zooXmlCleanupNs();
1508}
1509
1510xmlNodePtr createExceptionReportNode(maps* m,map* s,int use_ns){
1511 
1512  int buffersize;
1513  xmlChar *xmlbuff;
1514  xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi;
1515  xmlNodePtr n,nc,nc1,nc2;
1516
1517  maps* tmpMap=getMaps(m,"main");
1518
1519  int nsid=zooXmlAddNs(NULL,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows");
1520  ns=usedNs[nsid];
1521  n = xmlNewNode(ns, BAD_CAST "ExceptionReport");
1522
1523  if(use_ns==1){
1524    ns_ows=xmlNewNs(n,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows");
1525    int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi");
1526    ns_xsi=usedNs[xsiId];
1527    int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink");
1528    ns_xlink=usedNs[xlinkId];
1529    xmlNewNsProp(n,ns_xsi,BAD_CAST "schemaLocation",BAD_CAST "http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd");
1530  }
1531  addLangAttr(n,m);
1532  xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.1.0");
1533 
1534  nc = xmlNewNode(ns, BAD_CAST "Exception");
1535
1536  map* tmp=getMap(s,"code");
1537  if(tmp!=NULL)
1538    xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST tmp->value);
1539  else
1540    xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST "NoApplicableCode");
1541
1542  tmp=getMap(s,"text");
1543  nc1 = xmlNewNode(ns, BAD_CAST "ExceptionText");
1544  nc2=NULL;
1545  if(tmp!=NULL){
1546    xmlNodeSetContent(nc1, BAD_CAST tmp->value);
1547  }
1548  else{
1549    xmlNodeSetContent(nc1, BAD_CAST _("No debug message available"));
1550  }
1551  xmlAddChild(nc,nc1);
1552  xmlAddChild(n,nc);
1553  return n;
1554}
1555
1556
1557void outputResponse(service* s,maps* request_inputs,maps* request_outputs,
1558                    map* request_inputs1,int cpid,maps* m,int res){
1559#ifdef DEBUG
1560  dumpMaps(request_inputs);
1561  dumpMaps(request_outputs);
1562  fprintf(stderr,"printProcessResponse\n");
1563#endif
1564  map* toto=getMap(request_inputs1,"RawDataOutput");
1565  int asRaw=0;
1566  if(toto!=NULL)
1567    asRaw=1;
1568 
1569  if(asRaw==0){
1570#ifdef DEBUG
1571    fprintf(stderr,"REQUEST_OUTPUTS FINAL\n");
1572    dumpMaps(request_outputs);
1573#endif
1574    maps* tmpI=request_outputs;
1575    while(tmpI!=NULL){
1576      toto=getMap(tmpI->content,"asReference");
1577      if(toto!=NULL && strcasecmp(toto->value,"true")==0){
1578        elements* in=getElements(s->outputs,tmpI->name);
1579        char *format=NULL;
1580        if(in!=NULL){
1581          format=strdup(in->format);
1582        }else
1583          format=strdup("LiteralData");
1584        if(strcasecmp(format,"BoundingBoxData")==0){
1585          addToMap(tmpI->content,"extension","xml");
1586          addToMap(tmpI->content,"mimeType","text/xml");
1587          addToMap(tmpI->content,"encoding","UTF-8");
1588          addToMap(tmpI->content,"schema","http://schemas.opengis.net/ows/1.1.0/owsCommon.xsd");
1589        }
1590        map *ext=getMap(tmpI->content,"extension");
1591        map *tmp1=getMapFromMaps(m,"main","tmpPath");
1592        char *file_name;
1593        bool hasExt=true;
1594        if(ext==NULL){
1595          // We can fallback to a default list of supported formats using
1596          // mimeType information if present here. Maybe we can add more formats
1597          // here.
1598          // If mimeType was not found, we then set txt as the default extension.
1599          map* mtype=getMap(tmpI->content,"mimeType");
1600          if(mtype!=NULL){
1601            if(strcasecmp(mtype->value,"text/xml")==0)
1602              ext=createMap("extension","xml");
1603            else if(strcasecmp(mtype->value,"application/json")==0)
1604              ext=createMap("extension","js");
1605            else
1606              ext=createMap("extension","txt");
1607          }
1608          else
1609            ext=createMap("extension","txt");
1610          hasExt=false;
1611        }
1612        file_name=(char*)malloc((strlen(tmp1->value)+strlen(s->name)+strlen(ext->value)+strlen(tmpI->name)+13)*sizeof(char));
1613        sprintf(file_name,"%s/%s_%s_%i.%s",tmp1->value,s->name,tmpI->name,cpid+100000,ext->value);
1614        FILE *ofile=fopen(file_name,"w");
1615        if(ofile==NULL)
1616          fprintf(stderr,"Unable to create file on disk implying segfault ! \n");
1617        map *tmp2=getMapFromMaps(m,"main","tmpUrl");
1618        map *tmp3=getMapFromMaps(m,"main","serverAddress");
1619        char *file_url;
1620        file_url=(char*)malloc((strlen(tmp3->value)+strlen(tmp2->value)+strlen(s->name)+strlen(ext->value)+strlen(tmpI->name)+13)*sizeof(char));
1621        sprintf(file_url,"%s/%s/%s_%s_%i.%s",tmp3->value,tmp2->value,s->name,tmpI->name,cpid+100000,ext->value);
1622        addToMap(tmpI->content,"Reference",file_url);
1623        if(hasExt!=true){
1624          freeMap(&ext);
1625          free(ext);
1626        }
1627        toto=getMap(tmpI->content,"value");
1628        if(strcasecmp(format,"BoundingBoxData")!=0){
1629          map* size=getMap(tmpI->content,"size");
1630          if(size!=NULL && toto!=NULL)
1631            fwrite(toto->value,1,atoi(size->value)*sizeof(char),ofile);
1632          else
1633            if(toto!=NULL && toto->value!=NULL)
1634              fwrite(toto->value,1,strlen(toto->value)*sizeof(char),ofile);
1635        }else{
1636          printBoundingBoxDocument(m,tmpI,ofile);
1637        }
1638        free(format);
1639        fclose(ofile);
1640        free(file_name);
1641        free(file_url); 
1642      }
1643      tmpI=tmpI->next;
1644    }
1645    map *r_inputs=getMap(s->content,"serviceProvider");
1646#ifdef DEBUG
1647    fprintf(stderr,"SERVICE : %s\n",r_inputs->value);
1648    dumpMaps(m);
1649#endif
1650    printProcessResponse(m,request_inputs1,cpid,
1651                         s,r_inputs->value,res,
1652                         request_inputs,
1653                         request_outputs);
1654  }
1655  else
1656    if(res!=SERVICE_FAILED){
1657      /**
1658       * We get the requested output or fallback to the first one if the
1659       * requested one is not present in the resulting outputs maps.
1660       */
1661      maps* tmpI=NULL;
1662      map* tmpIV=getMap(request_inputs1,"RawDataOutput");
1663      if(tmpIV!=NULL){
1664        tmpI=getMaps(request_outputs,tmpIV->value);
1665      }
1666      if(tmpI==NULL)
1667        tmpI=request_outputs;
1668      elements* e=getElements(s->outputs,tmpI->name);
1669      if(e!=NULL && strcasecmp(e->format,"BoundingBoxData")==0){
1670        printBoundingBoxDocument(m,tmpI,NULL);
1671      }else{
1672        toto=getMap(tmpI->content,"value");
1673        if(toto==NULL){
1674          char tmpMsg[1024];
1675          sprintf(tmpMsg,_("Wrong RawDataOutput parameter, unable to fetch any result for the name your provided : \"%s\"."),tmpI->name);
1676          map * errormap = createMap("text",tmpMsg);
1677          addToMap(errormap,"code", "InvalidParameterValue");
1678          printExceptionReportResponse(m,errormap);
1679          freeMap(&errormap);
1680          free(errormap);
1681          return 1;
1682        }
1683        char mime[1024];
1684        map* mi=getMap(tmpI->content,"mimeType");
1685#ifdef DEBUG
1686        fprintf(stderr,"SERVICE OUTPUTS\n");
1687        dumpMaps(request_outputs);
1688        fprintf(stderr,"SERVICE OUTPUTS\n");
1689#endif
1690        map* en=getMap(tmpI->content,"encoding");
1691        if(mi!=NULL && en!=NULL)
1692          sprintf(mime,
1693                  "Content-Type: %s; charset=%s\r\nStatus: 200 OK\r\n\r\n",
1694                  mi->value,en->value);
1695        else
1696          if(mi!=NULL)
1697            sprintf(mime,
1698                    "Content-Type: %s; charset=UTF-8\r\nStatus: 200 OK\r\n\r\n",
1699                    mi->value);
1700          else
1701            sprintf(mime,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
1702        printf("%s",mime);
1703        if(mi!=NULL && strncmp(mi->value,"image",5)==0){
1704          map* rs=getMapFromMaps(tmpI,tmpI->name,"size");
1705          fwrite(toto->value,atoi(rs->value),1,stdout);
1706        }
1707        else
1708          printf("%s",toto->value);
1709#ifdef DEBUG
1710        dumpMap(toto);
1711#endif
1712      }
1713    }else{
1714      char tmp[1024];
1715      map * errormap;
1716      map *lenv;
1717      lenv=getMapFromMaps(m,"lenv","message");
1718      if(lenv!=NULL)
1719        sprintf(tmp,_("Unable to run the Service. The message returned back by the Service was the following : %s"),lenv->value);
1720      else
1721        sprintf(tmp,_("Unable to run the Service. No more information was returned back by the Service."));
1722      errormap = createMap("text",tmp);     
1723      addToMap(errormap,"code", "InternalError");
1724      printExceptionReportResponse(m,errormap);
1725      freeMap(&errormap);
1726      free(errormap);
1727    }
1728}
1729
1730char *base64(const unsigned char *input, int length)
1731{
1732  BIO *bmem, *b64;
1733  BUF_MEM *bptr;
1734
1735  b64 = BIO_new(BIO_f_base64());
1736  bmem = BIO_new(BIO_s_mem());
1737  b64 = BIO_push(b64, bmem);
1738  BIO_write(b64, input, length);
1739  BIO_flush(b64);
1740  BIO_get_mem_ptr(b64, &bptr);
1741
1742  char *buff = (char *)malloc(bptr->length);
1743  memcpy(buff, bptr->data, bptr->length-1);
1744  buff[bptr->length-1] = 0;
1745
1746  BIO_free_all(b64);
1747
1748  return buff;
1749}
1750
1751char* addDefaultValues(maps** out,elements* in,maps* m,int type){
1752  elements* tmpInputs=in;
1753  maps* out1=*out;
1754  while(tmpInputs!=NULL){
1755    maps *tmpMaps=getMaps(out1,tmpInputs->name);
1756    if(tmpMaps==NULL){
1757      maps* tmpMaps2=(maps*)malloc(MAPS_SIZE);
1758      tmpMaps2->name=strdup(tmpInputs->name);
1759      tmpMaps2->content=NULL;
1760      tmpMaps2->next=NULL;
1761     
1762      if(type==0){
1763        map* tmpMapMinO=getMap(tmpInputs->content,"minOccurs");
1764        if(tmpMapMinO!=NULL)
1765          if(atoi(tmpMapMinO->value)>=1){
1766            freeMaps(&tmpMaps2);
1767            free(tmpMaps2);
1768            return tmpInputs->name;
1769          }
1770          else{
1771            if(tmpMaps2->content==NULL)
1772              tmpMaps2->content=createMap("minOccurs",tmpMapMinO->value);
1773            else
1774              addToMap(tmpMaps2->content,"minOccurs",tmpMapMinO->value);
1775          }
1776        map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
1777        if(tmpMaxO!=NULL)
1778          if(tmpMaps2->content==NULL)
1779            tmpMaps2->content=createMap("maxOccurs",tmpMaxO->value);
1780          else
1781            addToMap(tmpMaps2->content,"maxOccurs",tmpMaxO->value);
1782      }
1783
1784      iotype* tmpIoType=tmpInputs->defaults;
1785      if(tmpIoType!=NULL){
1786        map* tmpm=tmpIoType->content;
1787        while(tmpm!=NULL){
1788          if(tmpMaps2->content==NULL)
1789            tmpMaps2->content=createMap(tmpm->name,tmpm->value);
1790          else
1791            addToMap(tmpMaps2->content,tmpm->name,tmpm->value);
1792          tmpm=tmpm->next;
1793        }
1794      }
1795      if(type==1){
1796        map *tmpMap=getMap(tmpMaps2->content,"value");
1797        if(tmpMap==NULL)
1798          addToMap(tmpMaps2->content,"value","NULL");
1799      }
1800      if(out1==NULL){
1801        *out=dupMaps(&tmpMaps2);
1802        out1=*out;
1803      }
1804      else
1805        addMapsToMaps(&out1,tmpMaps2);
1806      freeMap(&tmpMaps2->content);
1807      free(tmpMaps2->content);
1808      tmpMaps2->content=NULL;
1809      freeMaps(&tmpMaps2);
1810      free(tmpMaps2);
1811      tmpMaps2=NULL;
1812    }
1813    else{
1814      iotype* tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name,
1815                                             tmpMaps->content);
1816
1817      if(type==0) {
1818        /**
1819         * In case of an Input maps, then add the minOccurs and maxOccurs to the
1820         * content map.
1821         */
1822        map* tmpMap1=getMap(tmpInputs->content,"minOccurs");
1823        if(tmpMap1!=NULL){
1824          if(tmpMaps->content==NULL)
1825            tmpMaps->content=createMap("minOccurs",tmpMap1->value);
1826          else
1827            addToMap(tmpMaps->content,"minOccurs",tmpMap1->value);
1828        }
1829        map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
1830        if(tmpMaxO!=NULL){
1831          if(tmpMaps->content==NULL)
1832            tmpMaps->content=createMap("maxOccurs",tmpMap1->value);
1833          else
1834            addToMap(tmpMaps->content,"maxOccurs",tmpMap1->value);
1835        }
1836        /**
1837         * Parsing BoundingBoxData, fill the following map and then add it to
1838         * the content map of the Input maps:
1839         * lowerCorner, upperCorner, srs and dimensions
1840         * cf. parseBoundingBox
1841         */
1842        if(strcasecmp(tmpInputs->format,"BoundingBoxData")==0){
1843          maps* tmpI=getMaps(*out,tmpInputs->name);
1844          if(tmpI!=NULL){
1845            map* tmpV=getMap(tmpI->content,"value");
1846            if(tmpV!=NULL){
1847              char *tmpVS=strdup(tmpV->value);
1848              map* tmp=parseBoundingBox(tmpVS);
1849              free(tmpVS);
1850              map* tmpC=tmp;
1851              while(tmpC!=NULL){
1852                addToMap(tmpMaps->content,tmpC->name,tmpC->value);
1853                tmpC=tmpC->next;
1854              }
1855              freeMap(&tmp);
1856              free(tmp);
1857            }
1858          }
1859        }
1860      }
1861
1862      if(tmpIoType!=NULL){
1863        map* tmpContent=tmpIoType->content;
1864        map* cval=NULL;
1865
1866        while(tmpContent!=NULL){
1867          if((cval=getMap(tmpMaps->content,tmpContent->name))==NULL){
1868#ifdef DEBUG
1869            fprintf(stderr,"addDefaultValues %s => %s\n",tmpContent->name,tmpContent->value);
1870#endif
1871            if(tmpMaps->content==NULL)
1872              tmpMaps->content=createMap(tmpContent->name,tmpContent->value);
1873            else
1874              addToMap(tmpMaps->content,tmpContent->name,tmpContent->value);
1875          }
1876          tmpContent=tmpContent->next;
1877        }
1878      }
1879    }
1880    tmpInputs=tmpInputs->next;
1881  }
1882  return "";
1883}
1884
1885/**
1886 * parseBoundingBox : parse a BoundingBox string
1887 *
1888 * OGC 06-121r3 : 10.2 Bounding box
1889 *
1890 * value is provided as : lowerCorner,upperCorner,crs,dimension
1891 * exemple : 189000,834000,285000,962000,urn:ogc:def:crs:OGC:1.3:CRS84
1892 *
1893 * Need to create a map to store boundingbox informations :
1894 *  - lowerCorner : double,double (minimum within this bounding box)
1895 *  - upperCorner : double,double (maximum within this bounding box)
1896 *  - crs : URI (Reference to definition of the CRS)
1897 *  - dimensions : int
1898 *
1899 * Note : support only 2D bounding box.
1900 */
1901map* parseBoundingBox(char* value){
1902  map *res=NULL;
1903  if(value!=NULL){
1904    char *cv,*cvp;
1905    cv=strtok_r(value,",",&cvp);
1906    int cnt=0;
1907    int icnt=0;
1908    char *currentValue=NULL;
1909    while(cv){
1910      if(cnt<2)
1911        if(currentValue!=NULL){
1912          char *finalValue=(char*)malloc((strlen(currentValue)+strlen(cv)+1)*sizeof(char));
1913          sprintf(finalValue,"%s%s",currentValue,cv);
1914          switch(cnt){
1915          case 0:
1916            res=createMap("lowerCorner",finalValue);
1917            break;
1918          case 1:
1919            addToMap(res,"upperCorner",finalValue);
1920            icnt=-1;
1921            break;
1922          }
1923          cnt++;
1924          free(currentValue);
1925          currentValue=NULL;
1926          free(finalValue);
1927        }
1928        else{
1929          currentValue=(char*)malloc((strlen(cv)+2)*sizeof(char));
1930          sprintf(currentValue,"%s ",cv);
1931        }
1932      else
1933        if(cnt==2){
1934          addToMap(res,"crs",cv);
1935          cnt++;
1936        }
1937        else
1938          addToMap(res,"dimensions",cv);
1939      icnt++;
1940      cv=strtok_r(NULL,",",&cvp);
1941    }
1942  }
1943  return res;
1944}
1945
1946/**
1947 * printBoundingBox : fill a BoundingBox node (ows:BoundingBox or
1948 * wps:BoundingBoxData). Set crs and dimensions attributes, add
1949 * Lower/UpperCorner nodes to a pre-existing XML node.
1950 */
1951void printBoundingBox(xmlNsPtr ns_ows,xmlNodePtr n,map* boundingbox){
1952
1953  xmlNodePtr bb,lw,uc;
1954
1955  map* tmp=getMap(boundingbox,"value");
1956
1957  tmp=getMap(boundingbox,"lowerCorner");
1958  if(tmp!=NULL){
1959    lw=xmlNewNode(ns_ows,BAD_CAST "LowerCorner");
1960    xmlAddChild(lw,xmlNewText(BAD_CAST tmp->value));
1961  }
1962
1963  tmp=getMap(boundingbox,"upperCorner");
1964  if(tmp!=NULL){
1965    uc=xmlNewNode(ns_ows,BAD_CAST "UpperCorner");
1966    xmlAddChild(uc,xmlNewText(BAD_CAST tmp->value));
1967  }
1968
1969  tmp=getMap(boundingbox,"crs");
1970  if(tmp!=NULL)
1971    xmlNewProp(n,BAD_CAST "crs",BAD_CAST tmp->value);
1972
1973  tmp=getMap(boundingbox,"dimensions");
1974  if(tmp!=NULL)
1975    xmlNewProp(n,BAD_CAST "dimensions",BAD_CAST tmp->value);
1976
1977  xmlAddChild(n,lw);
1978  xmlAddChild(n,uc);
1979
1980}
1981
1982void printBoundingBoxDocument(maps* m,maps* boundingbox,FILE* file){
1983  if(file==NULL)
1984    rewind(stdout);
1985  xmlNodePtr n;
1986  xmlDocPtr doc;
1987  xmlNsPtr ns_ows,ns_xsi;
1988  xmlChar *xmlbuff;
1989  int buffersize;
1990  char *encoding=getEncoding(m);
1991  map *tmp;
1992  if(file==NULL){
1993    int pid=0;
1994    tmp=getMapFromMaps(m,"lenv","sid");
1995    if(tmp!=NULL)
1996      pid=atoi(tmp->value);
1997    if(pid==getpid()){
1998      printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1999    }
2000    fflush(stdout);
2001  }
2002
2003  doc = xmlNewDoc(BAD_CAST "1.0");
2004  int owsId=zooXmlAddNs(NULL,"http://www.opengis.net/ows/1.1","ows");
2005  ns_ows=usedNs[owsId];
2006  n = xmlNewNode(ns_ows, BAD_CAST "BoundingBox");
2007  xmlNewNs(n,BAD_CAST "http://www.opengis.net/ows/1.1","ows");
2008  int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi");
2009  ns_xsi=usedNs[xsiId];
2010  xmlNewNsProp(n,ns_xsi,BAD_CAST "schemaLocation",BAD_CAST "http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsCommon.xsd");
2011  map *tmp1=getMap(boundingbox->content,"value");
2012  tmp=parseBoundingBox(tmp1->value);
2013  printBoundingBox(ns_ows,n,tmp);
2014  xmlDocSetRootElement(doc, n);
2015
2016  xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
2017  if(file==NULL)
2018    printf((char *) xmlbuff);
2019  else{
2020    fprintf(file,"%s",xmlbuff);
2021  }
2022
2023  if(tmp!=NULL){
2024    freeMap(&tmp);
2025    free(tmp);
2026  }
2027  xmlFree(xmlbuff);
2028  xmlFreeDoc(doc);
2029  xmlCleanupParser();
2030  zooXmlCleanupNs();
2031 
2032}
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