return;
}
final ByteString subscriberId = subRequest.getSubscriberId();
final InMemorySubscriptionState subscriptionState = topicSubscriptions.get(subscriberId);
CreateOrAttach createOrAttach = subRequest.getCreateOrAttach();
if (subscriptionState != null) {
if (createOrAttach.equals(CreateOrAttach.CREATE)) {
String msg = "Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " requested creating a subscription but it is already subscribed with state: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionState());
logger.error(msg);
cb.operationFailed(ctx, new PubSubException.ClientAlreadySubscribedException(msg));
return;
}
// Subscription existed before, check whether new preferences provided
// if new preferences provided, merged the subscription data and updated them
// TODO: needs ACL mechanism when changing preferences
if (subRequest.hasPreferences() &&
subscriptionState.updatePreferences(subRequest.getPreferences())) {
updateSubscriptionPreferences(topic, subscriberId, subscriptionState, new Callback<Void>() {
@Override
public void operationFailed(Object ctx, PubSubException exception) {
cb.operationFailed(ctx, exception);
}
@Override
public void operationFinished(Object ctx, Void resultOfOperation) {
if (logger.isDebugEnabled()) {
logger.debug("Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " attaching to subscription with state: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionState())
+ ", with preferences: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionPreferences()));
}
// update message bound if necessary
updateMessageBound(topic);
cb.operationFinished(ctx, subscriptionState.toSubscriptionData());
}
}, ctx);
return;
}
// otherwise just attach
if (logger.isDebugEnabled()) {
logger.debug("Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " attaching to subscription with state: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionState())
+ ", with preferences: "
+ SubscriptionStateUtils.toString(subscriptionState.getSubscriptionPreferences()));
}
cb.operationFinished(ctx, subscriptionState.toSubscriptionData());
return;
}
// we don't have a mapping for this subscriber
if (createOrAttach.equals(CreateOrAttach.ATTACH)) {
String msg = "Topic: " + topic.toStringUtf8() + " subscriberId: " + subscriberId.toStringUtf8()
+ " requested attaching to an existing subscription but it is not subscribed";
logger.error(msg);
cb.operationFailed(ctx, new PubSubException.ClientNotSubscribedException(msg));
return;