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

import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.channel.Conf;
import org.buddycloud.channelserver.db.CloseableIterator;
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.pubsub.model.GlobalItemID;
import org.buddycloud.channelserver.pubsub.model.NodeItem;
import org.buddycloud.channelserver.pubsub.model.impl.GlobalItemIDImpl;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.PacketError.Condition;
import org.xmpp.packet.PacketError.Type;

public class RecentItemsGet extends PubSubElementProcessorAbstract {

	private static final Logger LOGGER = Logger.getLogger(RecentItemsGet.class);
	private static final String NODE_SUFIX = "/posts";
	private static final SimpleDateFormat SDF = new SimpleDateFormat(Conf.DATE_FORMAT);
	static {
		SDF.setTimeZone(TimeZone.getTimeZone("UTC"));
	}

	private Date maxAge;
	private Integer maxItems;

	private Element pubsub;
	private SAXReader xmlReader;

	// RSM details
	private String firstItemId = null;
	private String lastItemId = null;
	private GlobalItemID afterItemId = null;
	private int maxResults = -1;

	public RecentItemsGet(BlockingQueue<Packet> outQueue,
			ChannelManager channelManager) {
		setChannelManager(channelManager);
		setOutQueue(outQueue);
		xmlReader = new SAXReader();
	}

	@Override
	public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
			throws Exception {
		response = IQ.createResultIQ(reqIQ);
		request = reqIQ;
		actor = actorJID;
		node = elm.attributeValue("node");
		resultSetManagement = rsm;

		if (null == actor) {
			actor = request.getFrom();
		}

		if (!isValidStanza()) {
			outQueue.put(response);
			return;
		}

		if (!channelManager.isLocalJID(request.getFrom())) {
			response.getElement().addAttribute("remote-server-discover",
					"false");
		}
		pubsub = response.getElement().addElement("pubsub",
				JabberPubsub.NAMESPACE_URI);
		try {
			parseRsmElement();
			addRecentItems();
			addRsmElement();
			outQueue.put(response);
		} catch (NodeStoreException e) {
			LOGGER.error(e);
			response.getElement().remove(pubsub);
			setErrorCondition(PacketError.Type.wait,
					PacketError.Condition.internal_server_error);
		}
		outQueue.put(response);

	}

	private void parseRsmElement() {
		if (null == resultSetManagement) {
			return;
		}
		
		Element max = null;
		Element after = null;
		if (null != (max = resultSetManagement.element("max"))) {
			maxResults = Integer.parseInt(max.getTextTrim());
		}
		
		if (null != (after = resultSetManagement.element("after"))) {
			try {
				afterItemId = GlobalItemIDImpl.fromString(after.getTextTrim());
			} catch(IllegalArgumentException e) {
				LOGGER.error(e);
				createExtendedErrorReply(Type.modify, Condition.bad_request, "Could not parse the 'after' id: " + after);
				return;
			}
		}
	}

	private void addRsmElement() throws NodeStoreException {
		if (null == firstItemId) {
			return;
		}
		Element rsm = pubsub.addElement("set");
		rsm.addNamespace("", NS_RSM);
		rsm.addElement("first").setText(firstItemId);
		rsm.addElement("last").setText(lastItemId);
		rsm.addElement("count").setText(
				String.valueOf(channelManager.getCountRecentItems(actor,
						maxAge, maxItems, NODE_SUFIX)));
	}

	private void addRecentItems() throws NodeStoreException {
		CloseableIterator<NodeItem> items = channelManager.getRecentItems(
				actor, maxAge, maxItems, maxResults, afterItemId, NODE_SUFIX);
		String lastNodeId = "";
		Element itemsElement = null;
		while (items.hasNext()) {
			NodeItem item = items.next();
            if (!item.getNodeId().equals(lastNodeId)) {
				itemsElement = pubsub.addElement("items");
				itemsElement.addAttribute("node", item.getNodeId());
				lastNodeId = item.getNodeId();<commit_msg>Using correct namespace for RSM.<commit_after>package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.get;

import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.channel.Conf;
import org.buddycloud.channelserver.db.CloseableIterator;
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.pubsub.model.GlobalItemID;
import org.buddycloud.channelserver.pubsub.model.NodeItem;
import org.buddycloud.channelserver.pubsub.model.impl.GlobalItemIDImpl;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.PacketError.Condition;
import org.xmpp.packet.PacketError.Type;

public class RecentItemsGet extends PubSubElementProcessorAbstract {

	private static final Logger LOGGER = Logger.getLogger(RecentItemsGet.class);
	private static final String NODE_SUFIX = "/posts";
	private static final SimpleDateFormat SDF = new SimpleDateFormat(Conf.DATE_FORMAT);
	static {
		SDF.setTimeZone(TimeZone.getTimeZone("UTC"));
	}

	private Date maxAge;
	private Integer maxItems;

	private Element pubsub;
	private SAXReader xmlReader;

	// RSM details
	private String firstItemId = null;
	private String lastItemId = null;
	private GlobalItemID afterItemId = null;
	private int maxResults = -1;

	public RecentItemsGet(BlockingQueue<Packet> outQueue,
			ChannelManager channelManager) {
		setChannelManager(channelManager);
		setOutQueue(outQueue);
		xmlReader = new SAXReader();
	}

	@Override
	public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
			throws Exception {
		response = IQ.createResultIQ(reqIQ);
		request = reqIQ;
		actor = actorJID;
		node = elm.attributeValue("node");
		resultSetManagement = rsm;

		if (null == actor) {
			actor = request.getFrom();
		}

		if (!isValidStanza()) {
			outQueue.put(response);
			return;
		}

		if (!channelManager.isLocalJID(request.getFrom())) {
			response.getElement().addAttribute("remote-server-discover",
					"false");
		}
		pubsub = response.getElement().addElement("pubsub",
				JabberPubsub.NAMESPACE_URI);
		try {
			parseRsmElement();
			addRecentItems();
			addRsmElement();
			outQueue.put(response);
		} catch (NodeStoreException e) {
			LOGGER.error(e);
			response.getElement().remove(pubsub);
			setErrorCondition(PacketError.Type.wait,
					PacketError.Condition.internal_server_error);
		}
		outQueue.put(response);

	}

	private void parseRsmElement() {
		if (null == resultSetManagement) {
			return;
		}
		
		Element max = null;
		Element after = null;
		if (null != (max = resultSetManagement.element("max"))) {
			maxResults = Integer.parseInt(max.getTextTrim());
		}
		
		if (null != (after = resultSetManagement.element("after"))) {
			try {
				afterItemId = GlobalItemIDImpl.fromString(after.getTextTrim());
			} catch(IllegalArgumentException e) {
				LOGGER.error(e);
				createExtendedErrorReply(Type.modify, Condition.bad_request, "Could not parse the 'after' id: " + after);
				return;
			}
		}
	}

	private void addRsmElement() throws NodeStoreException {
		if (null == firstItemId) {
			return;
		}
		Element rsm = pubsub.addElement("set", NS_RSM);
		rsm.addElement("first", NS_RSM).setText(firstItemId);
		rsm.addElement("last", NS_RSM).setText(lastItemId);
		rsm.addElement("count", NS_RSM).setText(
				String.valueOf(channelManager.getCountRecentItems(actor,
						maxAge, maxItems, NODE_SUFIX)));
	}

	private void addRecentItems() throws NodeStoreException {
		CloseableIterator<NodeItem> items = channelManager.getRecentItems(
				actor, maxAge, maxItems, maxResults, afterItemId, NODE_SUFIX);
		String lastNodeId = "";
		Element itemsElement = null;
		while (items.hasNext()) {
			NodeItem item = items.next();
            if (!item.getNodeId().equals(lastNodeId)) {
				itemsElement = pubsub.addElement("items");
				itemsElement.addAttribute("node", item.getNodeId());
				lastNodeId = item.getNodeId();
{'fraction_non_alphanumeric': 0.11584213451917733, 'fraction_numerical': 0.0011117287381878821, 'mean_word_length': 5.041638683680322, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, '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': '3146615', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2825, 'n_words': 529}