Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Proper exception throwing withing CompletableFuture methods. (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracyboehrer authored Jan 27, 2021
1 parent 41b3ff7 commit aa1ea57
Show file tree
Hide file tree
Showing 30 changed files with 560 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.bot.builder;

import com.microsoft.bot.connector.Async;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -53,17 +54,21 @@ public class ActivityHandler implements Bot {
@Override
public CompletableFuture<Void> onTurn(TurnContext turnContext) {
if (turnContext == null) {
throw new IllegalArgumentException("TurnContext cannot be null.");
return Async.completeExceptionally(new IllegalArgumentException(
"TurnContext cannot be null."
));
}

if (turnContext.getActivity() == null) {
throw new IllegalArgumentException("turnContext must have a non-null Activity.");
return Async.completeExceptionally(new IllegalArgumentException(
"turnContext must have a non-null Activity."
));
}

if (turnContext.getActivity().getType() == null) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"turnContext.getActivity must have a non-null Type."
);
));
}

switch (turnContext.getActivity().getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.bot.builder;

import com.microsoft.bot.connector.Async;
import com.microsoft.bot.connector.authentication.ClaimsIdentity;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ConversationReference;
Expand Down Expand Up @@ -187,7 +188,9 @@ protected CompletableFuture<Void> runPipeline(
TurnContext context,
BotCallbackHandler callback
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}

// Call any registered Middleware Components looking for ReceiveActivity()
if (context.getActivity() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.builder.integration.AdapterIntegration;
import com.microsoft.bot.connector.Async;
import com.microsoft.bot.connector.Channels;
import com.microsoft.bot.connector.ConnectorClient;
import com.microsoft.bot.connector.Conversations;
Expand Down Expand Up @@ -154,7 +155,6 @@ public BotFrameworkAdapter(
RetryStrategy withRetryStrategy,
Middleware withMiddleware
) {

this(
withCredentialProvider,
new AuthenticationConfiguration(),
Expand All @@ -181,7 +181,6 @@ public BotFrameworkAdapter(
RetryStrategy withRetryStrategy,
Middleware withMiddleware
) {

if (withCredentialProvider == null) {
throw new IllegalArgumentException("CredentialProvider cannot be null");
}
Expand Down Expand Up @@ -226,7 +225,6 @@ public BotFrameworkAdapter(
RetryStrategy withRetryStrategy,
Middleware withMiddleware
) {

if (withCredentials == null) {
throw new IllegalArgumentException("credentials");
}
Expand Down Expand Up @@ -287,11 +285,11 @@ public CompletableFuture<Void> continueConversation(
BotCallbackHandler callback
) {
if (reference == null) {
throw new IllegalArgumentException("reference");
return Async.completeExceptionally(new IllegalArgumentException("reference"));
}

if (callback == null) {
throw new IllegalArgumentException("callback");
return Async.completeExceptionally(new IllegalArgumentException("callback"));
}

botAppId = botAppId == null ? "" : botAppId;
Expand All @@ -303,7 +301,6 @@ public CompletableFuture<Void> continueConversation(
claims.put(AuthenticationConstants.APPID_CLAIM, botAppId);

ClaimsIdentity claimsIdentity = new ClaimsIdentity("ExternalBearer", claims);

String audience = getBotFrameworkOAuthScope();

return continueConversation(claimsIdentity, reference, audience, callback);
Expand Down Expand Up @@ -356,19 +353,21 @@ public CompletableFuture<Void> continueConversation(
BotCallbackHandler callback
) {
if (claimsIdentity == null) {
throw new IllegalArgumentException("claimsIdentity");
return Async.completeExceptionally(new IllegalArgumentException("claimsIdentity"));
}

if (reference == null) {
throw new IllegalArgumentException("reference");
return Async.completeExceptionally(new IllegalArgumentException("reference"));
}

if (callback == null) {
throw new IllegalArgumentException("callback");
return Async.completeExceptionally(new IllegalArgumentException("callback"));
}

if (StringUtils.isEmpty(audience)) {
throw new IllegalArgumentException("audience cannot be null or empty");
return Async.completeExceptionally(new IllegalArgumentException(
"audience cannot be null or empty"
));
}

CompletableFuture<Void> pipelineResult = new CompletableFuture<>();
Expand Down Expand Up @@ -434,7 +433,9 @@ public CompletableFuture<InvokeResponse> processActivity(
Activity activity,
BotCallbackHandler callback
) {
BotAssert.activityNotNull(activity);
if (activity == null) {
return Async.completeExceptionally(new IllegalArgumentException("Activity"));
}

return JwtTokenValidation.authenticateRequest(
activity, authHeader, credentialProvider, channelProvider, authConfiguration
Expand All @@ -460,7 +461,9 @@ public CompletableFuture<InvokeResponse> processActivity(
Activity activity,
BotCallbackHandler callback
) {
BotAssert.activityNotNull(activity);
if (activity == null) {
return Async.completeExceptionally(new IllegalArgumentException("Activity"));
}

CompletableFuture<InvokeResponse> pipelineResult = new CompletableFuture<>();

Expand Down Expand Up @@ -552,17 +555,17 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
List<Activity> activities
) {
if (context == null) {
throw new IllegalArgumentException("context");
return Async.completeExceptionally(new IllegalArgumentException("context"));
}

if (activities == null) {
throw new IllegalArgumentException("activities");
return Async.completeExceptionally(new IllegalArgumentException("activities"));
}

if (activities.size() == 0) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"Expecting one or more activities, but the array was empty."
);
));
}

return CompletableFuture.supplyAsync(() -> {
Expand Down Expand Up @@ -690,15 +693,15 @@ public CompletableFuture<Void> deleteConversationMember(
String memberId
) {
if (context.getActivity().getConversation() == null) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.deleteConversationMember(): missing conversation"
);
));
}

if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.deleteConversationMember(): missing conversation.id"
);
));
}

ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
Expand Down Expand Up @@ -735,15 +738,15 @@ public CompletableFuture<List<ChannelAccount>> getActivityMembers(
}

if (context.getActivity().getConversation() == null) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.GetActivityMembers(): missing conversation"
);
));
}

if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.GetActivityMembers(): missing conversation.id"
);
));
}

ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
Expand All @@ -760,15 +763,15 @@ public CompletableFuture<List<ChannelAccount>> getActivityMembers(
*/
public CompletableFuture<List<ChannelAccount>> getConversationMembers(TurnContextImpl context) {
if (context.getActivity().getConversation() == null) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.GetActivityMembers(): missing conversation"
);
));
}

if (StringUtils.isEmpty(context.getActivity().getConversation().getId())) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.GetActivityMembers(): missing conversation.id"
);
));
}

ConnectorClient connectorClient = context.getTurnState().get(CONNECTOR_CLIENT_KEY);
Expand Down Expand Up @@ -823,11 +826,11 @@ public CompletableFuture<ConversationsResult> getConversations(
String continuationToken
) {
if (StringUtils.isEmpty(serviceUrl)) {
throw new IllegalArgumentException("serviceUrl");
return Async.completeExceptionally(new IllegalArgumentException("serviceUrl"));
}

if (credentials == null) {
throw new IllegalArgumentException("credentials");
return Async.completeExceptionally(new IllegalArgumentException("credentials"));
}

return getOrCreateConnectorClient(serviceUrl, credentials)
Expand Down Expand Up @@ -893,28 +896,27 @@ public CompletableFuture<TokenResponse> getUserToken(
String connectionName,
String magicCode
) {
BotAssert.contextNotNull(context);

if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}
if (
context.getActivity().getFrom() == null
|| StringUtils.isEmpty(context.getActivity().getFrom().getId())
) {
throw new IllegalArgumentException(
return Async.completeExceptionally(new IllegalArgumentException(
"BotFrameworkAdapter.getUserToken(): missing from or from.id"
);
));
}

if (StringUtils.isEmpty(connectionName)) {
throw new IllegalArgumentException("connectionName");
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
return oAuthClient.getUserToken()
.getToken(
context.getActivity().getFrom().getId(), connectionName,
context.getActivity().getChannelId(), magicCode
);
});
return createOAuthClient(context, null).thenCompose(oAuthClient -> oAuthClient.getUserToken()
.getToken(
context.getActivity().getFrom().getId(), connectionName,
context.getActivity().getChannelId(), magicCode
));
}

/**
Expand All @@ -931,9 +933,12 @@ public CompletableFuture<String> getOauthSignInLink(
TurnContext context,
String connectionName
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}

if (StringUtils.isEmpty(connectionName)) {
throw new IllegalArgumentException("connectionName");
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
Expand Down Expand Up @@ -988,12 +993,14 @@ public CompletableFuture<String> getOauthSignInLink(
String userId,
String finalRedirect
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}
if (StringUtils.isEmpty(connectionName)) {
throw new IllegalArgumentException("connectionName");
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
}
if (StringUtils.isEmpty(userId)) {
throw new IllegalArgumentException("userId");
return Async.completeExceptionally(new IllegalArgumentException("userId"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
Expand Down Expand Up @@ -1044,9 +1051,11 @@ public CompletableFuture<Void> signOutUser(
String connectionName,
String userId
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}
if (StringUtils.isEmpty(connectionName)) {
throw new IllegalArgumentException("connectionName");
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
Expand Down Expand Up @@ -1075,9 +1084,11 @@ public CompletableFuture<List<TokenStatus>> getTokenStatus(
String userId,
String includeFilter
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}
if (StringUtils.isEmpty(userId)) {
throw new IllegalArgumentException("userId");
return Async.completeExceptionally(new IllegalArgumentException("userId"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
Expand Down Expand Up @@ -1107,13 +1118,14 @@ public CompletableFuture<Map<String, TokenResponse>> getAadTokens(
String[] resourceUrls,
String userId
) {
BotAssert.contextNotNull(context);
if (context == null) {
return Async.completeExceptionally(new IllegalArgumentException("TurnContext"));
}
if (StringUtils.isEmpty(connectionName)) {
throw new IllegalArgumentException("connectionName");
return Async.completeExceptionally(new IllegalArgumentException("connectionName"));
}

if (resourceUrls == null) {
throw new IllegalArgumentException("resourceUrls");
return Async.completeExceptionally(new IllegalArgumentException("resourceUrls"));
}

return createOAuthClient(context, null).thenCompose(oAuthClient -> {
Expand Down Expand Up @@ -1348,9 +1360,9 @@ private CompletableFuture<ConnectorClient> createConnectorClient(
String audience
) {
if (claimsIdentity == null) {
throw new UnsupportedOperationException(
return Async.completeExceptionally(new UnsupportedOperationException(
"ClaimsIdentity cannot be null. Pass Anonymous ClaimsIdentity if authentication is turned off."
);
));
}

// For requests from channel App Id is in Audience claim of JWT token. For
Expand Down Expand Up @@ -1477,7 +1489,7 @@ private CompletableFuture<AppCredentials> getAppCredentials(String appId, String
});
}

private String getBotAppId(TurnContext turnContext) {
private String getBotAppId(TurnContext turnContext) throws IllegalStateException {
ClaimsIdentity botIdentity = turnContext.getTurnState().get(BOT_IDENTITY_KEY);
if (botIdentity == null) {
throw new IllegalStateException(
Expand Down
Loading

0 comments on commit aa1ea57

Please sign in to comment.