1 | /** |
---|
2 | * Author : David Saggiorato |
---|
3 | * |
---|
4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
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 | |
---|
26 | |
---|
27 | #include <string.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <glib.h> |
---|
31 | #include <sys/stat.h> |
---|
32 | #include "service.h" |
---|
33 | #include "service_internal.h" |
---|
34 | extern "C" { |
---|
35 | #include <libxml/tree.h> |
---|
36 | #include <libxml/xmlmemory.h> |
---|
37 | #include <libxml/parser.h> |
---|
38 | #include <libxml/xpath.h> |
---|
39 | #include <libxml/xpathInternals.h> |
---|
40 | #include "service.h" |
---|
41 | #include "service_internal.h" |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | static int SCAN_DEPTH = 7; |
---|
46 | |
---|
47 | |
---|
48 | static GList *serviceCfgList; |
---|
49 | |
---|
50 | //static maps * main_conf = NULL; |
---|
51 | |
---|
52 | |
---|
53 | /*int |
---|
54 | load_main_conf(char * path){ |
---|
55 | if (main_conf != NULL) |
---|
56 | freeMaps(&main_conf); |
---|
57 | main_conf = (maps *) malloc (MAP_SIZE); |
---|
58 | return conf_read (path, main_conf); |
---|
59 | } |
---|
60 | |
---|
61 | maps * get_main_conf(){ |
---|
62 | return dupMaps(&main_conf); |
---|
63 | } |
---|
64 | |
---|
65 | */ |
---|
66 | |
---|
67 | static int |
---|
68 | scanServiceCfg (char *rootDir, GList ** serviceList, int level) |
---|
69 | { |
---|
70 | if (level >= SCAN_DEPTH) |
---|
71 | return 1; |
---|
72 | struct dirent *d; |
---|
73 | DIR *dir = opendir (rootDir); |
---|
74 | if (dir != NULL) |
---|
75 | { |
---|
76 | while ((d = readdir (dir))) |
---|
77 | { |
---|
78 | if ((d->d_type == DT_DIR || d->d_type == DT_LNK) |
---|
79 | && d->d_name[0] != '.' && strstr (d->d_name, ".") == NULL) |
---|
80 | { |
---|
81 | char *name = |
---|
82 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + 2); |
---|
83 | name[0] = '\0'; |
---|
84 | strncat (name, rootDir, strlen (rootDir)); |
---|
85 | strncat (name, "/", strlen ("/")); |
---|
86 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
87 | scanServiceCfg (name, serviceList, level + 1); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | if (d->d_name[0] != '.') |
---|
92 | if (strstr (d->d_name, ".zcfg") != NULL) |
---|
93 | { |
---|
94 | char *name = |
---|
95 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + |
---|
96 | 2); |
---|
97 | name[0] = '\0'; |
---|
98 | strncat (name, rootDir, strlen (rootDir)); |
---|
99 | strncat (name, "/", strlen ("/")); |
---|
100 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
101 | *serviceList = g_list_append (*serviceList, name); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | if (level > 0) |
---|
106 | free(rootDir); |
---|
107 | } |
---|
108 | closedir (dir); |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | static int |
---|
113 | get_identifier (char *root_dir, char *zcfg_path, char **identifier) |
---|
114 | { |
---|
115 | // On extrait le repertoire racine ainsi que l'extention .zcfg pour contruire l'identifiant |
---|
116 | // root_dir = /var/www/zoo/cgi-bin/ zcfg_path=/var/www/zoo/cgi-bin/gdal/ndvi/ExtractNDVI.zcfg ===> gdal.ndvi.ExtractNDVI |
---|
117 | char *identifier_tmp = |
---|
118 | (char *) malloc ((strlen (zcfg_path) + 1) * sizeof (char)); |
---|
119 | identifier_tmp[0] = '\0'; |
---|
120 | int ext_len = strlen (".zcfg"); |
---|
121 | int s_tmp_len = strlen (zcfg_path) - ext_len - strlen (root_dir); |
---|
122 | if (s_tmp_len > 1) |
---|
123 | { |
---|
124 | char *s_tmp = (char *) malloc ((s_tmp_len + 1) * sizeof (char)); |
---|
125 | int j; |
---|
126 | for (j = 0; j < s_tmp_len; j++) |
---|
127 | s_tmp[j] = zcfg_path[strlen (root_dir) + j]; |
---|
128 | s_tmp[s_tmp_len] = '\0'; |
---|
129 | char *save_ptr_strtok; |
---|
130 | char *token = strtok_r (s_tmp, "/", &save_ptr_strtok); |
---|
131 | strncat (identifier_tmp, token, strlen (token)); |
---|
132 | while (token != NULL) |
---|
133 | { |
---|
134 | token = strtok_r (NULL, "/", &save_ptr_strtok); |
---|
135 | if (token != NULL) |
---|
136 | { |
---|
137 | strncat (identifier_tmp, ".", strlen (".")); |
---|
138 | strncat (identifier_tmp, token, strlen (token)); |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | *identifier = |
---|
143 | (char *) malloc ((strlen (identifier_tmp) + 1) * sizeof (char)); |
---|
144 | strncpy (*identifier, identifier_tmp, strlen (identifier_tmp) + 1); |
---|
145 | free (s_tmp); |
---|
146 | } |
---|
147 | free (identifier_tmp); |
---|
148 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | void |
---|
152 | init_services_conf (char *rootDir) |
---|
153 | { |
---|
154 | maps *m = (maps *) malloc (MAPS_SIZE); |
---|
155 | GList *L = NULL; |
---|
156 | scanServiceCfg (rootDir, &L, 0); |
---|
157 | GList *l = NULL; |
---|
158 | for (l = L; l; l = l->next) |
---|
159 | { |
---|
160 | service *s1 = (service *) malloc (SERVICE_SIZE); |
---|
161 | get_identifier (rootDir, (char *) (l->data), &(s1->identifier)); |
---|
162 | s1->zcfg = (char *) (l->data); |
---|
163 | readServiceFile (m, (char *) l->data, &s1, s1->identifier); |
---|
164 | serviceCfgList = g_list_append (serviceCfgList, s1); |
---|
165 | } |
---|
166 | |
---|
167 | //freeMaps(&m); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | service * |
---|
172 | search_service (char *identifier) |
---|
173 | { |
---|
174 | GList *l; |
---|
175 | int i = 0; |
---|
176 | for (l = serviceCfgList; l; l = l->next) |
---|
177 | { |
---|
178 | #ifdef DEBUG |
---|
179 | fprintf (stderr, "%d ### %s ###\n", i, |
---|
180 | ((service *) (l->data))->identifier); |
---|
181 | i++; |
---|
182 | #endif |
---|
183 | if (strcasecmp (identifier, ((service *) (l->data))->identifier) == 0) |
---|
184 | return (service *) l->data; |
---|
185 | } |
---|
186 | return NULL; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void |
---|
191 | XML_CapabilitiesAllProcess (maps * m, xmlNodePtr nc) |
---|
192 | { |
---|
193 | GList *l; |
---|
194 | for (l = serviceCfgList; l; l = l->next) |
---|
195 | { |
---|
196 | |
---|
197 | service *serv = (service *) l->data; |
---|
198 | |
---|
199 | xmlNsPtr ns, ns_ows, ns_xlink; |
---|
200 | xmlNodePtr n = NULL, nc1, nc2; |
---|
201 | /** |
---|
202 | * Initialize or get existing namspaces |
---|
203 | */ |
---|
204 | int wpsId = |
---|
205 | zooXmlAddNs (NULL, "http://www.opengis.net/wps/1.0.0", "wps"); |
---|
206 | ns = usedNs[wpsId]; |
---|
207 | int owsId = zooXmlAddNs (NULL, "http://www.opengis.net/ows/1.1", "ows"); |
---|
208 | ns_ows = usedNs[owsId]; |
---|
209 | int xlinkId = zooXmlAddNs (n, "http://www.w3.org/1999/xlink", "xlink"); |
---|
210 | ns_xlink = usedNs[xlinkId]; |
---|
211 | |
---|
212 | map *tmp1; |
---|
213 | if (serv->content != NULL) |
---|
214 | { |
---|
215 | nc1 = xmlNewNode (ns, BAD_CAST "Process"); |
---|
216 | tmp1 = getMap (serv->content, "processVersion"); |
---|
217 | if (tmp1 != NULL) |
---|
218 | xmlNewNsProp (nc1, ns, BAD_CAST "processVersion", |
---|
219 | BAD_CAST tmp1->value); |
---|
220 | //map *tmp3 = getMapFromMaps (m, "lenv", "level"); |
---|
221 | //addPrefix (m, tmp3, serv); |
---|
222 | printDescription (nc1, ns_ows, serv->identifier, serv->content); |
---|
223 | tmp1 = serv->metadata; |
---|
224 | while (tmp1 != NULL) |
---|
225 | { |
---|
226 | nc2 = xmlNewNode (ns_ows, BAD_CAST "Metadata"); |
---|
227 | xmlNewNsProp (nc2, ns_xlink, BAD_CAST tmp1->name, |
---|
228 | BAD_CAST tmp1->value); |
---|
229 | xmlAddChild (nc1, nc2); |
---|
230 | tmp1 = tmp1->next; |
---|
231 | } |
---|
232 | xmlAddChild (nc, nc1); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void |
---|
238 | XML_Describe_Process (maps * m, xmlNodePtr nc, char *identifiers) |
---|
239 | { |
---|
240 | if (strcasecmp ("all", identifiers) == 0) |
---|
241 | { |
---|
242 | GList *l; |
---|
243 | for (l = serviceCfgList; l; l = l->next) |
---|
244 | { |
---|
245 | service *serv = (service *) l->data; |
---|
246 | printDescribeProcessForProcess (m, nc, serv); |
---|
247 | } |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | char *save_ptr_strtok; |
---|
252 | char *token = strtok_r (identifiers, ",", &save_ptr_strtok); |
---|
253 | while (token != NULL) |
---|
254 | { |
---|
255 | service *serv = search_service (token); |
---|
256 | if (serv != NULL) |
---|
257 | printDescribeProcessForProcess (m, nc, serv); |
---|
258 | token = strtok_r (NULL, ",", &save_ptr_strtok); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|