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