Changeset 967 for trunk/zoo-project/zoo-kernel/service_json.c
- Timestamp:
- Dec 18, 2020, 2:13:22 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/service_json.c
r965 r967 603 603 604 604 map* tmp=getMap(s,"code"); 605 exceptionCode=produceStatusString(m,tmp); 605 606 if(tmp!=NULL){ 606 if(strcmp(tmp->value,"OperationNotSupported")==0 ||607 strcmp(tmp->value,"NoApplicableCode")==0)608 exceptionCode="500 Not Implemented";609 else610 if(strcmp(tmp->value,"MissingParameterValue")==0 ||611 strcmp(tmp->value,"InvalidUpdateSequence")==0 ||612 strcmp(tmp->value,"OptionNotSupported")==0 ||613 strcmp(tmp->value,"VersionNegotiationFailed")==0 ||614 strcmp(tmp->value,"InvalidParameterValue")==0)615 exceptionCode="400 Bad request";616 else617 if(strcmp(tmp->value,"NotFound")==0 ||618 strcmp(tmp->value,"NoSuchProcess")==0 ||619 strcmp(tmp->value,"NoSuchJob")==0 ||620 strcmp(tmp->value,"ResultNotReady")==0)621 exceptionCode="404 Not Found";622 else623 exceptionCode="500 Internal Server Error";624 607 json_object_object_add(res,"code",json_object_new_string(tmp->value)); 625 608 } 626 609 else{ 627 exceptionCode="500 Internal Server Error";628 610 json_object_object_add(res,"code",json_object_new_string("NoApplicableCode")); 629 611 } … … 716 698 json_object* json_cinput=NULL; 717 699 if(json_object_object_get_ex(req,"value",&json_cinput)!=FALSE){ 700 output->content=createMap("value",json_object_get_string(json_cinput)); 701 } 702 else{ 718 703 json_object* json_value=NULL; 719 if(json_object_object_get_ex(json_cinput,"inlineValue",&json_value)!=FALSE) 720 output->content=createMap("value",json_object_get_string(json_value)); 721 else{ 722 if(json_object_object_get_ex(json_cinput,"href",&json_value)!=FALSE){ 723 output->content=createMap("xlink:href",json_object_get_string(json_value)); 724 int len=0; 725 int createdStr=0; 726 char *tmpStr="url"; 727 char *tmpStr1="input"; 728 if(getMaps(conf,"http_requests")==NULL){ 729 maps* tmpMaps=createMaps("http_requests"); 730 tmpMaps->content=createMap("length","1"); 731 addMapsToMaps(&conf,tmpMaps); 732 freeMaps(&tmpMaps); 733 free(tmpMaps); 734 }else{ 735 map* tmpMap=getMapFromMaps(conf,"http_requests","length"); 736 int len=atoi(tmpMap->value); 737 createdStr=1; 738 tmpStr=(char*) malloc((12)*sizeof(char)); 739 sprintf(tmpStr,"%d",len+1); 740 setMapInMaps(conf,"http_requests","length",tmpStr); 741 sprintf(tmpStr,"url_%d",len); 742 tmpStr1=(char*) malloc((14)*sizeof(char)); 743 sprintf(tmpStr1,"input_%d",len); 744 } 745 setMapInMaps(conf,"http_requests",tmpStr,json_object_get_string(json_value)); 746 setMapInMaps(conf,"http_requests",tmpStr1,name); 747 if(createdStr>0){ 748 free(tmpStr); 749 free(tmpStr1); 750 } 704 if(json_object_object_get_ex(req,"href",&json_value)!=FALSE){ 705 output->content=createMap("xlink:href",json_object_get_string(json_value)); 706 int len=0; 707 int createdStr=0; 708 char *tmpStr="url"; 709 char *tmpStr1="input"; 710 if(getMaps(conf,"http_requests")==NULL){ 711 maps* tmpMaps=createMaps("http_requests"); 712 tmpMaps->content=createMap("length","1"); 713 addMapsToMaps(&conf,tmpMaps); 714 freeMaps(&tmpMaps); 715 free(tmpMaps); 716 }else{ 717 map* tmpMap=getMapFromMaps(conf,"http_requests","length"); 718 int len=atoi(tmpMap->value); 719 createdStr=1; 720 tmpStr=(char*) malloc((12)*sizeof(char)); 721 sprintf(tmpStr,"%d",len+1); 722 setMapInMaps(conf,"http_requests","length",tmpStr); 723 sprintf(tmpStr,"url_%d",len); 724 tmpStr1=(char*) malloc((14)*sizeof(char)); 725 sprintf(tmpStr1,"input_%d",len); 726 } 727 setMapInMaps(conf,"http_requests",tmpStr,json_object_get_string(json_value)); 728 setMapInMaps(conf,"http_requests",tmpStr1,name); 729 if(createdStr>0){ 730 free(tmpStr); 731 free(tmpStr1); 751 732 } 752 733 } … … 807 788 808 789 /** 809 * Parse inputs / outputs 790 * Parse a single input / output entity 791 * 792 * @param conf the maps containing the settings of the main.cfg file 793 * @param ioElements the elements extracted from the zcfg 794 * @param ioMaps the produced maps containing inputs (or outputs) 795 * @param ioType the char* set to inputs or outputs 796 * @param key char* the input/output name 797 * @param val json_object pointing to the input/output 798 * @param cMaps the outputed maps containing input (or output) metedata 799 */ 800 void _parseJIOSingle(maps* conf,elements* cio, maps** ioMaps,const char* ioType,const char* key,json_object* val,maps* cMaps){ 801 json_object* json_cinput; 802 if(json_object_object_get_ex(val,"dataType",&json_cinput)!=FALSE){ 803 parseJLiteral(conf,val,cio,cMaps); 804 } else if(json_object_object_get_ex(val,"format",&json_cinput)!=FALSE){ 805 parseJComplex(conf,val,cio,cMaps,key); 806 } else if(json_object_object_get_ex(val,"bbox",&json_cinput)!=FALSE){ 807 parseJBoundingBox(conf,val,cio,cMaps); 808 }// else error! 809 else{ 810 if(json_object_object_get_ex(val,"value",&json_cinput)!=FALSE){ 811 map* error=createMap("code","BadRequest"); 812 char tmpS[1024]; 813 sprintf(tmpS,_("Missing input for %s"),cio->name); 814 addToMap(error,"message",tmpS); 815 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 816 printExceptionReportResponseJ(conf,error); 817 return; 818 }else{ 819 if(json_object_get_type(json_cinput)==json_type_string){ 820 parseJLiteral(conf,val,cio,cMaps); 821 }else if(json_object_get_type(json_cinput)==json_type_object){ 822 json_object* json_ccinput=NULL; 823 if(json_object_object_get_ex(json_cinput,"bbox",&json_ccinput)!=FALSE){ 824 parseJComplex(conf,val,cio,cMaps,key); 825 } 826 else{ 827 parseJBoundingBox(conf,val,cio,cMaps); 828 } 829 }else{ 830 if(strcmp(ioType,"input")==0){ 831 map* error=createMap("code","BadRequest"); 832 char tmpS1[1024]; 833 sprintf(tmpS1,_("Issue with input %s"),cio->name); 834 addToMap(error,"message",tmpS1); 835 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 836 printExceptionReportResponseJ(conf,error); 837 return; 838 } 839 } 840 } 841 } 842 } 843 844 /** 845 * Parse a single input / output entity that can be an array 846 * 847 * @param conf the maps containing the settings of the main.cfg file 848 * @param ioElements the elements extracted from the zcfg 849 * @param ioMaps the produced maps containing inputs (or outputs) 850 * @param ioType the char* set to inputs or outputs 851 * @param key char* the input/output name 852 * @param val json_object pointing to the input/output 853 */ 854 void parseJIOSingle(maps* conf,elements* ioElements, maps** ioMaps,const char* ioType,const char* key,json_object* val){ 855 maps *cMaps=NULL; 856 cMaps=createMaps(key); 857 elements* cio=getElements(ioElements,key); 858 if(json_object_is_type(val,json_type_array)){ 859 int i=0; 860 size_t len=json_object_array_length(val); 861 for(i=0;i<len;i++){ 862 json_object* json_current_io=json_object_array_get_idx(val,i); 863 maps* pmsExtra=createMaps(key); 864 _parseJIOSingle(conf,cio,ioMaps,ioType,key,json_current_io,pmsExtra); 865 map* pmCtx=pmsExtra->content; 866 while(pmCtx!=NULL){ 867 if(cMaps->content==NULL) 868 cMaps->content=createMap(pmCtx->name,pmCtx->value); 869 else 870 setMapArray(cMaps->content,pmCtx->name,i,pmCtx->value); 871 pmCtx=pmCtx->next; 872 } 873 freeMaps(&pmsExtra); 874 free(pmsExtra); 875 } 876 }else{ 877 _parseJIOSingle(conf,cio,ioMaps,ioType,key,val,cMaps); 878 } 879 880 881 if(strcmp(ioType,"outputs")==0){ 882 // Outputs 883 json_object* json_cinput; 884 if(json_object_object_get_ex(val,"transmissionMode",&json_cinput)!=FALSE){ 885 if(cMaps->content==NULL) 886 cMaps->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 887 else 888 addToMap(cMaps->content,"transmissionMode",json_object_get_string(json_cinput)); 889 } 890 if(json_object_object_get_ex(val,"format",&json_cinput)!=FALSE){ 891 json_object_object_foreach(json_cinput, key1, val1) { 892 if(cMaps->content==NULL) 893 cMaps->content=createMap(key1,json_object_get_string(val1)); 894 else 895 addToMap(cMaps->content,key1,json_object_get_string(val1)); 896 } 897 } 898 } 899 900 addToMap(cMaps->content,"inRequest","true"); 901 if (ioMaps == NULL) 902 *ioMaps = dupMaps(&cMaps); 903 else 904 addMapsToMaps (ioMaps, cMaps); 905 906 freeMaps(&cMaps); 907 free(cMaps); 908 } 909 910 /** 911 * Parse all the inputs / outputs entities 912 * 810 913 * @param conf the maps containing the settings of the main.cfg file 811 914 * @param req json_object pointing to the input/output … … 816 919 void parseJIO(maps* conf, json_object* req, elements* ioElements, maps** ioMaps,const char* ioType){ 817 920 json_object* json_io=NULL; 818 if(json_object_object_get_ex(req,ioType,&json_io)!=FALSE){ 921 if(json_object_object_get_ex(req,ioType,&json_io)!=FALSE){ 819 922 json_object* json_current_io=NULL; 820 size_t len=json_object_array_length(json_io); 821 for(int i=0;i<len;i++){ 822 maps *cMaps=NULL; 823 json_current_io=json_object_array_get_idx(json_io,i); 824 json_object* cname=NULL; 825 if(json_object_object_get_ex(json_current_io,"id",&cname)!=FALSE){ 826 cMaps=createMaps(json_object_get_string(cname)); 827 elements* cio=getElements(ioElements,json_object_get_string(cname)); 828 json_object* json_input; 829 json_object* json_cinput; 830 if(json_object_object_get_ex(json_current_io,"input",&json_input)!=FALSE){ 831 if(json_object_object_get_ex(json_input,"dataType",&json_cinput)!=FALSE){ 832 parseJLiteral(conf,json_input,cio,cMaps); 833 } else if(json_object_object_get_ex(json_input,"format",&json_cinput)!=FALSE){ 834 parseJComplex(conf,json_input,cio,cMaps,json_object_get_string(cname)); 835 } else if(json_object_object_get_ex(json_input,"bbox",&json_cinput)!=FALSE){ 836 parseJBoundingBox(conf,json_input,cio,cMaps); 837 }// else error! 838 else{ 839 if(json_object_object_get_ex(json_input,"value",&json_cinput)!=FALSE){ 840 map* error=createMap("code","BadRequest"); 841 char tmpS[1024]; 842 sprintf(tmpS,_("Missing input for %s"),ioElements->name); 843 addToMap(error,"message",tmpS); 844 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 845 printExceptionReportResponseJ(conf,error); 846 return; 847 }else{ 848 if(json_object_get_type(json_cinput)==json_type_string){ 849 parseJLiteral(conf,json_input,cio,cMaps); 850 }else if(json_object_get_type(json_cinput)==json_type_object){ 851 json_object* json_ccinput=NULL; 852 if(json_object_object_get_ex(json_cinput,"bbox",&json_ccinput)!=FALSE){ 853 parseJComplex(conf,json_input,cio,cMaps,json_object_get_string(cname)); 854 } 855 else{ 856 parseJBoundingBox(conf,json_input,cio,cMaps); 857 } 858 }else{ 859 if(strcmp(ioType,"input")==0){ 860 map* error=createMap("code","BadRequest"); 861 char tmpS1[1024]; 862 sprintf(tmpS1,_("Issue with input %s"),ioElements->name); 863 addToMap(error,"message",tmpS1); 864 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 865 printExceptionReportResponseJ(conf,error); 866 return; 867 } 868 } 869 } 870 } 871 }else{ 872 if(strcmp(ioType,"input")==0){ 873 map* error=createMap("code","BadRequest"); 874 char tmpS1[1024]; 875 sprintf(tmpS1,_("Missing input for %s"),ioElements->name); 876 addToMap(error,"message",tmpS1); 877 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 878 printExceptionReportResponseJ(conf,error); 879 return; 880 }else{ 881 // Outputs 882 if(json_object_object_get_ex(json_current_io,"transmissionMode",&json_cinput)!=FALSE){ 883 if(cMaps->content==NULL) 884 cMaps->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 885 else 886 addToMap(cMaps->content,"transmissionMode",json_object_get_string(json_cinput)); 887 } 888 if(json_object_object_get_ex(json_current_io,"format",&json_cinput)!=FALSE){ 889 json_object_object_foreach(json_cinput, key, val) { 890 if(cMaps->content==NULL) 891 cMaps->content=createMap(key,json_object_get_string(val)); 892 else 893 addToMap(cMaps->content,key,json_object_get_string(val)); 894 } 895 } 896 } 923 if(json_object_is_type(json_io,json_type_array)){ 924 size_t len=json_object_array_length(json_io); 925 for(int i=0;i<len;i++){ 926 maps *cMaps=NULL; 927 json_current_io=json_object_array_get_idx(json_io,i); 928 json_object* cname=NULL; 929 if(json_object_object_get_ex(json_current_io,"id",&cname)!=FALSE) { 930 json_object* json_input=NULL; 931 if(json_object_object_get_ex(json_current_io,"input",&json_input)!=FALSE) { 932 parseJIOSingle(conf,ioElements,ioMaps,ioType,json_object_get_string(cname),json_input); 933 }else 934 parseJIOSingle(conf,ioElements,ioMaps,ioType,json_object_get_string(cname),json_current_io); 897 935 } 898 936 } 899 addToMap(cMaps->content,"inRequest","true"); 900 if (ioMaps == NULL) 901 *ioMaps = dupMaps(&cMaps); 902 else 903 addMapsToMaps (ioMaps, cMaps); 937 }else{ 938 json_object_object_foreach(json_io, key, val) { 939 parseJIOSingle(conf,ioElements,ioMaps,ioType,key,val); 940 } 904 941 } 905 942 } … … 924 961 maps* in=*inputs; 925 962 parseJIO(conf,req,s->inputs,inputs,"inputs"); 963 parseJIO(conf,req,s->outputs,outputs,"outputs"); 926 964 927 965 if(parsed==0){ 928 json_io=NULL;929 if(json_object_object_get_ex(req,"outputs",&json_io)!=FALSE){930 parseJIO(conf,req,s->outputs,outputs,"outputs");931 }932 966 json_io=NULL; 933 967 if(json_object_object_get_ex(req,"mode",&json_io)!=FALSE){ … … 967 1001 } 968 1002 1003 /** 1004 * Print the jobs status info 1005 * cf. 1006 * 1007 * @param conf the maps containing the settings of the main.cfg file 1008 * @return the JSON object pointer to the jobs list 1009 */ 1010 json_object* printJobStatus(maps* pmsConf,char* pcJobId){ 1011 json_object* pjoRes=NULL; 1012 runGetStatus(pmsConf,pcJobId,"GetStatus"); 1013 map* pmError=getMapFromMaps(pmsConf,"lenv","error"); 1014 if(pmError!=NULL && strncasecmp(pmError->value,"true",4)==0){ 1015 printExceptionReportResponseJ(pmsConf,getMapFromMaps(pmsConf,"lenv","code")); 1016 return NULL; 1017 }else{ 1018 map* pmStatus=getMapFromMaps(pmsConf,"lenv","status"); 1019 setMapInMaps(pmsConf,"lenv","gs_location","false"); 1020 setMapInMaps(pmsConf,"lenv","gs_usid",pcJobId); 1021 if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Failed",6)==0) 1022 pjoRes=createStatus(pmsConf,SERVICE_FAILED); 1023 else 1024 if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Succeeded",9)==0) 1025 pjoRes=createStatus(pmsConf,SERVICE_SUCCEEDED); 1026 else 1027 if(pmStatus!=NULL && strncasecmp(pmStatus->value,"Running",7)==0){ 1028 map* tmpMap=getMapFromMaps(pmsConf,"lenv","Message"); 1029 if(tmpMap!=NULL) 1030 setMapInMaps(pmsConf,"lenv","gs_message",tmpMap->value); 1031 pjoRes=createStatus(pmsConf,SERVICE_STARTED); 1032 } 1033 else 1034 pjoRes=createStatus(pmsConf,SERVICE_FAILED); 1035 } 1036 return pjoRes; 1037 } 1038 969 1039 /** 970 1040 * Print the jobs list … … 985 1055 char* extn = strstr(dp->d_name, ".json"); 986 1056 if(extn!=NULL){ 987 json_object* cjob=json_object_new_object();988 1057 char* tmpStr=zStrdup(dp->d_name); 989 1058 tmpStr[strlen(dp->d_name)-5]=0; 1059 json_object* cjob=json_object_new_object(); 990 1060 json_object_object_add(cjob,"id",json_object_new_string(tmpStr)); 991 char *tmps1=(char*)malloc((strlen(cpath)+strlen(dp->d_name)+2)*sizeof(char)); 992 sprintf (tmps1, "%s/%s", cpath, dp->d_name); 993 FILE* cdat=fopen(tmps1,"rb"); 994 if(cdat!=NULL){ 995 zStatStruct f_status; 996 int s=zStat(tmps1, &f_status); 997 char* mystring=(char*)malloc((f_status.st_size+1)*sizeof(char)); 998 fread(mystring,1,f_status.st_size,cdat); 999 mystring[f_status.st_size]=0; 1000 json_object *jobj = NULL; 1001 int slen = 0; 1002 enum json_tokener_error jerr; 1003 struct json_tokener* tok=json_tokener_new(); 1004 do { 1005 slen = strlen(mystring); 1006 jobj = json_tokener_parse_ex(tok, mystring, slen); 1007 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 1008 if (jerr != json_tokener_success) { 1009 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 1010 return NULL; 1011 } 1012 if (tok->char_offset < slen){ 1013 return NULL; 1014 } 1061 json_object *jobj=printJobStatus(conf,tmpStr); 1062 if(jobj!=NULL) 1015 1063 json_object_object_add(cjob,"infos",jobj); 1016 free(mystring);1017 fclose(cdat);1018 }1019 1064 free(tmpStr); 1020 1065 json_object_array_add(res,cjob); … … 1036 1081 */ 1037 1082 json_object* printJResult(maps* conf,service* s,maps* result,int res){ 1038 json_object* eres1=json_object_new_object();1039 1083 if(res==SERVICE_FAILED){ 1040 1084 char* pacTmp=produceErrorMessage(conf); … … 1051 1095 if(getMapFromMaps(conf,"lenv","no-headers")==NULL) 1052 1096 printHeaders(conf); 1053 json_object* eres=json_object_new_array(); 1054 maps* resu=result; 1055 int itn=0; 1056 while(resu!=NULL){ 1057 json_object* res1=json_object_new_object(); 1058 json_object_object_add(res1,"id",json_object_new_string(resu->name)); 1059 map* tmpMap=getMap(resu->content,"mimeType"); 1060 json_object* res3=json_object_new_object(); 1061 if(tmpMap!=NULL) 1062 json_object_object_add(res3,"mimeType",json_object_new_string(tmpMap->value)); 1063 if((tmpMap=getMap(resu->content,"value"))!=NULL || 1064 (getMap(resu->content,"generated_file"))!=NULL){ 1065 json_object* res2=json_object_new_object(); 1066 map* tmpMap1=NULL; 1067 if((tmpMap1=getMap(resu->content,"transmissionMode"))!=NULL) { 1068 if(strcmp(tmpMap1->value,"value")==0) { 1069 map* tmpMap2=getMap(resu->content,"mimeType"); 1070 if(tmpMap2!=NULL && strstr(tmpMap2->value,"json")!=NULL){ 1071 json_object *jobj = NULL; 1072 int slen = 0; 1073 enum json_tokener_error jerr; 1074 struct json_tokener* tok=json_tokener_new(); 1075 do { 1076 slen = strlen(tmpMap->value); 1077 jobj = json_tokener_parse_ex(tok, tmpMap->value, slen); 1078 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 1079 if (jerr != json_tokener_success) { 1080 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 1081 return eres1; 1097 map* pmMode=getMapFromMaps(conf,"request","response"); 1098 if(pmMode!=NULL && strncasecmp(pmMode->value,"raw",3)==0){ 1099 maps* resu=result; 1100 printRawdataOutput(conf,resu); 1101 map* pmError=getMapFromMaps(conf,"lenv","error"); 1102 if(pmError!=NULL && strncasecmp(pmError->value,"true",4)==0){ 1103 printExceptionReportResponseJ(conf,pmError); 1104 } 1105 return NULL; 1106 }else{ 1107 json_object* eres1=json_object_new_object(); 1108 map* pmIOAsArray=getMapFromMaps(conf,"openapi","io_as_array"); 1109 json_object* eres; 1110 if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0) 1111 eres=json_object_new_array(); 1112 else 1113 eres=json_object_new_object(); 1114 maps* resu=result; 1115 int itn=0; 1116 while(resu!=NULL){ 1117 json_object* res1=json_object_new_object(); 1118 if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0) 1119 json_object_object_add(res1,"id",json_object_new_string(resu->name)); 1120 map* tmpMap=getMap(resu->content,"mimeType"); 1121 json_object* res3=json_object_new_object(); 1122 if(tmpMap!=NULL) 1123 json_object_object_add(res3,"mimeType",json_object_new_string(tmpMap->value)); 1124 else{ 1125 json_object_object_add(res3,"mimeType",json_object_new_string("text/plain")); 1126 map* pmDataType=getMap(resu->content,"dataType"); 1127 if(pmDataType!=NULL){ 1128 json_object* pjoTmp3=json_object_new_object(); 1129 json_object_object_add(pjoTmp3,"name",json_object_new_string(pmDataType->value)); 1130 json_object_object_add(res1,"dataType",pjoTmp3); 1131 } 1132 } 1133 if((tmpMap=getMap(resu->content,"value"))!=NULL || 1134 (getMap(resu->content,"generated_file"))!=NULL){ 1135 //json_object* res2=json_object_new_object(); 1136 map* tmpMap1=NULL; 1137 if((tmpMap1=getMap(resu->content,"transmissionMode"))!=NULL) { 1138 if(strcmp(tmpMap1->value,"value")==0) { 1139 map* tmpMap2=getMap(resu->content,"mimeType"); 1140 if(tmpMap2!=NULL && strstr(tmpMap2->value,"json")!=NULL){ 1141 json_object *jobj = NULL; 1142 int slen = 0; 1143 enum json_tokener_error jerr; 1144 struct json_tokener* tok=json_tokener_new(); 1145 do { 1146 slen = strlen(tmpMap->value); 1147 jobj = json_tokener_parse_ex(tok, tmpMap->value, slen); 1148 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 1149 if (jerr != json_tokener_success) { 1150 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 1151 return eres1; 1152 } 1153 if (tok->char_offset < slen){ 1154 return eres1; 1155 } 1156 json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); 1157 json_object_object_add(res1,"value",jobj); 1158 }else{ 1159 map* tmp1=getMapFromMaps(conf,"main","tmpPath"); 1160 map *gfile=getMap(resu->content,"generated_file"); 1161 if(gfile!=NULL){ 1162 gfile=getMap(resu->content,"expected_generated_file"); 1163 if(gfile==NULL){ 1164 gfile=getMap(resu->content,"generated_file"); 1165 } 1166 FILE* pfData=fopen(gfile->value,"rb"); 1167 if(pfData!=NULL){ 1168 zStatStruct f_status; 1169 int s=zStat(gfile->value, &f_status); 1170 char* pcaTmp=(char*)malloc((f_status.st_size+1)*sizeof(char)); 1171 fread(pcaTmp,1,f_status.st_size,pfData); 1172 pcaTmp[f_status.st_size]=0; 1173 fclose(pfData); 1174 json_object_object_add(res1,"value",json_object_new_string(base64(pcaTmp,f_status.st_size))); 1175 json_object_object_add(res3,"encoding",json_object_new_string("base64")); 1176 free(pcaTmp); 1177 } 1178 }else{ 1179 json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); 1180 json_object_object_add(res1,"value",json_object_new_string(tmpMap->value)); 1181 } 1082 1182 } 1083 if (tok->char_offset < slen){ 1084 return eres1; 1085 } 1086 json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); 1087 json_object_object_add(res2,"inlineValue",jobj); 1088 }else{ 1089 map* tmp1=getMapFromMaps(conf,"main","tmpPath"); 1090 map *gfile=getMap(resu->content,"generated_file"); 1091 if(gfile!=NULL){ 1092 gfile=getMap(resu->content,"expected_generated_file"); 1093 if(gfile==NULL){ 1094 gfile=getMap(resu->content,"generated_file"); 1095 } 1096 FILE* pfData=fopen(gfile->value,"rb"); 1097 if(pfData!=NULL){ 1098 zStatStruct f_status; 1099 int s=zStat(gfile->value, &f_status); 1100 char* pcaTmp=(char*)malloc((f_status.st_size+1)*sizeof(char)); 1101 fread(pcaTmp,1,f_status.st_size,pfData); 1102 pcaTmp[f_status.st_size]=0; 1103 fclose(pfData); 1104 json_object_object_add(res2,"inlineValue",json_object_new_string(base64(pcaTmp,f_status.st_size))); 1105 json_object_object_add(res3,"encoding",json_object_new_string("base64")); 1106 free(pcaTmp); 1107 } 1108 }else{ 1109 json_object_object_add(res3,"encoding",json_object_new_string("utf-8")); 1110 json_object_object_add(res2,"inlineValue",json_object_new_string(tmpMap->value)); 1183 } 1184 else{ 1185 char *pcaFileUrl=produceFileUrl(s,conf,resu,NULL,itn); 1186 itn++; 1187 if(pcaFileUrl!=NULL){ 1188 json_object_object_add(res1,"href", 1189 json_object_new_string(pcaFileUrl)); 1190 free(pcaFileUrl); 1111 1191 } 1112 1192 } 1113 1193 } 1114 else{ 1115 // Create file for reference data if not existing 1116 map *gfile=getMap(resu->content,"generated_file"); 1117 char *file_name=NULL; 1118 char *file_path=NULL; 1119 char *file_url=NULL; 1120 map *tmp1=getMapFromMaps(conf,"main","tmpPath"); 1121 if(gfile!=NULL){ 1122 gfile=getMap(resu->content,"expected_generated_file"); 1123 if(gfile==NULL){ 1124 gfile=getMap(resu->content,"generated_file"); 1125 } 1126 if(strstr(gfile->value,tmp1->value)!=NULL) 1127 file_name=zStrdup(strstr(gfile->value,tmp1->value)+strlen(tmp1->value)); 1128 }/*else*/ 1129 { 1130 // Create file for reference data 1131 map *tmpUrl=getMapFromMaps(conf,"main","tmpUrl"); 1132 map *usid=getMapFromMaps(conf,"lenv","usid"); 1133 map *ext=getMap(resu->content,"extension"); 1134 if(gfile==NULL){ 1135 char file_ext[32]; 1136 if( ext != NULL && ext->value != NULL) { 1137 strncpy(file_ext, ext->value, 32); 1138 } 1139 else { 1140 // Obtain default file extension (see mimetypes.h). 1141 // If the MIME type is not recognized, txt is used as the default extension 1142 map* mtype=getMap(resu->content,"mimeType"); 1143 getFileExtension(mtype != NULL ? mtype->value : NULL, file_ext, 32); 1144 } 1145 if(file_name!=NULL) 1146 free(file_name); 1147 1148 file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(resu->name)+45)*sizeof(char)); 1149 sprintf(file_name,"ZOO_DATA_%s_%s_%s_%d.%s",s->name,resu->name,usid->value,itn,file_ext); 1150 1151 file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char)); 1152 sprintf(file_path,"%s/%s",tmp1->value,file_name); 1153 }else{ 1154 file_path=(char*)malloc((strlen(gfile->value)+1)*sizeof(char)); 1155 sprintf(file_path,"%s",gfile->value); 1156 } 1157 1158 1159 itn++; 1160 1161 file_url=(char*)malloc((strlen(tmpUrl->value)+ 1162 strlen(file_name)+2)*sizeof(char)); 1163 sprintf(file_url,"%s/%s",tmpUrl->value,file_name); 1164 1165 if(gfile==NULL){ 1166 FILE *ofile=fopen(file_path,"wb"); 1167 if(ofile==NULL){ 1168 char tmpMsg[1024]; 1169 sprintf(tmpMsg, 1170 _("Unable to create the file \"%s\" for storing the %s final result."), 1171 file_name,resu->name); 1172 map* error=createMap("code","InternalError"); 1173 addToMap(error,"message",tmpMsg); 1174 printExceptionReportResponseJ(conf,error); 1175 free(file_name); 1176 free(file_path); 1177 return NULL; 1178 } 1179 1180 map* toto=getMap(resu->content,"value"); 1181 if(toto==NULL){ 1182 char tmpMsg[1024]; 1183 sprintf(tmpMsg, 1184 _("No value found for the requested output %s."), 1185 resu->name); 1186 map* error=createMap("code","InternalError"); 1187 addToMap(error,"message",tmpMsg); 1188 printExceptionReportResponseJ(conf,error); 1189 fclose(ofile); 1190 free(file_name); 1191 free(file_path); 1192 return NULL; 1193 } 1194 map* size=getMap(resu->content,"size"); 1195 if(size!=NULL && toto!=NULL) 1196 fwrite(toto->value,1,(atoi(size->value))*sizeof(char),ofile); 1197 else 1198 if(toto!=NULL && toto->value!=NULL) 1199 fwrite(toto->value,1, 1200 strlen(toto->value)*sizeof(char),ofile); 1201 fclose(ofile); 1202 } 1203 json_object_object_add(res2,"href", 1204 json_object_new_string(file_url)); 1205 free(file_url); 1206 free(file_name); 1207 free(file_path); 1208 1209 } 1210 1211 } 1212 } 1213 json_object_object_add(res1,"value",res2); 1214 json_object_object_add(res1,"format",res3); 1215 json_object_array_add(eres,res1); 1216 } 1217 resu=resu->next; 1218 } 1219 json_object_object_add(eres1,"outputs",eres); 1220 const char* jsonStr = 1221 json_object_to_json_string_ext(eres1,JSON_C_TO_STRING_PLAIN); 1222 map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); 1223 map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); 1224 map *sessId = getMapFromMaps (conf, "lenv", "usid"); 1225 char *pacTmp=(char*)malloc((strlen(tmpPath->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char)); 1226 sprintf(pacTmp,"%s/%s_%s.json", 1227 tmpPath->value,cIdentifier->value,sessId->value); 1228 zStatStruct zsFStatus; 1229 int iS=zStat(pacTmp, &zsFStatus); 1230 if(iS==0 && zsFStatus.st_size>0){ 1231 map* tmpPath1 = getMapFromMaps (conf, "main", "tmpUrl"); 1232 char* pacTmpUrl=(char*)malloc((strlen(tmpPath1->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char));; 1233 sprintf(pacTmpUrl,"%s/%s_%s.json",tmpPath1->value, 1234 cIdentifier->value,sessId->value); 1235 if(getMapFromMaps(conf,"lenv","gs_location")==NULL) 1236 setMapInMaps(conf,"headers","Location",pacTmpUrl); 1237 free(pacTmpUrl); 1238 } 1239 free(pacTmp); 1240 if(res==3){ 1241 map* mode=getMapFromMaps(conf,"request","mode"); 1242 if(mode!=NULL && strncasecmp(mode->value,"async",5)==0) 1243 setMapInMaps(conf,"headers","Status","201 Created"); 1244 else 1245 setMapInMaps(conf,"headers","Status","200 Ok"); 1246 } 1247 else{ 1248 setMapInMaps(conf,"headers","Status","500 Issue running your service"); 1249 } 1250 return eres1; 1194 //json_object_object_add(res1,"value",res2); 1195 json_object_object_add(res1,"format",res3); 1196 if(pmIOAsArray!=NULL && strncasecmp(pmIOAsArray->value,"true",4)==0){ 1197 json_object_array_add(eres,res1); 1198 }else 1199 json_object_object_add(eres,resu->name,res1); 1200 } 1201 resu=resu->next; 1202 } 1203 json_object_object_add(eres1,"outputs",eres); 1204 const char* jsonStr = 1205 json_object_to_json_string_ext(eres1,JSON_C_TO_STRING_PLAIN); 1206 map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); 1207 map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); 1208 map *sessId = getMapFromMaps (conf, "lenv", "usid"); 1209 char *pacTmp=(char*)malloc((strlen(tmpPath->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char)); 1210 sprintf(pacTmp,"%s/%s_%s.json", 1211 tmpPath->value,cIdentifier->value,sessId->value); 1212 zStatStruct zsFStatus; 1213 int iS=zStat(pacTmp, &zsFStatus); 1214 if(iS==0 && zsFStatus.st_size>0){ 1215 map* tmpPath1 = getMapFromMaps (conf, "main", "tmpUrl"); 1216 char* pacTmpUrl=(char*)malloc((strlen(tmpPath1->value)+strlen(cIdentifier->value)+strlen(sessId->value)+8)*sizeof(char));; 1217 sprintf(pacTmpUrl,"%s/%s_%s.json",tmpPath1->value, 1218 cIdentifier->value,sessId->value); 1219 if(getMapFromMaps(conf,"lenv","gs_location")==NULL) 1220 setMapInMaps(conf,"headers","Location",pacTmpUrl); 1221 free(pacTmpUrl); 1222 } 1223 free(pacTmp); 1224 if(res==3){ 1225 map* mode=getMapFromMaps(conf,"request","mode"); 1226 if(mode!=NULL && strncasecmp(mode->value,"async",5)==0) 1227 setMapInMaps(conf,"headers","Status","201 Created"); 1228 else 1229 setMapInMaps(conf,"headers","Status","200 Ok"); 1230 } 1231 else{ 1232 setMapInMaps(conf,"headers","Status","500 Issue running your service"); 1233 } 1234 return eres1; 1235 } 1251 1236 } 1252 1237 … … 1338 1323 } 1339 1324 tmp1=(char*) realloc(tmp1,(strlen(tmpPath->value)+ 1340 strlen(cIdentifier->value)+1341 strlen(sessId->value)+21)*sizeof(char));1325 strlen(cIdentifier->value)+ 1326 strlen(sessId->value)+21)*sizeof(char)); 1342 1327 int needResult=0; 1343 1328 char *message, *rstatus; … … 1652 1637 json_object_object_add(cc,"$ref",json_object_new_string(pmSchema->value)); 1653 1638 if(useContent!=NULL && strncasecmp(useContent->value,"true",4)!=0){ 1654 1639 json_object_object_add(res,code,cc); 1655 1640 }else{ 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1641 json_object *cc0=json_object_new_object(); 1642 if(pmSchema!=NULL) 1643 json_object_object_add(cc0,"schema",cc); 1644 json_object *cc1=json_object_new_object(); 1645 if(pmType!=NULL) 1646 json_object_object_add(cc1,pmType->value,cc0); 1647 else 1648 json_object_object_add(cc1,"application/json",cc0); 1649 json_object *cc2=json_object_new_object(); 1650 json_object_object_add(cc2,"content",cc1); 1651 json_object_object_add(cc2,"description",json_object_new_string(msg)); 1652 json_object_object_add(res,code,cc2); 1668 1653 } 1669 1654 }
Note: See TracChangeset
for help on using the changeset viewer.