Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2010 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <ssrc/wispers/ws/service.h>
18 : :
19 : : __BEGIN_NS_SSRC_WSPR_WS
20 : :
21 : 0 : void validate_call(const ActionConfig & action,
22 : : const call_handler_type & call_handler,
23 : : const WebServiceCall & wc,
24 : : const MessageInfo & msginfo)
25 : : SSRC_DECL_THROW(std:::invalid_argument)
26 : : {
27 : : typedef WebServiceProtocol::parameter_map parameter_map;
28 : :
29 : : #ifdef WSPR_DEBUG
30 : 0 : std::cerr << "validate_call: " << action.action << " : " << action.call
31 : 0 : << std::endl;
32 : : #endif
33 : :
34 [ # # # # : 0 : if(action.requires_session && !wc.session) {
# # ]
35 [ # # # # ]: 0 : throw std::invalid_argument("valid session required");
36 : : }
37 : :
38 [ # # ]: 0 : if(action.parameters) {
39 : : typedef
40 : : std::pair<parameter_map::const_iterator,parameter_map::const_iterator>
41 : : parameter_range;
42 : 0 : parameter_range p;
43 : :
44 [ # # ]: 0 : for(parameter_sequence::const_iterator param = action.parameters->begin(),
45 : 0 : end = action.parameters->end(); param != end; ++param)
46 : : {
47 [ # # ]: 0 : if(param->multi_value) {
48 : 0 : p = wc.parameters.equal_range(param->name);
49 : : } else {
50 : 0 : p.first = wc.parameters.find(param->name);
51 : 0 : p.second = wc.parameters.end();
52 : : }
53 : :
54 [ # # ]: 0 : if(p.first != p.second) {
55 [ # # ]: 0 : if(param->multi_value) {
56 [ # # ]: 0 : while(p.first != p.second) {
57 [ # # ]: 0 : if(!param->validate(p.first->second)) {
58 [ # # # # : 0 : throw std::invalid_argument(string("invalid parameter value: ").append(param->name).append(" = ").append(p.first->second));;
# # # # #
# # # #
# ]
59 : : }
60 : :
61 : 0 : ++p.first;
62 : : }
63 : : } else {
64 [ # # ]: 0 : if(!param->validate(p.first->second)) {
65 [ # # # # : 0 : throw std::invalid_argument(string("invalid parameter value: ").append(param->name).append(" = ").append(p.first->second));;
# # # # #
# # # #
# ]
66 : : }
67 : : }
68 [ # # ]: 0 : } else if(!param->optional) {
69 [ # # # # : 0 : throw std::invalid_argument(string("missing parameter: ").append(param->name));
# # # # ]
70 : : }
71 : : }
72 : : }
73 : :
74 : 0 : call_handler(wc, msginfo);
75 : 0 : }
76 : :
77 : : ActionConfig &
78 : 0 : WebService::new_action(const Properties & action_map, const string & key)
79 : : SSRC_DECL_THROW(std:::domain_error)
80 : : {
81 : 0 : const Properties *p = action_map.find(key);
82 : :
83 [ # # ]: 0 : if(p == 0) {
84 [ # # ]: 0 : string msg("action not found: ");
85 [ # # # # ]: 0 : throw std::domain_error(msg.append(key));
86 : : }
87 : :
88 [ # # ]: 0 : _actions.push_back(new ActionConfig(key, *p));
89 : 0 : return _actions.back();
90 : : }
91 : :
92 : 0 : void WebService::clear_call_handlers() {
93 : 0 : _call_handlers_one_way.clear();
94 : 0 : _call_handlers_two_way.clear();
95 : 0 : }
96 : :
97 : : bool
98 : 1 : WebService::set_call_handler_one_way(const string & call,
99 : : const call_handler_type & call_handler)
100 : : {
101 : : return
102 [ + - ]: 1 : _call_handlers_one_way.insert(call_handler_map::value_type(call, call_handler)).second;
103 : : }
104 : :
105 : : bool
106 : 1 : WebService::set_call_handler_two_way(const string & call,
107 : : const call_handler_type & call_handler)
108 : : {
109 : : return
110 [ + - ]: 1 : _call_handlers_two_way.insert(call_handler_map::value_type(call, call_handler)).second;
111 : : }
112 : :
113 : 0 : bool WebService::set_action_handler(ActionConfig & action,
114 : : const call_handler_type & call_handler)
115 : : {
116 : 0 : bool handler_set = false;
117 : :
118 [ # # ]: 0 : if(action.one_way) {
119 [ # # # # : 0 : if(action.requires_session || action.parameters) {
# # ]
120 [ # # # # : 0 : handler_set = set_call_handler_one_way(action.action, boost::bind(validate_call, std::ref(action), call_handler, _1, _2));
# # # # #
# # # ]
121 : : } else {
122 : 0 : handler_set = set_call_handler_one_way(action.action, call_handler);
123 : : }
124 : : } else {
125 [ # # # # : 0 : if(action.requires_session || action.parameters) {
# # ]
126 [ # # # # : 0 : handler_set = set_call_handler_two_way(action.action, boost::bind(validate_call, std::ref(action), call_handler, _1, _2));
# # # # #
# # # ]
127 : : } else {
128 : 0 : handler_set = set_call_handler_two_way(action.action, call_handler);
129 : : }
130 : : }
131 : :
132 : 0 : return handler_set;
133 : : }
134 : :
135 [ + - + - : 1 : WebService::WebService(super::caller_type & caller) : super(caller) {
+ - ]
136 [ + - + - : 1 : super::add_service_type(protocol_traits::service_type());
+ - ]
137 : :
138 [ + - ]: 1 : WISP_SERVICE_REQUEST(MessageOneWay);
139 [ + - ]: 1 : WISP_SERVICE_REQUEST(MessageTwoWay);
140 : 1 : }
141 : :
142 [ + - + - : 21 : __END_NS_SSRC_WSPR_WS
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
|