source: tags/rel-1.2.0-rc2/zoo-kernel/service_internal.c @ 268

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

Merge branch-1.2 to current rev. 267. Tag release-1.2.0-rc2.

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

Search

Context Navigation

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