KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > ClientSocketReader


1 /**
2  * $RCSfile: ClientSocketReader.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/05/23 17:45:48 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.dom4j.Element;
15 import org.jivesoftware.messenger.ClientSession;
16 import org.jivesoftware.messenger.PacketRouter;
17 import org.jivesoftware.messenger.auth.UnauthorizedException;
18 import org.xmlpull.v1.XmlPullParserException;
19 import org.xmpp.packet.IQ;
20 import org.xmpp.packet.Message;
21 import org.xmpp.packet.Presence;
22
23 import java.io.IOException JavaDoc;
24 import java.net.Socket JavaDoc;
25
26 /**
27  * A SocketReader specialized for client connections. This reader will be used when the open
28  * stream contains a jabber:client namespace. Received packet will have their FROM attribute
29  * overriden to avoid spoofing.
30  *
31  * @author Gaston Dombiak
32  */

33 public class ClientSocketReader extends SocketReader {
34
35     public ClientSocketReader(PacketRouter router, String JavaDoc serverName, Socket JavaDoc socket,
36             SocketConnection connection) {
37         super(router, serverName, socket, connection);
38     }
39
40     protected void processIQ(IQ packet) throws UnauthorizedException {
41         // Overwrite the FROM attribute to avoid spoofing
42
packet.setFrom(session.getAddress());
43         super.processIQ(packet);
44     }
45
46     protected void processPresence(Presence packet) throws UnauthorizedException {
47         // Overwrite the FROM attribute to avoid spoofing
48
packet.setFrom(session.getAddress());
49         super.processPresence(packet);
50     }
51
52     protected void processMessage(Message packet) throws UnauthorizedException {
53         // Overwrite the FROM attribute to avoid spoofing
54
packet.setFrom(session.getAddress());
55         super.processMessage(packet);
56     }
57
58     /**
59      * Only packets of type Message, Presence and IQ can be processed by this class. Any other
60      * type of packet is unknown and thus rejected generating the connection to be closed.
61      *
62      * @param doc the unknown DOM element that was received
63      * @return always false.
64      */

65     protected boolean processUnknowPacket(Element doc) {
66         return false;
67     }
68
69     boolean createSession(String JavaDoc namespace) throws UnauthorizedException, XmlPullParserException,
70             IOException JavaDoc {
71         if ("jabber:client".equals(namespace)) {
72             // The connected client is a regular client so create a ClientSession
73
session = ClientSession.createSession(serverName, reader, connection);
74             return true;
75         }
76         return false;
77     }
78
79 }
80
Popular Tags