Add shorthand methods for obtaining properties using standard precedence rules.
authorcantor <cantor@cb58f699-b61c-0410-a6fe-9272a202ed29>
Fri, 5 Mar 2010 21:28:27 +0000 (21:28 +0000)
committercantor <cantor@cb58f699-b61c-0410-a6fe-9272a202ed29>
Fri, 5 Mar 2010 21:28:27 +0000 (21:28 +0000)
git-svn-id: https://svn.middleware.georgetown.edu/cpp-sp/branches/REL_2@3242 cb58f699-b61c-0410-a6fe-9272a202ed29

shibsp/handler/AbstractHandler.h
shibsp/handler/impl/AbstractHandler.cpp

index d797b27..78f3183 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2009 Internet2
+ *  Copyright 2001-2010 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -182,6 +182,62 @@ namespace shibsp {
             DDF& postData
             ) const;
 
+        /**
+         * Bitmask of property sources to read from
+         * (request query parameter, request mapper, fixed handler property).
+         */
+        enum PropertySourceTypes {
+            HANDLER_PROPERTY_REQUEST = 1,
+            HANDLER_PROPERTY_MAP = 2,
+            HANDLER_PROPERTY_FIXED = 4,
+            HANDLER_PROPERTY_ALL = 255
+        };
+
+        using DOMPropertySet::getBool;
+        using DOMPropertySet::getString;
+        using DOMPropertySet::getUnsignedInt;
+        using DOMPropertySet::getInt;
+
+        /**
+         * Returns a boolean-valued property.
+         * 
+         * @param name      property name
+         * @param request   reference to incoming request
+         * @param type      bitmask of property sources to use
+         * @return a pair consisting of a NULL indicator and the property value iff the indicator is true
+         */
+        std::pair<bool,bool> getBool(const char* name, const SPRequest& request, unsigned int type=HANDLER_PROPERTY_ALL) const;
+
+        /**
+         * Returns a string-valued property.
+         * 
+         * @param name      property name
+         * @param request   reference to incoming request
+         * @param type      bitmask of property sources to use
+         * @return a pair consisting of a NULL indicator and the property value iff the indicator is true
+         */
+        std::pair<bool,const char*> getString(const char* name, const SPRequest& request, unsigned int type=HANDLER_PROPERTY_ALL) const;
+
+        /**
+         * Returns an unsigned integer-valued property.
+         * 
+         * @param name      property name
+         * @param request   reference to incoming request
+         * @param type      bitmask of property sources to use
+         * @return a pair consisting of a NULL indicator and the property value iff the indicator is true
+         */
+        std::pair<bool,unsigned int> getUnsignedInt(const char* name, const SPRequest& request, unsigned int type=HANDLER_PROPERTY_ALL) const;
+
+        /**
+         * Returns an integer-valued property.
+         * 
+         * @param name      property name
+         * @param request   reference to incoming request
+         * @param type      bitmask of property sources to use
+         * @return a pair consisting of a NULL indicator and the property value iff the indicator is true
+         */
+        std::pair<bool,int> getInt(const char* name, const SPRequest& request, unsigned int type=HANDLER_PROPERTY_ALL) const;
+
         /** Logging object. */
         xmltooling::logging::Category& m_log;
         
index 39ac7dc..aa86b54 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2009 Internet2
+ *  Copyright 2001-2010 Internet2
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -700,3 +700,87 @@ DDF AbstractHandler::getPostData(const Application& application, const HTTPReque
     }
     return DDF();
 }
+
+pair<bool,bool> AbstractHandler::getBool(const char* name, const SPRequest& request, unsigned int type) const
+{
+    if (type & HANDLER_PROPERTY_REQUEST) {
+        const char* param = request.getParameter(name);
+        if (param && *param)
+            return make_pair(true, (*param=='t' || *param=='1'));
+    }
+    
+    if (type & HANDLER_PROPERTY_MAP) {
+        pair<bool,bool> ret = request.getRequestSettings().first->getBool(name);
+        if (ret.first)
+            return ret;
+    }
+
+    if (type & HANDLER_PROPERTY_FIXED) {
+        return getBool(name);
+    }
+
+    return make_pair(false,false);
+}
+
+pair<bool,const char*> AbstractHandler::getString(const char* name, const SPRequest& request, unsigned int type) const
+{
+    if (type & HANDLER_PROPERTY_REQUEST) {
+        const char* param = request.getParameter(name);
+        if (param && *param)
+            return make_pair(true, param);
+    }
+    
+    if (type & HANDLER_PROPERTY_MAP) {
+        pair<bool,const char*> ret = request.getRequestSettings().first->getString(name);
+        if (ret.first)
+            return ret;
+    }
+
+    if (type & HANDLER_PROPERTY_FIXED) {
+        return getString(name);
+    }
+
+    return pair<bool,const char*>(false,NULL);
+}
+
+pair<bool,unsigned int> AbstractHandler::getUnsignedInt(const char* name, const SPRequest& request, unsigned int type) const
+{
+    if (type & HANDLER_PROPERTY_REQUEST) {
+        const char* param = request.getParameter(name);
+        if (param && *param)
+            return pair<bool,unsigned int>(true, strtol(param,NULL,10));
+    }
+    
+    if (type & HANDLER_PROPERTY_MAP) {
+        pair<bool,unsigned int> ret = request.getRequestSettings().first->getUnsignedInt(name);
+        if (ret.first)
+            return ret;
+    }
+
+    if (type & HANDLER_PROPERTY_FIXED) {
+        return getUnsignedInt(name);
+    }
+
+    return pair<bool,unsigned int>(false,0);
+}
+
+pair<bool,int> AbstractHandler::getInt(const char* name, const SPRequest& request, unsigned int type) const
+{
+    if (type & HANDLER_PROPERTY_REQUEST) {
+        const char* param = request.getParameter(name);
+        if (param && *param)
+            return pair<bool,int>(true, atoi(param));
+    }
+    
+    if (type & HANDLER_PROPERTY_MAP) {
+        pair<bool,int> ret = request.getRequestSettings().first->getInt(name);
+        if (ret.first)
+            return ret;
+    }
+
+    if (type & HANDLER_PROPERTY_FIXED) {
+        return getInt(name);
+    }
+
+    return pair<bool,int>(false,0);
+}