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