001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.xhtmlim;
019
020import java.util.List;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.SmackException.NoResponseException;
024import org.jivesoftware.smack.SmackException.NotConnectedException;
025import org.jivesoftware.smack.XMPPConnection;
026import org.jivesoftware.smack.XMPPConnectionRegistry;
027import org.jivesoftware.smack.XMPPException.XMPPErrorException;
028import org.jivesoftware.smack.packet.Message;
029import org.jivesoftware.smack.packet.MessageBuilder;
030import org.jivesoftware.smack.packet.MessageView;
031
032import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
033import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
034
035import org.jxmpp.jid.Jid;
036
037/**
038 * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to
039 * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP
040 * clients support XHTML.
041 *
042 * @author Gaston Dombiak
043 */
044public class XHTMLManager {
045    static {
046        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
047            @Override
048            public void connectionCreated(XMPPConnection connection) {
049                // Enable the XHTML support on every established connection
050                XHTMLManager.setServiceEnabled(connection, true);
051            }
052        });
053    }
054
055    /**
056     * Returns an Iterator for the XHTML bodies in the message. Returns null if
057     * the message does not contain an XHTML extension.
058     *
059     * @param message an XHTML message
060     * @return an Iterator for the bodies in the message or null if none.
061     */
062    public static List<CharSequence> getBodies(MessageView message) {
063        XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
064        if (xhtmlExtension != null)
065            return xhtmlExtension.getBodies();
066        else
067            return null;
068    }
069
070    /**
071     * Adds an XHTML body to the message.
072     *
073     * @param messageBuilder the message that will receive the XHTML body
074     * @param xhtmlText the string to add as an XHTML body to the message
075     */
076    public static void addBody(MessageBuilder messageBuilder, XHTMLText xhtmlText) {
077        XHTMLExtension xhtmlExtension = XHTMLExtension.from(messageBuilder);
078        if (xhtmlExtension == null) {
079            // Create an XHTMLExtension and add it to the message
080            xhtmlExtension = new XHTMLExtension();
081            messageBuilder.addExtension(xhtmlExtension);
082        }
083        // Add the required bodies to the message
084        xhtmlExtension.addBody(xhtmlText.toXML());
085    }
086
087    /**
088     * Adds an XHTML body to the message.
089     *
090     * @param message the message that will receive the XHTML body
091     * @param xhtmlText the string to add as an XHTML body to the message
092     * @deprecated use {@link #addBody(MessageBuilder, XHTMLText)} instead.
093     */
094    // TODO: Remove in Smack 4.6
095    @Deprecated
096    public static void addBody(Message message, XHTMLText xhtmlText) {
097        XHTMLExtension xhtmlExtension = XHTMLExtension.from(message);
098        if (xhtmlExtension == null) {
099            // Create an XHTMLExtension and add it to the message
100            xhtmlExtension = new XHTMLExtension();
101            message.addExtension(xhtmlExtension);
102        }
103        // Add the required bodies to the message
104        xhtmlExtension.addBody(xhtmlText.toXML());
105    }
106
107    /**
108     * Returns true if the message contains an XHTML extension.
109     *
110     * @param message the message to check if contains an XHTML extension or not
111     * @return a boolean indicating whether the message is an XHTML message
112     */
113    public static boolean isXHTMLMessage(Message message) {
114        return message.getExtensionElement(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
115    }
116
117    /**
118     * Enables or disables the XHTML support on a given connection.<p>
119     *
120     * Before starting to send XHTML messages to a user, check that the user can handle XHTML
121     * messages. Enable the XHTML support to indicate that this client handles XHTML messages.
122     *
123     * @param connection the connection where the service will be enabled or disabled
124     * @param enabled indicates if the service will be enabled or disabled
125     */
126    public static synchronized void setServiceEnabled(XMPPConnection connection, boolean enabled) {
127        if (isServiceEnabled(connection) == enabled)
128            return;
129
130        if (enabled) {
131            ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
132        }
133        else {
134            ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
135        }
136    }
137
138    /**
139     * Returns true if the XHTML support is enabled for the given connection.
140     *
141     * @param connection the connection to look for XHTML support
142     * @return a boolean indicating if the XHTML support is enabled for the given connection
143     */
144    public static boolean isServiceEnabled(XMPPConnection connection) {
145        return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
146    }
147
148    /**
149     * Returns true if the specified user handles XHTML messages.
150     *
151     * @param connection the connection to use to perform the service discovery
152     * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com
153     * @return a boolean indicating whether the specified user handles XHTML messages
154     * @throws XMPPErrorException if there was an XMPP error returned.
155     * @throws NoResponseException if there was no response from the remote entity.
156     * @throws NotConnectedException if the XMPP connection is not connected.
157     * @throws InterruptedException if the calling thread was interrupted.
158     */
159    public static boolean isServiceEnabled(XMPPConnection connection, Jid userID)
160                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
161        return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
162    }
163}