Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #72 from bhuvanalakshmi/master
Browse files Browse the repository at this point in the history
Fix for issue#71
  • Loading branch information
Matthias247 committed Oct 5, 2015
2 parents 34ee595 + 6a268d5 commit dd48039
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
23 changes: 23 additions & 0 deletions jawampa-core/src/main/java/ws/wamp/jawampa/EventDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ws.wamp.jawampa;

public class EventDetails<T> {


final T message;
final String topic;

public EventDetails(T msg, String topic){
this.message = msg;
this.topic = topic;
}

public T message() {
return message;
}

public String topic() {
return topic;
}


}
60 changes: 60 additions & 0 deletions jawampa-core/src/main/java/ws/wamp/jawampa/WampClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,66 @@ public T call(PubSubData ev) {
});
}

/**
* Returns an observable that allows to subscribe on the given topic.<br>
* The actual subscription will only be made after subscribe() was called
* on it.<br>
* makeSubscriptionWithDetails will automatically transform the
* received events data into the type eventClass and will therefore return
* a mapped Observable of type EventDetails. It will only look at and transform the first
* argument of the received events arguments, therefore it can only be used
* for events that carry either a single or no argument.<br>
* Received publications will be pushed to the Subscriber via it's
* onNext method.<br>
* The client can unsubscribe from the topic by calling unsubscribe() on
* it's Subscription.<br>
* If the connection closes onCompleted will be called.<br>
* In case of errors during subscription onError will be called.
* @param topic The topic to subscribe on.<br>
* Must be valid WAMP URI.
* @param flags Flags to indicate type of subscription. This cannot be null.
* @param eventClass The class type into which the received event argument
* should be transformed. E.g. use String.class to let the client try to
* transform the first argument into a String and let the return value of
* of the call be Observable&lt;EventDetails&lt;String&gt;&gt;.
* @return An observable of type EventDetails that can be used to subscribe on the topic.
* EventDetails contains topic and message. EventDetails.topic can be useful in getting
* the complete topic name during wild card or prefix subscriptions
*/
public <T> Observable<EventDetails<T>> makeSubscriptionWithDetails(final String topic, SubscriptionFlags flags, final Class<T> eventClass)
{
return makeSubscription(topic, flags).map(new Func1<PubSubData,EventDetails<T>>() {
@Override
public EventDetails<T> call(PubSubData ev) {
if (eventClass == null || eventClass == Void.class) {
// We don't need a value
return null;
}

//get the complete topic name
//which may not be the same as method parameter 'topic' during wildcard or prefix subscriptions
String actualTopic = null;
if(ev.details != null && ev.details.get("topic") != null){
actualTopic = ev.details.get("topic").asText();
}

if (ev.arguments == null || ev.arguments.size() < 1)
throw OnErrorThrowable.from(new ApplicationError(ApplicationError.MISSING_VALUE));

JsonNode eventNode = ev.arguments.get(0);
if (eventNode.isNull()) return null;

T eventValue;
try {
eventValue = clientConfig.objectMapper().convertValue(eventNode, eventClass);
} catch (IllegalArgumentException e) {
throw OnErrorThrowable.from(new ApplicationError(ApplicationError.INVALID_VALUE_TYPE));
}
return new EventDetails<T>(eventValue, actualTopic);
}
});
}

/**
* Returns an observable that allows to subscribe on the given topic.<br>
* The actual subscription will only be made after subscribe() was called
Expand Down

0 comments on commit dd48039

Please sign in to comment.