Savarese Software Research Corporation

ActionConfig.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 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 
00023 #ifndef __SSRC_WSPR_WS_ACTION_CONFIG_H
00024 #define __SSRC_WSPR_WS_ACTION_CONFIG_H
00025 
00026 #include <ssrc/wispers/utility/Properties.h>
00027 #include <ssrc/wispers/utility/NumberVisitor.h>
00028 #include <ssrc/wispers/ws/types.h>
00029 
00030 #include <boost/scoped_ptr.hpp>
00031 
00032 __BEGIN_NS_SSRC_WSPR_WS
00033 
00034 using std::string;
00035 using NS_SSRC_WSPR_UTILITY::Properties;
00036 
00037 struct ActionConfig {
00038   // TODO: consider changing these to a bitmap and use bit masks to access.
00039   bool one_way;
00040   bool requires_session;
00041   bool verify_session;
00042   bool updates_access_time;
00043   bool clears_session;
00044   // A value of 0 means doesn't create session, 1 means session expires
00045   // when user agent terminates, > 1 indicates lifetime in number of seconds.
00046   unsigned int creates_session;
00047   bool cache_result_template;
00048   bool permits_get;
00049   bool result_is_event;
00050   string action;
00051   string target;
00052   string call;
00053   string redirect;
00054   string failure_redirect;
00055   string result_template;
00056   string failure_template;
00057   // This is not currently used by Relay.
00058   // It is used by WebServiceRunner (and optionally WebService) to
00059   // perform parameter validation.  In the future, it may be used
00060   // by Relay for the same purpose.  If we use it for the relay,
00061   // we have to catch any exceptions thrown by bad parameter
00062   // validation patterns.  This is not a problem for WebServiceRunner
00063   // or WebService which load all action configs at startup.
00064   boost::scoped_ptr<parameter_sequence> parameters;
00065 
00066   void init(const string & action_name, const Properties & action_map) {
00067     action              = action_name;
00068     one_way             = action_map.get<bool>(false, "one_way");
00069     requires_session    = action_map.get<bool>(true, "requires_session");
00070     verify_session = action_map.get<bool>(requires_session, "verify_session");
00071     updates_access_time = action_map.get<bool>(true, "updates_access_time");
00072     clears_session      = action_map.get<bool>(false, "clears_session");
00073     creates_session     = action_map.get<unsigned int>(0, "creates_session");
00074     permits_get         = action_map.get<bool>(false, "permits_get");
00075     result_is_event     = action_map.get<bool>(false, "result_is_event");
00076     target              = action_map.get<string>("", "target");
00077     call                = action_map.get<string>("", "call");
00078     redirect            = action_map.get<string>("", "redirect");
00079     failure_redirect    = action_map.get<string>("", "redirect_on_failure");
00080     result_template     = action_map.get<string>("", "template");
00081     cache_result_template = action_map.get<bool>(false, "cache_template");
00082     failure_template    = action_map.get<string>("", "template_on_failure");
00083 
00084     if(result_template.empty()) {
00085       result_template = call;
00086       result_template.append(".result");
00087     }
00088 
00089     const Properties *p = action_map.find("parameters");
00090 
00091     if(p != 0) {
00092       SSRC_UNIQUE_PTR<CallParameter> param;
00093       Properties::child_container_const_iterator && it = p->child_begin();
00094       Properties::child_container_const_iterator && end = p->child_end();
00095 
00096       parameters.reset(new parameter_sequence);
00097 
00098       while(it != end) {
00099         const Properties* node  = it->second;
00100         const string & name = it->first;
00101         const unsigned int min_size =
00102           utility::get_number<unsigned int>(*node, 1, "min_size");
00103         const unsigned int max_size =
00104           utility::get_number<unsigned int>(*node, 128, "max_size");
00105         const bool multi_value = node->get<bool>(false, "multi_value");
00106         const bool optional = node->get<bool>(false, "optional");
00107         const string & pattern = node->get<string>("", "matches");
00108 
00109         if(pattern.empty()) {
00110           param.reset(new CallParameter(name, min_size, max_size, multi_value,
00111                                         optional));
00112         } else {
00113           param.reset(new PatternParameter(name, pattern,
00114                                            min_size, max_size, multi_value,
00115                                            optional));
00116         }
00117 
00118         parameters->push_back(param.release());
00119 
00120         ++it;
00121       }
00122     }
00123   }
00124 
00125   ActionConfig() { }
00126 
00127   //TODO: Reconcile these two constructors
00128   ActionConfig(const string & action_name, const Properties & action_node) {
00129     init(action_name, action_node);
00130   }
00131 
00132   bool invalid() const {
00133     return (target.empty() || call.empty());
00134   }
00135 };
00136 
00137 
00138 template<typename container_type>
00139 void get_actions(container_type & result,
00140                  const Properties & action_map)
00141 {
00142   const Properties *p = action_map.find("Action");
00143 
00144   if(p != 0) {
00145     Properties::child_container_const_iterator && it = p->child_begin();
00146     Properties::child_container_const_iterator && end = p->child_end();
00147 
00148     while(it != end) {
00149       result.push_back(new ActionConfig(it->first, *(it->second)));
00150       ++it;
00151     }
00152   }
00153 }
00154 
00155 __END_NS_SSRC_WSPR_WS
00156 
00157 #endif

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