1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.chat.web;
35
36 import fr.paris.lutece.plugins.chat.business.ChatEntry;
37 import fr.paris.lutece.plugins.chat.business.ChatRoom;
38 import fr.paris.lutece.plugins.chat.business.ChatUser;
39 import fr.paris.lutece.plugins.chat.service.ChatConstantes;
40 import fr.paris.lutece.plugins.chat.service.ChatService;
41 import fr.paris.lutece.portal.service.util.AppPropertiesService;
42
43 import java.io.IOException;
44 import java.io.PrintWriter;
45
46 import java.util.Date;
47 import java.util.Enumeration;
48
49 import javax.servlet.ServletConfig;
50 import javax.servlet.ServletException;
51 import javax.servlet.http.HttpServlet;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54
55
56
57
58
59
60 public class ChatServlet extends HttpServlet
61 {
62 private static final long serialVersionUID = -6400074588556875395L;
63 private static final String SEPARATOR = "\nend\n";
64 private static final String CMD_ADD_USER = "ADD USER:";
65 private static final String CMD_ADD_MSG = "ADD MESSAGE:";
66 private static final String CMD_SET_TOPIC = "SET TOPIC:";
67 private static final String CMD_NEW_PSEUDO = "NEW PSEUDO:";
68 private static final String CMD_KICK = "KICK:";
69 private static final String CONTENT_TYPE = "text/html";
70 private static final String MESSAGE_RECEPTION = "chat.msg.message.received";
71 private static final String MESSAGE_CONNEXION_CONFIRMATION = "chat.msg.connexion.established";
72 private static final String MESSAGE_INVALID_ROOM = "chat.msg.invalid.room";
73 private static final String MESSAGE_USER_BANNED = "chat.msg.user.banned";
74 private static final String MESSAGE_USER_ALREADY_EXIST = "chat.msg.user.already.exist";
75 private static final String MESSAGE_CONNEXION_FAILED = "chat.msg.connexion.failed";
76 private static final String MESSAGE_USER_KICKED = "chat.msg.user.kicked";
77
78
79
80
81
82
83 public void init( ServletConfig config ) throws ServletException
84 {
85 super.init( config );
86 }
87
88
89
90
91
92
93
94
95 public void doGet( HttpServletRequest request, HttpServletResponse response )
96 throws ServletException, IOException
97 {
98 response.setContentType( CONTENT_TYPE );
99
100 PrintWriter out = response.getWriter( );
101 String strUserData = getUserData( request );
102 out.println( strUserData );
103 }
104
105
106
107
108
109
110
111
112 public void doPost( HttpServletRequest request, HttpServletResponse response )
113 throws ServletException, IOException
114 {
115 response.setContentType( CONTENT_TYPE );
116
117 PrintWriter out = response.getWriter( );
118
119 if ( request.getParameter( ChatConstantes.PARAM_MESSAGE ) != null )
120 {
121 ChatService.newMessage( request );
122 out.println( AppPropertiesService.getProperty( MESSAGE_RECEPTION ) );
123 }
124 else
125 {
126 switch ( ChatService.doEnterRoom( request ) )
127 {
128 case ChatRoom.USER_ADDED:
129 out.println( buildMessage( AppPropertiesService.getProperty( MESSAGE_CONNEXION_CONFIRMATION ) ) );
130
131 break;
132
133 case ChatRoom.INVALID_ROOM:
134 out.println( buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_INVALID_ROOM ) ) );
135
136 break;
137
138 case ChatRoom.USER_IS_BANNED:
139 out.println( buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_USER_BANNED ) ) );
140
141 break;
142
143 case ChatRoom.USER_ALREADY_EXISTS:
144 out.println( buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_USER_ALREADY_EXIST ) ) );
145
146 break;
147
148 default:
149 out.println( AppPropertiesService.getProperty( MESSAGE_CONNEXION_FAILED ) );
150
151 break;
152 }
153 }
154 }
155
156
157
158
159 public void destroy( )
160 {
161 }
162
163
164
165
166
167
168 String buildMessage( String strMessage )
169 {
170 return buildMessage( CMD_ADD_MSG, strMessage );
171 }
172
173
174
175
176
177
178
179 String buildMessage( String strCommand, String strMessage )
180 {
181 StringBuffer strBuffer = new StringBuffer( );
182 strBuffer.append( strCommand );
183 strBuffer.append( strMessage );
184 strBuffer.append( SEPARATOR );
185
186 return strBuffer.toString( );
187 }
188
189
190
191
192
193
194 String getUserData( HttpServletRequest request )
195 {
196 StringBuffer strData = new StringBuffer( );
197 ChatRoom room = ChatService.getRoom( request );
198 String strPseudo = ChatService.getNickname( request );
199 ChatUser user = room.getUser( strPseudo );
200
201 if ( user == null )
202 {
203 return buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_USER_KICKED ) );
204 }
205
206 if ( user.isKicked( ) )
207 {
208 String strMessage = user.getKickComment( );
209 room.removeUser( strPseudo );
210
211 return buildMessage( CMD_KICK, strMessage );
212 }
213
214 if ( user.hasNewPseudo( ) )
215 {
216 room.removeOldPseudo( strPseudo );
217
218 return buildMessage( CMD_NEW_PSEUDO, user.getNickname( ) );
219 }
220
221
222 Enumeration entries = user.getChatEntries( );
223
224 while ( entries.hasMoreElements( ) )
225 {
226 ChatEntry entry = (ChatEntry) entries.nextElement( );
227
228 if ( entry.getTime( ) < user.getLastAccessTime( ).getTime( ) )
229 {
230
231 continue;
232 }
233
234 switch ( entry.getType( ) )
235 {
236 case ChatEntry.TYPE_MESSAGE:
237 strData.append( buildMessage( "<" + entry.getNickname( ) + "> " + entry.getChatMessage( ) ) );
238
239 break;
240
241 case ChatEntry.TYPE_NOTIFICATION:
242 strData.append( buildMessage( entry.getChatMessage( ) ) );
243
244 break;
245
246 default:
247
248
249 break;
250 }
251 }
252
253
254 Enumeration users = room.getUsers( );
255
256 while ( users.hasMoreElements( ) )
257 {
258 ChatUser u = (ChatUser) users.nextElement( );
259 strData.append( CMD_ADD_USER );
260
261 if ( u.getMode( ) == ChatUser.MODE_OP )
262 {
263 strData.append( "@" );
264 }
265 else if ( u.getMode( ) == ChatUser.MODE_VOICE )
266 {
267 strData.append( "+" );
268 }
269
270 strData.append( u.getNickname( ) );
271
272 if ( strPseudo.equals( u.getNickname( ) ) )
273 {
274 strData.append( "*" );
275 }
276
277 if ( u.isAway( ) )
278 {
279 if ( u.getAwayComment( ) != null )
280 {
281 strData.append( " (absent:" + u.getAwayComment( ) + ")" );
282 }
283 else
284 {
285 strData.append( " (absent)" );
286 }
287 }
288
289 strData.append( SEPARATOR );
290 }
291
292
293 strData.append( buildMessage( CMD_SET_TOPIC, room.getDescription( ) ) );
294 user.setLastAccessTime( new Date( ) );
295
296 return strData.toString( );
297 }
298 }