Moved ReplayCache into xmltooling
authorScott Cantor <cantor.2@osu.edu>
Mon, 9 Oct 2006 16:29:07 +0000 (16:29 +0000)
committerScott Cantor <cantor.2@osu.edu>
Mon, 9 Oct 2006 16:29:07 +0000 (16:29 +0000)
xmltooling/Makefile.am
xmltooling/XMLToolingConfig.cpp
xmltooling/XMLToolingConfig.h
xmltooling/util/ReplayCache.cpp [new file with mode: 0644]
xmltooling/util/ReplayCache.h [new file with mode: 0644]

index 38cd95e..8099251 100644 (file)
@@ -81,6 +81,7 @@ utilinclude_HEADERS = \
        util/DateTime.h \
        util/NDC.h \
        util/ParserPool.h \
+       util/ReplayCache.h \
        util/StorageService.h \
        util/Threads.h \
        util/XMLConstants.h \
@@ -144,6 +145,7 @@ libxmltooling_la_SOURCES = \
        util/DateTime.cpp \
        util/NDC.cpp \
        util/ParserPool.cpp \
+       util/ReplayCache.cpp \
        util/StorageService.cpp \
        util/XMLConstants.cpp \
        util/XMLHelper.cpp \
index 5427e6f..e9bfc5e 100644 (file)
@@ -30,6 +30,7 @@
 #include "signature/CredentialResolver.h"
 #include "soap/SOAP.h"
 #include "util/NDC.h"
+#include "util/ReplayCache.h"
 #include "util/StorageService.h"
 #include "util/XMLConstants.h"
 #include "validation/Validator.h"
@@ -143,6 +144,12 @@ bool XMLToolingInternalConfig::log_config(const char* config)
     return true;
 }
 
+void XMLToolingConfig::setReplayCache(ReplayCache* replayCache)
+{
+    delete m_replayCache;
+    m_replayCache = replayCache;
+}
+
 bool XMLToolingInternalConfig::init()
 {
 #ifdef _DEBUG
@@ -233,6 +240,9 @@ void XMLToolingInternalConfig::term()
     KeyResolverManager.deregisterFactories();
 #endif
 
+    delete m_replayCache;
+    m_replayCache = NULL;
+
     for (vector<void*>::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) {
 #if defined(WIN32)
         FARPROC fn=GetProcAddress(static_cast<HMODULE>(*i),"xmltooling_extension_term");
index e1ce7f0..737fe31 100644 (file)
@@ -41,6 +41,7 @@ namespace xmlsignature {
 \r
 namespace xmltooling {\r
     \r
+    class XMLTOOL_API ReplayCache;\r
     class XMLTOOL_API StorageService;\r
     class XMLTOOL_API TrustEngine;\r
     class XMLTOOL_API XSECCryptoX509CRL;\r
@@ -56,7 +57,10 @@ namespace xmltooling {
     {\r
         MAKE_NONCOPYABLE(XMLToolingConfig);\r
     protected:\r
-        XMLToolingConfig() : clock_skew_secs(180) {}\r
+        XMLToolingConfig() : m_replayCache(NULL), clock_skew_secs(180) {}\r
+        \r
+        /** Global ReplayCache instance. */\r
+        ReplayCache* m_replayCache;\r
     public:\r
         virtual ~XMLToolingConfig() {}\r
 \r
@@ -128,7 +132,25 @@ namespace xmltooling {
          * @return reference to a validating parser pool.\r
          */\r
         virtual ParserPool& getValidatingParser() const=0;\r
-        \r
+\r
+        /**\r
+         * Sets the global ReplayCache instance.\r
+         * This method must be externally synchronized with any code that uses the object.\r
+         * Any previously set object is destroyed.\r
+         * \r
+         * @param replayCache   new ReplayCache instance to store\r
+         */\r
+        void setReplayCache(ReplayCache* replayCache);\r
+\r
+        /**\r
+         * Returns the global ReplayCache instance.\r
+         * \r
+         * @return  global ReplayCache or NULL\r
+         */\r
+        ReplayCache* getReplayCache() const {\r
+            return m_replayCache;\r
+        }\r
+                \r
         /**\r
          * List of catalog files to load into validating parser pool at initialization time.\r
          * Like other path settings, the separator depends on the platform\r
diff --git a/xmltooling/util/ReplayCache.cpp b/xmltooling/util/ReplayCache.cpp
new file mode 100644 (file)
index 0000000..6ed4ab2
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ *  Copyright 2001-2006 Internet2
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * ReplayCache.cpp
+ * 
+ * Helper class on top of StorageService for detecting message replay. 
+ */
+
+#include "internal.h"
+#include "util/ReplayCache.h"
+
+using namespace xmltooling;
+using namespace std;
+
+ReplayCache::ReplayCache(StorageService* storage) : m_storage(storage)
+{
+    if (!m_storage)
+        m_storage = XMLToolingConfig::getConfig().StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE, NULL);
+}
+
+ReplayCache::~ReplayCache()
+{
+    delete m_storage;
+}
+
+bool ReplayCache::check(const char* context, const char* s, time_t expires)
+{
+    // In storage already?
+    if (m_storage->readString(context, s))
+        return false;
+    m_storage->createText(context, s, "x", expires);
+    return true;
+}
diff --git a/xmltooling/util/ReplayCache.h b/xmltooling/util/ReplayCache.h
new file mode 100644 (file)
index 0000000..94fd22d
--- /dev/null
@@ -0,0 +1,66 @@
+/*\r
+ *  Copyright 2001-2006 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * @file xmltooling/util/ReplayCache.h\r
+ * \r
+ * Helper class on top of StorageService for detecting message replay.\r
+ */\r
+\r
+#ifndef __xmltooling_replay_h__\r
+#define __xmltooling_replay_h__\r
+\r
+#include <xmltooling/util/StorageService.h>\r
+\r
+namespace xmltooling {\r
+\r
+    /**\r
+     * Helper class on top of StorageService for detecting message replay.\r
+     */\r
+    class XMLTOOL_API ReplayCache\r
+    {\r
+        MAKE_NONCOPYABLE(ReplayCache);\r
+    public:\r
+        \r
+        /**\r
+         * Creates a replay cache on top of a particular StorageService.\r
+         * \r
+         * @param storage       pointer to a StorageService, or NULL to keep cache in memory\r
+         */\r
+        ReplayCache(StorageService* storage=NULL);\r
+\r
+        virtual ~ReplayCache();\r
+        \r
+        /**\r
+         * Returns true iff the check value is not found in the cache, and stores it.\r
+         * \r
+         * @param context   a context label to subdivide the cache\r
+         * @param s         value to check\r
+         * @param expires   time for disposal of value from cache\r
+         */\r
+        virtual bool check(const char* context, const char* s, time_t expires);\r
+    \r
+        bool check(const char* context, const XMLCh* str, time_t expires) {\r
+            auto_ptr_char temp(str);\r
+            return check(context, temp.get(), expires);\r
+        }\r
+        \r
+    private:\r
+        StorageService* m_storage;\r
+    };\r
+};\r
+\r
+#endif /* __xmltooling_replay_h__ */\r