2003-09-14 16:46:38 +02:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-09-14 16:46:38 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:37:00 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-09-14 16:46:38 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-09-14 16:46:38 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* 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.
|
2003-09-14 16:46:38 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.xhtmlim;
|
2003-09-14 16:46:38 +02:00
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2006-09-16 01:05:01 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2003-09-14 16:46:38 +02:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
|
|
import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
|
2006-09-16 01:05:01 +02:00
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
2003-09-14 16:46:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to
|
|
|
|
* get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP
|
|
|
|
* clients support XHTML.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class XHTMLManager {
|
2003-12-19 19:00:16 +01:00
|
|
|
static {
|
2014-05-25 12:28:08 +02:00
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-03-10 09:45:50 +01:00
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
2015-01-05 05:11:06 +01:00
|
|
|
// Enable the XHTML support on every established connection
|
2003-12-19 19:00:16 +01:00
|
|
|
XHTMLManager.setServiceEnabled(connection, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2003-09-14 16:46:38 +02:00
|
|
|
/**
|
|
|
|
* Returns an Iterator for the XHTML bodies in the message. Returns null if
|
|
|
|
* the message does not contain an XHTML extension.
|
|
|
|
*
|
|
|
|
* @param message an XHTML message
|
|
|
|
* @return an Iterator for the bodies in the message or null if none.
|
|
|
|
*/
|
2014-07-05 15:04:35 +02:00
|
|
|
public static List<CharSequence> getBodies(Message message) {
|
2015-01-05 05:11:06 +01:00
|
|
|
XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
|
2003-09-14 16:46:38 +02:00
|
|
|
if (xhtmlExtension != null)
|
|
|
|
return xhtmlExtension.getBodies();
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an XHTML body to the message.
|
|
|
|
*
|
|
|
|
* @param message the message that will receive the XHTML body
|
2015-01-05 05:11:06 +01:00
|
|
|
* @param xhtmlText the string to add as an XHTML body to the message
|
2003-09-14 16:46:38 +02:00
|
|
|
*/
|
2015-01-05 05:11:06 +01:00
|
|
|
public static void addBody(Message message, XHTMLText xhtmlText) {
|
|
|
|
XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
|
2003-09-14 16:46:38 +02:00
|
|
|
if (xhtmlExtension == null) {
|
|
|
|
// Create an XHTMLExtension and add it to the message
|
|
|
|
xhtmlExtension = new XHTMLExtension();
|
|
|
|
message.addExtension(xhtmlExtension);
|
|
|
|
}
|
|
|
|
// Add the required bodies to the message
|
2015-01-05 05:11:06 +01:00
|
|
|
xhtmlExtension.addBody(xhtmlText.toXML());
|
2003-09-14 16:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the message contains an XHTML extension.
|
|
|
|
*
|
2017-12-13 23:10:11 +01:00
|
|
|
* @param message the message to check if contains an XHTML extension or not
|
2003-09-14 16:46:38 +02:00
|
|
|
* @return a boolean indicating whether the message is an XHTML message
|
|
|
|
*/
|
|
|
|
public static boolean isXHTMLMessage(Message message) {
|
2014-07-05 14:19:31 +02:00
|
|
|
return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
|
2003-10-14 01:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables the XHTML support on a given connection.<p>
|
|
|
|
*
|
|
|
|
* Before starting to send XHTML messages to a user, check that the user can handle XHTML
|
|
|
|
* messages. Enable the XHTML support to indicate that this client handles XHTML messages.
|
|
|
|
*
|
|
|
|
* @param connection the connection where the service will be enabled or disabled
|
|
|
|
* @param enabled indicates if the service will be enabled or disabled
|
|
|
|
*/
|
2018-03-29 12:35:11 +02:00
|
|
|
public static synchronized void setServiceEnabled(XMPPConnection connection, boolean enabled) {
|
2003-10-14 01:10:15 +02:00
|
|
|
if (isServiceEnabled(connection) == enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (enabled) {
|
2014-07-05 14:19:31 +02:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
|
2003-12-19 19:00:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-07-05 14:19:31 +02:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
|
2003-10-14 01:10:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the XHTML support is enabled for the given connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection to look for XHTML support
|
|
|
|
* @return a boolean indicating if the XHTML support is enabled for the given connection
|
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static boolean isServiceEnabled(XMPPConnection connection) {
|
2014-07-05 14:19:31 +02:00
|
|
|
return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
|
2003-09-14 16:46:38 +02:00
|
|
|
}
|
|
|
|
|
2003-10-14 01:10:15 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the specified user handles XHTML messages.
|
|
|
|
*
|
|
|
|
* @param connection the connection to use to perform the service discovery
|
|
|
|
* @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
|
|
|
|
* @return a boolean indicating whether the specified user handles XHTML messages
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException
|
|
|
|
* @throws NoResponseException
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2003-10-14 01:10:15 +02:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public static boolean isServiceEnabled(XMPPConnection connection, Jid userID)
|
2015-02-14 09:43:44 +01:00
|
|
|
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
2014-07-05 14:19:31 +02:00
|
|
|
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
|
2003-10-14 01:10:15 +02:00
|
|
|
}
|
2003-09-14 16:46:38 +02:00
|
|
|
}
|