KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > MessageHandler


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.server;
21
22 import java.io.IOException JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.lucane.common.Logging;
27 import org.lucane.common.Message;
28 import org.lucane.common.concepts.ServiceConcept;
29 import org.lucane.common.concepts.UserConcept;
30 import org.lucane.common.net.ObjectConnection;
31 import org.lucane.server.store.Store;
32
33 /**
34  * Reads messages from the network.
35  * A message is either a command for the server
36  * or for an internal Service.
37  */

38 public class MessageHandler extends Thread JavaDoc
39 {
40     private Socket JavaDoc socket;
41     
42     private ObjectConnection connection;
43     private Message message;
44     
45     boolean isAlreadyConnected;
46     boolean isAuthenticationMessage;
47     boolean isServerInfoMessage;
48     
49     /**
50      * Constructor
51      *
52      * @param socket the client socket
53      */

54     public MessageHandler(Socket JavaDoc socket)
55     {
56         this.socket = socket;
57     }
58     
59     /**
60      * The main method
61      */

62     public void run()
63     {
64         //init handler
65
try {
66             init();
67         }
68         catch (Exception JavaDoc ex)
69         {
70             ex.printStackTrace();
71             Logging.getLogger().warning("#Err > Unable to read message.");
72             closeConnection();
73             return;
74         }
75                 
76         //check if the user is connected
77
if(!isAlreadyConnected && !isAuthenticationMessage && !isServerInfoMessage)
78         {
79             Logging.getLogger().info("Access denied to " + message.getSender());
80             try {
81                 connection.write("FAILED No Connection");
82             } catch (Exception JavaDoc e) {}
83         }
84         
85         //the message is for the server
86
else if (message.getApplication().equals("Server"))
87             handleServerMessage();
88         
89         //the message is for a service
90
else
91             handleServiceMessage();
92         
93         closeConnection();
94     }
95     
96     /**
97      * Init attributes
98      */

99     private void init()
100     throws IOException JavaDoc, ClassNotFoundException JavaDoc
101     {
102         connection = new ObjectConnection(socket);
103         message = (Message)connection.read();
104         
105         isAlreadyConnected = ConnectInfoManager.getInstance().isConnected(message.getSender());
106         isAuthenticationMessage = message.getApplication().equals("Server")
107             && ((String JavaDoc)message.getData()).startsWith("AUTH");
108         isServerInfoMessage = message.getApplication().equals("Server")
109             && ((String JavaDoc)message.getData()).startsWith("GET_SERVER_INFO");
110     }
111     
112     /**
113      * Handle a server message
114      */

115     private void handleServerMessage()
116     {
117         String JavaDoc command = "";
118         String JavaDoc parameters = "";
119         
120         //separate command and parameters
121
try
122         {
123             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc((String JavaDoc)message.getData());
124             command = stk.nextToken();
125             parameters = stk.nextToken("\0").substring(1);
126         }
127         catch (Exception JavaDoc ex)
128         {
129             //unable to parse
130
}
131         
132         // if the user asks for authentication, we try to do it and exits this method
133
if(isAuthenticationMessage)
134         {
135             sendAck();
136             Server.getInstance().getAuthenticator().authenticate(connection, message, parameters);
137             return;
138         }
139         
140         //all checks are ok, execute the command
141
executeCommand(command, parameters);
142     }
143     
144     /**
145      * Handle a service message
146      */

147     private void handleServiceMessage()
148     {
149         String JavaDoc userName = message.getSender().getName();
150         String JavaDoc serviceName = message.getApplication();
151         
152         //get the service
153
Store store = Server.getInstance().getStore();
154         Service s = ServiceManager.getInstance().getService(serviceName);
155         
156         //ensure that the service is running
157
if(s == null)
158         {
159             try {
160                 connection.write("FAILED not running");
161             } catch (Exception JavaDoc e) {}
162             Logging.getLogger().warning("Service "+ serviceName + " not running");
163             return;
164         }
165         
166         
167         //check for permission
168
boolean isAuthorizedService = false;
169         try {
170             UserConcept user = store.getUserStore().getUser(userName);
171             ServiceConcept service = store.getServiceStore().getService(serviceName);
172             isAuthorizedService = store.getServiceStore().isAuthorizedService(user, service);
173         } catch(Exception JavaDoc e) {
174             Logging.getLogger().warning("Error while checking service permission");
175             e.printStackTrace();
176         }
177         
178         if(!isAuthorizedService)
179         {
180             Logging.getLogger().warning(serviceName + " : Service denied to " + userName);
181             try {
182                 connection.write("FAILED You don't have acces to this service");
183             } catch (Exception JavaDoc e) {}
184             return;
185         }
186         
187         //process the message
188
sendAck();
189         s.process(connection, message);
190     }
191     
192     /**
193      * Execute server commands
194      */

195     private void executeCommand(String JavaDoc command, String JavaDoc parameters)
196     {
197         if (command.equals("GET_SERVER_INFO"))
198         {
199             sendAck();
200             Server.getInstance().sendServerConnectInfo(connection);
201         }
202
203         else if (command.equals("CONNECT_DEL"))
204         {
205             sendAck();
206             ConnectInfoManager.getInstance().removeConnectInfo(message.getSender());
207         }
208         
209         else if (command.equals("CONNECT_GET"))
210         {
211             sendAck();
212             Server.getInstance().sendConnectInfo(parameters, connection);
213         }
214         
215         else if (command.equals("CONNECT_LIST"))
216         {
217             sendAck();
218             Server.getInstance().sendUserList(connection);
219         }
220         
221         else if (command.equals("PLUGIN_LIST"))
222         {
223             sendAck();
224             Server.getInstance().sendPluginList(connection, message.getSender().getName());
225         }
226         
227         else if (command.equals("PLUGIN_GET"))
228         {
229             sendAck();
230             Server.getInstance().sendPluginFile(connection, parameters);
231         }
232         
233         else if (command.equals("STARTUP_PLUGINS"))
234         {
235             sendAck();
236             Server.getInstance().sendStartupPlugin(connection, message.getSender().getName());
237         }
238         
239         else
240         {
241             try {
242                 connection.write("FAILED Unknown command");
243             } catch (Exception JavaDoc e) {}
244         }
245     }
246     
247     /**
248      * Send "OK" to the client
249      */

250     private void sendAck()
251     {
252         try {
253             connection.write("OK");
254         } catch (Exception JavaDoc e) {}
255     }
256
257     /**
258      * Close the connection & socket
259      */

260     private void closeConnection()
261     {
262         try {
263             this.connection.close();
264             this.socket.close();
265         } catch (IOException JavaDoc e) {}
266     }
267 }
Popular Tags