PublishedItem.java 10 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1
/**
2
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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.
Matt Tucker's avatar
Matt Tucker committed
15 16
 */

17
package org.jivesoftware.openfire.pubsub;
Matt Tucker's avatar
Matt Tucker committed
18

19 20
import java.io.Serializable;
import java.io.StringReader;
Matt Tucker's avatar
Matt Tucker committed
21
import java.util.Date;
22 23 24 25 26 27 28 29 30 31
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.pep.PEPServiceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
Matt Tucker's avatar
Matt Tucker committed
32 33 34 35 36 37 38 39 40 41 42 43 44

/**
 * A published item to a node. Once an item was published to a node, node subscribers will be
 * notified of the new published item. The item publisher may be allowed to delete published
 * items. After a published item was deleted node subscribers will get an event notification.<p>
 *
 * Published items may be persisted to the database depending on the node configuration.
 * Actually, even when the node is configured to not persist items the last published
 * item is going to be persisted to the database. The reason for this is that the node
 * may need to send the last published item to new subscribers.
 *
 * @author Matt Tucker
 */
45
public class PublishedItem implements Serializable {
Matt Tucker's avatar
Matt Tucker committed
46

47 48 49
    private static final Logger log = LoggerFactory.getLogger(PublishedItem.class);

    private static final int POOL_SIZE = 50;
Matt Tucker's avatar
Matt Tucker committed
50
    /**
51 52
     * Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers.
     */
53
    private static BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<>(POOL_SIZE);
54 55 56 57 58 59 60 61 62 63 64 65 66

    private static final long serialVersionUID = 7012925993623144574L;
    
    static {
        // Initialize the pool of sax readers
        for (int i=0; i<POOL_SIZE; i++) {
            SAXReader xmlReader = new SAXReader();
            xmlReader.setEncoding("UTF-8");
            xmlReaders.add(xmlReader);
        }    	
    }
	
	/**
Matt Tucker's avatar
Matt Tucker committed
67 68
     * JID of the entity that published the item to the node. This is the full JID
     * of the publisher.
Matt Tucker's avatar
Matt Tucker committed
69 70 71 72 73
     */
    private JID publisher;
    /**
     * The node where the item was published.
     */
74
	private volatile transient LeafNode node;
75 76 77 78 79 80 81 82
    /**
     * The id for the node where the item was published.
     */
    private String nodeId;
    /**
     * The id for the service hosting the node for this item
     */
    private String serviceId;
Matt Tucker's avatar
Matt Tucker committed
83 84 85 86 87 88 89 90 91
    /**
     * ID that uniquely identifies the published item in the node.
     */
    private String id;
    /**
     * The datetime when the items was published.
     */
    private Date creationDate;
    /**
92 93
     * The optional payload is included when publishing the item. This value
     * is created from the payload XML and cached as/when needed.
Matt Tucker's avatar
Matt Tucker committed
94
     */
95
	private volatile transient Element payload;
Matt Tucker's avatar
Matt Tucker committed
96
    /**
97
     * XML representation of the payload (for serialization)
Matt Tucker's avatar
Matt Tucker committed
98 99
     */
    private String payloadXML;
100 101 102 103 104 105 106 107
    
    /**
     * Creates a published item
     * @param node
     * @param publisher
     * @param id
     * @param creationDate
     */
Matt Tucker's avatar
Matt Tucker committed
108 109
    PublishedItem(LeafNode node, JID publisher, String id, Date creationDate) {
        this.node = node;
110 111
        this.nodeId = node.getNodeID();
        this.serviceId = node.getService().getServiceID();
Matt Tucker's avatar
Matt Tucker committed
112 113 114 115 116
        this.publisher = publisher;
        this.id = id;
        this.creationDate = creationDate;
    }

117 118 119 120 121 122 123 124 125
    /**
     * Returns the id for the {@link LeafNode} where this item was published.
     *
     * @return the ID for the leaf node where this item was published.
     */
    public String getNodeID() {
        return nodeId;
    }

Matt Tucker's avatar
Matt Tucker committed
126 127 128 129 130 131
    /**
     * Returns the {@link LeafNode} where this item was published.
     *
     * @return the leaf node where this item was published.
     */
    public LeafNode getNode() {
132
    	if (node == null) {
133 134
			synchronized (this) {
				if (node == null) {
135
					if (XMPPServer.getInstance().getPubSubModule().getServiceID().equals(serviceId))
136 137 138 139 140 141 142 143 144 145 146
					{
						node = (LeafNode) XMPPServer.getInstance().getPubSubModule().getNode(nodeId);
					}
					else
					{
						PEPServiceManager serviceMgr = XMPPServer.getInstance().getIQPEPHandler().getServiceManager();
						node = serviceMgr.hasCachedService(new JID(serviceId)) ? (LeafNode) serviceMgr.getPEPService(
								serviceId).getNode(nodeId) : null;
					}
				}
			}
147 148
    	}
    	return node;
Matt Tucker's avatar
Matt Tucker committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }

    /**
     * Returns the ID that uniquely identifies the published item in the node.
     *
     * @return the ID that uniquely identifies the published item in the node.
     */
    public String getID() {
        return id;
    }

    /**
     * Returns the JID of the entity that published the item to the node.
     *
     * @return the JID of the entity that published the item to the node.
     */
    public JID getPublisher() {
        return publisher;
    }

    /**
     * Returns the datetime when the items was published.
     *
     * @return the datetime when the items was published.
     */
    public Date getCreationDate() {
        return creationDate;
    }

    /**
     * Returns the payload included when publishing the item. A published item may or may not
     * have a payload. Transient nodes that are configured to not broadcast payloads may allow
     * published items to have no payload.
     *
     * @return the payload included when publishing the item or <tt>null</tt> if none was found.
     */
    public Element getPayload() {
186
    	if (payload == null && payloadXML != null) {
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    		synchronized (this) {
				if (payload == null) {
		    		// payload initialized as XML string from DB
		            SAXReader xmlReader = null;
		    		try {
		    			xmlReader = xmlReaders.take();
		    			payload = xmlReader.read(new StringReader(payloadXML)).getRootElement(); 
		    		} catch (Exception ex) {
		    			 log.error("Failed to parse payload XML", ex);
		    		} finally {
		    			if (xmlReader != null) {
		    				xmlReaders.add(xmlReader);
		    			}
		    		}
				}
			}
203
    	}
Matt Tucker's avatar
Matt Tucker committed
204 205 206
        return payload;
    }

Matt Tucker's avatar
Matt Tucker committed
207 208 209 210 211 212 213 214 215 216 217
    /**
     * Returns a textual representation of the payload or <tt>null</tt> if no payload
     * was specified with the item.
     *
     * @return a textual representation of the payload or null if no payload was specified
     *         with the item.
     */
    public String getPayloadXML() {
        return payloadXML;
    }

218 219 220 221 222 223 224 225 226 227 228 229 230
    /**
     * Sets the payload included when publishing the item. A published item may or may not
     * have a payload. Transient nodes that are configured to not broadcast payloads may allow
     * published items to have no payload.
     *
     * @param payloadXML the payload included when publishing the item or <tt>null</tt>
     *        if none was found.
     */
    void setPayloadXML(String payloadXML) {
    	this.payloadXML = payloadXML;
    	this.payload = null; // will be recreated only if needed
    }

Matt Tucker's avatar
Matt Tucker committed
231 232 233 234 235 236 237 238 239 240 241 242 243
    /**
     * Sets the payload included when publishing the item. A published item may or may not
     * have a payload. Transient nodes that are configured to not broadcast payloads may allow
     * published items to have no payload.
     *
     * @param payload the payload included when publishing the item or <tt>null</tt>
     *        if none was found.
     */
    void setPayload(Element payload) {
        this.payload = payload;
        // Update XML representation of the payload
        if (payload == null) {
            payloadXML = null;
244
        } else {
Matt Tucker's avatar
Matt Tucker committed
245 246 247 248 249 250 251 252 253 254 255 256
            payloadXML = payload.asXML();
        }
    }

    /**
     * Returns true if payload contains the specified keyword. If the item has no payload
     * or keyword is <tt>null</tt> then return true.
     *
     * @param keyword the keyword to look for in the payload.
     * @return true if payload contains the specified keyword.
     */
    boolean containsKeyword(String keyword) {
257
        if (getPayloadXML() == null || keyword == null) {
Matt Tucker's avatar
Matt Tucker committed
258 259 260 261
            return true;
        }
        return payloadXML.contains(keyword);
    }
Matt Tucker's avatar
Matt Tucker committed
262 263 264 265 266 267 268 269 270 271

    /**
     * Returns true if the user that is trying to delete an item is allowed to delete it.
     * Only the publisher or node admins (i.e. owners and sysadmins) are allowed to delete items.
     *
     * @param user the full JID of the user trying to delete the item.
     * @return true if the user that is trying to delete an item is allowed to delete it.
     */
    public boolean canDelete(JID user) {
        if (publisher.equals(user) || publisher.toBareJID().equals(user.toBareJID()) ||
272
                getNode().isAdmin(user)) {
Matt Tucker's avatar
Matt Tucker committed
273 274 275 276
            return true;
        }
        return false;
    }
277 278 279 280 281 282 283 284 285 286

    /**
     * Returns a string that uniquely identifies this published item
     * in the following format: <i>nodeId:itemId</i>
     * @return Unique identifier for this item
     */
    public String getItemKey() {
    	return getItemKey(nodeId,id);
    }

287
	/**
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
     * Returns a string that uniquely identifies this published item
     * in the following format: <i>nodeId:itemId</i>
     * @param node Node for the published item
     * @param itemId Id for the published item (unique within the node)
     * @return Unique identifier for this item
     */
    public static String getItemKey(LeafNode node, String itemId) {
    	return getItemKey(node.getNodeID(), itemId);
    }

    /**
     * Returns a string that uniquely identifies this published item
     * in the following format: <i>nodeId:itemId</i>
     * @param nodeId Node id for the published item
     * @param itemId Id for the published item (unique within the node)
     * @return Unique identifier for this item
     */
    public static String getItemKey(String nodeId, String itemId) {
    	return new StringBuilder(nodeId)
307
    		.append(':').append(itemId).toString();
308
    }
Matt Tucker's avatar
Matt Tucker committed
309
}