Author: prabath
Date: Wed May 19 10:09:52 2010
New Revision: 946101

URL: http://svn.apache.org/viewvc?rev=946101&view=rev
Log:
patch for RAMPART-294

Added:
    
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/AbstractUniqueMessageAttributeCache.java
    
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/NonceCache.java
Modified:
    
axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/errors.properties

Added: 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/AbstractUniqueMessageAttributeCache.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/AbstractUniqueMessageAttributeCache.java?rev=946101&view=auto
==============================================================================
--- 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/AbstractUniqueMessageAttributeCache.java
 (added)
+++ 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/AbstractUniqueMessageAttributeCache.java
 Wed May 19 10:09:52 2010
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+package org.apache.rampart;
+
+/**
+ * An abstract class which implements UniqueMessageAttributeCache interface.
+ */
+public abstract class AbstractUniqueMessageAttributeCache implements 
UniqueMessageAttributeCache {
+
+    /**
+     * Maximum lift time of a cached value. If cached value exceeds this value 
it will be discarded.
+     */
+    private int maximumLifeTimeOfNonce = 60 * 5;
+
+    /**
+     * Default constructor.
+     */
+    public AbstractUniqueMessageAttributeCache()
+    {
+    }
+
+    /**
+     * Constructor with maximum life time as a parameter.
+     * @param maxTime Maximum life time in seconds.
+     */
+    public AbstractUniqueMessageAttributeCache(int maxTime)
+    {
+        maximumLifeTimeOfNonce = maxTime;
+    }
+
+    /**
+     * Sets the maximum life time of a message id.
+     * @param maxTime Maximum life time in seconds.
+     */
+    public void setMaximumLifeTimeOfAnAttribute(int maxTime)
+    {
+        maximumLifeTimeOfNonce = maxTime;
+    }
+
+    /**
+     * Gets the maximum life time of a message id.
+     * @return Gets message id life time in seconds.
+     */
+    public int getMaximumLifeTimeOfAnAttribute()
+    {
+        return maximumLifeTimeOfNonce;
+    }
+}

Added: 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/NonceCache.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/NonceCache.java?rev=946101&view=auto
==============================================================================
--- 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/NonceCache.java
 (added)
+++ 
axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/NonceCache.java
 Wed May 19 10:09:52 2010
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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.
+ */
+package org.apache.rampart;
+
+import java.util.*;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * This is a basic implementation of UniqueMessageAttributeCache. In this 
implementation we will cache incomming
+ * nonce value for a period of time. The life time can be defined in the 
services.xml. If not defined
+ * the default value will be 5 minutes.
+ */
+public class NonceCache extends AbstractUniqueMessageAttributeCache {
+
+    class Nonce
+    {
+        String nonceValue;
+        String userName;
+
+        public Nonce(String nonce, String user)
+        {
+            this.nonceValue = nonce;
+            this.userName = user;
+        }
+
+        @Override
+        public boolean equals(Object another)
+        {
+            Nonce otherNonce = (Nonce)another;
+            if (this.userName.equals(otherNonce.userName))
+            {
+                if (this.nonceValue.equals(otherNonce.nonceValue))
+                {
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
+            }
+
+            return false;
+        }
+
+        @Override
+        public int hashCode()
+        {
+            return (this.userName.hashCode() * 13 +  
this.nonceValue.hashCode() * 7);
+        }
+    }
+
+    private Map<Nonce, Calendar> mapIdNonce = new HashMap<Nonce, Calendar>();
+
+    private final ReentrantLock lock = new ReentrantLock();
+
+    public NonceCache()
+    {
+        super();
+    }
+    
+    public NonceCache(int maxLifeTime)
+    {
+        super(maxLifeTime);
+    }
+
+    /**
+     * @inheritdoc
+     */    
+    public void addToCache(String id, String userName) {
+
+        Nonce nonce = new Nonce(id, userName);
+        Calendar rightNow = Calendar.getInstance();
+
+        lock.lock();
+        try {
+            mapIdNonce.put(nonce, rightNow);
+        } finally {
+            lock.unlock();
+        }
+
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public boolean valueExistsInCache(String id, String userName) {
+
+        lock.lock();
+
+        try {
+            clearStaleNonceIds();
+        } finally {
+            lock.unlock();
+        }
+        
+        Nonce nonce = new Nonce(id, userName);
+        return mapIdNonce.containsKey(nonce);
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public void clearCache() {
+
+        lock.lock();
+        try {
+            mapIdNonce.clear();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * This method will clear stale nonce ids from the map.
+     */
+    private void clearStaleNonceIds()
+    {
+        Calendar rightNow = Calendar.getInstance();
+
+        int maxLifeTime = getMaximumLifeTimeOfAnAttribute();
+
+        rightNow.add(Calendar.SECOND, -(maxLifeTime));
+        long timeBeforeMaxLifeTime = rightNow.getTimeInMillis();
+        
+        Iterator iterator = mapIdNonce.entrySet().iterator();
+
+        while (iterator.hasNext()) {
+
+            Map.Entry pair = (Map.Entry)iterator.next();
+            Calendar itemDate = (Calendar)pair.getValue();
+
+            long itemAddedTime = itemDate.getTimeInMillis();
+
+            if (timeBeforeMaxLifeTime > itemAddedTime)
+            {
+                iterator.remove();
+            }
+        }
+
+
+    }
+}

Modified: 
axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/errors.properties
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/errors.properties?rev=946101&r1=946100&r2=946101&view=diff
==============================================================================
--- 
axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/errors.properties
 (original)
+++ 
axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/errors.properties
 Wed May 19 10:09:52 2010
@@ -85,4 +85,5 @@ tokenNotFound = Token with ID \"{0}\" ca
 configurationIsNull = Configuration is null
 errorInCancelingToken = Error occurred while trying to cancel token
 
-errorExtractingTokenId  = Error occurred while extracting token id from the 
Security Token Reference
\ No newline at end of file
+errorExtractingTokenId  = Error occurred while extracting token id from the 
Security Token Reference
+lifeTimeElemMissing = Lifetime element is missing in the RSTR
\ No newline at end of file


Reply via email to