Author: beaton
Date: Tue Jul 29 13:58:39 2008
New Revision: 680842
URL: http://svn.apache.org/viewvc?rev=680842&view=rev
Log:
Move some OAuthFetcher configuration options into a containing class so the
code
reads a little more easily.
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java
(with props)
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java
(with props)
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherFactory.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/BasicGadgetOAuthTokenStoreTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java?rev=680842&r1=680841&r2=680842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcher.java
Tue Jul 29 13:58:39 2008
@@ -17,11 +17,9 @@
package org.apache.shindig.gadgets.oauth;
import org.apache.shindig.common.SecurityToken;
-import org.apache.shindig.common.crypto.BlobCrypter;
import org.apache.shindig.common.crypto.BlobCrypterException;
import org.apache.shindig.gadgets.ChainedContentFetcher;
import org.apache.shindig.gadgets.GadgetException;
-import org.apache.shindig.gadgets.http.HttpCache;
import org.apache.shindig.gadgets.http.HttpCacheKey;
import org.apache.shindig.gadgets.http.HttpFetcher;
import org.apache.shindig.gadgets.http.HttpResponse;
@@ -93,22 +91,12 @@
protected final OAuthRequestParams requestParams;
/**
- * Reference to our persistent store for OAuth metadata.
- */
- protected GadgetOAuthTokenStore tokenStore;
-
- /**
* The accessor we use for signing messages. This also holds metadata about
* the service provider, such as their URLs and the keys we use to access
* those URLs.
*/
private OAuthStore.AccessorInfo accessorInfo;
-
- /**
- * We use this to encrypt and sign the state we cache on the client.
- */
- private BlobCrypter oauthCrypter;
-
+
/**
* State the client sent with their request.
*/
@@ -146,30 +134,23 @@
private final boolean bypassSpecCache;
/**
- * Cache for HTTP responses, probably shared across multiple different
- * fetchers.
+ * Configuration options for the fetcher.
*/
- private HttpCache cache;
+ private final OAuthFetcherConfig fetcherConfig;
/**
- *
- * @param oauthCrypter used to encrypt transient information we store on the
- * client.
+ * @param fetcherConfig configuration options for the fetcher
* @param nextFetcher fetcher to use for actually making requests
* @param authToken user's gadget security token
* @param params OAuth fetch parameters sent from makeRequest
- * @param tokenStore storage for long lived tokens.
- * @param cache cache to use for HTTP responses.
*/
public OAuthFetcher(
- GadgetOAuthTokenStore tokenStore,
- BlobCrypter oauthCrypter,
+ OAuthFetcherConfig fetcherConfig,
HttpFetcher nextFetcher,
SecurityToken authToken,
- OAuthRequestParams params,
- HttpCache cache) {
+ OAuthRequestParams params) {
super(nextFetcher);
- this.oauthCrypter = oauthCrypter;
+ this.fetcherConfig = fetcherConfig;
this.authToken = authToken;
this.requestParams = params;
this.bypassSpecCache = params.getBypassSpecCache();
@@ -180,8 +161,8 @@
String origClientState = params.getOrigClientState();
if (origClientState != null && origClientState.length() > 0) {
try {
- this.origClientState =
- oauthCrypter.unwrap(origClientState, CLIENT_STATE_MAX_AGE_SECS);
+ this.origClientState = fetcherConfig.getStateCrypter()
+ .unwrap(origClientState, CLIENT_STATE_MAX_AGE_SECS);
} catch (BlobCrypterException e) {
// Probably too old, pretend we never saw it at all.
}
@@ -189,8 +170,6 @@
if (this.origClientState == null) {
this.origClientState = new HashMap<String, String>();
}
- this.tokenStore = tokenStore;
- this.cache = cache;
}
/**
@@ -203,7 +182,7 @@
*/
private void lookupOAuthMetadata() throws GadgetException {
OAuthStore.TokenKey tokenKey = buildTokenKey();
- accessorInfo = tokenStore.getOAuthAccessor(tokenKey, bypassSpecCache);
+ accessorInfo = fetcherConfig.getTokenStore().getOAuthAccessor(tokenKey,
bypassSpecCache);
// The persistent data store may be out of sync with reality; we trust
// the state we stored on the client to be accurate.
@@ -238,7 +217,7 @@
public HttpResponse fetch(HttpRequest request) throws GadgetException {
HttpCacheKey cacheKey = makeCacheKey(request);
- HttpResponse response = cache.getResponse(cacheKey, request);
+ HttpResponse response = fetcherConfig.getHttpCache().getResponse(cacheKey,
request);
if (response != null) {
return response;
}
@@ -272,7 +251,7 @@
GadgetException.Code.INTERNAL_SERVER_ERROR,
"No response for OAuth fetch to " + realRequest.getUri());
}
- return cache.addResponse(cacheKey, request, response);
+ return fetcherConfig.getHttpCache().addResponse(cacheKey, request,
response);
}
// Builds up a cache key based on the same key that we use into the OAuth
@@ -313,7 +292,7 @@
OAuthProtocolException pe, int attempts) throws OAuthStoreException {
if (pe.startFromScratch()) {
OAuthStore.TokenKey tokenKey = buildTokenKey();
- tokenStore.removeToken(tokenKey);
+ fetcherConfig.getTokenStore().removeToken(tokenKey);
accessorInfo.accessor.accessToken = null;
accessorInfo.accessor.requestToken = null;
@@ -562,7 +541,7 @@
oauthState.put(REQ_TOKEN_KEY, accessor.requestToken);
oauthState.put(REQ_TOKEN_SECRET_KEY, accessor.tokenSecret);
oauthState.put(OWNER_KEY, authToken.getOwnerId());
- newClientState = oauthCrypter.wrap(oauthState);
+ newClientState = fetcherConfig.getStateCrypter().wrap(oauthState);
} catch (BlobCrypterException e) {
throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
}
@@ -659,7 +638,7 @@
OAuthStore.TokenKey tokenKey = buildTokenKey();
OAuthStore.TokenInfo tokenInfo = new OAuthStore.TokenInfo(
accessor.accessToken, accessor.tokenSecret);
- tokenStore.storeTokenKeyAndSecret(tokenKey, tokenInfo);
+ fetcherConfig.getTokenStore().storeTokenKeyAndSecret(tokenKey, tokenInfo);
}
/**
@@ -672,7 +651,7 @@
oauthState.put(ACCESS_TOKEN_KEY, accessor.accessToken);
oauthState.put(ACCESS_TOKEN_SECRET_KEY, accessor.tokenSecret);
oauthState.put(OWNER_KEY, authToken.getOwnerId());
- newClientState = oauthCrypter.wrap(oauthState);
+ newClientState = fetcherConfig.getStateCrypter().wrap(oauthState);
} catch (BlobCrypterException e) {
throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e);
}
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java?rev=680842&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java
Tue Jul 29 13:58:39 2008
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you 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.shindig.gadgets.oauth;
+
+import org.apache.shindig.common.crypto.BlobCrypter;
+import org.apache.shindig.gadgets.http.HttpCache;
+
+/**
+ * Configuration parameters for an OAuthFetcher
+ */
+public class OAuthFetcherConfig {
+
+ private final BlobCrypter stateCrypter;
+ private final GadgetOAuthTokenStore tokenStore;
+ private final HttpCache httpCache;
+
+ /**
+ * Used to encrypt state stored on the client.
+ */
+ public BlobCrypter getStateCrypter() {
+ return stateCrypter;
+ }
+
+ /**
+ * Persistent token storage.
+ */
+ public GadgetOAuthTokenStore getTokenStore() {
+ return tokenStore;
+ }
+
+ /**
+ * Cache for OAuth responses.
+ */
+ public HttpCache getHttpCache() {
+ return httpCache;
+ }
+
+ public OAuthFetcherConfig(BlobCrypter stateCrypter, GadgetOAuthTokenStore
tokenStore,
+ HttpCache httpCache) {
+ this.stateCrypter = stateCrypter;
+ this.tokenStore = tokenStore;
+ this.httpCache = httpCache;
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfig.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherFactory.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherFactory.java?rev=680842&r1=680841&r2=680842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherFactory.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/oauth/OAuthFetcherFactory.java
Tue Jul 29 13:58:39 2008
@@ -37,14 +37,7 @@
*/
public class OAuthFetcherFactory {
- /** used to encrypt state stored on the client */
- protected BlobCrypter oauthCrypter;
-
- /** persistent storage for OAuth tokens */
- protected GadgetOAuthTokenStore tokenStore;
-
- /** shared HTTP cache */
- private HttpCache cache;
+ protected OAuthFetcherConfig fetcherConfig;
private static final Logger logger
= Logger.getLogger(OAuthFetcherFactory.class.getName());
@@ -56,14 +49,13 @@
@Inject
public OAuthFetcherFactory(GadgetSpecFactory specFactory, HttpCache cache) {
try {
- this.oauthCrypter = new BasicBlobCrypter(
+ BlobCrypter oauthCrypter = new BasicBlobCrypter(
Crypto.getRandomBytes(BasicBlobCrypter.MASTER_KEY_MIN_LEN));
BasicGadgetOAuthTokenStore store =
new BasicGadgetOAuthTokenStore(new BasicOAuthStore(), specFactory);
store.initFromConfigFile();
- this.tokenStore = store;
- this.cache = cache;
+ fetcherConfig = new OAuthFetcherConfig(oauthCrypter, store, cache);
} catch (Throwable t) {
// Since this happens at startup, we don't want to kill the server just
// because we can't initialize the OAuth config.
@@ -74,17 +66,10 @@
/**
* Creates an OAuthFetcherFactory based on prepared crypter and token store.
*
- * @param oauthCrypter used to wrap client side state
- * @param tokenStore used as interface to persistent token store.
- * @param cache shared HTTP cache
+ * @param fetcherConfig configuration
*/
- protected OAuthFetcherFactory(
- BlobCrypter oauthCrypter,
- GadgetOAuthTokenStore tokenStore,
- HttpCache cache) {
- this.oauthCrypter = oauthCrypter;
- this.tokenStore = tokenStore;
- this.cache = cache;
+ protected OAuthFetcherFactory(OAuthFetcherConfig fetcherConfig) {
+ this.fetcherConfig = fetcherConfig;
}
/**
@@ -102,8 +87,7 @@
HttpFetcher nextFetcher,
SecurityToken token,
OAuthRequestParams params) throws GadgetException {
- OAuthFetcher fetcher = new OAuthFetcher(
- tokenStore, oauthCrypter, nextFetcher, token, params, cache);
+ OAuthFetcher fetcher = new OAuthFetcher(fetcherConfig, nextFetcher, token,
params);
return fetcher;
}
}
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/BasicGadgetOAuthTokenStoreTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/BasicGadgetOAuthTokenStoreTest.java?rev=680842&r1=680841&r2=680842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/BasicGadgetOAuthTokenStoreTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/BasicGadgetOAuthTokenStoreTest.java
Tue Jul 29 13:58:39 2008
@@ -40,6 +40,7 @@
private BasicGadgetOAuthTokenStore store;
+ @Override
@Before
public void setUp() throws Exception {
OAuthStore backingStore = new BasicOAuthStore();
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java?rev=680842&view=auto
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java
(added)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java
Tue Jul 29 13:58:39 2008
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you 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.shindig.gadgets.oauth;
+
+import org.apache.shindig.common.crypto.BlobCrypter;
+import org.apache.shindig.gadgets.EasyMockTestCase;
+import org.apache.shindig.gadgets.http.HttpCache;
+import org.junit.Test;
+
+/**
+ * Simple test for a simple class.
+ */
+public class OAuthFetcherConfigTest extends EasyMockTestCase {
+
+ @Test
+ public void testOAuthFetcherConfig() {
+ BlobCrypter crypter = mock(BlobCrypter.class);
+ HttpCache cache = mock(HttpCache.class);
+ GadgetOAuthTokenStore tokenStore = mock(GadgetOAuthTokenStore.class);
+ OAuthFetcherConfig config = new OAuthFetcherConfig(crypter, tokenStore,
cache);
+ assertEquals(crypter, config.getStateCrypter());
+ assertEquals(cache, config.getHttpCache());
+ assertEquals(tokenStore, config.getTokenStore());
+ }
+}
Propchange:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherConfigTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java?rev=680842&r1=680841&r2=680842&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/oauth/OAuthFetcherTest.java
Tue Jul 29 13:58:39 2008
@@ -27,11 +27,9 @@
import org.apache.shindig.common.BasicSecurityToken;
import org.apache.shindig.common.SecurityToken;
import org.apache.shindig.common.crypto.BasicBlobCrypter;
-import org.apache.shindig.common.crypto.BlobCrypter;
import org.apache.shindig.gadgets.FakeGadgetSpecFactory;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.http.BasicHttpCache;
-import org.apache.shindig.gadgets.http.HttpCache;
import org.apache.shindig.gadgets.http.HttpFetcher;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
@@ -48,10 +46,8 @@
*/
public class OAuthFetcherTest {
- private GadgetOAuthTokenStore tokenStore;
- private BlobCrypter blobCrypter;
+ private OAuthFetcherConfig fetcherConfig;
private FakeOAuthServiceProvider serviceProvider;
- private HttpCache cache;
public static final String GADGET_URL = "http://www.example.com/gadget.xml";
public static final String GADGET_URL_NO_KEY =
@@ -60,9 +56,10 @@
@Before
public void setUp() throws Exception {
serviceProvider = new FakeOAuthServiceProvider();
- tokenStore = getOAuthStore();
- blobCrypter = new BasicBlobCrypter("abcdefghijklmnop".getBytes());
- cache = new BasicHttpCache(10);
+ fetcherConfig = new OAuthFetcherConfig(
+ new BasicBlobCrypter("abcdefghijklmnop".getBytes()),
+ getOAuthStore(),
+ new BasicHttpCache(10));
}
/**
@@ -136,8 +133,7 @@
@SuppressWarnings("unused")
public HttpFetcher getFetcher(SecurityToken authToken,
OAuthRequestParams params) throws GadgetException {
- OAuthFetcher fetcher = new OAuthFetcher(
- tokenStore, blobCrypter, serviceProvider, authToken, params, cache);
+ OAuthFetcher fetcher = new OAuthFetcher(fetcherConfig, serviceProvider,
authToken, params);
return fetcher;
}