Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/ws - ActionConfig.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 0 45 0.0 %
Date: 2012-04-09 Functions: 0 3 0.0 %
Branches: 0 140 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 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                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the ActionConfig class for configuring Web service
      20                 :            :  * actions.
      21                 :            :  */
      22                 :            : 
      23                 :            : #ifndef __SSRC_WSPR_WS_ACTION_CONFIG_H
      24                 :            : #define __SSRC_WSPR_WS_ACTION_CONFIG_H
      25                 :            : 
      26                 :            : #include <ssrc/wispers/utility/Properties.h>
      27                 :            : #include <ssrc/wispers/utility/NumberVisitor.h>
      28                 :            : #include <ssrc/wispers/ws/types.h>
      29                 :            : 
      30                 :            : #include <boost/scoped_ptr.hpp>
      31                 :            : 
      32                 :            : __BEGIN_NS_SSRC_WSPR_WS
      33                 :            : 
      34                 :            : using std::string;
      35                 :            : using NS_SSRC_WSPR_UTILITY::Properties;
      36                 :            : 
      37   [ #  #  #  #  :          0 : struct ActionConfig {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      38                 :            :   // TODO: consider changing these to a bitmap and use bit masks to access.
      39                 :            :   bool one_way;
      40                 :            :   bool requires_session;
      41                 :            :   bool verify_session;
      42                 :            :   bool updates_access_time;
      43                 :            :   bool clears_session;
      44                 :            :   // A value of 0 means doesn't create session, 1 means session expires
      45                 :            :   // when user agent terminates, > 1 indicates lifetime in number of seconds.
      46                 :            :   unsigned int creates_session;
      47                 :            :   bool creates_secure_session;
      48                 :            :   bool cache_result_template;
      49                 :            :   bool permits_get;
      50                 :            :   bool result_is_event;
      51                 :            :   string action;
      52                 :            :   string target;
      53                 :            :   string call;
      54                 :            :   string redirect;
      55                 :            :   string failure_redirect;
      56                 :            :   string result_template;
      57                 :            :   string failure_template;
      58                 :            :   // This is not currently used by Relay.
      59                 :            :   // It is used by WebServiceRunner (and optionally WebService) to
      60                 :            :   // perform parameter validation.  In the future, it may be used
      61                 :            :   // by Relay for the same purpose.  If we use it for the relay,
      62                 :            :   // we have to catch any exceptions thrown by bad parameter
      63                 :            :   // validation patterns.  This is not a problem for WebServiceRunner
      64                 :            :   // or WebService which load all action configs at startup.
      65                 :            :   boost::scoped_ptr<parameter_sequence> parameters;
      66                 :            : 
      67                 :          0 :   void init(const string & action_name, const Properties & action_map) {
      68                 :          0 :     action              = action_name;
      69                 :          0 :     one_way             = action_map.get<bool>(false, "one_way");
      70                 :          0 :     requires_session    = action_map.get<bool>(true, "requires_session");
      71                 :          0 :     verify_session = action_map.get<bool>(requires_session, "verify_session");
      72                 :          0 :     updates_access_time = action_map.get<bool>(true, "updates_access_time");
      73                 :          0 :     clears_session      = action_map.get<bool>(false, "clears_session");
      74                 :          0 :     creates_session     = action_map.get<unsigned int>(0, "creates_session");
      75                 :            :     creates_secure_session =
      76                 :          0 :       action_map.get<bool>(false, "creates_secure_session");
      77                 :          0 :     permits_get         = action_map.get<bool>(false, "permits_get");
      78                 :          0 :     result_is_event     = action_map.get<bool>(false, "result_is_event");
      79   [ #  #  #  #  :          0 :     target              = action_map.get<string>("", "target");
             #  #  #  # ]
      80   [ #  #  #  #  :          0 :     call                = action_map.get<string>("", "call");
             #  #  #  # ]
      81   [ #  #  #  #  :          0 :     redirect            = action_map.get<string>("", "redirect");
             #  #  #  # ]
      82   [ #  #  #  #  :          0 :     failure_redirect    = action_map.get<string>("", "redirect_on_failure");
             #  #  #  # ]
      83   [ #  #  #  #  :          0 :     result_template     = action_map.get<string>("", "template");
             #  #  #  # ]
      84                 :          0 :     cache_result_template = action_map.get<bool>(false, "cache_template");
      85   [ #  #  #  #  :          0 :     failure_template    = action_map.get<string>("", "template_on_failure");
             #  #  #  # ]
      86                 :            : 
      87         [ #  # ]:          0 :     if(result_template.empty()) {
      88                 :          0 :       result_template = call;
      89                 :          0 :       result_template.append(".result");
      90                 :            :     }
      91                 :            : 
      92   [ #  #  #  #  :          0 :     const Properties *p = action_map.find("parameters");
                   #  # ]
      93                 :            : 
      94         [ #  # ]:          0 :     if(p != 0) {
      95                 :          0 :       SSRC_UNIQUE_PTR<CallParameter> param;
      96         [ #  # ]:          0 :       Properties::child_container_const_iterator && it = p->child_begin();
      97         [ #  # ]:          0 :       Properties::child_container_const_iterator && end = p->child_end();
      98                 :            : 
      99   [ #  #  #  #  :          0 :       parameters.reset(new parameter_sequence);
                   #  # ]
     100                 :            : 
     101   [ #  #  #  # ]:          0 :       while(it != end) {
     102         [ #  # ]:          0 :         const Properties* node  = it->second;
     103         [ #  # ]:          0 :         const string & name = it->first;
     104                 :            :         const unsigned int min_size =
     105         [ #  # ]:          0 :           utility::get_number<unsigned int>(*node, 1, "min_size");
     106                 :            :         const unsigned int max_size =
     107         [ #  # ]:          0 :           utility::get_number<unsigned int>(*node, 128, "max_size");
     108         [ #  # ]:          0 :         const bool multi_value = node->get<bool>(false, "multi_value");
     109         [ #  # ]:          0 :         const bool optional = node->get<bool>(false, "optional");
     110   [ #  #  #  #  :          0 :         const string & pattern = node->get<string>("", "matches");
                   #  # ]
     111                 :            : 
     112   [ #  #  #  # ]:          0 :         if(pattern.empty()) {
     113                 :            :           param.reset(new CallParameter(name, min_size, max_size, multi_value,
     114   [ #  #  #  #  :          0 :                                         optional));
                   #  # ]
     115                 :            :         } else {
     116                 :            :           param.reset(new PatternParameter(name, pattern,
     117                 :            :                                            min_size, max_size, multi_value,
     118   [ #  #  #  #  :          0 :                                            optional));
                   #  # ]
     119                 :            :         }
     120                 :            : 
     121   [ #  #  #  # ]:          0 :         parameters->push_back(param.release());
     122                 :            : 
     123         [ #  # ]:          0 :         ++it;
     124                 :            :       }
     125                 :            :     }
     126                 :          0 :   }
     127                 :            : 
     128                 :            :   ActionConfig() { }
     129                 :            : 
     130                 :            :   //TODO: Reconcile these two constructors
     131   [ #  #  #  #  :          0 :   ActionConfig(const string & action_name, const Properties & action_node) {
          #  #  #  #  #  
                #  #  # ]
     132         [ #  # ]:          0 :     init(action_name, action_node);
     133                 :          0 :   }
     134                 :            : 
     135                 :            :   bool invalid() const {
     136                 :            :     return (target.empty() || call.empty());
     137                 :            :   }
     138                 :            : };
     139                 :            : 
     140                 :            : 
     141                 :            : template<typename container_type>
     142                 :            : void get_actions(container_type & result,
     143                 :            :                  const Properties & action_map)
     144                 :            : {
     145                 :            :   const Properties *p = action_map.find("Action");
     146                 :            : 
     147                 :            :   if(p != 0) {
     148                 :            :     Properties::child_container_const_iterator && it = p->child_begin();
     149                 :            :     Properties::child_container_const_iterator && end = p->child_end();
     150                 :            : 
     151                 :            :     while(it != end) {
     152                 :            :       result.push_back(new ActionConfig(it->first, *(it->second)));
     153                 :            :       ++it;
     154                 :            :     }
     155                 :            :   }
     156                 :            : }
     157                 :            : 
     158                 :            : __END_NS_SSRC_WSPR_WS
     159                 :            : 
     160                 :            : #endif