Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 860)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 862)
@@ -60,5 +60,6 @@
   while(cur!=NULL && cur->defaults!=NULL){
     map* mimeType=getMap(cur->defaults->content,"mimeType");
-    if(mimeType!=NULL){
+    map* useMS=getMap(cur->defaults->content,"useMapserver");
+    if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
       int geo=isGeographic(mimeType->value);
       if(geo>0){
@@ -159,5 +160,5 @@
 	free(tmp[0]);
 	//addToMap(cur->content,"internal","true");
-      }
+      }      
     }
     cur=cur->next;
@@ -332,5 +333,9 @@
   int parameters_cnt=0;
   while(input!=NULL && input->content!=NULL){
-    if(getMaps(*real_outputs,input->name)==NULL){
+    map* isInRequest=getMap(input->content,"inRequest");
+    map* minNb=getMap(input->content,"minoccurs");
+    if(getMaps(*real_outputs,input->name)==NULL &&
+       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
+	 || (minNb!=NULL && atoi(minNb->value)>0) ) ){
       parameters_cnt+=1;
       if(parameters_cnt==1)
@@ -345,5 +350,5 @@
 	  dumpMapsValuesToFiles(main_conf,&input);
 	  addToMap(input->content,"toPublish","true");
-	  addToMap(input->content,"useMapserver","true");
+	  //addToMap(input->content,"useMapserver","true");
 	}
 	if(getMap(input->content,"cache_file")!=NULL){
@@ -362,16 +367,25 @@
 	    setMapArray(input->content,"targetPath",i,targetPath);
 	    setMapArray(input->content,"localPath",i,tmp->value);
-	    addToUploadQueue(main_conf,input);
+	    map* tmp1=getMapArray(input->content,"value",i);
+	    if(tmp1!=NULL){
+	      free(tmp1->value);
+	      tmp1->value=strdup("empty");
+	    }
 	    if(i==0){
 	      parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
 	      sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
 	    }else{
+	      fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	      fflush(stderr);
 	      char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
 	      parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
 	      sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
 	      free(tmpStr);
+	      fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	      fflush(stderr);
 	    }
 	    free(targetPath);
 	  }
+	  addToUploadQueue(main_conf,input);
 	}else{
 	  // ???
@@ -396,9 +410,9 @@
 #endif
   invokeCallback(m,inputs,NULL,1,1);
+  invokeCallback(m,inputs,NULL,2,0);
   if(getMapFromMaps(m,"lenv","mapError")!=NULL){
-    invokeCallback(m,inputs,NULL,7,0);
+    invokeCallback(*main_conf,inputs,NULL,7,0);
     return -1;
   }
-  invokeCallback(m,inputs,NULL,2,0);
 #ifdef HPC_DEBUG
   fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -408,10 +422,10 @@
   // Upload data on HPC
   if(runUpload(main_conf)==false){
-    errorException (m, _("Unable to lock the file for upload!"),
+    errorException (*main_conf, _("Unable to lock the file for upload!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 #endif
-    invokeCallback(m,inputs,NULL,7,0);
+    invokeCallback(*main_conf,inputs,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -429,11 +443,9 @@
   // Add the filename to generate for every output to parameters
   input=*real_outputs;
-  // TODO: fix appendOutputParameters
-  //appendOutputParameters(input,parameters,&parameters_cnt,s,uuid,targetPathMap);
 #ifdef HPC_DEBUG
   dumpMaps(input);
 #endif
   while(input!=NULL){
-    // TODO: parse all outputs including inner outputs if required.
+    // Parse all outputs including inner outputs if required.
     if(input->child==NULL){
       // Name every files that should be produced by the service execution
@@ -453,9 +465,12 @@
       char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
       sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
+      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
+      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
       free(targetName);
       setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
-      if(strcasecmp(input->name,"wms_link")!=0&&
-	 strcasecmp(input->name,"wcs_link")!=0 &&
-	 strcasecmp(input->name,"wfs_link")!=0){
+      addToMap(input->content,"generated_url",targetUrl);
+      free(targetUrl);
+      {
 	parameters_cnt+=1;
 	if(parameters_cnt==1)
@@ -494,7 +509,12 @@
       char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
       sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
+      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
+      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
       free(targetName);
       addToMap(input->content,"generated_file",targetPath);
       addToMap(input->content,"storage",targetPath);
+      addToMap(input->content,"generated_url",targetUrl);
+      free(targetUrl);
       if(strcasecmp(input->name,"wms_link")!=0&&
 	 strcasecmp(input->name,"wcs_link")!=0 &&
@@ -623,5 +643,5 @@
     setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
     setMapInMaps(*main_conf,"lenv","status","failed");
-    errorException (m, _("There is no remote_work_path defined in your section!"),
+    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -629,5 +649,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -645,7 +665,7 @@
   free(scriptPath);
   if(copy0!=true){
-    setMapInMaps(m,"lenv","message",_("Unable to upload the script"));
-    invokeCallback(m,NULL,NULL,7,0);
-    errorException(m,_("Unable to upload the script"),"NoApplicableCode",NULL);
+    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
+    invokeCallback(*main_conf,NULL,NULL,7,0);
+    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
     return -1;
   }
@@ -688,5 +708,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -694,10 +714,9 @@
 #endif
     sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
-    errorException(m,tmpS,"NoApplicableCode",NULL);
+    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
     free(command);
     free(targetPath);
     ssh_close(*main_conf);
     removeReadLocks(main_conf);
-    sleep(1);
     return -1;
   }
@@ -728,6 +747,6 @@
   if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
     perror("bind error");
-    setMapInMaps(m,"lenv","message",_("Unable to bind socket!"));
-    errorException (m, _("Unable to bind socket!"),
+    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
+    errorException (*main_conf, _("Unable to bind socket!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -735,5 +754,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -749,5 +768,5 @@
   if (listen(fd, 5) == -1) {
     setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
-    errorException (m, _("Listen error"),
+    errorException (*main_conf, _("Listen error"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -755,5 +774,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -765,5 +784,5 @@
   if ( (cl = accept(fd, NULL, NULL)) == -1) {
     setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
-    errorException (m, _("Accept error"),
+    errorException (*main_conf, _("Accept error"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -771,5 +790,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -788,7 +807,6 @@
     while ( (rc=read(cl,buf,10)) ) {	  
       if(rc==0){
-	sleep(1);
 	setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
-	errorException (m, _("Read closed"),
+	errorException (*main_conf, _("Read closed"),
 			"InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -796,5 +814,5 @@
 	fflush(stderr);
 #endif
-	invokeCallback(m,NULL,NULL,7,0);
+	invokeCallback(*main_conf,NULL,NULL,7,0);
 	removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -806,5 +824,5 @@
 	if(rc<0){
 	  setMapInMaps(*main_conf,"lenv","message",_("Read error"));
-	  errorException (m, _("Read error"),
+	  errorException (*main_conf, _("Read error"),
 			  "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -812,5 +830,5 @@
 	  fflush(stderr);
 #endif
-	  invokeCallback(m,NULL,NULL,7,0);
+	  invokeCallback(*main_conf,NULL,NULL,7,0);
 	  removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -872,9 +890,31 @@
 		free(targetPath);
 	      }else{
-		char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
-		sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
-		setMapInMaps(m,"lenv","message",tmpStr);
-		free(tmpStr);
-		invokeCallback(m,NULL,NULL,7,0);
+                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
+                if(hpcStdErr!=NULL && ssh_fetch(*main_conf,targetPath,hpcStdErr->value,ssh_get_cnt(m))==0){
+		  struct stat f_status;
+		  int ts=stat(targetPath, &f_status);
+		  if(ts==0) {
+		    char* fcontent = NULL;
+		    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+		    FILE* f=fopen(targetPath,"rb");
+		    fread(fcontent,f_status.st_size,1,f);
+		    int fsize=f_status.st_size;
+		    fcontent[fsize]=0;
+		    fclose(f);
+		    setMapInMaps(*main_conf,"lenv","message",fcontent);
+		    free(fcontent);
+		  }else{
+		    char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
+		    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
+		    setMapInMaps(*main_conf,"lenv","message",tmpStr);
+		    free(tmpStr);
+		  }
+                }else{
+                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
+                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
+                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
+                  free(tmpStr);
+                }
+		invokeCallback(*main_conf,NULL,NULL,7,0);
 		return SERVICE_FAILED;
 	      }
@@ -882,4 +922,5 @@
 	  }else{
 	    map* generatedFile=getMap(input->content,"generated_file");
+	    map* generatedUrl=getMap(input->content,"generated_url");
 	    if(generatedFile!=NULL){
 	      char* filename=strrchr(generatedFile->value,'/');
@@ -895,4 +936,5 @@
 		maps* output=getMaps(*real_outputs,input->name);
 		setMapInMaps(output->child,"download_link","generated_file",targetPath);
+		setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
 		setMapInMaps(output->child,"download_link","storage",targetPath);
 		setMapInMaps(output->child,"download_link","useMapserver","false");
@@ -908,12 +950,9 @@
 		if(getMaps(output->child,"wcs_link")!=NULL){
 		  sprintf(serviceName,"wcs_link");
-		  setMapInMaps(output->child,serviceName,"msOgc","WCS");
-		  setMapInMaps(output->child,serviceName,"requestedMimeType","image/tiff");
-		}
-		else{
+		  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
+		}else{
 		  sprintf(serviceName,"wfs_link");
-		  setMapInMaps(output->child,serviceName,"msOgc","WFS");
-		  setMapInMaps(output->child,serviceName,"requestedMimeType","text/xml");
-		}
+		  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
+                }
 		setMapInMaps(output->child,serviceName,"storage",targetPath);
 		setMapInMaps(output->child,serviceName,"generated_file",targetPath);
@@ -958,6 +997,6 @@
       }else{
 	// Try to access remotely to the log file and return a more relevant error message
-	setMapInMaps(m,"lenv","message",_("HPC Execution failed!"));
-	errorException (m, _("HPC Execution failed!"),
+	setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
+	errorException (*main_conf, _("HPC Execution failed!"),
 			"InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -965,5 +1004,5 @@
 	fflush(stderr);
 #endif
-	invokeCallback(m,NULL,NULL,7,0);
+	invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
 	fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -971,10 +1010,9 @@
 #endif
       }
-      //free(buf);
     }
     if(hasPassed<0){
       perror("Failed to read");
       setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
-      errorException (m, _("Unable to parse the value returned by remote execution"),
+      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
 		      "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -982,5 +1020,5 @@
       fflush(stderr);
 #endif
-      invokeCallback(m,NULL,NULL,7,0);
+      invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
       fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
