Index: branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/response_print.c
===================================================================
--- branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/response_print.c	(revision 677)
+++ branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/response_print.c	(revision 741)
@@ -93,10 +93,10 @@
  * @param m the map containing the headers informations
  */
-void printHeaders(maps* m){
+void printHeaders(maps* m,FCGX_Stream *out){
   maps *_tmp=getMaps(m,"headers");
   if(_tmp!=NULL){
     map* _tmp1=_tmp->content;
     while(_tmp1!=NULL){
-      printf("%s: %s\r\n",_tmp1->name,_tmp1->value);
+      FCGX_FPrintF(out,"%s: %s\r\n", _tmp1->name, _tmp1->value);
       _tmp1=_tmp1->next;
     }
@@ -1445,5 +1445,5 @@
  * @param outputs the outputs generated by the service
  */
-void printProcessResponse(maps* m,map* request, int pid,service* serv,const char* service,int status,maps* inputs,maps* outputs){
+void printProcessResponse(maps* m,map* request, int pid,service* serv,const char* service,int status,maps* inputs,maps* outputs,FCGX_Stream * out){
   xmlNsPtr ns,ns_ows,ns_xlink;
   xmlNodePtr nr,n,nc,nc1=NULL,nc3;
@@ -1540,5 +1540,4 @@
       hasStoredExecuteResponse=true;
     }
-
     nc = xmlNewNode(ns, BAD_CAST "Process");
     map* tmp2=getMap(serv->content,"processVersion");
@@ -1548,5 +1547,4 @@
     map* tmpI=getMapFromMaps(m,"lenv","oIdentifier");
     printDescription(nc,ns_ows,tmpI->value,serv->content,0);
-
     xmlAddChild(n,nc);
 
@@ -1695,5 +1693,5 @@
     if(lid<0){
       /* If the lock failed */
-      errorException(m,_("Lock failed."),"InternalError",NULL);
+      errorException(m,_("Lock failed."),"InternalError",NULL,out);
       xmlFreeDoc(doc);
       xmlCleanupParser();
@@ -1710,5 +1708,5 @@
 	sprintf(tmpMsg,_("Unable to create the file \"%s\" for storing the ExecuteResponse."),stored_path);
 
-	errorException(m,tmpMsg,"InternalError",NULL);
+	errorException(m,tmpMsg,"InternalError",NULL,out);
 	xmlFreeDoc(doc);
 	xmlCleanupParser();
@@ -1738,5 +1736,5 @@
 #endif
   }
-  printDocument(m,doc,pid);
+  printDocument(m,doc,pid,out);
 
   xmlCleanupParser();
@@ -1751,11 +1749,10 @@
  * @param pid the process identifier linked to a service
  */
-void printDocument(maps* m, xmlDocPtr doc,int pid){
+void printDocument(maps* m, xmlDocPtr doc,int pid,FCGX_Stream * out){
   char *encoding=getEncoding(m);
   if(pid==getpid()){
-    printHeaders(m);
-    printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
-  }
-  fflush(stdout);
+    printHeaders(m,out);
+    FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+  }
   xmlChar *xmlbuff;
   int buffersize;
@@ -1765,6 +1762,8 @@
    */
   xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
-  printf("%s",xmlbuff);
-  fflush(stdout);
+  if (out != NULL){
+    FCGX_FPrintF(out,(char *)xmlbuff);
+    FCGX_FFlush(out);
+  }
   /*
    * Free associated memory.
@@ -2099,5 +2098,5 @@
  * @param s the map containing the text,code,locator keys
  */
-void printExceptionReportResponse(maps* m,map* s){
+void printExceptionReportResponse(maps* m,map* s,FCGX_Stream * out){
   if(getMapFromMaps(m,"lenv","hasPrinted")!=NULL)
     return;
@@ -2135,20 +2134,21 @@
     if(tmpSid!=NULL){
       if( getpid()==atoi(tmpSid->value) ){
-	printHeaders(m);
-	printf("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding,exceptionCode);
-      }
+	printHeaders(m,out);
+    FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
+	  }
     }
     else{
-      printHeaders(m);
-      printf("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding,exceptionCode);
+      printHeaders(m,out);
+      FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
     }
   }else{
-    printf("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding,exceptionCode);
-  }
+    FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
+    }
   n=createExceptionReportNode(m,s,1);
   xmlDocSetRootElement(doc, n);
   xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
-  printf("%s",xmlbuff);
-  fflush(stdout);
+  FCGX_FPrintF(out,"%s",xmlbuff);
+  FCGX_FFlush(out);
+
   xmlFreeDoc(doc);
   xmlFree(xmlbuff);
@@ -2161,5 +2161,5 @@
 /**
  * Create an OWS ExceptionReport Node.
- * 
+ * FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
  * @param m the conf maps
  * @param s the map containing the text,code,locator keys
@@ -2241,5 +2241,5 @@
  * @param locator the potential locator
  */
-int errorException(maps *m, const char *message, const char *errorcode, const char *locator) 
+int errorException(maps *m, const char *message, const char *errorcode, const char *locator,FCGX_Stream * out) 
 {
   map* errormap = createMap("text", message);
@@ -2249,5 +2249,5 @@
   else
     addToMap(errormap,"locator", "NULL");
-  printExceptionReportResponse(m,errormap);
+  printExceptionReportResponse(m,errormap,out);
   freeMap(&errormap);
   free(errormap);
@@ -2267,5 +2267,5 @@
  */
 void outputResponse(service* s,maps* request_inputs,maps* request_outputs,
-		    map* request_inputs1,int cpid,maps* m,int res){
+		    map* request_inputs1,int cpid,maps* m,int res,FCGX_Stream * out){
 #ifdef DEBUG
   dumpMaps(request_inputs);
@@ -2278,13 +2278,12 @@
     asRaw=1;
   map* version=getMapFromMaps(m,"main","rversion");
-  int vid=getVersionId(version->value);
-  
-  maps* tmpSess=getMaps(m,"senv");
+   int vid=getVersionId(version->value);
+   maps* tmpSess=getMaps(m,"senv");
   if(tmpSess!=NULL){
     map *_tmp=getMapFromMaps(m,"lenv","cookie");
     char* sessId=NULL;
     if(_tmp!=NULL){
-      printf("Set-Cookie: %s; HttpOnly\r\n",_tmp->value);
-      printf("P3P: CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"\r\n");
+      FCGX_FPrintF(out,"Set-Cookie: %s; HttpOnly\r\n",_tmp->value);
+      FCGX_FPrintF(out,"P3P: CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"\r\n");
       char session_file_path[100];
       char *tmp1=strtok(_tmp->value,";");
@@ -2314,5 +2313,5 @@
       char tmpMsg[1024];
       sprintf(tmpMsg,_("Unable to create the file \"%s\" for storing the session maps."),session_file_path);
-      errorException(m,tmpMsg,"InternalError",NULL);
+      errorException(m,tmpMsg,"InternalError",NULL,out);
 
       return;
@@ -2323,5 +2322,4 @@
     }
   }
-  
   if(res==SERVICE_FAILED){
     map *lenv;
@@ -2336,19 +2334,17 @@
       sprintf(tmp0,"%s",_("Unable to run the Service. No more information was returned back by the Service."));
     }
-    errorException(m,tmp0,"InternalError",NULL);
+    errorException(m,tmp0,"InternalError",NULL,out);
     free(tmp0);
     return;
   }
-
   if(res==SERVICE_ACCEPTED && vid==1){
     map* statusInfo=createMap("Status","Accepted");
     map *usid=getMapFromMaps(m,"lenv","usid");
     addToMap(statusInfo,"JobID",usid->value);
-    printStatusInfo(m,statusInfo,"Execute");
+    printStatusInfo(m,statusInfo,"Execute",out);
     freeMap(&statusInfo);
     free(statusInfo);
     return;
   }
-
   map *tmp1=getMapFromMaps(m,"main","tmpPath");
   if(asRaw==0){
@@ -2411,5 +2407,4 @@
 	      getFileExtension(mtype != NULL ? mtype->value : NULL, file_ext, 32);
 	    }
-		
 	    file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(tmpI->name)+45)*sizeof(char));
 	    sprintf(file_name,"%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
@@ -2422,5 +2417,5 @@
 	      char tmpMsg[1024];
 	      sprintf(tmpMsg,_("Unable to create the file \"%s\" for storing the %s final result."),file_name,tmpI->name);
-	      errorException(m,tmpMsg,"InternalError",NULL);
+	      errorException(m,tmpMsg,"InternalError",NULL,out);
 	      free(file_name);
 	      free(file_path);
@@ -2428,5 +2423,4 @@
 	    }
 	    free(file_path);
-
 	    toto=getMap(tmpI->content,"value");
 	    if(strcasecmp(format,"BoundingBoxData")!=0){
@@ -2438,10 +2432,10 @@
 		  fwrite(toto->value,1,strlen(toto->value)*sizeof(char),ofile);
 	    }else{
-	      printBoundingBoxDocument(m,tmpI,ofile);
+	      printBoundingBoxDocument(m,tmpI,ofile,out);
 	    }
 	    fclose(ofile);
 
 	  }
-	  map *tmp2=getMapFromMaps(m,"main","tmpUrl");
+      map *tmp2=getMapFromMaps(m,"main","tmpUrl");
 	  map *tmp3=getMapFromMaps(m,"main","serverAddress");
 	  char *file_url;
@@ -2473,8 +2467,10 @@
     dumpMaps(m);
 #endif
+
     printProcessResponse(m,request_inputs1,cpid,
 			 s, s->name,res,  // replace serviceProvider with serviceName in stored response file name
 			 request_inputs,
-			 request_outputs);
+			 request_outputs,out);
+
   }
   else{
@@ -2492,5 +2488,5 @@
     elements* e=getElements(s->outputs,tmpI->name);
     if(e!=NULL && strcasecmp(e->format,"BoundingBoxData")==0){
-      printBoundingBoxDocument(m,tmpI,NULL);
+      printBoundingBoxDocument(m,tmpI,NULL,out);
     }else{
       map *gfile=getMap(tmpI->content,"generated_file");
@@ -2506,14 +2502,14 @@
 	char tmpMsg[1024];
 	sprintf(tmpMsg,_("Wrong RawDataOutput parameter: unable to fetch any result for the given parameter name: \"%s\"."),tmpI->name);
-	errorException(m,tmpMsg,"InvalidParameterValue","RawDataOutput");
+	errorException(m,tmpMsg,"InvalidParameterValue","RawDataOutput",out);
 	return;
       }
       map* fname=getMapFromMaps(tmpI,tmpI->name,"filename");
       if(fname!=NULL)
-	printf("Content-Disposition: attachment; filename=\"%s\"\r\n",fname->value);
+      FCGX_FPrintF(out,"Content-Disposition: attachment; filename=\"%s\"\r\n",fname->value);
       map* rs=getMapFromMaps(tmpI,tmpI->name,"size");
       if(rs!=NULL)
-	printf("Content-Length: %s\r\n",rs->value);
-      printHeaders(m);
+      FCGX_FPrintF(out,"Content-Length: %s\r\n",rs->value);
+      printHeaders(m,out);
       char mime[1024];
       map* mi=getMap(tmpI->content,"mimeType");
@@ -2524,20 +2520,21 @@
 #endif
       map* en=getMap(tmpI->content,"encoding");
-      if(mi!=NULL && en!=NULL)
-	sprintf(mime,
-		"Content-Type: %s; charset=%s\r\nStatus: 200 OK\r\n\r\n",
-		mi->value,en->value);
-      else
-	if(mi!=NULL)
-	  sprintf(mime,
-		  "Content-Type: %s; charset=UTF-8\r\nStatus: 200 OK\r\n\r\n",
-		  mi->value);
-	else
-	  sprintf(mime,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
-      printf("%s",mime);
-      if(rs!=NULL)
-	fwrite(toto->value,1,atoi(rs->value),stdout);
-      else
-	fwrite(toto->value,1,strlen(toto->value),stdout);
+      if(mi!=NULL && en!=NULL){
+        sprintf(mime,"Content-Type: %s; charset=%s\r\nStatus: 200 OK\r\n\r\n",mi->value,en->value);
+      } 
+      else {
+        if(mi!=NULL)
+            sprintf(mime,"Content-Type: %s; charset=UTF-8\r\nStatus: 200 OK\r\n\r\n",mi->value);
+        else
+            sprintf(mime,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
+      }
+      FCGX_FPrintF(out,"%s",mime);
+      if(rs!=NULL){
+        FCGX_PutStr(toto->value,atoi(rs->value),out);
+      }
+      else {
+        FCGX_PutStr(toto->value,strlen (toto->value),out);
+      }
+    FCGX_FFlush(out);
 #ifdef DEBUG
       dumpMap(toto);
@@ -2659,7 +2656,5 @@
  * @see parseBoundingBox, printBoundingBox
  */
-void printBoundingBoxDocument(maps* m,maps* boundingbox,FILE* file){
-  if(file==NULL)
-    rewind(stdout);
+void printBoundingBoxDocument(maps* m,maps* boundingbox,FILE* file,FCGX_Stream * out){
   xmlNodePtr n;
   xmlDocPtr doc;
@@ -2675,7 +2670,8 @@
       pid=atoi(tmp->value);
     if(pid==getpid()){
-      printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
-    }
-    fflush(stdout);
+      
+      FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+    }
+    FCGX_FFlush(out);
   }
 
@@ -2695,5 +2691,5 @@
   xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
   if(file==NULL)
-    printf("%s",xmlbuff);
+   FCGX_FPrintF(out,"%s",xmlbuff);
   else{
     fprintf(file,"%s",xmlbuff);
@@ -2723,6 +2719,6 @@
  * @param req the WPS requests (GetResult, GetStatus or Dismiss)
  */
-void printStatusInfo(maps* conf,map* statusInfo,char* req){
-  rewind(stdout);
+void printStatusInfo(maps* conf,map* statusInfo,char* req,FCGX_Stream * out){
+  //rewind(stdout);
   xmlNodePtr n,n1;
   xmlDocPtr doc;
@@ -2733,9 +2729,9 @@
   map *tmp;
   int pid=0;
-  printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+  
+  FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
 
   map* version=getMapFromMaps(conf,"main","rversion");
   int vid=getVersionId(version->value);
-
   doc = xmlNewDoc(BAD_CAST "1.0");
   n1=printWPSHeader(doc,conf,req,"StatusInfo",version->value,1);
@@ -2772,6 +2768,5 @@
 
   xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, encoding, 1);
-  printf("%s",xmlbuff);
-
+  FCGX_FPrintF(out,"%s",xmlbuff);
   xmlFree(xmlbuff);
   xmlFreeDoc(doc);
