00001
00032 #include "linden_common.h"
00033 #include "llapp.h"
00034 #include "llfile.h"
00035 #include "llservicebuilder.h"
00036 #include "llsd.h"
00037 #include "llsdserialize.h"
00038
00039 void LLServiceBuilder::loadServiceDefinitionsFromFile(
00040 const std::string& service_filename)
00041 {
00042 llifstream service_file(service_filename.c_str(), std::ios::binary);
00043 if(service_file.is_open())
00044 {
00045 LLSD service_data;
00046 LLSDSerialize::fromXML(service_data, service_file);
00047 service_file.close();
00048
00049 LLSD service_map = service_data["services"];
00050 for(LLSD::array_iterator array_itr = service_map.beginArray();
00051 array_itr != service_map.endArray();
00052 ++array_itr)
00053 {
00054 LLSD service_llsd = (*array_itr)["service-builder"];
00055 std::string service_name = (*array_itr)["name"].asString();
00056 createServiceDefinition(service_name, service_llsd);
00057 }
00058 llinfos << "loaded config file: " << service_filename << llendl;
00059 }
00060 else
00061 {
00062 llwarns << "unable to find config file: " << service_filename << llendl;
00063 }
00064 }
00065
00066 void LLServiceBuilder::createServiceDefinition(
00067 const std::string& service_name,
00068 LLSD& service_llsd)
00069 {
00070 if(service_llsd.isString())
00071 {
00072 mServiceMap[ service_name ] = service_llsd.asString();
00073 }
00074 else if(service_llsd.isMap())
00075 {
00076 for(LLSD::map_iterator map_itr = service_llsd.beginMap();
00077 map_itr != service_llsd.endMap();
00078 ++map_itr)
00079 {
00080 std::string compound_builder_name = service_name;
00081 compound_builder_name.append("-");
00082 compound_builder_name.append((*map_itr).first);
00083 mServiceMap[ compound_builder_name ] = (*map_itr).second.asString();
00084 }
00085 }
00086 }
00087
00088 std::string LLServiceBuilder::buildServiceURI(const std::string& service_name)
00089 {
00090 std::ostringstream service_url;
00091
00092 if(mServiceMap.find(service_name) != mServiceMap.end())
00093 {
00094
00095 LLApp* app = LLApp::instance();
00096 if(app)
00097 {
00098 LLSD base_url = app->getOption("services-base-url");
00099 service_url << base_url.asString();
00100 }
00101 service_url << mServiceMap[service_name];
00102 }
00103 else
00104 {
00105 llwarns << "Cannot find service " << service_name << llendl;
00106 }
00107 return service_url.str();
00108 }
00109
00110 std::string LLServiceBuilder::buildServiceURI(
00111 const std::string& service_name,
00112 const LLSD& option_map)
00113 {
00114 std::string service_url = buildServiceURI(service_name);
00115
00116
00117 if(!service_url.empty() && option_map.isMap())
00118 {
00119
00120 for(LLSD::map_const_iterator option_itr = option_map.beginMap();
00121 option_itr != option_map.endMap();
00122 ++option_itr)
00123 {
00124 std::string variable_name = "{$";
00125 variable_name.append((*option_itr).first);
00126 variable_name.append("}");
00127 std::string::size_type find_pos = service_url.find(variable_name);
00128 if(find_pos != std::string::npos)
00129 {
00130 service_url.replace(
00131 find_pos,
00132 variable_name.length(),
00133 (*option_itr).second.asString());
00134 continue;
00135 }
00136 variable_name.assign("{%");
00137 variable_name.append((*option_itr).first);
00138 variable_name.append("}");
00139 find_pos = service_url.find(variable_name);
00140 if(find_pos != std::string::npos)
00141 {
00142 std::string query_str = LLURI::mapToQueryString(
00143 (*option_itr).second);
00144 service_url.replace(
00145 find_pos,
00146 variable_name.length(),
00147 query_str);
00148 }
00149 }
00150 }
00151
00152 if (service_url.find('{') != std::string::npos)
00153 {
00154 llwarns << "Constructed a likely bogus service URL: " << service_url
00155 << llendl;
00156 }
00157
00158 return service_url;
00159 }