Savarese Software Research Corporation
ws/service.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2010 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <ssrc/wispers/ws/service.h>
00018 
00019 __BEGIN_NS_SSRC_WSPR_WS
00020 
00021 void validate_call(const ActionConfig & action,
00022                    const call_handler_type & call_handler,
00023                    const WebServiceCall & wc,
00024                    const MessageInfo & msginfo)
00025   SSRC_DECL_THROW(std:::invalid_argument)
00026 {
00027   typedef WebServiceProtocol::parameter_map parameter_map;
00028 
00029 #ifdef WSPR_DEBUG
00030   std::cerr << "validate_call: " << action.action << " : " << action.call
00031             << std::endl;
00032 #endif
00033 
00034   if(action.requires_session && !wc.session) {
00035     throw std::invalid_argument("valid session required");
00036   }
00037 
00038   if(action.parameters) {
00039     typedef
00040       std::pair<parameter_map::const_iterator,parameter_map::const_iterator>
00041       parameter_range;
00042     parameter_range p;
00043 
00044     for(parameter_sequence::const_iterator param = action.parameters->begin(),
00045           end = action.parameters->end(); param != end; ++param)
00046     {
00047       if(param->multi_value) {
00048         p = wc.parameters.equal_range(param->name);
00049       } else {
00050         p.first = wc.parameters.find(param->name);
00051         p.second = wc.parameters.end();
00052       }
00053 
00054       if(p.first != p.second) {
00055         if(param->multi_value) {
00056           while(p.first != p.second) {
00057             if(!param->validate(p.first->second)) {
00058               throw std::invalid_argument(string("invalid parameter value: ").append(param->name).append(" = ").append(p.first->second));;
00059             }
00060 
00061             ++p.first;
00062           }
00063         } else {
00064           if(!param->validate(p.first->second)) {
00065             throw std::invalid_argument(string("invalid parameter value: ").append(param->name).append(" = ").append(p.first->second));;
00066           }
00067         }
00068       } else if(!param->optional) {
00069         throw std::invalid_argument(string("missing parameter: ").append(param->name));
00070       }
00071     }
00072   }
00073 
00074   call_handler(wc, msginfo);
00075 }
00076 
00077 ActionConfig &
00078 WebService::new_action(const Properties & action_map, const string & key)
00079   SSRC_DECL_THROW(std:::domain_error)
00080 {
00081   const Properties *p = action_map.find(key);
00082 
00083   if(p == 0) {
00084     string msg("action not found: ");
00085     throw std::domain_error(msg.append(key));
00086   }
00087 
00088   _actions.push_back(new ActionConfig(key, *p));
00089   return _actions.back();
00090 }
00091 
00092 void WebService::clear_call_handlers() {
00093   _call_handlers_one_way.clear();
00094   _call_handlers_two_way.clear();
00095 }
00096 
00097 bool
00098 WebService::set_call_handler_one_way(const string & call,
00099                                      const call_handler_type & call_handler)
00100 {
00101   return
00102     _call_handlers_one_way.insert(call_handler_map::value_type(call, call_handler)).second;
00103 }
00104 
00105 bool
00106 WebService::set_call_handler_two_way(const string & call,
00107                                      const call_handler_type & call_handler)
00108 {
00109   return
00110     _call_handlers_two_way.insert(call_handler_map::value_type(call, call_handler)).second;
00111 }
00112 
00113 bool WebService::set_action_handler(ActionConfig & action,
00114                                     const call_handler_type & call_handler)
00115 {
00116   bool handler_set = false;
00117 
00118   if(action.one_way) {
00119     if(action.requires_session || action.parameters) {
00120       handler_set = set_call_handler_one_way(action.action, boost::bind(validate_call, std::ref(action), call_handler, _1, _2));
00121     } else {
00122       handler_set = set_call_handler_one_way(action.action, call_handler);
00123     }
00124   } else {
00125     if(action.requires_session || action.parameters) {
00126       handler_set = set_call_handler_two_way(action.action, boost::bind(validate_call, std::ref(action), call_handler, _1, _2));
00127     } else {
00128       handler_set = set_call_handler_two_way(action.action, call_handler);
00129     }
00130   }
00131 
00132   return handler_set;
00133 }
00134 
00135 WebService::WebService(super::caller_type & caller) : super(caller) {
00136   super::add_service_type(protocol_traits::service_type());
00137 
00138   WISP_SERVICE_REQUEST(MessageOneWay);
00139   WISP_SERVICE_REQUEST(MessageTwoWay);
00140 }
00141 
00142 __END_NS_SSRC_WSPR_WS

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.