You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2015/02/04 13:21:17 UTC

svn commit: r1657142 - /chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java

Author: fmui
Date: Wed Feb  4 12:21:17 2015
New Revision: 1657142

URL: http://svn.apache.org/r1657142
Log:
OAuth authentication provider: added token listener

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java?rev=1657142&r1=1657141&r2=1657142&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/OAuthAuthenticationProvider.java Wed Feb  4 12:21:17 2015
@@ -26,6 +26,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -111,6 +112,7 @@ public class OAuthAuthenticationProvider
 
     private Token token = null;
     private long defaultTokenLifetime = 3600;
+    private List<TokenListener> tokenListeners;
 
     @Override
     public void setSession(BindingSession session) {
@@ -156,6 +158,7 @@ public class OAuthAuthenticationProvider
             }
 
             token = new Token(accessToken, refreshToken, expirationTimestamp);
+            fireTokenListner(token);
         }
     }
 
@@ -185,6 +188,63 @@ public class OAuthAuthenticationProvider
         }
     }
 
+    /**
+     * Adds a token listener.
+     * 
+     * @param listner
+     *            the listener object
+     */
+    public void addTokenListener(TokenListener listner) {
+        if (listner == null) {
+            return;
+        }
+
+        lock.writeLock().lock();
+        try {
+            if (tokenListeners == null) {
+                tokenListeners = new ArrayList<OAuthAuthenticationProvider.TokenListener>();
+            }
+
+            tokenListeners.add(listner);
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * Removes a token listener.
+     * 
+     * @param listner
+     *            the listener object
+     */
+    public void removeTokenListener(TokenListener listner) {
+        if (listner == null) {
+            return;
+        }
+
+        lock.writeLock().lock();
+        try {
+            if (tokenListeners != null) {
+                tokenListeners.remove(listner);
+            }
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * Lets all token listeners know that there is a new token.
+     */
+    protected void fireTokenListner(Token token) {
+        if (tokenListeners == null) {
+            return;
+        }
+
+        for (TokenListener listner : tokenListeners) {
+            listner.tokenRefreshed(token);
+        }
+    }
+
     @Override
     protected boolean getSendBearerToken() {
         // the super class should not handle bearer tokens
@@ -355,6 +415,7 @@ public class OAuthAuthenticationProvider
 
         token = new Token(jsonAccessToken.toString(), (jsonRefreshToken == null ? null : jsonRefreshToken.toString()),
                 expiresIn * 1000 + System.currentTimeMillis());
+        fireTokenListner(token);
     }
 
     private JSONObject parseResponse(HttpURLConnection conn) {
@@ -467,4 +528,18 @@ public class OAuthAuthenticationProvider
                     + expirationTimestamp;
         }
     }
+
+    /**
+     * Listener for OAuth token events.
+     */
+    public interface TokenListener {
+
+        /**
+         * Called when a token is requested of refreshed.
+         * 
+         * @param token
+         *            the new token
+         */
+        void tokenRefreshed(Token token);
+    }
 }