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 creates_secure_session;
00048   bool cache_result_template;
00049   bool permits_get;
00050   bool result_is_event;
00051   string action;
00052   string target;
00053   string call;
00054   string redirect;
00055   string failure_redirect;
00056   string result_template;
00057   string failure_template;
00058   // This is not currently used by Relay.
00059   // It is used by WebServiceRunner (and optionally WebService) to
00060   // perform parameter validation.  In the future, it may be used
00061   // by Relay for the same purpose.  If we use it for the relay,
00062   // we have to catch any exceptions thrown by bad parameter
00063   // validation patterns.  This is not a problem for WebServiceRunner
00064   // or WebService which load all action configs at startup.
00065   boost::scoped_ptr<parameter_sequence> parameters;
00066 
00067   void init(const string & action_name, const Properties & action_map) {
00068     action              = action_name;
00069     one_way             = action_map.get<bool>(false, "one_way");
00070     requires_session    = action_map.get<bool>(true, "requires_session");
00071     verify_session = action_map.get<bool>(requires_session, "verify_session");
00072     updates_access_time = action_map.get<bool>(true, "updates_access_time");
00073     clears_session      = action_map.get<bool>(false, "clears_session");
00074     creates_session     = action_map.get<unsigned int>(0, "creates_session");
00075     creates_secure_session =
00076       action_map.get<bool>(false, "creates_secure_session");
00077     permits_get         = action_map.get<bool>(false, "permits_get");
00078     result_is_event     = action_map.get<bool>(false, "result_is_event");
00079     target              = action_map.get<string>("", "target");
00080     call                = action_map.get<string>("", "call");
00081     redirect            = action_map.get<string>("", "redirect");
00082     failure_redirect    = action_map.get<string>("", "redirect_on_failure");
00083     result_template     = action_map.get<string>("", "template");
00084     cache_result_template = action_map.get<bool>(false, "cache_template");
00085     failure_template    = action_map.get<string>("", "template_on_failure");
00086 
00087     if(result_template.empty()) {
00088       result_template = call;
00089       result_template.append(".result");
00090     }
00091 
00092     const Properties *p = action_map.find("parameters");
00093 
00094     if(p != 0) {
00095       SSRC_UNIQUE_PTR<CallParameter> param;
00096       Properties::child_container_const_iterator && it = p->child_begin();
00097       Properties::child_container_const_iterator && end = p->child_end();
00098 
00099       parameters.reset(new parameter_sequence);
00100 
00101       while(it != end) {
00102         const Properties* node  = it->second;
00103         const string & name = it->first;
00104         const unsigned int min_size =
00105           utility::get_number<unsigned int>(*node, 1, "min_size");
00106         const unsigned int max_size =
00107           utility::get_number<unsigned int>(*node, 128, "max_size");
00108         const bool multi_value = node->get<bool>(false, "multi_value");
00109         const bool optional = node->get<bool>(false, "optional");
00110         const string & pattern = node->get<string>("", "matches");
00111 
00112         if(pattern.empty()) {
00113           param.reset(new CallParameter(name, min_size, max_size, multi_value,
00114                                         optional));
00115         } else {
00116           param.reset(new PatternParameter(name, pattern,
00117                                            min_size, max_size, multi_value,
00118                                            optional));
00119         }
00120 
00121         parameters->push_back(param.release());
00122 
00123         ++it;
00124       }
00125     }
00126   }
00127 
00128   ActionConfig() { }
00129 
00130   //TODO: Reconcile these two constructors
00131   ActionConfig(const string & action_name, const Properties & action_node) {
00132     init(action_name, action_node);
00133   }
00134 
00135   bool invalid() const {
00136     return (target.empty() || call.empty());
00137   }
00138 };
00139 
00140 
00141 template<typename container_type>
00142 void get_actions(container_type & result,
00143                  const Properties & action_map)
00144 {
00145   const Properties *p = action_map.find("Action");
00146 
00147   if(p != 0) {
00148     Properties::child_container_const_iterator && it = p->child_begin();
00149     Properties::child_container_const_iterator && end = p->child_end();
00150 
00151     while(it != end) {
00152       result.push_back(new ActionConfig(it->first, *(it->second)));
00153       ++it;
00154     }
00155   }
00156 }
00157 
00158 __END_NS_SSRC_WSPR_WS
00159 
00160 #endif

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