<commit_before>package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.set;

import java.util.Collection;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.channel.ValidatePayload;
import org.buddycloud.channelserver.channel.validate.UnknownContentTypeException;
import org.buddycloud.channelserver.channel.validate.ValidateEntry;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubSet;
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
import org.buddycloud.channelserver.pubsub.model.impl.GlobalItemIDImpl;
import org.buddycloud.channelserver.pubsub.model.impl.NodeItemImpl;
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.resultsetmanagement.ResultSet;

public class Publish extends PubSubElementProcessorAbstract {

	private static final Logger LOGGER = Logger.getLogger(Publish.class);

	public static final String MISSING_ITEM_ELEMENT = "item-required";
	public static final String NODE_ID_REQUIRED = "nodeid-required";

	private Element entry;
	private String id;
	private JID publishersJID;
	private String inReplyTo;
	private Element item;

	private ValidateEntry validator;

	private String globalItemId;

	public Publish(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
		this.outQueue = outQueue;
		this.channelManager = channelManager;
	}

	@Override
	public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
			throws Exception {

		request = reqIQ;
		response = IQ.createResultIQ(reqIQ);
		publishersJID = request.getFrom();

		node = request.getChildElement().element("publish")
				.attributeValue("node");

		if (false == checkNode())
			return;

		boolean isLocalSubscriber = false;

		if (actorJID != null) {
			publishersJID = actorJID;
		} else {

			isLocalSubscriber = channelManager.isLocalJID(publishersJID);

			// Check that user is registered.
			if (!isLocalSubscriber) {

				// If the packet did not have actor, and the sender is not a
				// local user.
				// publishing is not allowed.

				/*
				 * <iq type='error' from='pubsub.shakespeare.lit'
				 * to='hamlet@denmark.lit/elsinore' id='create1'> <error
				 * type='auth'> <registration-required
				 * xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> </error> </iq>
				 */
				response.setType(Type.error);
				PacketError pe = new PacketError(
						org.xmpp.packet.PacketError.Condition.registration_required,
						org.xmpp.packet.PacketError.Type.auth);
				response.setError(pe);
				outQueue.put(response);
				return;
			}
		}

		try {
			if (false == nodeExists())
				return;
			if (false == userCanPost())
				return;
			if (false == isRequestValid())
				return;
			extractItemDetails();
			saveNodeItem();
			updateThreadParent();
			sendResponseStanza();
			sendNotifications();

		} catch (NodeStoreException e) {
			setErrorCondition(PacketError.Type.wait,
					PacketError.Condition.internal_server_error);
			outQueue.put(response);
		} catch (UnknownContentTypeException e) {
			createExtendedErrorReply(PacketError.Type.modify,
					PacketError.Condition.not_acceptable,
					ValidatePayload.UNSUPPORTED_CONTENT_TYPE);
		}

	}

	private void updateThreadParent() throws NodeStoreException {
		if (null == inReplyTo) return;
		channelManager.updateThreadParent(node, inReplyTo);
	}

	private void saveNodeItem() throws NodeStoreException {
		// Let's store the new item.
		channelManager.addNodeItem(new NodeItemImpl(node, id, new Date(), entry
				.asXML(), inReplyTo));
	}

	private void extractItemDetails() throws InterruptedException {

		entry = validator.getPayload();
		globalItemId = entry.element("id").getText();
		if (false == GlobalItemIDImpl.isGlobalId(globalItemId)) {
			globalItemId = new GlobalItemIDImpl(request.getTo(), node,
					globalItemId).toString();<commit_msg>Add braces, like a proper gentleman<commit_after>package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.set;

import java.util.Collection;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.channel.ValidatePayload;
import org.buddycloud.channelserver.channel.validate.UnknownContentTypeException;
import org.buddycloud.channelserver.channel.validate.ValidateEntry;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubSet;
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
import org.buddycloud.channelserver.pubsub.model.impl.GlobalItemIDImpl;
import org.buddycloud.channelserver.pubsub.model.impl.NodeItemImpl;
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.resultsetmanagement.ResultSet;

public class Publish extends PubSubElementProcessorAbstract {

	private static final Logger LOGGER = Logger.getLogger(Publish.class);

	public static final String MISSING_ITEM_ELEMENT = "item-required";
	public static final String NODE_ID_REQUIRED = "nodeid-required";

	private Element entry;
	private String id;
	private JID publishersJID;
	private String inReplyTo;
	private Element item;

	private ValidateEntry validator;

	private String globalItemId;

	public Publish(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
		this.outQueue = outQueue;
		this.channelManager = channelManager;
	}

	@Override
	public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
			throws Exception {

		request = reqIQ;
		response = IQ.createResultIQ(reqIQ);
		publishersJID = request.getFrom();

		node = request.getChildElement().element("publish")
				.attributeValue("node");

		if (false == checkNode())
			return;

		boolean isLocalSubscriber = false;

		if (actorJID != null) {
			publishersJID = actorJID;
		} else {

			isLocalSubscriber = channelManager.isLocalJID(publishersJID);

			// Check that user is registered.
			if (!isLocalSubscriber) {

				// If the packet did not have actor, and the sender is not a
				// local user.
				// publishing is not allowed.

				/*
				 * <iq type='error' from='pubsub.shakespeare.lit'
				 * to='<EMAIL>/elsinore' id='create1'> <error
				 * type='auth'> <registration-required
				 * xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> </error> </iq>
				 */
				response.setType(Type.error);
				PacketError pe = new PacketError(
						org.xmpp.packet.PacketError.Condition.registration_required,
						org.xmpp.packet.PacketError.Type.auth);
				response.setError(pe);
				outQueue.put(response);
				return;
			}
		}

		try {
			if (false == nodeExists())
				return;
			if (false == userCanPost())
				return;
			if (false == isRequestValid())
				return;
			extractItemDetails();
			saveNodeItem();
			updateThreadParent();
			sendResponseStanza();
			sendNotifications();

		} catch (NodeStoreException e) {
			setErrorCondition(PacketError.Type.wait,
					PacketError.Condition.internal_server_error);
			outQueue.put(response);
		} catch (UnknownContentTypeException e) {
			createExtendedErrorReply(PacketError.Type.modify,
					PacketError.Condition.not_acceptable,
					ValidatePayload.UNSUPPORTED_CONTENT_TYPE);
		}

	}

	private void updateThreadParent() throws NodeStoreException {
		if (null == inReplyTo) {
			return;
		}
		channelManager.updateThreadParent(node, inReplyTo);
	}

	private void saveNodeItem() throws NodeStoreException {
		// Let's store the new item.
		channelManager.addNodeItem(new NodeItemImpl(node, id, new Date(), entry
				.asXML(), inReplyTo));
	}

	private void extractItemDetails() throws InterruptedException {

		entry = validator.getPayload();
		globalItemId = entry.element("id").getText();
		if (false == GlobalItemIDImpl.isGlobalId(globalItemId)) {
			globalItemId = new GlobalItemIDImpl(request.getTo(), node,
					globalItemId).toString();
{'fraction_non_alphanumeric': 0.10597408302218318, 'fraction_numerical': 0.0008785416209092906, 'mean_word_length': 4.883074935400517, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4828745', 'n_tokens_mistral': 3121, 'n_tokens_neox': 2761, 'n_words': 538}