1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2010 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_internal.h" |
---|
26 | |
---|
27 | /* Converts a hex character to its integer value */ |
---|
28 | char from_hex(char ch) { |
---|
29 | return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
---|
30 | } |
---|
31 | |
---|
32 | /* Converts an integer value to its hex character*/ |
---|
33 | char to_hex(char code) { |
---|
34 | static char hex[] = "0123456789abcdef"; |
---|
35 | return hex[code & 15]; |
---|
36 | } |
---|
37 | |
---|
38 | void* unhandleStatus(maps *conf){ |
---|
39 | int shmid,i; |
---|
40 | key_t key; |
---|
41 | void *shm; |
---|
42 | struct shmid_ds shmids; |
---|
43 | char *s,*s1; |
---|
44 | map *tmpMap=getMapFromMaps(conf,"lenv","sid"); |
---|
45 | if(tmpMap!=NULL){ |
---|
46 | key=atoi(tmpMap->value); |
---|
47 | if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { |
---|
48 | #ifdef DEBUG |
---|
49 | fprintf(stderr,"shmget failed to update value\n"); |
---|
50 | #endif |
---|
51 | }else{ |
---|
52 | if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { |
---|
53 | #ifdef DEBUG |
---|
54 | fprintf(stderr,"shmat failed to update value\n"); |
---|
55 | #endif |
---|
56 | }else{ |
---|
57 | shmdt(shm); |
---|
58 | shmctl(shmid,IPC_RMID,&shmids); |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | #ifdef USE_JS |
---|
65 | |
---|
66 | JSBool |
---|
67 | JSUpdateStatus(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
---|
68 | { |
---|
69 | JS_MaybeGC(cx); |
---|
70 | char *sid; |
---|
71 | int istatus=0; |
---|
72 | char *status=NULL; |
---|
73 | maps *conf; |
---|
74 | int i=0; |
---|
75 | if(argc>2){ |
---|
76 | #ifdef JS_DEBUG |
---|
77 | fprintf(stderr,"Number of arguments used to call the function : %i",argc); |
---|
78 | #endif |
---|
79 | return JS_FALSE; |
---|
80 | } |
---|
81 | conf=mapsFromJSObject(cx,argv[0]); |
---|
82 | if(JS_ValueToInt32(cx,argv[1],&istatus)==JS_TRUE){ |
---|
83 | char tmpStatus[4]; |
---|
84 | sprintf(tmpStatus,"%i",istatus); |
---|
85 | tmpStatus[3]=0; |
---|
86 | status=strdup(tmpStatus); |
---|
87 | } |
---|
88 | if(getMapFromMaps(conf,"lenv","status")!=NULL){ |
---|
89 | if(status!=NULL) |
---|
90 | setMapInMaps(conf,"lenv","status",status); |
---|
91 | else |
---|
92 | setMapInMaps(conf,"lenv","status","15"); |
---|
93 | updateStatus(conf); |
---|
94 | } |
---|
95 | freeMaps(&conf); |
---|
96 | free(conf); |
---|
97 | JS_MaybeGC(cx); |
---|
98 | return JS_TRUE; |
---|
99 | } |
---|
100 | |
---|
101 | #endif |
---|
102 | |
---|
103 | void* updateStatus(maps *conf){ |
---|
104 | int shmid,i; |
---|
105 | key_t key; |
---|
106 | char *shm,*s,*s1; |
---|
107 | map *tmpMap=NULL; |
---|
108 | tmpMap=getMapFromMaps(conf,"lenv","sid"); |
---|
109 | if(tmpMap!=NULL){ |
---|
110 | key=atoi(tmpMap->value); |
---|
111 | if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { |
---|
112 | #ifdef DEBUG |
---|
113 | fprintf(stderr,"shmget failed to update value\n"); |
---|
114 | #endif |
---|
115 | }else{ |
---|
116 | if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) { |
---|
117 | #ifdef DEBUG |
---|
118 | fprintf(stderr,"shmat failed to update value\n"); |
---|
119 | #endif |
---|
120 | } |
---|
121 | else{ |
---|
122 | tmpMap=getMapFromMaps(conf,"lenv","status"); |
---|
123 | s1=shm; |
---|
124 | for(s=tmpMap->value;*s!=NULL;s++) |
---|
125 | *s1++=*s; |
---|
126 | shmdt((void *)shm); |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | char* getStatus(int pid){ |
---|
133 | int shmid,i; |
---|
134 | key_t key; |
---|
135 | void *shm; |
---|
136 | char *s; |
---|
137 | key=pid; |
---|
138 | if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { |
---|
139 | #ifdef DEBUG |
---|
140 | fprintf(stderr,"shmget failed in getStatus\n"); |
---|
141 | #endif |
---|
142 | }else{ |
---|
143 | if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { |
---|
144 | #ifdef DEBUG |
---|
145 | fprintf(stderr,"shmat failed in getStatus\n"); |
---|
146 | #endif |
---|
147 | }else{ |
---|
148 | return (char*)shm; |
---|
149 | } |
---|
150 | } |
---|
151 | return "-1"; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | /* Returns a url-encoded version of str */ |
---|
156 | /* IMPORTANT: be sure to free() the returned string after use */ |
---|
157 | char *url_encode(char *str) { |
---|
158 | char *pstr = str, *buf = (char*) malloc(strlen(str) * 3 + 1), *pbuf = buf; |
---|
159 | while (*pstr) { |
---|
160 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
---|
161 | *pbuf++ = *pstr; |
---|
162 | else if (*pstr == ' ') |
---|
163 | *pbuf++ = '+'; |
---|
164 | else |
---|
165 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
---|
166 | pstr++; |
---|
167 | } |
---|
168 | *pbuf = '\0'; |
---|
169 | return buf; |
---|
170 | } |
---|
171 | |
---|
172 | /* Returns a url-decoded version of str */ |
---|
173 | /* IMPORTANT: be sure to free() the returned string after use */ |
---|
174 | char *url_decode(char *str) { |
---|
175 | char *pstr = str, *buf = (char*) malloc(strlen(str) + 1), *pbuf = buf; |
---|
176 | while (*pstr) { |
---|
177 | if (*pstr == '%') { |
---|
178 | if (pstr[1] && pstr[2]) { |
---|
179 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
---|
180 | pstr += 2; |
---|
181 | } |
---|
182 | } else if (*pstr == '+') { |
---|
183 | *pbuf++ = ' '; |
---|
184 | } else { |
---|
185 | *pbuf++ = *pstr; |
---|
186 | } |
---|
187 | pstr++; |
---|
188 | } |
---|
189 | *pbuf = '\0'; |
---|
190 | return buf; |
---|
191 | } |
---|
192 | |
---|
193 | char *zCapitalize1(char *tmp){ |
---|
194 | char *res=strdup(tmp); |
---|
195 | if(res[0]>=97 && res[0]<=122) |
---|
196 | res[0]-=32; |
---|
197 | return res; |
---|
198 | } |
---|
199 | |
---|
200 | char *zCapitalize(char *tmp){ |
---|
201 | int i=0; |
---|
202 | char *res=strdup(tmp); |
---|
203 | for(i=0;i<strlen(res);i++) |
---|
204 | if(res[i]>=97 && res[i]<=122) |
---|
205 | res[i]-=32; |
---|
206 | return res; |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | int zooXmlSearchForNs(char* name){ |
---|
211 | int i; |
---|
212 | int res=-1; |
---|
213 | for(i=0;i<nbNs;i++) |
---|
214 | if(strncasecmp(name,nsName[i],strlen(nsName[i]))==0){ |
---|
215 | res=i; |
---|
216 | break; |
---|
217 | } |
---|
218 | return res; |
---|
219 | } |
---|
220 | |
---|
221 | int zooXmlAddNs(xmlNodePtr nr,char* url,char* name){ |
---|
222 | #ifdef DEBUG |
---|
223 | fprintf(stderr,"zooXmlAddNs %d \n",nbNs); |
---|
224 | #endif |
---|
225 | int currId=-1; |
---|
226 | if(nbNs==0){ |
---|
227 | nbNs++; |
---|
228 | currId=0; |
---|
229 | nsName[currId]=strdup(name); |
---|
230 | usedNs[currId]=xmlNewNs(nr,BAD_CAST url,BAD_CAST name); |
---|
231 | }else{ |
---|
232 | currId=zooXmlSearchForNs(name); |
---|
233 | if(currId<0){ |
---|
234 | nbNs++; |
---|
235 | currId=nbNs-1; |
---|
236 | nsName[currId]=strdup(name); |
---|
237 | usedNs[currId]=xmlNewNs(nr,BAD_CAST url,BAD_CAST name); |
---|
238 | } |
---|
239 | } |
---|
240 | return currId; |
---|
241 | } |
---|
242 | |
---|
243 | void zooXmlCleanupNs(){ |
---|
244 | int j; |
---|
245 | #ifdef DEBUG |
---|
246 | fprintf(stderr,"zooXmlCleanup %d\n",nbNs); |
---|
247 | #endif |
---|
248 | for(j=nbNs-1;j>=0;j--){ |
---|
249 | #ifdef DEBUG |
---|
250 | fprintf(stderr,"zooXmlCleanup %d\n",j); |
---|
251 | #endif |
---|
252 | if(j==0) |
---|
253 | xmlFreeNs(usedNs[j]); |
---|
254 | free(nsName[j]); |
---|
255 | nbNs--; |
---|
256 | } |
---|
257 | nbNs=0; |
---|
258 | } |
---|
259 | |
---|
260 | xmlNodePtr printGetCapabilitiesHeader(xmlDocPtr doc,char* service,maps* m){ |
---|
261 | |
---|
262 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
263 | xmlNodePtr n,nc,nc1,nc2,nc3,nc4,nc5,nc6,pseudor; |
---|
264 | xmlChar *xmlbuff; |
---|
265 | int buffersize; |
---|
266 | /** |
---|
267 | * Create the document and its temporary root. |
---|
268 | */ |
---|
269 | int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps"); |
---|
270 | ns=usedNs[wpsId]; |
---|
271 | maps* toto1=getMaps(m,"main"); |
---|
272 | |
---|
273 | n = xmlNewNode(ns, BAD_CAST "Capabilities"); |
---|
274 | int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows"); |
---|
275 | ns_ows=usedNs[owsId]; |
---|
276 | xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps"); |
---|
277 | int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi"); |
---|
278 | ns_xsi=usedNs[xsiId]; |
---|
279 | int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink"); |
---|
280 | ns_xlink=usedNs[xlinkId]; |
---|
281 | 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"); |
---|
282 | xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS"); |
---|
283 | |
---|
284 | if(toto1!=NULL){ |
---|
285 | map* tmp=getMap(toto1->content,"version"); |
---|
286 | if(tmp!=NULL){ |
---|
287 | xmlNewProp(n,BAD_CAST "version",BAD_CAST tmp->value); |
---|
288 | } |
---|
289 | else |
---|
290 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0"); |
---|
291 | } |
---|
292 | else |
---|
293 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0"); |
---|
294 | |
---|
295 | char tmp[256]; |
---|
296 | |
---|
297 | nc = xmlNewNode(ns_ows, BAD_CAST "ServiceIdentification"); |
---|
298 | maps* tmp4=getMaps(m,"identification"); |
---|
299 | if(tmp4!=NULL){ |
---|
300 | map* tmp2=tmp4->content; |
---|
301 | while(tmp2!=NULL){ |
---|
302 | if(strncasecmp(tmp2->name,"keywords",8)!=0 && |
---|
303 | strncasecmp(tmp2->name,"serverAddress",13)!=0 && |
---|
304 | strncasecmp(tmp2->name,"lang",4)!=0 && |
---|
305 | strncasecmp(tmp2->name,"encoding",8)!=0 && |
---|
306 | strncasecmp(tmp2->name,"version",7)!=0){ |
---|
307 | tmp2->name[0]=toupper(tmp2->name[0]); |
---|
308 | nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name); |
---|
309 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value)); |
---|
310 | xmlAddChild(nc,nc1); |
---|
311 | } |
---|
312 | else |
---|
313 | if(strcmp(tmp2->name,"keywords")==0){ |
---|
314 | nc1 = xmlNewNode(ns_ows, BAD_CAST "Keywords"); |
---|
315 | char *toto=tmp2->value; |
---|
316 | char buff[256]; |
---|
317 | int i=0; |
---|
318 | int j=0; |
---|
319 | while(toto[i]){ |
---|
320 | if(toto[i]!=',' && toto[i]!=0){ |
---|
321 | buff[j]=toto[i]; |
---|
322 | buff[j+1]=0; |
---|
323 | j++; |
---|
324 | } |
---|
325 | else{ |
---|
326 | nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword"); |
---|
327 | xmlAddChild(nc2,xmlNewText(BAD_CAST buff)); |
---|
328 | xmlAddChild(nc1,nc2); |
---|
329 | j=0; |
---|
330 | } |
---|
331 | i++; |
---|
332 | } |
---|
333 | if(strlen(buff)>0){ |
---|
334 | nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword"); |
---|
335 | xmlAddChild(nc2,xmlNewText(BAD_CAST buff)); |
---|
336 | xmlAddChild(nc1,nc2); |
---|
337 | } |
---|
338 | xmlAddChild(nc,nc1); |
---|
339 | nc2 = xmlNewNode(ns_ows, BAD_CAST "ServiceType"); |
---|
340 | xmlAddChild(nc2,xmlNewText(BAD_CAST "WPS")); |
---|
341 | xmlAddChild(nc,nc2); |
---|
342 | nc2 = xmlNewNode(ns_ows, BAD_CAST "ServiceTypeVersion"); |
---|
343 | xmlAddChild(nc2,xmlNewText(BAD_CAST "1.0.0")); |
---|
344 | xmlAddChild(nc,nc2); |
---|
345 | } |
---|
346 | tmp2=tmp2->next; |
---|
347 | } |
---|
348 | } |
---|
349 | else{ |
---|
350 | fprintf(stderr,"TMP4 NOT FOUND !!"); |
---|
351 | return NULL; |
---|
352 | } |
---|
353 | xmlAddChild(n,nc); |
---|
354 | |
---|
355 | nc = xmlNewNode(ns_ows, BAD_CAST "ServiceProvider"); |
---|
356 | nc3 = xmlNewNode(ns_ows, BAD_CAST "ServiceContact"); |
---|
357 | nc4 = xmlNewNode(ns_ows, BAD_CAST "ContactInfo"); |
---|
358 | nc5 = xmlNewNode(ns_ows, BAD_CAST "Phone"); |
---|
359 | nc6 = xmlNewNode(ns_ows, BAD_CAST "Address"); |
---|
360 | tmp4=getMaps(m,"provider"); |
---|
361 | if(tmp4!=NULL){ |
---|
362 | map* tmp2=tmp4->content; |
---|
363 | while(tmp2!=NULL){ |
---|
364 | if(strcmp(tmp2->name,"keywords")!=0 && |
---|
365 | strcmp(tmp2->name,"serverAddress")!=0 && |
---|
366 | strcmp(tmp2->name,"lang")!=0){ |
---|
367 | tmp2->name[0]=toupper(tmp2->name[0]); |
---|
368 | if(strcmp(tmp2->name,"ProviderName")==0){ |
---|
369 | nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name); |
---|
370 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value)); |
---|
371 | xmlAddChild(nc,nc1); |
---|
372 | } |
---|
373 | else{ |
---|
374 | if(strcmp(tmp2->name,"ProviderSite")==0){ |
---|
375 | nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name); |
---|
376 | xmlNewNsProp(nc1,ns_xlink,BAD_CAST "href",BAD_CAST tmp2->value); |
---|
377 | xmlAddChild(nc,nc1); |
---|
378 | } |
---|
379 | else |
---|
380 | if(strcmp(tmp2->name,"IndividualName")==0 || |
---|
381 | strcmp(tmp2->name,"PositionName")==0){ |
---|
382 | nc1 = xmlNewNode(ns_ows, BAD_CAST tmp2->name); |
---|
383 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value)); |
---|
384 | xmlAddChild(nc3,nc1); |
---|
385 | } |
---|
386 | else |
---|
387 | if(strncmp(tmp2->name,"Phone",5)==0){ |
---|
388 | char *toto=NULL; |
---|
389 | char *toto1=tmp2->name; |
---|
390 | toto=strstr(toto1,"Phone"); |
---|
391 | nc1 = xmlNewNode(ns_ows, BAD_CAST toto1+5); |
---|
392 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value)); |
---|
393 | xmlAddChild(nc5,nc1); |
---|
394 | } |
---|
395 | else |
---|
396 | if(strncmp(tmp2->name,"Address",7)==0){ |
---|
397 | char *toto=NULL; |
---|
398 | char *toto1=tmp2->name; |
---|
399 | toto=strstr(toto1,"Address"); |
---|
400 | nc1 = xmlNewNode(ns_ows, BAD_CAST toto1+7); |
---|
401 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp2->value)); |
---|
402 | xmlAddChild(nc6,nc1); |
---|
403 | } |
---|
404 | } |
---|
405 | } |
---|
406 | else |
---|
407 | if(strcmp(tmp2->name,"keywords")==0){ |
---|
408 | nc1 = xmlNewNode(ns_ows, BAD_CAST "Keywords"); |
---|
409 | char *toto=tmp2->value; |
---|
410 | char buff[256]; |
---|
411 | int i=0; |
---|
412 | int j=0; |
---|
413 | while(toto[i]){ |
---|
414 | if(toto[i]!=',' && toto[i]!=0){ |
---|
415 | buff[j]=toto[i]; |
---|
416 | buff[j+1]=0; |
---|
417 | j++; |
---|
418 | } |
---|
419 | else{ |
---|
420 | nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword"); |
---|
421 | xmlAddChild(nc2,xmlNewText(BAD_CAST buff)); |
---|
422 | xmlAddChild(nc1,nc2); |
---|
423 | j=0; |
---|
424 | } |
---|
425 | i++; |
---|
426 | } |
---|
427 | if(strlen(buff)>0){ |
---|
428 | nc2 = xmlNewNode(ns_ows, BAD_CAST "Keyword"); |
---|
429 | xmlAddChild(nc2,xmlNewText(BAD_CAST buff)); |
---|
430 | xmlAddChild(nc1,nc2); |
---|
431 | } |
---|
432 | xmlAddChild(nc,nc1); |
---|
433 | } |
---|
434 | tmp2=tmp2->next; |
---|
435 | } |
---|
436 | } |
---|
437 | else{ |
---|
438 | fprintf(stderr,"TMP4 NOT FOUND !!"); |
---|
439 | } |
---|
440 | xmlAddChild(nc4,nc5); |
---|
441 | xmlAddChild(nc4,nc6); |
---|
442 | xmlAddChild(nc3,nc4); |
---|
443 | xmlAddChild(nc,nc3); |
---|
444 | xmlAddChild(n,nc); |
---|
445 | |
---|
446 | |
---|
447 | nc = xmlNewNode(ns_ows, BAD_CAST "OperationsMetadata"); |
---|
448 | char *tmp2[3]; |
---|
449 | tmp2[0]=strdup("GetCapabilities"); |
---|
450 | tmp2[1]=strdup("DescribeProcess"); |
---|
451 | tmp2[2]=strdup("Execute"); |
---|
452 | int j=0; |
---|
453 | |
---|
454 | if(toto1!=NULL){ |
---|
455 | map* tmp=getMap(toto1->content,"serverAddress"); |
---|
456 | if(tmp!=NULL){ |
---|
457 | SERVICE_URL = strdup(tmp->value); |
---|
458 | } |
---|
459 | else |
---|
460 | SERVICE_URL = strdup("not_found"); |
---|
461 | } |
---|
462 | else |
---|
463 | SERVICE_URL = strdup("not_found"); |
---|
464 | |
---|
465 | for(j=0;j<3;j++){ |
---|
466 | nc1 = xmlNewNode(ns_ows, BAD_CAST "Operation"); |
---|
467 | xmlNewProp(nc1,BAD_CAST "name",BAD_CAST tmp2[j]); |
---|
468 | nc2 = xmlNewNode(ns_ows, BAD_CAST "DCP"); |
---|
469 | nc3 = xmlNewNode(ns_ows, BAD_CAST "HTTP"); |
---|
470 | nc4 = xmlNewNode(ns_ows, BAD_CAST "Get"); |
---|
471 | sprintf(tmp,"%s/%s",SERVICE_URL,service); |
---|
472 | xmlNewNsProp(nc4,ns_xlink,BAD_CAST "href",BAD_CAST tmp); |
---|
473 | xmlAddChild(nc3,nc4); |
---|
474 | if(j>0){ |
---|
475 | nc4 = xmlNewNode(ns_ows, BAD_CAST "Post"); |
---|
476 | xmlNewNsProp(nc4,ns_xlink,BAD_CAST "href",BAD_CAST tmp); |
---|
477 | xmlAddChild(nc3,nc4); |
---|
478 | } |
---|
479 | xmlAddChild(nc2,nc3); |
---|
480 | xmlAddChild(nc1,nc2); |
---|
481 | xmlAddChild(nc,nc1); |
---|
482 | } |
---|
483 | for(j=2;j>=0;j--) |
---|
484 | free(tmp2[j]); |
---|
485 | xmlAddChild(n,nc); |
---|
486 | |
---|
487 | nc = xmlNewNode(ns, BAD_CAST "ProcessOfferings"); |
---|
488 | xmlAddChild(n,nc); |
---|
489 | |
---|
490 | nc1 = xmlNewNode(ns, BAD_CAST "Languages"); |
---|
491 | nc2 = xmlNewNode(ns, BAD_CAST "Default"); |
---|
492 | nc3 = xmlNewNode(ns, BAD_CAST "Supported"); |
---|
493 | |
---|
494 | toto1=getMaps(m,"main"); |
---|
495 | if(toto1!=NULL){ |
---|
496 | map* tmp1=getMap(toto1->content,"lang"); |
---|
497 | char *toto=tmp1->value; |
---|
498 | char buff[256]; |
---|
499 | int i=0; |
---|
500 | int j=0; |
---|
501 | int dcount=0; |
---|
502 | while(toto[i]){ |
---|
503 | if(toto[i]!=',' && toto[i]!=0){ |
---|
504 | buff[j]=toto[i]; |
---|
505 | buff[j+1]=0; |
---|
506 | j++; |
---|
507 | } |
---|
508 | else{ |
---|
509 | nc4 = xmlNewNode(ns_ows, BAD_CAST "Language"); |
---|
510 | xmlAddChild(nc4,xmlNewText(BAD_CAST buff)); |
---|
511 | if(dcount==0){ |
---|
512 | xmlAddChild(nc2,nc4); |
---|
513 | xmlAddChild(nc1,nc2); |
---|
514 | xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST buff); |
---|
515 | dcount++; |
---|
516 | } |
---|
517 | nc4 = xmlNewNode(ns_ows, BAD_CAST "Language"); |
---|
518 | xmlAddChild(nc4,xmlNewText(BAD_CAST buff)); |
---|
519 | xmlAddChild(nc3,nc4); |
---|
520 | j=0; |
---|
521 | buff[j]=0; |
---|
522 | } |
---|
523 | i++; |
---|
524 | } |
---|
525 | if(strlen(buff)>0){ |
---|
526 | nc4 = xmlNewNode(ns_ows, BAD_CAST "Language"); |
---|
527 | xmlAddChild(nc4,xmlNewText(BAD_CAST buff)); |
---|
528 | xmlAddChild(nc3,nc4); |
---|
529 | } |
---|
530 | } |
---|
531 | xmlAddChild(nc1,nc3); |
---|
532 | xmlAddChild(n,nc1); |
---|
533 | |
---|
534 | xmlDocSetRootElement(doc, n); |
---|
535 | //xmlFreeNs(ns); |
---|
536 | free(SERVICE_URL); |
---|
537 | return nc; |
---|
538 | } |
---|
539 | |
---|
540 | void printGetCapabilitiesForProcess(maps* m,xmlNodePtr nc,service* serv){ |
---|
541 | xmlNsPtr ns,ns_ows,ns_xlink; |
---|
542 | xmlNodePtr nr,n,nc1,nc2,nc3,nc4,nc5,nc6,pseudor; |
---|
543 | /** |
---|
544 | * Initialize or get existing namspaces |
---|
545 | */ |
---|
546 | int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps"); |
---|
547 | ns=usedNs[wpsId]; |
---|
548 | int owsId=zooXmlAddNs(NULL,"http://www.opengis.net/ows/1.1","ows"); |
---|
549 | ns_ows=usedNs[owsId]; |
---|
550 | int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink"); |
---|
551 | ns_xlink=usedNs[xlinkId]; |
---|
552 | |
---|
553 | int cursor=0; |
---|
554 | map* tmp1; |
---|
555 | if(serv->content!=NULL){ |
---|
556 | nc1 = xmlNewNode(ns, BAD_CAST "Process"); |
---|
557 | tmp1=getMap(serv->content,"processVersion"); |
---|
558 | if(tmp1!=NULL) |
---|
559 | xmlNewNsProp(nc1,ns,BAD_CAST "processVersion",BAD_CAST tmp1->value); |
---|
560 | printDescription(nc1,ns_ows,serv->name,serv->content); |
---|
561 | tmp1=serv->metadata; |
---|
562 | while(tmp1!=NULL){ |
---|
563 | nc2 = xmlNewNode(ns_ows, BAD_CAST "Metadata"); |
---|
564 | xmlNewNsProp(nc2,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
565 | xmlAddChild(nc1,nc2); |
---|
566 | tmp1=tmp1->next; |
---|
567 | } |
---|
568 | xmlAddChild(nc,nc1); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | xmlNodePtr printDescribeProcessHeader(xmlDocPtr doc,char* service,maps* m){ |
---|
573 | |
---|
574 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
575 | xmlNodePtr n,nr; |
---|
576 | xmlChar *xmlbuff; |
---|
577 | int buffersize; |
---|
578 | |
---|
579 | int wpsId=zooXmlAddNs(NULL,"http://schemas.opengis.net/wps/1.0.0","wps"); |
---|
580 | ns=usedNs[wpsId]; |
---|
581 | n = xmlNewNode(ns, BAD_CAST "ProcessDescriptions"); |
---|
582 | int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows"); |
---|
583 | ns_ows=usedNs[owsId]; |
---|
584 | xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps"); |
---|
585 | zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink"); |
---|
586 | int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi"); |
---|
587 | ns_xsi=usedNs[xsiId]; |
---|
588 | |
---|
589 | 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"); |
---|
590 | xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS"); |
---|
591 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0"); |
---|
592 | xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST "en"); |
---|
593 | |
---|
594 | xmlDocSetRootElement(doc, n); |
---|
595 | |
---|
596 | return n; |
---|
597 | } |
---|
598 | |
---|
599 | void printDescribeProcessForProcess(maps* m,xmlNodePtr nc,service* serv,int sc){ |
---|
600 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
601 | xmlNodePtr nr,n,nc1,nc2,nc3,nc4,nc5,nc6,pseudor; |
---|
602 | |
---|
603 | char tmp[256]; |
---|
604 | n=nc; |
---|
605 | |
---|
606 | int wpsId=zooXmlAddNs(NULL,"http://schemas.opengis.net/wps/1.0.0","wps"); |
---|
607 | ns=usedNs[wpsId]; |
---|
608 | int owsId=zooXmlAddNs(NULL,"http://www.opengis.net/ows/1.1","ows"); |
---|
609 | ns_ows=usedNs[owsId]; |
---|
610 | int xlinkId=zooXmlAddNs(NULL,"http://www.w3.org/1999/xlink","xlink"); |
---|
611 | ns_xlink=usedNs[xlinkId]; |
---|
612 | |
---|
613 | nc = xmlNewNode(NULL, BAD_CAST "ProcessDescription"); |
---|
614 | char *tmp4[3]; |
---|
615 | tmp4[0]="processVersion"; |
---|
616 | tmp4[1]="storeSupported"; |
---|
617 | tmp4[2]="statusSupported"; |
---|
618 | int j=0; |
---|
619 | map* tmp1=NULL; |
---|
620 | for(j=0;j<3;j++){ |
---|
621 | tmp1=getMap(serv->content,tmp4[j]); |
---|
622 | if(tmp1!=NULL){ |
---|
623 | if(j==0) |
---|
624 | xmlNewNsProp(nc,ns,BAD_CAST "processVersion",BAD_CAST tmp1->value); |
---|
625 | else |
---|
626 | xmlNewProp(nc,BAD_CAST tmp4[j],BAD_CAST tmp1->value); |
---|
627 | } |
---|
628 | else{ |
---|
629 | if(j>0) |
---|
630 | xmlNewProp(nc,BAD_CAST tmp4[j],BAD_CAST "false"); |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | printDescription(nc,ns_ows,serv->name,serv->content); |
---|
635 | |
---|
636 | tmp1=serv->metadata; |
---|
637 | while(tmp1!=NULL){ |
---|
638 | nc1 = xmlNewNode(ns_ows, BAD_CAST "Metadata"); |
---|
639 | xmlNewNsProp(nc1,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
640 | xmlAddChild(nc,nc1); |
---|
641 | tmp1=tmp1->next; |
---|
642 | } |
---|
643 | |
---|
644 | tmp1=getMap(serv->content,"Profile"); |
---|
645 | if(tmp1!=NULL){ |
---|
646 | nc1 = xmlNewNode(ns, BAD_CAST "Profile"); |
---|
647 | xmlAddChild(nc1,xmlNewText(BAD_CAST tmp1->value)); |
---|
648 | xmlAddChild(nc,nc1); |
---|
649 | } |
---|
650 | |
---|
651 | nc1 = xmlNewNode(NULL, BAD_CAST "DataInputs"); |
---|
652 | |
---|
653 | elements* e=serv->inputs; |
---|
654 | while(e!=NULL){ |
---|
655 | nc2 = xmlNewNode(NULL, BAD_CAST "Input"); |
---|
656 | tmp1=getMap(e->content,"minOccurs"); |
---|
657 | if(tmp1){ |
---|
658 | xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
659 | } |
---|
660 | tmp1=getMap(e->content,"maxOccurs"); |
---|
661 | if(tmp1){ |
---|
662 | xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
663 | } |
---|
664 | |
---|
665 | printDescription(nc2,ns_ows,e->name,e->content); |
---|
666 | |
---|
667 | |
---|
668 | nc3 = xmlNewNode(NULL, BAD_CAST e->format); |
---|
669 | iotype* _tmp=e->defaults; |
---|
670 | int datatype=0; |
---|
671 | if(_tmp!=NULL){ |
---|
672 | int isAnyValue=1; |
---|
673 | if(strcmp(e->format,"LiteralData")!=0){ |
---|
674 | nc4 = xmlNewNode(NULL, BAD_CAST "Default"); |
---|
675 | nc5 = xmlNewNode(NULL, BAD_CAST "Format"); |
---|
676 | } |
---|
677 | else{ |
---|
678 | nc4 = xmlNewNode(NULL, BAD_CAST "UOMs"); |
---|
679 | nc5 = xmlNewNode(NULL, BAD_CAST "Default"); |
---|
680 | datatype=1; |
---|
681 | } |
---|
682 | tmp1=_tmp->content; |
---|
683 | int default1=0; |
---|
684 | xmlNodePtr nc7; |
---|
685 | while(tmp1!=NULL){ |
---|
686 | #ifdef DEBUG |
---|
687 | printf("DATATYPE DEFAULT ? %s\n",tmp1->name); |
---|
688 | #endif |
---|
689 | if(strncasecmp(tmp1->name,"DataType",8)==0){ |
---|
690 | nc6 = xmlNewNode(ns_ows, BAD_CAST "DataType"); |
---|
691 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
692 | char tmp[1024]; |
---|
693 | sprintf(tmp,"http://www.w3.org/TR/xmlschema-2/#%s",tmp1->value); |
---|
694 | xmlNewNsProp(nc6,ns_ows,BAD_CAST "reference",BAD_CAST tmp); |
---|
695 | xmlAddChild(nc3,nc6); |
---|
696 | tmp1=tmp1->next; |
---|
697 | continue; |
---|
698 | } |
---|
699 | if(strcasecmp(tmp1->name,"asReference")!=0 && |
---|
700 | strcasecmp(tmp1->name,"DataType")!=0 && |
---|
701 | strncasecmp(tmp1->name,"AllowedValues",13)!=0 && |
---|
702 | strcasecmp(tmp1->name,"value")!=0 && |
---|
703 | strcasecmp(tmp1->name,"extension")!=0){ |
---|
704 | if(datatype==0){ |
---|
705 | char *tmp2=zCapitalize1(tmp1->name); |
---|
706 | nc6 = xmlNewNode(NULL, BAD_CAST tmp2); |
---|
707 | free(tmp2); |
---|
708 | } |
---|
709 | else{ |
---|
710 | char *tmp2=zCapitalize(tmp1->name); |
---|
711 | nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2); |
---|
712 | free(tmp2); |
---|
713 | } |
---|
714 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
715 | xmlAddChild(nc5,nc6); |
---|
716 | } |
---|
717 | else{ |
---|
718 | if(strcmp(tmp1->name,"value")==0){ |
---|
719 | nc7 = xmlNewNode(NULL, BAD_CAST "DefaultValue"); |
---|
720 | xmlAddChild(nc7,xmlNewText(BAD_CAST tmp1->value)); |
---|
721 | default1=1; |
---|
722 | } |
---|
723 | if(strncasecmp(tmp1->name,"AllowedValues",13)==0){ |
---|
724 | nc6 = xmlNewNode(ns_ows, BAD_CAST "AllowedValues"); |
---|
725 | char *token,*saveptr1; |
---|
726 | token=strtok_r(tmp1->value,",",&saveptr1); |
---|
727 | while(token!=NULL){ |
---|
728 | nc7 = xmlNewNode(ns_ows, BAD_CAST "Value"); |
---|
729 | char *tmps=strdup(token); |
---|
730 | tmps[strlen(tmps)]=0; |
---|
731 | xmlAddChild(nc7,xmlNewText(BAD_CAST tmps)); |
---|
732 | fprintf(stderr,"strgin : %s\n",tmps); |
---|
733 | xmlAddChild(nc6,nc7); |
---|
734 | token=strtok_r(NULL,",",&saveptr1); |
---|
735 | } |
---|
736 | xmlAddChild(nc3,nc6); |
---|
737 | isAnyValue=-1; |
---|
738 | } |
---|
739 | } |
---|
740 | tmp1=tmp1->next; |
---|
741 | } |
---|
742 | xmlAddChild(nc4,nc5); |
---|
743 | xmlAddChild(nc3,nc4); |
---|
744 | if(datatype==1 && isAnyValue==1){ |
---|
745 | xmlAddChild(nc3,xmlNewNode(ns_ows, BAD_CAST "AnyValue")); |
---|
746 | if(default1>0) |
---|
747 | xmlAddChild(nc3,nc7); |
---|
748 | } |
---|
749 | if(datatype==1 && default1>0) |
---|
750 | xmlAddChild(nc3,nc7); |
---|
751 | } |
---|
752 | xmlAddChild(nc2,nc3); |
---|
753 | |
---|
754 | _tmp=e->supported; |
---|
755 | while(_tmp!=NULL){ |
---|
756 | if(datatype==0){ |
---|
757 | nc4 = xmlNewNode(NULL, BAD_CAST "Supported"); |
---|
758 | nc5 = xmlNewNode(NULL, BAD_CAST "Format"); |
---|
759 | } |
---|
760 | else{ |
---|
761 | nc5 = xmlNewNode(NULL, BAD_CAST "Supported"); |
---|
762 | } |
---|
763 | tmp1=_tmp->content; |
---|
764 | while(tmp1!=NULL){ |
---|
765 | /*if(strcmp(e->format,"LiteralData")==0) |
---|
766 | xmlAddChild(nc5,nc6);*/ |
---|
767 | if(datatype==0){ |
---|
768 | char *tmp2=zCapitalize1(tmp1->name); |
---|
769 | nc6 = xmlNewNode(NULL, BAD_CAST tmp2); |
---|
770 | free(tmp2); |
---|
771 | } |
---|
772 | else{ |
---|
773 | char *tmp2=zCapitalize(tmp1->name); |
---|
774 | nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2); |
---|
775 | free(tmp2); |
---|
776 | } |
---|
777 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
778 | xmlAddChild(nc5,nc6); |
---|
779 | tmp1=tmp1->next; |
---|
780 | } |
---|
781 | if(datatype==0){ |
---|
782 | xmlAddChild(nc4,nc5); |
---|
783 | xmlAddChild(nc3,nc4); |
---|
784 | }else{ |
---|
785 | xmlAddChild(nc4,nc5); |
---|
786 | //xmlAddChild(nc3,xmlNewNode(ns_ows, BAD_CAST "AnyValue")); |
---|
787 | } |
---|
788 | _tmp=_tmp->next; |
---|
789 | if(strcmp(e->format,"LiteralData")!=0){ |
---|
790 | xmlAddChild(nc2,nc3); |
---|
791 | } |
---|
792 | xmlAddChild(nc1,nc2); |
---|
793 | } |
---|
794 | |
---|
795 | |
---|
796 | e=e->next; |
---|
797 | } |
---|
798 | xmlAddChild(nc,nc1); |
---|
799 | |
---|
800 | nc1 = xmlNewNode(NULL, BAD_CAST "ProcessOutputs"); |
---|
801 | |
---|
802 | e=serv->outputs; |
---|
803 | while(e!=NULL){ |
---|
804 | nc2 = xmlNewNode(NULL, BAD_CAST "Output"); |
---|
805 | tmp1=getMap(e->content,"minOccurs"); |
---|
806 | if(tmp1){ |
---|
807 | xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
808 | } |
---|
809 | tmp1=getMap(e->content,"maxOccurs"); |
---|
810 | if(tmp1){ |
---|
811 | xmlNewProp(nc2,BAD_CAST tmp1->name,BAD_CAST tmp1->value); |
---|
812 | } |
---|
813 | |
---|
814 | printDescription(nc2,ns_ows,e->name,e->content); |
---|
815 | |
---|
816 | if(strncasecmp(e->format,"LITERALDATA",strlen(e->format))==0) |
---|
817 | nc3 = xmlNewNode(NULL, BAD_CAST "LiteralOutput"); |
---|
818 | else |
---|
819 | if(strncasecmp(e->format,"COMPLEXDATA",strlen(e->format))==0) |
---|
820 | nc3 = xmlNewNode(NULL, BAD_CAST "ComplexOutput"); |
---|
821 | else |
---|
822 | nc3 = xmlNewNode(NULL, BAD_CAST e->format); |
---|
823 | iotype* _tmp=e->defaults; |
---|
824 | int datatype=0; |
---|
825 | if(_tmp!=NULL){ |
---|
826 | if(strcmp(e->format,"LiteralOutput")==0 || |
---|
827 | strcmp(e->format,"LiteralData")==0){ |
---|
828 | datatype=1; |
---|
829 | nc4 = xmlNewNode(NULL, BAD_CAST "UOMs"); |
---|
830 | nc5 = xmlNewNode(NULL, BAD_CAST "Default"); |
---|
831 | } |
---|
832 | else{ |
---|
833 | nc4 = xmlNewNode(NULL, BAD_CAST "Default"); |
---|
834 | nc5 = xmlNewNode(NULL, BAD_CAST "Format"); |
---|
835 | } |
---|
836 | tmp1=_tmp->content; |
---|
837 | while(tmp1!=NULL){ |
---|
838 | #ifdef DEBUG |
---|
839 | printf("DATATYPE DEFAULT ? %s\n",tmp1->name); |
---|
840 | #endif |
---|
841 | if(strncasecmp(tmp1->name,"DataType",8)==0){ |
---|
842 | nc6 = xmlNewNode(ns_ows, BAD_CAST "DataType"); |
---|
843 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
844 | char tmp[1024]; |
---|
845 | sprintf(tmp,"http://www.w3.org/TR/xmlschema-2/#%s",tmp1->value); |
---|
846 | xmlNewNsProp(nc6,ns_ows,BAD_CAST "reference",BAD_CAST tmp); |
---|
847 | xmlAddChild(nc3,nc6); |
---|
848 | tmp1=tmp1->next; |
---|
849 | datatype=1; |
---|
850 | continue; |
---|
851 | } |
---|
852 | if(strcmp(tmp1->name,"asReference")!=0 && |
---|
853 | strncasecmp(tmp1->name,"DataType",8)!=0 && |
---|
854 | strcasecmp(tmp1->name,"extension")!=0){ |
---|
855 | if(datatype==0){ |
---|
856 | char *tmp2=zCapitalize1(tmp1->name); |
---|
857 | nc6 = xmlNewNode(NULL, BAD_CAST tmp2); |
---|
858 | free(tmp2); |
---|
859 | } |
---|
860 | else{ |
---|
861 | char *tmp2=zCapitalize(tmp1->name); |
---|
862 | nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2); |
---|
863 | free(tmp2); |
---|
864 | } |
---|
865 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
866 | xmlAddChild(nc5,nc6); |
---|
867 | } |
---|
868 | tmp1=tmp1->next; |
---|
869 | } |
---|
870 | xmlAddChild(nc4,nc5); |
---|
871 | xmlAddChild(nc3,nc4); |
---|
872 | } |
---|
873 | _tmp=e->supported; |
---|
874 | while(_tmp!=NULL){ |
---|
875 | if(datatype==0){ |
---|
876 | nc4 = xmlNewNode(NULL, BAD_CAST "Supported"); |
---|
877 | nc5 = xmlNewNode(NULL, BAD_CAST "Format"); |
---|
878 | } |
---|
879 | else |
---|
880 | nc5 = xmlNewNode(NULL, BAD_CAST "Supported"); |
---|
881 | tmp1=_tmp->content; |
---|
882 | while(tmp1!=NULL){ |
---|
883 | #ifdef DEBUG |
---|
884 | printf("DATATYPE SUPPORTED ? %s\n",tmp1->name); |
---|
885 | #endif |
---|
886 | if(strcmp(tmp1->name,"asReference")!=0 && |
---|
887 | strcmp(tmp1->name,"DataType")!=0 && |
---|
888 | strcasecmp(tmp1->name,"extension")!=0){ |
---|
889 | if(datatype==0){ |
---|
890 | char *tmp2=zCapitalize1(tmp1->name); |
---|
891 | nc6 = xmlNewNode(NULL, BAD_CAST tmp2); |
---|
892 | free(tmp2); |
---|
893 | } |
---|
894 | else{ |
---|
895 | char *tmp2=zCapitalize(tmp1->name); |
---|
896 | nc6 = xmlNewNode(ns_ows, BAD_CAST tmp2); |
---|
897 | free(tmp2); |
---|
898 | } |
---|
899 | xmlAddChild(nc6,xmlNewText(BAD_CAST tmp1->value)); |
---|
900 | xmlAddChild(nc5,nc6); |
---|
901 | } |
---|
902 | tmp1=tmp1->next; |
---|
903 | } |
---|
904 | _tmp=_tmp->next; |
---|
905 | if(datatype==0){ |
---|
906 | xmlAddChild(nc4,nc5); |
---|
907 | xmlAddChild(nc3,nc4); |
---|
908 | }else |
---|
909 | xmlAddChild(nc4,nc5); |
---|
910 | } |
---|
911 | xmlAddChild(nc2,nc3); |
---|
912 | |
---|
913 | xmlAddChild(nc3,nc4); |
---|
914 | |
---|
915 | |
---|
916 | xmlAddChild(nc2,nc3); |
---|
917 | |
---|
918 | xmlAddChild(nc1,nc2); |
---|
919 | |
---|
920 | e=e->next; |
---|
921 | } |
---|
922 | xmlAddChild(nc,nc1); |
---|
923 | |
---|
924 | xmlAddChild(n,nc); |
---|
925 | |
---|
926 | } |
---|
927 | |
---|
928 | void printProcessResponse(maps* m,map* request, int pid,service* serv,char* service,int status,maps* inputs,maps* outputs){ |
---|
929 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
930 | xmlNodePtr nr,n,nc,nc1,nc2,nc3,pseudor; |
---|
931 | xmlDocPtr doc; |
---|
932 | xmlChar *xmlbuff; |
---|
933 | int buffersize; |
---|
934 | time_t time1; |
---|
935 | time(&time1); |
---|
936 | /** |
---|
937 | * Create the document and its temporary root. |
---|
938 | */ |
---|
939 | doc = xmlNewDoc(BAD_CAST "1.0"); |
---|
940 | int wpsId=zooXmlAddNs(NULL,"http://www.opengis.net/wps/1.0.0","wps"); |
---|
941 | ns=usedNs[wpsId]; |
---|
942 | |
---|
943 | n = xmlNewNode(ns, BAD_CAST "ExecuteResponse"); |
---|
944 | int owsId=zooXmlAddNs(n,"http://www.opengis.net/ows/1.1","ows"); |
---|
945 | ns_ows=usedNs[owsId]; |
---|
946 | int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink"); |
---|
947 | ns_xlink=usedNs[xlinkId]; |
---|
948 | int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi"); |
---|
949 | ns_xsi=usedNs[xsiId]; |
---|
950 | xmlNewNs(n,BAD_CAST "http://www.opengis.net/wps/1.0.0",BAD_CAST "wps"); |
---|
951 | |
---|
952 | 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"); |
---|
953 | |
---|
954 | xmlNewProp(n,BAD_CAST "service",BAD_CAST "WPS"); |
---|
955 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.0.0"); |
---|
956 | xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST "en"); |
---|
957 | char tmp[256]; |
---|
958 | char url[1024]; |
---|
959 | memset(tmp,0,256); |
---|
960 | memset(url,0,256); |
---|
961 | maps* tmp_maps=getMaps(m,"main"); |
---|
962 | if(tmp_maps!=NULL){ |
---|
963 | map* tmpm1=getMap(tmp_maps->content,"serverAddress"); |
---|
964 | /** |
---|
965 | * Check if the ZOO Service GetStatus is available in the local directory. |
---|
966 | * If yes, then it uses a reference to an URL which the client can access |
---|
967 | * to get information on the status of a running Service (using the |
---|
968 | * percentCompleted attribute). |
---|
969 | * Else fallback to the initial method using the xml file to write in ... |
---|
970 | */ |
---|
971 | char ntmp[1024]; |
---|
972 | #ifndef WIN32 |
---|
973 | getcwd(ntmp,1024); |
---|
974 | #else |
---|
975 | _getcwd(ntmp,1024); |
---|
976 | #endif |
---|
977 | struct stat myFileInfo; |
---|
978 | int statRes; |
---|
979 | char file_path[1024]; |
---|
980 | sprintf(file_path,"%s/GetStatus.zcfg",ntmp); |
---|
981 | statRes=stat(file_path,&myFileInfo); |
---|
982 | if(statRes==0){ |
---|
983 | char currentSid[128]; |
---|
984 | map* tmpm=getMap(tmp_maps->content,"rewriteUrl"); |
---|
985 | map *tmp_lenv=NULL; |
---|
986 | tmp_lenv=getMapFromMaps(m,"lenv","sid"); |
---|
987 | if(tmp_lenv==NULL) |
---|
988 | sprintf(currentSid,"%i",pid); |
---|
989 | else |
---|
990 | sprintf(currentSid,"%s",tmp_lenv->value); |
---|
991 | if(tmpm==NULL || strcasecmp(tmpm->value,"false")==0){ |
---|
992 | sprintf(url,"%s/?request=Execute&service=WPS&version=1.0.0&Identifier=GetStatus&DataInputs=sid=%s&RawDataOutput=Result",tmpm1->value,currentSid); |
---|
993 | }else{ |
---|
994 | if(strlen(tmpm->value)>0) |
---|
995 | if(strcasecmp(tmpm->value,"true")!=0) |
---|
996 | sprintf(url,"%s/%s/GetStatus/%s",tmpm1->value,tmpm->value,currentSid); |
---|
997 | else |
---|
998 | sprintf(url,"%s/GetStatus/%s",tmpm1->value,currentSid); |
---|
999 | else |
---|
1000 | sprintf(url,"%s/?request=Execute&service=WPS&version=1.0.0&Identifier=GetStatus&DataInputs=sid=%s&RawDataOutput=Result",tmpm1->value,currentSid); |
---|
1001 | } |
---|
1002 | }else{ |
---|
1003 | map* tmpm2=getMap(tmp_maps->content,"tmpUrl"); |
---|
1004 | if(tmpm1!=NULL && tmpm2!=NULL){ |
---|
1005 | sprintf(url,"%s/%s/%s_%i.xml",tmpm1->value,tmpm2->value,service,pid); |
---|
1006 | } |
---|
1007 | } |
---|
1008 | if(tmpm1!=NULL) |
---|
1009 | sprintf(tmp,"%s/",tmpm1->value); |
---|
1010 | } |
---|
1011 | |
---|
1012 | xmlNewProp(n,BAD_CAST "serviceInstance",BAD_CAST tmp); |
---|
1013 | if(status!=SERVICE_SUCCEEDED && status!=SERVICE_FAILED){ |
---|
1014 | xmlNewProp(n,BAD_CAST "statusLocation",BAD_CAST url); |
---|
1015 | } |
---|
1016 | |
---|
1017 | nc = xmlNewNode(ns, BAD_CAST "Process"); |
---|
1018 | map* tmp2=getMap(serv->content,"processVersion"); |
---|
1019 | |
---|
1020 | if(tmp2!=NULL) |
---|
1021 | xmlNewNsProp(nc,ns,BAD_CAST "processVersion",BAD_CAST tmp2->value); |
---|
1022 | |
---|
1023 | printDescription(nc,ns_ows,serv->name,serv->content); |
---|
1024 | fflush(stderr); |
---|
1025 | |
---|
1026 | xmlAddChild(n,nc); |
---|
1027 | |
---|
1028 | nc = xmlNewNode(ns, BAD_CAST "Status"); |
---|
1029 | const struct tm *tm; |
---|
1030 | size_t len; |
---|
1031 | time_t now; |
---|
1032 | char *tmp1; |
---|
1033 | map *tmpStatus; |
---|
1034 | |
---|
1035 | now = time ( NULL ); |
---|
1036 | tm = localtime ( &now ); |
---|
1037 | |
---|
1038 | tmp1 = (char*)malloc((TIME_SIZE+1)*sizeof(char)); |
---|
1039 | |
---|
1040 | len = strftime ( tmp1, TIME_SIZE, "%Y-%m-%dT%I:%M:%SZ", tm ); |
---|
1041 | |
---|
1042 | xmlNewProp(nc,BAD_CAST "creationTime",BAD_CAST tmp1); |
---|
1043 | |
---|
1044 | char sMsg[2048]; |
---|
1045 | switch(status){ |
---|
1046 | case SERVICE_SUCCEEDED: |
---|
1047 | nc1 = xmlNewNode(ns, BAD_CAST "ProcessSucceeded"); |
---|
1048 | sprintf(sMsg,"Service \"%s\" run successfully.",serv->name); |
---|
1049 | nc3=xmlNewText(BAD_CAST sMsg); |
---|
1050 | xmlAddChild(nc1,nc3); |
---|
1051 | break; |
---|
1052 | case SERVICE_STARTED: |
---|
1053 | nc1 = xmlNewNode(ns, BAD_CAST "ProcessStarted"); |
---|
1054 | tmpStatus=getMapFromMaps(m,"lenv","status"); |
---|
1055 | xmlNewProp(nc1,BAD_CAST "percentCompleted",BAD_CAST tmpStatus->value); |
---|
1056 | sprintf(sMsg,"ZOO Service \"%s\" is currently running. Please, reload this document to get the up-to-date status of the Service.",serv->name); |
---|
1057 | nc3=xmlNewText(BAD_CAST sMsg); |
---|
1058 | xmlAddChild(nc1,nc3); |
---|
1059 | break; |
---|
1060 | case SERVICE_ACCEPTED: |
---|
1061 | nc1 = xmlNewNode(ns, BAD_CAST "ProcessAccepted"); |
---|
1062 | 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); |
---|
1063 | nc3=xmlNewText(BAD_CAST sMsg); |
---|
1064 | xmlAddChild(nc1,nc3); |
---|
1065 | break; |
---|
1066 | case SERVICE_FAILED: |
---|
1067 | nc1 = xmlNewNode(ns, BAD_CAST "ProcessFailed"); |
---|
1068 | map *errorMap; |
---|
1069 | map *te; |
---|
1070 | te=getMapFromMaps(m,"lenv","code"); |
---|
1071 | if(te!=NULL) |
---|
1072 | errorMap=createMap("code",te->value); |
---|
1073 | else |
---|
1074 | errorMap=createMap("code","NoApplicableCode"); |
---|
1075 | te=getMapFromMaps(m,"lenv","message"); |
---|
1076 | if(te!=NULL) |
---|
1077 | addToMap(errorMap,"text",te->value); |
---|
1078 | else |
---|
1079 | addToMap(errorMap,"text","No more information available"); |
---|
1080 | nc3=createExceptionReportNode(m,errorMap,0); |
---|
1081 | xmlAddChild(nc1,nc3); |
---|
1082 | break; |
---|
1083 | default : |
---|
1084 | printf("error code not know : %i\n",status); |
---|
1085 | //exit(1); |
---|
1086 | break; |
---|
1087 | } |
---|
1088 | xmlAddChild(nc,nc1); |
---|
1089 | xmlAddChild(n,nc); |
---|
1090 | free(tmp1); |
---|
1091 | |
---|
1092 | #ifdef DEBUG |
---|
1093 | fprintf(stderr,"printProcessResponse 1 161\n"); |
---|
1094 | #endif |
---|
1095 | |
---|
1096 | map* lineage=getMap(request,"lineage"); |
---|
1097 | if(lineage!=NULL){ |
---|
1098 | nc = xmlNewNode(ns, BAD_CAST "DataInputs"); |
---|
1099 | int i; |
---|
1100 | maps* mcursor=inputs; |
---|
1101 | elements* scursor=serv->inputs; |
---|
1102 | while(mcursor!=NULL /*&& scursor!=NULL*/){ |
---|
1103 | printIOType(doc,nc,ns,ns_ows,scursor,mcursor,"Input"); |
---|
1104 | mcursor=mcursor->next; |
---|
1105 | //scursor=scursor->next; |
---|
1106 | } |
---|
1107 | xmlAddChild(n,nc); |
---|
1108 | |
---|
1109 | #ifdef DEBUG |
---|
1110 | fprintf(stderr,"printProcessResponse 1 177\n"); |
---|
1111 | #endif |
---|
1112 | |
---|
1113 | nc = xmlNewNode(ns, BAD_CAST "OutputDefinitions"); |
---|
1114 | mcursor=outputs; |
---|
1115 | scursor=serv->outputs; |
---|
1116 | while(mcursor!=NULL /*&& scursor!=NULL*/){ |
---|
1117 | printOutputDefinitions1(doc,nc,ns,ns_ows,scursor,mcursor,"Output"); |
---|
1118 | mcursor=mcursor->next; |
---|
1119 | //scursor=scursor->next; |
---|
1120 | } |
---|
1121 | xmlAddChild(n,nc); |
---|
1122 | } |
---|
1123 | #ifdef DEBUG |
---|
1124 | fprintf(stderr,"printProcessResponse 1 190\n"); |
---|
1125 | #endif |
---|
1126 | |
---|
1127 | /** |
---|
1128 | * Display the process output only when requested ! |
---|
1129 | */ |
---|
1130 | if(status==SERVICE_SUCCEEDED){ |
---|
1131 | nc = xmlNewNode(ns, BAD_CAST "ProcessOutputs"); |
---|
1132 | maps* mcursor=outputs; |
---|
1133 | elements* scursor=serv->outputs; |
---|
1134 | while(mcursor!=NULL && scursor!=NULL){ |
---|
1135 | printIOType(doc,nc,ns,ns_ows,scursor,mcursor,"Output"); |
---|
1136 | mcursor=mcursor->next; |
---|
1137 | scursor=scursor->next; |
---|
1138 | } |
---|
1139 | xmlAddChild(n,nc); |
---|
1140 | } |
---|
1141 | #ifdef DEBUG |
---|
1142 | fprintf(stderr,"printProcessResponse 1 202\n"); |
---|
1143 | #endif |
---|
1144 | xmlDocSetRootElement(doc, n); |
---|
1145 | printDocument(m,doc,pid); |
---|
1146 | |
---|
1147 | xmlCleanupParser(); |
---|
1148 | zooXmlCleanupNs(); |
---|
1149 | } |
---|
1150 | |
---|
1151 | |
---|
1152 | void printDocument(maps* m, xmlDocPtr doc,int pid){ |
---|
1153 | rewind(stdout); |
---|
1154 | char *encoding=getEncoding(m); |
---|
1155 | if(pid==getpid()){ |
---|
1156 | printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding); |
---|
1157 | } |
---|
1158 | fflush(stdout); |
---|
1159 | xmlChar *xmlbuff; |
---|
1160 | int buffersize; |
---|
1161 | /* |
---|
1162 | * Dump the document to a buffer and print it on stdout |
---|
1163 | * for demonstration purposes. |
---|
1164 | */ |
---|
1165 | xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1); |
---|
1166 | printf((char *) xmlbuff); |
---|
1167 | //fflush(stdout); |
---|
1168 | /* |
---|
1169 | * Free associated memory. |
---|
1170 | */ |
---|
1171 | xmlFree(xmlbuff); |
---|
1172 | xmlFreeDoc(doc); |
---|
1173 | xmlCleanupParser(); |
---|
1174 | zooXmlCleanupNs(); |
---|
1175 | } |
---|
1176 | |
---|
1177 | void printOutputDefinitions1(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,elements* e,maps* m,char* type){ |
---|
1178 | xmlNodePtr nc1; |
---|
1179 | nc1=xmlNewNode(ns_wps, BAD_CAST type); |
---|
1180 | map *tmp=NULL; |
---|
1181 | if(e!=NULL && e->defaults!=NULL) |
---|
1182 | tmp=e->defaults->content; |
---|
1183 | else{ |
---|
1184 | /* |
---|
1185 | dumpElements(e); |
---|
1186 | */ |
---|
1187 | return; |
---|
1188 | } |
---|
1189 | while(tmp!=NULL){ |
---|
1190 | if(strncasecmp(tmp->name,"MIMETYPE",strlen(tmp->name))==0 |
---|
1191 | || strncasecmp(tmp->name,"ENCODING",strlen(tmp->name))==0 |
---|
1192 | || strncasecmp(tmp->name,"SCHEMA",strlen(tmp->name))==0 |
---|
1193 | || strncasecmp(tmp->name,"UOM",strlen(tmp->name))==0) |
---|
1194 | xmlNewProp(nc1,BAD_CAST tmp->name,BAD_CAST tmp->value); |
---|
1195 | tmp=tmp->next; |
---|
1196 | } |
---|
1197 | tmp=getMap(e->defaults->content,"asReference"); |
---|
1198 | if(tmp==NULL) |
---|
1199 | xmlNewProp(nc1,BAD_CAST "asReference",BAD_CAST "false"); |
---|
1200 | |
---|
1201 | tmp=e->content; |
---|
1202 | |
---|
1203 | printDescription(nc1,ns_ows,m->name,e->content); |
---|
1204 | |
---|
1205 | xmlAddChild(nc,nc1); |
---|
1206 | |
---|
1207 | } |
---|
1208 | |
---|
1209 | void printOutputDefinitions(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,elements* e,map* m,char* type){ |
---|
1210 | xmlNodePtr nc1,nc2,nc3; |
---|
1211 | nc1=xmlNewNode(ns_wps, BAD_CAST type); |
---|
1212 | map *tmp=NULL; |
---|
1213 | if(e!=NULL && e->defaults!=NULL) |
---|
1214 | tmp=e->defaults->content; |
---|
1215 | else{ |
---|
1216 | /* |
---|
1217 | dumpElements(e); |
---|
1218 | */ |
---|
1219 | return; |
---|
1220 | } |
---|
1221 | while(tmp!=NULL){ |
---|
1222 | xmlNewProp(nc1,BAD_CAST tmp->name,BAD_CAST tmp->value); |
---|
1223 | tmp=tmp->next; |
---|
1224 | } |
---|
1225 | tmp=getMap(e->defaults->content,"asReference"); |
---|
1226 | if(tmp==NULL) |
---|
1227 | xmlNewProp(nc1,BAD_CAST "asReference",BAD_CAST "false"); |
---|
1228 | |
---|
1229 | tmp=e->content; |
---|
1230 | |
---|
1231 | printDescription(nc1,ns_ows,m->name,e->content); |
---|
1232 | |
---|
1233 | xmlAddChild(nc,nc1); |
---|
1234 | |
---|
1235 | } |
---|
1236 | |
---|
1237 | void printIOType(xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_wps,xmlNsPtr ns_ows,elements* e,maps* m,char* type){ |
---|
1238 | xmlNodePtr nc1,nc2,nc3; |
---|
1239 | nc1=xmlNewNode(ns_wps, BAD_CAST type); |
---|
1240 | map *tmp=e->content; |
---|
1241 | #ifdef DEBUG |
---|
1242 | dumpMap(tmp); |
---|
1243 | dumpElements(e); |
---|
1244 | #endif |
---|
1245 | nc2=xmlNewNode(ns_ows, BAD_CAST "Identifier"); |
---|
1246 | nc3=xmlNewText(BAD_CAST e->name); |
---|
1247 | xmlAddChild(nc2,nc3); |
---|
1248 | xmlAddChild(nc1,nc2); |
---|
1249 | xmlAddChild(nc,nc1); |
---|
1250 | // Extract Title required to be first element in the ZCFG file ! |
---|
1251 | nc2=xmlNewNode(ns_ows, BAD_CAST tmp->name); |
---|
1252 | nc3=xmlNewText(BAD_CAST tmp->value); |
---|
1253 | xmlAddChild(nc2,nc3); |
---|
1254 | xmlAddChild(nc1,nc2); |
---|
1255 | // Extract Abstract required to be second element in the ZCFG file ! |
---|
1256 | tmp=tmp->next; |
---|
1257 | nc2=xmlNewNode(ns_ows, BAD_CAST tmp->name); |
---|
1258 | nc3=xmlNewText(BAD_CAST tmp->value); |
---|
1259 | xmlAddChild(nc2,nc3); |
---|
1260 | xmlAddChild(nc1,nc2); |
---|
1261 | xmlAddChild(nc,nc1); |
---|
1262 | tmp=tmp->next; |
---|
1263 | |
---|
1264 | /** |
---|
1265 | * IO type Reference or full Data ? |
---|
1266 | */ |
---|
1267 | #ifdef DEBUG |
---|
1268 | fprintf(stderr,"FORMAT %s %s\n",e->format,e->format); |
---|
1269 | #endif |
---|
1270 | map *tmpMap=getMap(m->content,"Reference"); |
---|
1271 | if(tmpMap==NULL){ |
---|
1272 | nc2=xmlNewNode(ns_wps, BAD_CAST "Data"); |
---|
1273 | if(strncasecmp(e->format,"LITERALOUTPUT",strlen(e->format))==0) |
---|
1274 | nc3=xmlNewNode(ns_wps, BAD_CAST "LiteralData"); |
---|
1275 | else |
---|
1276 | if(strncasecmp(e->format,"COMPLEXOUTPUT",strlen(e->format))==0) |
---|
1277 | nc3=xmlNewNode(ns_wps, BAD_CAST "ComplexData"); |
---|
1278 | else |
---|
1279 | nc3=xmlNewNode(ns_wps, BAD_CAST e->format); |
---|
1280 | tmp=m->content; |
---|
1281 | while(tmp!=NULL){ |
---|
1282 | if(strncasecmp(tmp->name,"value",strlen(tmp->name))!=0 && |
---|
1283 | strncasecmp(tmp->name,"extension",strlen(tmp->name))!=0 && |
---|
1284 | strncasecmp(tmp->name,"asReference",strlen(tmp->name))!=0 && |
---|
1285 | strncasecmp(tmp->name,"status",strlen(tmp->name))!=0 && |
---|
1286 | strncasecmp(tmp->name,"storeExecuteResponse",strlen(tmp->name))!=0 && |
---|
1287 | strncasecmp(tmp->name,"extension",strlen(tmp->name))!=0) |
---|
1288 | xmlNewProp(nc3,BAD_CAST tmp->name,BAD_CAST tmp->value); |
---|
1289 | tmp=tmp->next; |
---|
1290 | xmlAddChild(nc2,nc3); |
---|
1291 | } |
---|
1292 | tmp=getMap(e->defaults->content,"mimeType"); |
---|
1293 | map* tmp1=getMap(m->content,"encoding"); |
---|
1294 | map* tmp2=getMap(m->content,"mimeType"); |
---|
1295 | map* toto=getMap(m->content,"value"); |
---|
1296 | if((tmp1!=NULL && strncmp(tmp1->value,"base64",6)==0) |
---|
1297 | || (tmp2!=NULL && (strncmp(tmp2->value,"image/",6)==0 |
---|
1298 | || strncmp(tmp2->value,"application/",6)==0)) ){ |
---|
1299 | map* rs=getMap(m->content,"size"); |
---|
1300 | if(rs==NULL){ |
---|
1301 | char tmp1[1024]; |
---|
1302 | sprintf(tmp1,"%d",strlen(toto->value)); |
---|
1303 | rs=createMap("z",tmp1); |
---|
1304 | } |
---|
1305 | xmlAddChild(nc3,xmlNewText(BAD_CAST base64((const unsigned char*)toto->value,atoi(rs->value)))); |
---|
1306 | } |
---|
1307 | else if(tmp!=NULL){ |
---|
1308 | if(strncmp(tmp->value,"text/js",4)==0 || |
---|
1309 | strncmp(tmp->value,"application/js",14)==0) |
---|
1310 | xmlAddChild(nc3,xmlNewCDataBlock(doc,BAD_CAST toto->value,strlen(toto->value))); |
---|
1311 | else |
---|
1312 | xmlAddChild(nc3,xmlNewText(BAD_CAST toto->value)); |
---|
1313 | xmlAddChild(nc2,nc3); |
---|
1314 | } |
---|
1315 | else |
---|
1316 | xmlAddChild(nc3,xmlNewText(BAD_CAST toto->value)); |
---|
1317 | } |
---|
1318 | else{ |
---|
1319 | nc3=nc2=xmlNewNode(ns_wps, BAD_CAST "Reference"); |
---|
1320 | xmlNewProp(nc3,BAD_CAST "href",BAD_CAST tmpMap->value); |
---|
1321 | tmp=m->content; |
---|
1322 | while(tmp!=NULL){ |
---|
1323 | if(strncasecmp(tmp->name,"value",strlen(tmp->name))!=0 && |
---|
1324 | strncasecmp(tmp->name,"reference",strlen(tmp->name))!=0 && |
---|
1325 | strncasecmp(tmp->name,"extension",strlen(tmp->name))!=0 && |
---|
1326 | strncasecmp(tmp->name,"abstract",strlen(tmp->name))!=0 && |
---|
1327 | strncasecmp(tmp->name,"status",strlen(tmp->name))!=0 && |
---|
1328 | strncasecmp(tmp->name,"storeExecuteResponse",strlen(tmp->name))!=0 && |
---|
1329 | strncasecmp(tmp->name,"asReference",strlen(tmp->name))!=0) |
---|
1330 | xmlNewProp(nc3,BAD_CAST tmp->name,BAD_CAST tmp->value); |
---|
1331 | tmp=tmp->next; |
---|
1332 | xmlAddChild(nc2,nc3); |
---|
1333 | } |
---|
1334 | } |
---|
1335 | |
---|
1336 | xmlAddChild(nc1,nc2); |
---|
1337 | xmlAddChild(nc,nc1); |
---|
1338 | |
---|
1339 | } |
---|
1340 | |
---|
1341 | void printDescription(xmlNodePtr root,xmlNsPtr ns_ows,char* identifier,map* amap){ |
---|
1342 | xmlNodePtr nc2 = xmlNewNode(ns_ows, BAD_CAST "Identifier"); |
---|
1343 | xmlAddChild(nc2,xmlNewText(BAD_CAST identifier)); |
---|
1344 | xmlAddChild(root,nc2); |
---|
1345 | map* tmp=amap; |
---|
1346 | char *tmp2[2]; |
---|
1347 | tmp2[0]="Title"; |
---|
1348 | tmp2[1]="Abstract"; |
---|
1349 | int j=0; |
---|
1350 | for(j=0;j<2;j++){ |
---|
1351 | map* tmp1=getMap(tmp,tmp2[j]); |
---|
1352 | if(tmp1!=NULL){ |
---|
1353 | nc2 = xmlNewNode(ns_ows, BAD_CAST tmp2[j]); |
---|
1354 | xmlAddChild(nc2,xmlNewText(BAD_CAST tmp1->value)); |
---|
1355 | xmlAddChild(root,nc2); |
---|
1356 | } |
---|
1357 | } |
---|
1358 | } |
---|
1359 | |
---|
1360 | char* getEncoding(maps* m){ |
---|
1361 | if(m!=NULL){ |
---|
1362 | map* tmp=getMap(m->content,"encoding"); |
---|
1363 | if(tmp!=NULL){ |
---|
1364 | return tmp->value; |
---|
1365 | } |
---|
1366 | else |
---|
1367 | return "UTF-8"; |
---|
1368 | } |
---|
1369 | else |
---|
1370 | return "UTF-8"; |
---|
1371 | } |
---|
1372 | |
---|
1373 | char* getVersion(maps* m){ |
---|
1374 | if(m!=NULL){ |
---|
1375 | map* tmp=getMap(m->content,"version"); |
---|
1376 | if(tmp!=NULL){ |
---|
1377 | return tmp->value; |
---|
1378 | } |
---|
1379 | else |
---|
1380 | return "1.0.0"; |
---|
1381 | } |
---|
1382 | else |
---|
1383 | return "1.0.0"; |
---|
1384 | } |
---|
1385 | |
---|
1386 | void printExceptionReportResponse(maps* m,map* s){ |
---|
1387 | |
---|
1388 | int buffersize; |
---|
1389 | xmlDocPtr doc; |
---|
1390 | xmlChar *xmlbuff; |
---|
1391 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
1392 | xmlNodePtr n,nc,nc1,nc2; |
---|
1393 | |
---|
1394 | doc = xmlNewDoc(BAD_CAST "1.0"); |
---|
1395 | maps* tmpMap=getMaps(m,"main"); |
---|
1396 | char *encoding=getEncoding(tmpMap); |
---|
1397 | if(m!=NULL){ |
---|
1398 | map *tmpSid=getMapFromMaps(m,"lenv","sid"); |
---|
1399 | if(tmpSid!=NULL){ |
---|
1400 | if( getpid()==atoi(tmpSid->value) ) |
---|
1401 | printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding); |
---|
1402 | } |
---|
1403 | else |
---|
1404 | printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding); |
---|
1405 | }else |
---|
1406 | printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding); |
---|
1407 | |
---|
1408 | ns=xmlNewNs(NULL,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows"); |
---|
1409 | n = xmlNewNode(ns, BAD_CAST "ExceptionReport"); |
---|
1410 | ns_ows=xmlNewNs(n,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows"); |
---|
1411 | ns_xlink=xmlNewNs(n,BAD_CAST "http://www.w3.org/1999/xlink",BAD_CAST "xlink"); |
---|
1412 | ns_xsi=xmlNewNs(n,BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
1413 | xmlNewProp(n,BAD_CAST "xsi:schemaLocation",BAD_CAST "http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd"); |
---|
1414 | xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST "en"); |
---|
1415 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.1.0"); |
---|
1416 | |
---|
1417 | nc = xmlNewNode(ns, BAD_CAST "Exception"); |
---|
1418 | |
---|
1419 | map* tmp=getMap(s,"code"); |
---|
1420 | if(tmp!=NULL) |
---|
1421 | xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST tmp->value); |
---|
1422 | else |
---|
1423 | xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST "NoApplicableCode"); |
---|
1424 | |
---|
1425 | tmp=getMap(s,"text"); |
---|
1426 | nc1 = xmlNewNode(ns, BAD_CAST "ExceptionText"); |
---|
1427 | nc2=NULL; |
---|
1428 | if(tmp!=NULL){ |
---|
1429 | xmlNodeSetContent(nc1, BAD_CAST tmp->value); |
---|
1430 | } |
---|
1431 | else{ |
---|
1432 | xmlNodeSetContent(nc1, BAD_CAST "No debug message available"); |
---|
1433 | } |
---|
1434 | xmlAddChild(nc,nc1); |
---|
1435 | xmlAddChild(n,nc); |
---|
1436 | xmlDocSetRootElement(doc, n); |
---|
1437 | |
---|
1438 | xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1); |
---|
1439 | printf("%s",xmlbuff); |
---|
1440 | fflush(stdout); |
---|
1441 | xmlFreeDoc(doc); |
---|
1442 | xmlFree(xmlbuff); |
---|
1443 | xmlFreeNs(ns); |
---|
1444 | xmlCleanupParser(); |
---|
1445 | } |
---|
1446 | |
---|
1447 | xmlNodePtr createExceptionReportNode(maps* m,map* s,int use_ns){ |
---|
1448 | |
---|
1449 | int buffersize; |
---|
1450 | xmlChar *xmlbuff; |
---|
1451 | xmlNsPtr ns,ns_ows,ns_xlink,ns_xsi; |
---|
1452 | xmlNodePtr n,nc,nc1,nc2; |
---|
1453 | |
---|
1454 | maps* tmpMap=getMaps(m,"main"); |
---|
1455 | |
---|
1456 | ns=xmlNewNs(NULL,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows"); |
---|
1457 | n = xmlNewNode(ns, BAD_CAST "ExceptionReport"); |
---|
1458 | |
---|
1459 | if(use_ns==1){ |
---|
1460 | ns_ows=xmlNewNs(n,BAD_CAST "http://www.opengis.net/ows/1.1",BAD_CAST "ows"); |
---|
1461 | int xsiId=zooXmlAddNs(n,"http://www.w3.org/2001/XMLSchema-instance","xsi"); |
---|
1462 | ns_xsi=usedNs[xsiId]; |
---|
1463 | int xlinkId=zooXmlAddNs(n,"http://www.w3.org/1999/xlink","xlink"); |
---|
1464 | ns_xlink=usedNs[xlinkId]; |
---|
1465 | 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"); |
---|
1466 | } |
---|
1467 | xmlNewProp(n,BAD_CAST "xml:lang",BAD_CAST "en"); |
---|
1468 | xmlNewProp(n,BAD_CAST "version",BAD_CAST "1.1.0"); |
---|
1469 | |
---|
1470 | nc = xmlNewNode(ns, BAD_CAST "Exception"); |
---|
1471 | |
---|
1472 | map* tmp=getMap(s,"code"); |
---|
1473 | if(tmp!=NULL) |
---|
1474 | xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST tmp->value); |
---|
1475 | else |
---|
1476 | xmlNewProp(nc,BAD_CAST "exceptionCode",BAD_CAST "NoApplicableCode"); |
---|
1477 | |
---|
1478 | tmp=getMap(s,"text"); |
---|
1479 | nc1 = xmlNewNode(ns, BAD_CAST "ExceptionText"); |
---|
1480 | nc2=NULL; |
---|
1481 | if(tmp!=NULL){ |
---|
1482 | xmlNodeSetContent(nc1, BAD_CAST tmp->value); |
---|
1483 | } |
---|
1484 | else{ |
---|
1485 | xmlNodeSetContent(nc1, BAD_CAST "No debug message available"); |
---|
1486 | } |
---|
1487 | xmlAddChild(nc,nc1); |
---|
1488 | xmlAddChild(n,nc); |
---|
1489 | return n; |
---|
1490 | } |
---|
1491 | |
---|
1492 | |
---|
1493 | void outputResponse(service* s,maps* request_inputs,maps* request_outputs, |
---|
1494 | map* request_inputs1,int cpid,maps* m,int res){ |
---|
1495 | #ifdef DEBUG |
---|
1496 | dumpMaps(request_inputs); |
---|
1497 | dumpMaps(request_outputs); |
---|
1498 | fprintf(stderr,"printProcessResponse\n"); |
---|
1499 | #endif |
---|
1500 | map* toto=getMap(request_inputs1,"RawDataOutput"); |
---|
1501 | int asRaw=0; |
---|
1502 | if(toto!=NULL) |
---|
1503 | asRaw=1; |
---|
1504 | |
---|
1505 | if(asRaw==0){ |
---|
1506 | #ifdef DEBUG |
---|
1507 | fprintf(stderr,"REQUEST_OUTPUTS FINAL\n"); |
---|
1508 | dumpMaps(request_outputs); |
---|
1509 | #endif |
---|
1510 | toto=getMap(request_outputs->content,"asReference"); |
---|
1511 | if(toto!=NULL && strcasecmp(toto->value,"true")==0){ |
---|
1512 | toto=getMap(request_outputs->content,"extension"); |
---|
1513 | map *tmp1=getMapFromMaps(m,"main","tmpPath"); |
---|
1514 | char *file_name=(char*)malloc((strlen(tmp1->value)+strlen(s->name)+strlen(toto->value)+13)*sizeof(char)); |
---|
1515 | sprintf(file_name,"%s/%s_%i.%s",tmp1->value,s->name,cpid+100000,toto->value); |
---|
1516 | FILE *ofile=fopen(file_name,"w"); |
---|
1517 | map *tmp2=getMapFromMaps(m,"main","tmpUrl"); |
---|
1518 | map *tmp3=getMapFromMaps(m,"main","serverAddress"); |
---|
1519 | char *file_url=(char*)malloc((strlen(tmp3->value)+strlen(tmp2->value)+strlen(s->name)+strlen(toto->value)+13)*sizeof(char)); |
---|
1520 | sprintf(file_url,"%s/%s/%s_%i.%s",tmp3->value,tmp2->value,s->name,cpid+100000,toto->value); |
---|
1521 | addToMap(request_outputs->content,"Reference",file_url); |
---|
1522 | toto=getMap(request_outputs->content,"value"); |
---|
1523 | if(toto!=NULL) |
---|
1524 | fwrite(toto->value,sizeof(char),strlen(toto->value),ofile); |
---|
1525 | fclose(ofile); |
---|
1526 | free(file_name); |
---|
1527 | free(file_url); |
---|
1528 | } |
---|
1529 | map *r_inputs=getMap(s->content,"serviceProvider"); |
---|
1530 | #ifdef DEBUG |
---|
1531 | fprintf(stderr,"SERVICE : %s\n",r_inputs->value); |
---|
1532 | dumpMaps(m); |
---|
1533 | #endif |
---|
1534 | printProcessResponse(m,request_inputs1,cpid, |
---|
1535 | s,r_inputs->value,res, |
---|
1536 | request_inputs, |
---|
1537 | request_outputs); |
---|
1538 | } |
---|
1539 | else |
---|
1540 | if(res!=SERVICE_FAILED){ |
---|
1541 | /** |
---|
1542 | * We get the first output only !! |
---|
1543 | */ |
---|
1544 | toto=getMap(request_outputs->content,"value"); |
---|
1545 | if(toto==NULL){ |
---|
1546 | map * errormap = createMap("text","Unable to fetch any result"); |
---|
1547 | addToMap(errormap,"code", "InternalError"); |
---|
1548 | printExceptionReportResponse(m,errormap); |
---|
1549 | freeMap(&errormap); |
---|
1550 | free(errormap); |
---|
1551 | } |
---|
1552 | char mime[1024]; |
---|
1553 | map* mi=getMap(request_outputs->content,"mimeType"); |
---|
1554 | #ifdef DEBUG |
---|
1555 | fprintf(stderr,"SERVICE OUTPUTS\n"); |
---|
1556 | dumpMaps(request_outputs); |
---|
1557 | fprintf(stderr,"SERVICE OUTPUTS\n"); |
---|
1558 | #endif |
---|
1559 | map* en=getMap(request_outputs->content,"encoding"); |
---|
1560 | if(mi!=NULL && en!=NULL) |
---|
1561 | sprintf(mime, |
---|
1562 | "Content-Type: %s; charset=%s\r\nStatus: 200 OK\r\n\r\n", |
---|
1563 | mi->value,en->value); |
---|
1564 | else |
---|
1565 | if(mi!=NULL) |
---|
1566 | sprintf(mime, |
---|
1567 | "Content-Type: %s; charset=UTF-8\r\nStatus: 200 OK\r\n\r\n", |
---|
1568 | mi->value); |
---|
1569 | else |
---|
1570 | sprintf(mime,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); |
---|
1571 | printf("%s",mime); |
---|
1572 | if(mi!=NULL && strncmp(mi->value,"image",5)==0){ |
---|
1573 | map* rs=getMapFromMaps(request_outputs,request_outputs->name,"size"); |
---|
1574 | fwrite(toto->value,atoi(rs->value),1,stdout); |
---|
1575 | } |
---|
1576 | else |
---|
1577 | printf("%s",toto->value); |
---|
1578 | #ifdef DEBUG |
---|
1579 | dumpMap(toto); |
---|
1580 | #endif |
---|
1581 | }else{ |
---|
1582 | char tmp[1024]; |
---|
1583 | map * errormap; |
---|
1584 | map *lenv; |
---|
1585 | lenv=getMapFromMaps(m,"lenv","message"); |
---|
1586 | if(lenv!=NULL) |
---|
1587 | sprintf(tmp,"Unable to run the Service. The message returned back by the Service was the following : %s",lenv->value); |
---|
1588 | else |
---|
1589 | sprintf(tmp,"Unable to run the Service. No more information was returned back by the Service."); |
---|
1590 | errormap = createMap("text",tmp); |
---|
1591 | addToMap(errormap,"code", "InternalError"); |
---|
1592 | printExceptionReportResponse(m,errormap); |
---|
1593 | freeMap(&errormap); |
---|
1594 | free(errormap); |
---|
1595 | } |
---|
1596 | } |
---|
1597 | |
---|
1598 | char *base64(const unsigned char *input, int length) |
---|
1599 | { |
---|
1600 | BIO *bmem, *b64; |
---|
1601 | BUF_MEM *bptr; |
---|
1602 | |
---|
1603 | b64 = BIO_new(BIO_f_base64()); |
---|
1604 | bmem = BIO_new(BIO_s_mem()); |
---|
1605 | b64 = BIO_push(b64, bmem); |
---|
1606 | BIO_write(b64, input, length); |
---|
1607 | BIO_flush(b64); |
---|
1608 | BIO_get_mem_ptr(b64, &bptr); |
---|
1609 | |
---|
1610 | char *buff = (char *)malloc(bptr->length); |
---|
1611 | memcpy(buff, bptr->data, bptr->length-1); |
---|
1612 | buff[bptr->length-1] = 0; |
---|
1613 | |
---|
1614 | BIO_free_all(b64); |
---|
1615 | |
---|
1616 | fprintf(stderr,"BASE64 [%s] \n",buff); |
---|
1617 | return buff; |
---|
1618 | } |
---|
1619 | |
---|
1620 | char* addDefaultValues(maps** out,elements* in,maps* m,char* type){ |
---|
1621 | elements* tmpInputs=in; |
---|
1622 | maps* out1=*out; |
---|
1623 | while(tmpInputs!=NULL){ |
---|
1624 | maps *tmpMaps=getMaps(out1,tmpInputs->name); |
---|
1625 | if(tmpMaps==NULL){ |
---|
1626 | map* tmpMap1=getMap(tmpInputs->content,"minOccurs"); |
---|
1627 | if(strncmp(type,"inputs",6)==0) |
---|
1628 | if(tmpMap1!=NULL && atoi(tmpMap1->value)>=1){ |
---|
1629 | return tmpInputs->name; |
---|
1630 | } |
---|
1631 | maps* tmpMaps2=(maps*)malloc(MAPS_SIZE); |
---|
1632 | tmpMaps2->name=strdup((char*)tmpInputs->name); |
---|
1633 | tmpMaps2->content=NULL; |
---|
1634 | tmpMaps2->next=NULL; |
---|
1635 | iotype* tmpIoType=tmpInputs->defaults; |
---|
1636 | while(tmpIoType!=NULL){ |
---|
1637 | addMapToMap(&tmpMaps2->content,tmpIoType->content); |
---|
1638 | tmpIoType=tmpIoType->next; |
---|
1639 | } |
---|
1640 | map *tmpMap=getMap(tmpMaps2->content,"value"); |
---|
1641 | if(tmpMap==NULL) |
---|
1642 | addToMap(tmpMaps2->content,"value","NULL"); |
---|
1643 | if(out1==NULL){ |
---|
1644 | *out=dupMaps(&tmpMaps2); |
---|
1645 | } |
---|
1646 | else |
---|
1647 | addMapsToMaps(&out1,tmpMaps2); |
---|
1648 | freeMaps(&tmpMaps2); |
---|
1649 | free(tmpMaps2); |
---|
1650 | tmpMaps2=NULL; |
---|
1651 | } |
---|
1652 | else{ |
---|
1653 | map* tmpContent=tmpInputs->defaults->content; |
---|
1654 | |
---|
1655 | map* cval=NULL; |
---|
1656 | |
---|
1657 | while(tmpContent!=NULL){ |
---|
1658 | if((cval=getMap(tmpMaps->content,tmpContent->name))==NULL){ |
---|
1659 | #ifdef DEBUG |
---|
1660 | fprintf(stderr,"addDefaultValues %s => %s\n",tmpContent->name,tmpContent->value); |
---|
1661 | #endif |
---|
1662 | if(tmpMaps->content==NULL) |
---|
1663 | tmpMaps->content=createMap(tmpContent->name,tmpContent->value); |
---|
1664 | else |
---|
1665 | addToMap(tmpMaps->content,tmpContent->name,tmpContent->value); |
---|
1666 | } |
---|
1667 | tmpContent=tmpContent->next; |
---|
1668 | } |
---|
1669 | } |
---|
1670 | tmpInputs=tmpInputs->next; |
---|
1671 | } |
---|
1672 | return ""; |
---|
1673 | } |
---|