Index: branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 677)
+++ branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 741)
@@ -32,5 +32,5 @@
 #endif
 
-#include "cgic.h"
+//#include "cgic.h"
 
 extern "C"
@@ -45,5 +45,5 @@
 #include "ulinet.h"
 
-#include <libintl.h>
+//#include <libintl.h>
 #include <locale.h>
 #include <string.h>
@@ -56,4 +56,6 @@
 #include "request_parser.h"
 #include "sqlapi.h"
+#include "zoo_amqp.h"
+#include "zoo_json.h"
 
 #ifdef USE_PYTHON
@@ -237,5 +239,5 @@
            ("ZOO Kernel failed to process your request, receiving signal %d = %s"),
            sig, ssig);
-  errorException (NULL, tmp, "InternalError", NULL);
+  errorException (NULL, tmp, "InternalError", NULL,NULL);
 #ifdef DEBUG
   fprintf (stderr, "Not this time!\n");
@@ -256,5 +258,5 @@
 void
 loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
-                   maps ** inputs, maps ** ioutputs, int *eres)
+                   maps ** inputs, maps ** ioutputs, int *eres,FCGX_Stream *out, FCGX_Stream *err)
 {
   char tmps1[1024];
@@ -386,5 +388,4 @@
               typedef int (*execute_t) (maps **, maps **, maps **);
 
-              fprintf(stderr,"%s \n",s1->name);
 #ifdef WIN32
               execute_t execute =
@@ -407,5 +408,5 @@
                            ("Error occured while running the %s function: %s"),
                            s1->name, errstr);
-                  errorException (m, tmpMsg, "InternalError", NULL);
+                  errorException (m, tmpMsg, "InternalError", NULL,out);
                   free (tmpMsg);
 #ifdef DEBUG
@@ -457,5 +458,5 @@
 #endif
           sprintf (tmps, _("Unable to load C Library %s"), errstr);
-	  errorException(m,tmps,"InternalError",NULL);
+	  errorException(m,tmps,"InternalError",NULL,out);
           *eres = -1;
         }
@@ -553,7 +554,8 @@
                ("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),
                r_inputs->value);
-      errorException (m, tmpv, "InternalError", NULL);
+      errorException (m, tmpv, "InternalError", NULL,out);
       *eres = -1;
     }
+   fflush(stderr);
   *myMap = m;
   *ioutputs = request_output_real_format;
@@ -728,7 +730,8 @@
  */
 int
-runRequest (map ** inputs)
+runRequest (map ** inputs,struct cgi_env ** c,FCGX_Request *request)
 {
-
+struct cgi_env *cgi = *c;
+char *cgiSid = cgi->cgiSid; 
 #ifndef USE_GDB
 #ifndef WIN32
@@ -757,5 +760,5 @@
     {
       return errorException (m, _("Unable to allocate memory."),
-                             "InternalError", NULL);
+                             "InternalError", NULL,request->out);
     }
   char ntmp[1024];
@@ -772,5 +775,5 @@
     {
       errorException (NULL, _("Unable to load the main.cfg file."),
-                      "InternalError", NULL);
+                      "InternalError", NULL,request->out);
       free (m);
       return 1;
@@ -819,5 +822,5 @@
                    ("The value %s is not supported for the <language> parameter"),
                    r_inputs->value);
-          errorException (m, tmp, "InvalidParameterValue", "language");
+          errorException (m, tmp, "InvalidParameterValue", "language",request->out);
           freeMaps (&m);
           free (m);
@@ -870,22 +873,22 @@
     setMapInMaps (m, "main", "isSoap", "false");
 
-  if(strlen(cgiServerName)>0)
+  if(strlen(cgi->cgiServerName)>0)
     {
       char tmpUrl[1024];
 	
       if ( getenv("HTTPS") != NULL && strncmp(getenv("HTTPS"), "on", 2) == 0 ) { // Knut: check if non-empty instead of "on"?		
-	if ( strncmp(cgiServerPort, "443", 3) == 0 ) { 
-	  sprintf(tmpUrl, "https://%s%s", cgiServerName, cgiScriptName);
+	if ( strncmp(cgi->cgiServerPort, "443", 3) == 0 ) { 
+	  sprintf(tmpUrl, "https://%s%s", cgi->cgiServerName, cgi->cgiScriptName);
 	}
 	else {
-	  sprintf(tmpUrl, "https://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
+	  sprintf(tmpUrl, "https://%s:%s%s", cgi->cgiServerName, cgi->cgiServerPort, cgi->cgiScriptName);
 	}
       }
       else {
-	if ( strncmp(cgiServerPort, "80", 2) == 0 ) { 
-	  sprintf(tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
+	if ( strncmp(cgi->cgiServerPort, "80", 2) == 0 ) { 
+	  sprintf(tmpUrl, "http://%s%s", cgi->cgiServerName, cgi->cgiScriptName);
 	}
 	else {
-	  sprintf(tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
+	  sprintf(tmpUrl, "http://%s:%s%s", cgi->cgiServerName, cgi->cgiServerPort, cgi->cgiScriptName);
 	}
       }
@@ -913,5 +916,5 @@
   if(err!=NULL){
     checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
-    printExceptionReportResponse (m, err);
+    printExceptionReportResponse (m, err,request->out);
     freeMap(&err);
     free(err);
@@ -969,5 +972,5 @@
   }
   if(err!=NULL){
-    printExceptionReportResponse (m, err);
+    printExceptionReportResponse (m, err,request->out);
     freeMap(&err);
     free(err);
@@ -1024,41 +1027,14 @@
       xmlNodePtr n=printGetCapabilitiesHeader(doc,m,(version!=NULL?version->value:"1.0.0"));
       CapabilitiesAllProcess(m,n);
+      
       /**
        * Here we need to close stdout to ensure that unsupported chars 
        * has been found in the zcfg and then printed on stdout
        */
-      /*
-      int saved_stdout = dup (fileno (stdout));
-      dup2 (fileno (stderr), fileno (stdout));
-      if (int res =		  
-          recursReaddirF (m, zooRegistry, n, conf_dir, NULL, saved_stdout, 0,
-                          printGetCapabilitiesForProcess) < 0)
-        {
-          freeMaps (&m);
-          free (m);
-	  if(zooRegistry!=NULL){
-	    freeRegistry(&zooRegistry);
-	    free(zooRegistry);
-	  }
-          free (REQUEST);
-          free (SERVICE_URL);
-          fflush (stdout);
-          return res;
-        }
-      fflush (stdout);
-      dup2 (saved_stdout, fileno (stdout));
-      */
-      printDocument (m, doc, getpid ());
+      printDocument (m, doc, getpid (),request->out);
       freeMaps (&m);
       free (m);
-      /*
-      if(zooRegistry!=NULL){
-	freeRegistry(&zooRegistry);
-	free(zooRegistry);
-      }
-*/
       free (REQUEST);
       free (SERVICE_URL);
-      fflush (stdout);
       return 0;
     }
@@ -1069,5 +1045,5 @@
 	if (strncasecmp (REQUEST, "GetStatus", strlen(REQUEST)) == 0 ||
 	    strncasecmp (REQUEST, "GetResult", strlen(REQUEST)) == 0){
-	  runGetStatus(m,r_inputs->value,REQUEST);
+	  runGetStatus(m,r_inputs->value,REQUEST,request->out);
 	  freeMaps (&m);
 	  free (m);
@@ -1078,5 +1054,5 @@
 	else
 	  if (strncasecmp (REQUEST, "Dismiss", strlen(REQUEST)) == 0){
-	    runDismiss(m,r_inputs->value);
+	    runDismiss(m,r_inputs->value,request->out);
 	    freeMaps (&m);
 	    free (m);
@@ -1091,16 +1067,4 @@
 	r_inputs = getMap (request_inputs, "Identifier");
 
-	struct dirent *dp;
-	DIR *dirp = opendir (conf_dir);
-	if (dirp == NULL)
-	  {
-	    errorException (m, _("The specified path path does not exist."),
-			    "InvalidParameterValue", conf_dir);
-	    freeMaps (&m);
-	    free (m);
-	    free (REQUEST);
-	    free (SERVICE_URL);
-	    return 0;
-	  }
 	if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0)
 	  {
@@ -1119,19 +1083,16 @@
 
         DescribeProcess(m,n,orig);
-	    fflush (stdout);
         int saved_stdout;
-	    dup2 (saved_stdout, fileno (stdout));
 	    free (orig);
-	    printDocument (m, doc, getpid ());
+	    printDocument (m, doc, getpid (),request->out);
 	    freeMaps (&m);
 	    free (m);
 	    free (REQUEST);
 	    free (SERVICE_URL);
-	    fflush (stdout);
 	    return 0;
 	  }
 	else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
 	  {
-	    map* version=getMapFromMaps(m,"main","rversion");
+        map* version=getMapFromMaps(m,"main","rversion");
 	    int vid=getVersionId(version->value);
 	    int len,j=0;
@@ -1169,17 +1130,14 @@
 	    char* message=(char*)malloc((61+len)*sizeof(char));
 	    sprintf(message,"The <request> value was not recognized. Allowed values are %s.",tmpStr);
-	    errorException (m,_(message),"InvalidParameterValue", "request");
+	    errorException (m,_(message),"InvalidParameterValue", "request",request->out);
 #ifdef DEBUG
 	    fprintf (stderr, "No request found %s", REQUEST);
 #endif
-	    closedir (dirp);
 	    freeMaps (&m);
 	    free (m);
 	    free (REQUEST);
 	    free (SERVICE_URL);
-	    fflush (stdout);
 	    return 0;
 	  }
-	closedir (dirp);
       }
     }
@@ -1187,19 +1145,18 @@
   map *postRequest = NULL;
   postRequest = getMap (request_inputs, "xrequest");
-
+/*
   if(vid==1 && postRequest==NULL){
-    errorException (m,_("Unable to run Execute request using the GET HTTP method"),"InvalidParameterValue", "request");  
+    errorException (m,_("Unable to run Execute request using the GET HTTP method"),"InvalidParameterValue", "request",request->out);  
     freeMaps (&m);
     free (m);
     free (REQUEST);
     free (SERVICE_URL);
-    fflush (stdout);
     return 0;
   }
-  
+  */
   s1 = NULL;
   r_inputs = getMap (request_inputs, "Identifier");
   s1 = search_service (r_inputs->value);
- int saved_stdout = dup (fileno (stdout));
+ int saved_stdout;// = dup (fileno (stdout));
   
   
@@ -1211,5 +1168,5 @@
                ("The value for <identifier> seems to be wrong (%s). Please specify one of the processes in the list returned by a GetCapabilities request."),
                r_inputs->value);
-      errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
+      errorException (m, tmpMsg, "InvalidParameterValue", "identifier",request->out);
       free (tmpMsg);
       freeMaps (&m);
@@ -1219,5 +1176,7 @@
       return 0;
     }
-  close (saved_stdout);
+  setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
+  setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
+
 
 #ifdef DEBUG
@@ -1248,5 +1207,5 @@
 
 
-  if(parseRequest(&m,&request_inputs,s1,&request_input_real_format,&request_output_real_format,&hInternet)<0){
+  if(parseRequest(&m,&request_inputs,s1,&request_input_real_format,&request_output_real_format,&hInternet,cgi)<0){
     freeMaps (&m);
     free (m);
@@ -1334,7 +1293,5 @@
 			_
 			("The status parameter cannot be set to true if storeExecuteResponse is set to false. Please modify your request parameters."),
-			"InvalidParameterValue", "storeExecuteResponse");
-	//freeService (&s1);
-	free (s1);
+			"InvalidParameterValue", "storeExecuteResponse",request->out);
 	freeMaps (&m);
 	free (m);
@@ -1361,6 +1318,5 @@
 	if(mode!=NULL){
 	  // see ref. http://docs.opengeospatial.org/is/14-065/14-065.html#61
-	  errorException (m,_("The process does not permit the desired execution mode."),"NoSuchMode", mode->value);  
-	  fflush (stdout);
+	  errorException (m,_("The process does not permit the desired execution mode."),"NoSuchMode", mode->value,request->out);  
 	  freeMaps (&m);
 	  free (m);
@@ -1440,8 +1396,8 @@
 
   // Parse the session file and add it to the main maps 
-  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
+  if (cgi->cgiCookie != NULL && strlen (cgi->cgiCookie) > 0)
     {
       int hasValidCookie = -1;
-      char *tcook = zStrdup (cgiCookie);
+      char *tcook = zStrdup (cgi->cgiCookie);
       char *tmp = NULL;
       map *testing = getMapFromMaps (m, "main", "cookiePrefix");
@@ -1456,8 +1412,8 @@
           sprintf (tmp, "%s=", testing->value);
         }
-      if (strstr (cgiCookie, ";") != NULL)
+      if (strstr (cgi->cgiCookie, ";") != NULL)
         {
           char *token, *saveptr;
-          token = strtok_r (cgiCookie, ";", &saveptr);
+          token = strtok_r (cgi->cgiCookie, ";", &saveptr);
           while (token != NULL)
             {
@@ -1474,8 +1430,8 @@
       else
         {
-          if (strstr (cgiCookie, "=") != NULL
-              && strcasestr (cgiCookie, tmp) != NULL)
+          if (strstr (cgi->cgiCookie, "=") != NULL
+              && strcasestr (cgi->cgiCookie, tmp) != NULL)
             {
-              tcook = zStrdup (cgiCookie);
+              tcook = zStrdup (cgi->cgiCookie);
               hasValidCookie = 1;
             }
@@ -1498,5 +1454,5 @@
           else
             sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
-                     strstr (cgiCookie, "=") + 1);
+                     strstr (cgi->cgiCookie, "=") + 1);
           free (tcook);
           maps *tmpSess = (maps *) malloc (MAPS_SIZE);
@@ -1539,4 +1495,5 @@
     }
 #endif
+
   char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog;
   FILE *f0, *f1;
@@ -1546,6 +1503,5 @@
   if (status == NULLMAP)
     {
-      if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
-	free (s1);
+      if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet,cgi)<0){
 	freeMaps (&m);
 	free (m);
@@ -1560,10 +1516,53 @@
 	return -1;
       }
-
+       fflush(stderr);
       loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
-                         &request_output_real_format, &eres);
-    }
-  else
-    {
+                         &request_output_real_format, &eres,request->out,request->err);
+    }
+  else
+    {
+        
+        eres = SERVICE_ACCEPTED;
+        vid = 1;
+        
+#ifdef AMQP
+
+
+    eres = SERVICE_ACCEPTED;
+    json_object *msg_jobj = json_object_new_object();
+    json_object *maps_obj;
+    mapstojson(&maps_obj,m);
+    json_object_object_add(msg_jobj,"maps",maps_obj);
+
+
+    json_object *req_format_jobj;
+    mapstojson(&req_format_jobj,request_input_real_format);
+    json_object_object_add(msg_jobj,"request_input_real_format",req_format_jobj);
+
+    json_object *req_jobj;
+    maptojson(&req_jobj,request_inputs);
+    json_object_object_add(msg_jobj,"request_inputs",req_jobj);
+
+ 
+    dumpMaps(request_output_real_format);
+    json_object *outputs_jobj;
+    mapstojson(&outputs_jobj,request_output_real_format);
+    json_object_object_add(msg_jobj,"request_output_real_format",outputs_jobj);
+  
+    bind_amqp();
+
+    if ( (send_msg(json_object_to_json_string(msg_jobj),"application/json") != 0) ){     
+        eres = SERVICE_FAILED;
+    }
+    close_amqp();
+    json_object_put(msg_jobj);
+    
+    init_sql(m);
+    recordServiceStatus(m);
+
+
+
+        
+#else
       int pid;
 #ifdef DEBUG
@@ -1687,6 +1686,6 @@
 	    printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
 				  SERVICE_STARTED, request_input_real_format,
-				  request_output_real_format);
-	    fflush (stdout);
+				  request_output_real_format,request->out);
+
 #ifdef RELY_ON_DB
 	    recordResponse(m,fbkp);
@@ -1705,7 +1704,5 @@
           f1 = freopen (fbkp1, "w+", stdout);
 
-	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
-	    //freeService (&s1);
-	    free (s1);
+	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet,cgi)<0){
 	    freeMaps (&m);
 	    free (m);
@@ -1718,12 +1715,12 @@
 	    freeMaps (&tmpmaps);
 	    free (tmpmaps);
-	    fflush (stdout);
 	    fflush (stderr);
 	    unhandleStatus (m);
 	    return -1;
 	  }
+      
           loadServiceAndRun (&m, s1, request_inputs,
                              &request_input_real_format,
-                             &request_output_real_format, &eres);
+                             &request_output_real_format, &eres,request->out,request->err);
 
         }
@@ -1736,7 +1733,13 @@
           eres = -1;
           errorException (m, _("Unable to run the child process properly"),
-                          "InternalError", NULL);
+                          "InternalError", NULL,request->out);
         }
-    }
+
+#endif
+
+    }
+
+
+
 
 #ifdef DEBUG
@@ -1746,7 +1749,8 @@
     outputResponse (s1, request_input_real_format,
                     request_output_real_format, request_inputs,
-                    cpid, m, eres);
-  fflush (stdout);
+                    cpid, m, eres,request->out);
+  //fflush (stdout);
   
+
   /**
    * Ensure that if error occurs when freeing memory, no signal will return
@@ -1819,7 +1823,4 @@
       free (tmps1);
     }
-
-  //freeService (&s1);
-  free (s1);
   freeMaps (&m);
   free (m);
