Author: ivol37 at gmail.com
Date: Fri Jan 21 16:16:17 2011
New Revision: 684
Log:
[AMDATU-200] Refactored throwing exception when consumer was not found
Modified:
trunk/amdatu-authentication/oauth-api/src/main/java/org/amdatu/authentication/oauth/api/OAuthServiceConsumerRegistry.java
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
trunk/integration-tests/src/test/java/org/amdatu/test/integration/mock/OAuthProtectedTestServlet.java
Modified:
trunk/amdatu-authentication/oauth-api/src/main/java/org/amdatu/authentication/oauth/api/OAuthServiceConsumerRegistry.java
==============================================================================
---
trunk/amdatu-authentication/oauth-api/src/main/java/org/amdatu/authentication/oauth/api/OAuthServiceConsumerRegistry.java
(original)
+++
trunk/amdatu-authentication/oauth-api/src/main/java/org/amdatu/authentication/oauth/api/OAuthServiceConsumerRegistry.java
Fri Jan 21 16:16:17 2011
@@ -69,7 +69,7 @@
* @throws ConsumerRegistryStorageException If an internal error occurred
in the consumer registry storage
*/
void updateConsumer(OAuthServiceConsumer consumer) throws
ConsumerNotFoundException, ConsumerRegistryStorageException;
-
+
/**
* Adds the id of a user that allowed this service consumer to access
protected resources on its behalf.
* This information can be used to support a 2-legged oAuth scenario. In
this scenario the user grants access
@@ -81,17 +81,18 @@
* @throws ConsumerRegistryStorageException If an internal error occurred
in the consumer registry storage
*/
void grantResourceAccess(OAuthServiceConsumer consumer, String userId)
throws ConsumerRegistryStorageException;
-
+
/**
* Removes the id of a user that allowed this service consumer to access
protected resources on its behalf.
* This method can be used to support a kind of 'unsubscribe'
functionality; a user that wants to withdraw
* the rights of a service consumer to access its resources on its behalf.
* @param consumer The service consumer for which the access should be
withdrawn
* @param userId the id of the user that withdraws the access
+ * @throws ConsumerNotFoundException if no consumer exists matching the
consumer key of the specified consumer
* @throws ConsumerRegistryStorageException If an internal error occurred
in the consumer registry storage
*/
- void withdrawResourceAccess(OAuthServiceConsumer consumer, String userId)
throws ConsumerRegistryStorageException;
-
+ void withdrawResourceAccess(OAuthServiceConsumer consumer, String userId)
throws ConsumerNotFoundException, ConsumerRegistryStorageException;
+
/**
* Returns if the specified user id granted access to the specified
consumer to access its protected resource
* (using a 3-legged oAuth dance) on its behalf before.
@@ -99,7 +100,8 @@
* @param userId The user id to check
* @return <code>true</code> if the user with this id granted access to
the specified service consumer
* to access its resources on its behalf.
+ * @throws ConsumerNotFoundException if no consumer exists matching the
consumer key of the specified consumer
* @throws ConsumerRegistryStorageException If an internal error occurred
in the consumer registry storage
*/
- boolean hasResourceAccess(OAuthServiceConsumer consumer, String userId)
throws ConsumerRegistryStorageException;
+ boolean hasResourceAccess(OAuthServiceConsumer consumer, String userId)
throws ConsumerNotFoundException, ConsumerRegistryStorageException;
}
Modified:
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
==============================================================================
---
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
(original)
+++
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
Fri Jan 21 16:16:17 2011
@@ -27,8 +27,8 @@
import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
-import
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerStorage;
import
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerEntity;
+import
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerStorage;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.log.LogService;
@@ -75,9 +75,10 @@
}
try {
m_storage = new FSConsumerStorage(dataDirectory);
- if (m_logService != null)
+ if (m_logService != null) {
m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: "
+ m_storage.getDataDirectory().getAbsolutePath());
+ }
}
catch (IOException e) {
throw new ConsumerRegistryStorageException(e);
@@ -95,16 +96,17 @@
}
public synchronized void addConsumer(OAuthServiceConsumer consumer) throws
ConsumerAlreadyExistsException,
- ConsumerRegistryStorageException {
+ ConsumerRegistryStorageException {
try {
if (m_storage.getEntity(consumer.getConsumerKey()) != null) {
throw new ConsumerAlreadyExistsException("Consumer with key '"
+ consumer.getConsumerKey()
+ "' already exists");
}
m_storage.addEntity(new FSConsumerEntity(consumer));
- if (m_logService != null)
+ if (m_logService != null) {
m_logService.log(LogService.LOG_DEBUG, "Added consumer with
key '" + consumer.getConsumerKey()
+ "' to the oAuth service consumer registry");
+ }
}
catch (IOException e) {
throw new ConsumerRegistryStorageException(e);
@@ -112,16 +114,17 @@
}
public synchronized void updateConsumer(OAuthServiceConsumer consumer)
throws ConsumerNotFoundException,
- ConsumerRegistryStorageException {
+ ConsumerRegistryStorageException {
try {
if (m_storage.getEntity(consumer.getConsumerKey()) == null) {
throw new ConsumerNotFoundException("Consumer with key '" +
consumer.getConsumerKey()
+ "' doesn't exist");
}
m_storage.addEntity(new FSConsumerEntity(consumer));
- if (m_logService != null)
+ if (m_logService != null) {
m_logService.log(LogService.LOG_DEBUG, "Updated consumer with
key '" + consumer.getConsumerKey()
+ "' in the oAuth service consumer registry");
+ }
}
catch (IOException e) {
throw new ConsumerRegistryStorageException(e);
@@ -129,16 +132,17 @@
}
public synchronized void removeConsumer(OAuthServiceConsumer consumer)
throws ConsumerNotFoundException,
- ConsumerRegistryStorageException {
+ ConsumerRegistryStorageException {
try {
if (m_storage.getEntity(consumer.getConsumerKey()) == null) {
throw new ConsumerNotFoundException("Consumer with key '" +
consumer.getConsumerKey()
+ "' doesn't exist");
}
m_storage.removeEntity(consumer.getConsumerKey());
- if (m_logService != null)
+ if (m_logService != null) {
m_logService.log(LogService.LOG_DEBUG, "Removed consumer with
key '" + consumer.getConsumerKey()
+ "' from the oAuth service consumer registry");
+ }
}
catch (IOException e) {
throw new ConsumerRegistryStorageException(e);
@@ -146,7 +150,7 @@
}
public synchronized void grantResourceAccess(OAuthServiceConsumer
consumer, String userId)
- throws ConsumerRegistryStorageException {
+ throws ConsumerRegistryStorageException {
try {
FSConsumerEntity fsConsumer =
m_storage.getEntity(consumer.getConsumerKey());
List<String> allowedUserIds = fsConsumer.getAllowedUserIds();
@@ -164,12 +168,12 @@
}
public synchronized void withdrawResourceAccess(OAuthServiceConsumer
consumer, String userId)
- throws ConsumerRegistryStorageException {
+ throws ConsumerNotFoundException, ConsumerRegistryStorageException {
try {
FSConsumerEntity entity =
m_storage.getEntity(consumer.getConsumerKey());
if (entity == null) {
- // FIXME for API consistency throw exception?
- return;
+ throw new ConsumerNotFoundException("Consumer with key '" +
consumer.getConsumerKey()
+ + "' doesn't exist");
}
List<String> allowedUserIds = entity.getAllowedUserIds();
if (allowedUserIds != null && allowedUserIds.contains(userId)) {
@@ -183,12 +187,12 @@
}
public boolean hasResourceAccess(OAuthServiceConsumer consumer, String
userId)
- throws ConsumerRegistryStorageException {
+ throws ConsumerNotFoundException, ConsumerRegistryStorageException {
try {
FSConsumerEntity entity =
m_storage.getEntity(consumer.getConsumerKey());
if (entity == null) {
- // FIXME for API consistency throw exception?
- return false;
+ throw new ConsumerNotFoundException("Consumer with key '" +
consumer.getConsumerKey()
+ + "' doesn't exist");
}
return entity.getAllowedUserIds() != null &&
entity.getAllowedUserIds().contains(userId);
}
Modified:
trunk/integration-tests/src/test/java/org/amdatu/test/integration/mock/OAuthProtectedTestServlet.java
==============================================================================
---
trunk/integration-tests/src/test/java/org/amdatu/test/integration/mock/OAuthProtectedTestServlet.java
(original)
+++
trunk/integration-tests/src/test/java/org/amdatu/test/integration/mock/OAuthProtectedTestServlet.java
Fri Jan 21 16:16:17 2011
@@ -37,10 +37,8 @@
import net.oauth.OAuthConsumer;
import net.oauth.OAuthException;
import net.oauth.OAuthMessage;
-import net.oauth.OAuthProblemException;
import net.oauth.server.OAuthServlet;
-import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
import org.amdatu.authentication.oauth.server.OAuthTokenProvider;
import org.osgi.service.log.LogService;
@@ -54,19 +52,19 @@
public static final String OAUTH_TYPE_SIGNED_REQUEST = "signedrequest";
public static final String OAUTH_TYPE_TWO_LEGGED = "2legged";
public static final String OAUTH_TYPE_THREE_LEGGED = "3legged";
-
-
+
+
private volatile LogService m_logService;
private volatile OAuthTokenProvider m_tokenProvider;
private volatile OAuthServiceConsumerRegistry m_consumerRegistry;
public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
+ throws IOException, ServletException {
processRequest("GET", request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
- throws IOException, ServletException {
+ throws IOException, ServletException {
processRequest("POST", request, response);
}
@@ -75,7 +73,7 @@
// Get the oAuth scenario type
String oAuthType = request.getParameter(OAUTH_TYPE_PARAM);
m_logService.log(LogService.LOG_DEBUG, "Protected resource test
servlet received incoming request, oAuth type=" + oAuthType);
-
+
// Validate oAuth message and get userId from it
String body = "";
OAuthAccessor accessor = validateOAuth(request);
@@ -124,16 +122,7 @@
out.close();
}
}
- catch (OAuthProblemException e) {
- throw new ServletException(e);
- }
- catch (OAuthException e) {
- throw new ServletException(e);
- }
- catch (URISyntaxException e) {
- throw new ServletException(e);
- }
- catch (ConsumerRegistryStorageException e) {
+ catch (Exception e) {
throw new ServletException(e);
}
}