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 org.jivesoftware.smack.ConnectionCreationListener; 021import org.jivesoftware.smack.SmackException.NoResponseException; 022import org.jivesoftware.smack.SmackException.NotConnectedException; 023import org.jivesoftware.smack.XMPPConnection; 024import org.jivesoftware.smack.XMPPException.XMPPErrorException; 025import org.jivesoftware.smack.packet.Message; 026import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 027import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension; 028 029import java.util.List; 030 031/** 032 * Manages XHTML formatted texts within messages. A XHTMLManager provides a high level access to 033 * get and set XHTML bodies to messages, enable and disable XHTML support and check if remote XMPP 034 * clients support XHTML. 035 * 036 * @author Gaston Dombiak 037 */ 038public class XHTMLManager { 039 private final static String namespace = "http://jabber.org/protocol/xhtml-im"; 040 041 // Enable the XHTML support on every established connection 042 // The ServiceDiscoveryManager class should have been already initialized 043 static { 044 XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() { 045 public void connectionCreated(XMPPConnection connection) { 046 XHTMLManager.setServiceEnabled(connection, true); 047 } 048 }); 049 } 050 051 /** 052 * Returns an Iterator for the XHTML bodies in the message. Returns null if 053 * the message does not contain an XHTML extension. 054 * 055 * @param message an XHTML message 056 * @return an Iterator for the bodies in the message or null if none. 057 */ 058 public static List<String> getBodies(Message message) { 059 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); 060 if (xhtmlExtension != null) 061 return xhtmlExtension.getBodies(); 062 else 063 return null; 064 } 065 066 /** 067 * Adds an XHTML body to the message. 068 * 069 * @param message the message that will receive the XHTML body 070 * @param body the string to add as an XHTML body to the message 071 */ 072 public static void addBody(Message message, String body) { 073 XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); 074 if (xhtmlExtension == null) { 075 // Create an XHTMLExtension and add it to the message 076 xhtmlExtension = new XHTMLExtension(); 077 message.addExtension(xhtmlExtension); 078 } 079 // Add the required bodies to the message 080 xhtmlExtension.addBody(body); 081 } 082 083 /** 084 * Returns true if the message contains an XHTML extension. 085 * 086 * @param message the message to check if contains an XHTML extentsion or not 087 * @return a boolean indicating whether the message is an XHTML message 088 */ 089 public static boolean isXHTMLMessage(Message message) { 090 return message.getExtension("html", namespace) != null; 091 } 092 093 /** 094 * Enables or disables the XHTML support on a given connection.<p> 095 * 096 * Before starting to send XHTML messages to a user, check that the user can handle XHTML 097 * messages. Enable the XHTML support to indicate that this client handles XHTML messages. 098 * 099 * @param connection the connection where the service will be enabled or disabled 100 * @param enabled indicates if the service will be enabled or disabled 101 */ 102 public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) { 103 if (isServiceEnabled(connection) == enabled) 104 return; 105 106 if (enabled) { 107 ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace); 108 } 109 else { 110 ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(namespace); 111 } 112 } 113 114 /** 115 * Returns true if the XHTML support is enabled for the given connection. 116 * 117 * @param connection the connection to look for XHTML support 118 * @return a boolean indicating if the XHTML support is enabled for the given connection 119 */ 120 public static boolean isServiceEnabled(XMPPConnection connection) { 121 return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(namespace); 122 } 123 124 /** 125 * Returns true if the specified user handles XHTML messages. 126 * 127 * @param connection the connection to use to perform the service discovery 128 * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com 129 * @return a boolean indicating whether the specified user handles XHTML messages 130 * @throws XMPPErrorException 131 * @throws NoResponseException 132 * @throws NotConnectedException 133 */ 134 public static boolean isServiceEnabled(XMPPConnection connection, String userID) 135 throws NoResponseException, XMPPErrorException, NotConnectedException { 136 return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, namespace); 137 } 138}