Block and Create a new Packet

Hello is it possible to block a Packet from a server then create a new one and sent it back to the same server (to simulate an answer) ?

Something like this:

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
    if (packet instanceof IQ && !incoming && !processed && packet.toString().contains("SOMETHING") {
        throw new PacketRejectedException();
    Packet responsePacket = (Packet) createElt(packet.getID(), packet.getTo().toString(), packet.getFrom().toString());

private static Element createElement(String id, String from, String to) {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("iq");
    root.addAttribute("type", "result").addAttribute("id", id).addAttribute("from", from).addAttribute("to", to);
    Element queryElt = root.addElement("query", "");
    return document.getRootElement();

Yup, that’s quite possible. You’re on the right track. For IQ stanzas, there’s a helpful utility method that allows you to create a ‘result’ stanza from any request stanza.

// Detect stanza to be blocked.
if (packet instanceof IQ && !incoming && !processed && packet.toString().contains("SOMETHING")) {
    final IQ blocked = (IQ) packet;

    if (blocked.isRequest()) {
        // Generate response.
        final IQ response = IQ.createResultIQ((IQ) packet);
        response.setError(new PacketError(PacketError.Condition.forbidden, PacketError.Type.auth, "You are not allowed to do this."));
        // Send the response.
    // Reject the original.
    throw new PacketRejectedException();