PingManager assumes class type which is not guaranteed

Hi Smack guys!

Your PingManager class-casts in some instances, to the Ping class from an IQ (line 128) in version 4.x.x.

The two lines:

Ping ping = (Ping) iqRequest;
return ping.getPong();

Can hopefully be transformed into:

if(iqRequest instanceof Ping){
Ping ping = (Ping) iqRequest;
return ping.getPong();
}else{
return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable));
}

In case of a bad request. The bad request will otherwise trigger:

java.lang.ClassCastException: org.jivesoftware.smack.packet.UnparsedIQ cannot be cast to org.jivesoftware.smackx.ping.packet.Ping

Best regards
Andreas Rudolph
YouSee Development, Denmark
ahr@tdc.dk

I think the cast in the two lines you mention is guranteed to succeed if the PingProvider is registered. What you describe is usually a sign that the corresponding provider was not registered.

Okay, so calling:

ProviderManager.addExtensionProvider(Ping.ELEMENT, Ping.NAMESPACE, new PingProvider());

Before establishing the connection, should solve the problem?

Thank you for the quick response!

Br, Andreas

That’s usually done automatically by Smacks initialization routine.

But I am getting the described error, so the PingProvider is not automatically registered?

I am now just registering it as soon as possible (onCreate in an Android Service).

Bh Andreas

Possibly, the question is why it’s not automatically registered. You will probably run into all sorts of issues if Smack isn’t fully initialized.

Alright, can force an initialization?

Then I can do it in my Android service.