View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Servlet which returns the last messages of the room on which the user is connected
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       * Initialize the servlet
80       * @param config The servlet config
81       * @throws ServletException If an exception occurs that interrupts the servlet's normal operation
82       */
83      public void init( ServletConfig config ) throws ServletException
84      {
85          super.init( config );
86      }
87  
88      /**
89       * Process HTTP Get request
90       * @param request The http request
91       * @param response The http response
92       * @throws ServletException If an exception occurs that interrupts the servlet's normal operation
93       * @throws IOException If an I/O exception occurs
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      * Process HTTP Post request
107      * @param request The http request
108      * @param response The http response
109      * @throws ServletException If an exception occurs that interrupts the servlet's normal operation
110      * @throws IOException If an I/O exception occurs
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      * Clean the ressources
158      */
159     public void destroy(  )
160     {
161     }
162 
163     /**
164          * Builds a message
165      * @param strMessage The body of the message
166      * @return The message string
167      */
168     String buildMessage( String strMessage )
169     {
170         return buildMessage( CMD_ADD_MSG, strMessage );
171     }
172 
173     /**
174      * Builds a message
175      * @param strCommand The message command
176      * @param strMessage The body of the message
177      * @return The message string
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      * Gets information about users
191      * @param request The Http request
192      * @return A string containing user information
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         // List of the messages
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                 // The message came before the arrival of the user, then go to the next one
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                     // No data is appended
249                     break;
250             }
251         }
252 
253         // List of the users
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         // Topic of the room
293         strData.append( buildMessage( CMD_SET_TOPIC, room.getDescription(  ) ) );
294         user.setLastAccessTime( new Date(  ) );
295 
296         return strData.toString(  );
297     }
298 }