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