-
Notifications
You must be signed in to change notification settings - Fork 886
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push Notifications (XEP-0357) implementation
- Loading branch information
Showing
13 changed files
with
784 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
...al/src/main/java/org/jivesoftware/smackx/push_notifications/PushNotificationsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/** | ||
* | ||
* Copyright © 2016 Fernando Ramirez | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.push_notifications; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
import org.jivesoftware.smack.ConnectionCreationListener; | ||
import org.jivesoftware.smack.Manager; | ||
import org.jivesoftware.smack.PacketCollector; | ||
import org.jivesoftware.smack.SmackException.NoResponseException; | ||
import org.jivesoftware.smack.SmackException.NotConnectedException; | ||
import org.jivesoftware.smack.XMPPConnection; | ||
import org.jivesoftware.smack.XMPPConnectionRegistry; | ||
import org.jivesoftware.smack.XMPPException.XMPPErrorException; | ||
import org.jivesoftware.smack.filter.IQReplyFilter; | ||
import org.jivesoftware.smack.packet.IQ; | ||
import org.jivesoftware.smack.packet.IQ.Type; | ||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; | ||
import org.jivesoftware.smackx.push_notifications.element.DisablePushNotificationsIQ; | ||
import org.jivesoftware.smackx.push_notifications.element.EnablePushNotificationsIQ; | ||
import org.jivesoftware.smackx.push_notifications.element.PushNotificationsElements; | ||
import org.jxmpp.jid.Jid; | ||
|
||
/** | ||
* Push Notifications manager class. | ||
* | ||
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push | ||
* Notifications</a> | ||
* @author Fernando Ramirez | ||
* | ||
*/ | ||
public final class PushNotificationsManager extends Manager { | ||
|
||
static { | ||
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { | ||
@Override | ||
public void connectionCreated(XMPPConnection connection) { | ||
getInstanceFor(connection); | ||
} | ||
}); | ||
} | ||
|
||
private static final Map<XMPPConnection, PushNotificationsManager> INSTANCES = new WeakHashMap<>(); | ||
|
||
/** | ||
* Get the singleton instance of PushNotificationsManager. | ||
* | ||
* @param connection | ||
* @return the instance of PushNotificationsManager | ||
*/ | ||
public static synchronized PushNotificationsManager getInstanceFor(XMPPConnection connection) { | ||
PushNotificationsManager pushNotificationsManager = INSTANCES.get(connection); | ||
|
||
if (pushNotificationsManager == null) { | ||
pushNotificationsManager = new PushNotificationsManager(connection); | ||
INSTANCES.put(connection, pushNotificationsManager); | ||
} | ||
|
||
return pushNotificationsManager; | ||
} | ||
|
||
private PushNotificationsManager(XMPPConnection connection) { | ||
super(connection); | ||
} | ||
|
||
/** | ||
* Returns true if Push Notifications is supported by the server. | ||
* | ||
* @return true if Push Notifications is supported by the server. | ||
* @throws NoResponseException | ||
* @throws XMPPErrorException | ||
* @throws NotConnectedException | ||
* @throws InterruptedException | ||
*/ | ||
public boolean isSupportedByServer() | ||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { | ||
return ServiceDiscoveryManager.getInstanceFor(connection()) | ||
.serverSupportsFeature(PushNotificationsElements.NAMESPACE); | ||
} | ||
|
||
/** | ||
* Enable push notifications. | ||
* | ||
* @param pushJid | ||
* @param node | ||
* @return true if it was successfully enabled, false if not | ||
* @throws NoResponseException | ||
* @throws XMPPErrorException | ||
* @throws NotConnectedException | ||
* @throws InterruptedException | ||
*/ | ||
public boolean enable(Jid pushJid, String node) | ||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { | ||
return enable(pushJid, node, null); | ||
} | ||
|
||
/** | ||
* Enable push notifications. | ||
* | ||
* @param pushJid | ||
* @param node | ||
* @param publishOptions | ||
* @return true if it was successfully enabled, false if not | ||
* @throws NoResponseException | ||
* @throws XMPPErrorException | ||
* @throws NotConnectedException | ||
* @throws InterruptedException | ||
*/ | ||
public boolean enable(Jid pushJid, String node, HashMap<String, String> publishOptions) | ||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { | ||
EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(pushJid, node, | ||
publishOptions); | ||
return changePushNotificationsStatus(enablePushNotificationsIQ); | ||
} | ||
|
||
/** | ||
* Disable all push notifications. | ||
* | ||
* @param pushJid | ||
* @return true if it was successfully disabled, false if not | ||
* @throws NoResponseException | ||
* @throws XMPPErrorException | ||
* @throws NotConnectedException | ||
* @throws InterruptedException | ||
*/ | ||
public boolean disableAll(Jid pushJid) | ||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { | ||
return disable(pushJid, null); | ||
} | ||
|
||
/** | ||
* Disable push notifications of an specific node. | ||
* | ||
* @param pushJid | ||
* @param node | ||
* @return true if it was successfully disabled, false if not | ||
* @throws NoResponseException | ||
* @throws XMPPErrorException | ||
* @throws NotConnectedException | ||
* @throws InterruptedException | ||
*/ | ||
public boolean disable(Jid pushJid, String node) | ||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { | ||
DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(pushJid, node); | ||
return changePushNotificationsStatus(disablePushNotificationsIQ); | ||
} | ||
|
||
private boolean changePushNotificationsStatus(IQ iq) | ||
throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { | ||
final XMPPConnection connection = connection(); | ||
|
||
IQ responseIQ = null; | ||
PacketCollector responseIQCollector = connection.createPacketCollector(new IQReplyFilter(iq, connection)); | ||
|
||
try { | ||
connection.sendStanza(iq); | ||
responseIQ = responseIQCollector.nextResultOrThrow(); | ||
} finally { | ||
responseIQCollector.cancel(); | ||
} | ||
|
||
return responseIQ.getType() != Type.error; | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...n/java/org/jivesoftware/smackx/push_notifications/element/DisablePushNotificationsIQ.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* | ||
* Copyright © 2016 Fernando Ramirez | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.push_notifications.element; | ||
|
||
import org.jivesoftware.smack.packet.IQ; | ||
import org.jxmpp.jid.Jid; | ||
|
||
/** | ||
* Disable Push Notifications IQ. | ||
* | ||
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push | ||
* Notifications</a> | ||
* @author Fernando Ramirez | ||
* | ||
*/ | ||
public class DisablePushNotificationsIQ extends IQ { | ||
|
||
/** | ||
* disable element. | ||
*/ | ||
public static final String ELEMENT = "disable"; | ||
|
||
/** | ||
* the IQ NAMESPACE. | ||
*/ | ||
public static final String NAMESPACE = PushNotificationsElements.NAMESPACE; | ||
|
||
private final Jid jid; | ||
private final String node; | ||
|
||
public DisablePushNotificationsIQ(Jid jid, String node) { | ||
super(ELEMENT, NAMESPACE); | ||
this.jid = jid; | ||
this.node = node; | ||
this.setType(Type.set); | ||
} | ||
|
||
public DisablePushNotificationsIQ(Jid jid) { | ||
this(jid, null); | ||
} | ||
|
||
/** | ||
* Get the JID. | ||
* | ||
* @return the JID | ||
*/ | ||
public Jid getJid() { | ||
return jid; | ||
} | ||
|
||
/** | ||
* Get the node. | ||
* | ||
* @return the node | ||
*/ | ||
public String getNode() { | ||
return node; | ||
} | ||
|
||
@Override | ||
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { | ||
xml.attribute("jid", jid); | ||
xml.optAttribute("node", node); | ||
xml.rightAngleBracket(); | ||
return xml; | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
...in/java/org/jivesoftware/smackx/push_notifications/element/EnablePushNotificationsIQ.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* | ||
* Copyright © 2016 Fernando Ramirez | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.push_notifications.element; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import org.jivesoftware.smack.packet.IQ; | ||
import org.jivesoftware.smackx.pubsub.packet.PubSub; | ||
import org.jivesoftware.smackx.xdata.FormField; | ||
import org.jivesoftware.smackx.xdata.packet.DataForm; | ||
import org.jxmpp.jid.Jid; | ||
|
||
/** | ||
* Enable Push Notifications IQ. | ||
* | ||
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push | ||
* Notifications</a> | ||
* @author Fernando Ramirez | ||
* | ||
*/ | ||
public class EnablePushNotificationsIQ extends IQ { | ||
|
||
/** | ||
* enable element. | ||
*/ | ||
public static final String ELEMENT = "enable"; | ||
|
||
/** | ||
* the IQ NAMESPACE. | ||
*/ | ||
public static final String NAMESPACE = PushNotificationsElements.NAMESPACE; | ||
|
||
private final Jid jid; | ||
private final String node; | ||
private final HashMap<String, String> publishOptions; | ||
|
||
public EnablePushNotificationsIQ(Jid jid, String node, HashMap<String, String> publishOptions) { | ||
super(ELEMENT, NAMESPACE); | ||
this.jid = jid; | ||
this.node = node; | ||
this.publishOptions = publishOptions; | ||
this.setType(Type.set); | ||
} | ||
|
||
public EnablePushNotificationsIQ(Jid jid, String node) { | ||
this(jid, node, null); | ||
} | ||
|
||
/** | ||
* Get the JID. | ||
* | ||
* @return the JID | ||
*/ | ||
public Jid getJid() { | ||
return jid; | ||
} | ||
|
||
/** | ||
* Get the node. | ||
* | ||
* @return the node | ||
*/ | ||
public String getNode() { | ||
return node; | ||
} | ||
|
||
/** | ||
* Get the publish options. | ||
* | ||
* @return the publish options | ||
*/ | ||
public HashMap<String, String> getPublishOptions() { | ||
return publishOptions; | ||
} | ||
|
||
@Override | ||
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { | ||
xml.attribute("jid", jid); | ||
xml.attribute("node", node); | ||
xml.rightAngleBracket(); | ||
|
||
if (publishOptions != null) { | ||
DataForm dataForm = new DataForm(DataForm.Type.form); | ||
|
||
FormField formTypeField = new FormField("FORM_TYPE"); | ||
formTypeField.addValue(PubSub.NAMESPACE + "#publish-options"); | ||
dataForm.addField(formTypeField); | ||
|
||
Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator(); | ||
while (publishOptionsIterator.hasNext()) { | ||
Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next(); | ||
FormField field = new FormField(pairVariableValue.getKey()); | ||
field.addValue(pairVariableValue.getValue()); | ||
dataForm.addField(field); | ||
} | ||
|
||
xml.element(dataForm); | ||
} | ||
|
||
return xml; | ||
} | ||
|
||
} |
Oops, something went wrong.