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.util.Date;
44  import java.util.Enumeration;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  
49  /**
50   *
51   */
52  public class ChatJspBean
53  {
54      private static final long serialVersionUID = -6400074588556875395L;
55      private static final String SEPARATOR = "\nend\n";
56      private static final String CMD_ADD_USER = "ADD USER:";
57      private static final String CMD_ADD_MSG = "ADD MESSAGE:";
58      private static final String CMD_SET_TOPIC = "SET TOPIC:";
59      private static final String CMD_NEW_PSEUDO = "NEW PSEUDO:";
60      private static final String CMD_KICK = "KICK:";
61      private static final String CONTENT_TYPE = "text/html";
62      private static final String MESSAGE_RECEPTION = "chat.msg.message.received";
63      private static final String MESSAGE_CONNEXION_CONFIRMATION = "chat.msg.connexion.established";
64      private static final String MESSAGE_INVALID_ROOM = "chat.msg.invalid.room";
65      private static final String MESSAGE_USER_BANNED = "chat.msg.user.banned";
66      private static final String MESSAGE_USER_ALREADY_EXIST = "chat.msg.user.already.exist";
67      private static final String MESSAGE_CONNEXION_FAILED = "chat.msg.connexion.failed";
68      private static final String MESSAGE_USER_KICKED = "chat.msg.user.kicked";
69  
70      public String process( HttpServletRequest request )
71      {
72          String strReturn = null;
73          System.out.println( request.getMethod(  ) + request.getParameterNames(  ).toString(  ) );
74  
75          if ( request.getMethod(  ).equalsIgnoreCase( "get" ) )
76          {
77              return getUserData( request );
78          }
79          else
80          {
81              if ( request.getParameter( ChatConstantes.PARAM_MESSAGE ) != null )
82              {
83                  ChatService.newMessage( request );
84                  strReturn = AppPropertiesService.getProperty( MESSAGE_RECEPTION );
85              }
86              else
87              {
88                  switch ( ChatService.doEnterRoom( request ) )
89                  {
90                      case ChatRoom.USER_ADDED:
91                          strReturn = buildMessage( AppPropertiesService.getProperty( MESSAGE_CONNEXION_CONFIRMATION ) );
92  
93                          break;
94  
95                      case ChatRoom.INVALID_ROOM:
96                          strReturn = buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_INVALID_ROOM ) );
97  
98                          break;
99  
100                     case ChatRoom.USER_IS_BANNED:
101                         strReturn = buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_USER_BANNED ) );
102 
103                         break;
104 
105                     case ChatRoom.USER_ALREADY_EXISTS:
106                         strReturn = buildMessage( CMD_KICK,
107                                 AppPropertiesService.getProperty( MESSAGE_USER_ALREADY_EXIST ) );
108 
109                         break;
110 
111                     default:
112                         strReturn = AppPropertiesService.getProperty( MESSAGE_CONNEXION_FAILED );
113 
114                         break;
115                 }
116             }
117         }
118 
119         return strReturn;
120     }
121 
122     /**
123      * Builds a message
124      * @param strMessage The body of the message
125      * @return The message string
126      */
127     private String buildMessage( String strMessage )
128     {
129         return buildMessage( CMD_ADD_MSG, strMessage );
130     }
131 
132     /**
133      * Builds a message
134      * @param strCommand The message command
135      * @param strMessage The body of the message
136      * @return The message string
137      */
138     private String buildMessage( String strCommand, String strMessage )
139     {
140         StringBuffer strBuffer = new StringBuffer(  );
141         strBuffer.append( strCommand );
142         strBuffer.append( strMessage );
143         strBuffer.append( SEPARATOR );
144 
145         return strBuffer.toString(  );
146     }
147 
148     /**
149      * Gets information about users
150      * @param request The Http request
151      * @return A string containing user information
152      */
153     private String getUserData( HttpServletRequest request )
154     {
155         StringBuffer strData = new StringBuffer(  );
156         ChatRoom room = ChatService.getRoom( request );
157         String strPseudo = ChatService.getNickname( request );
158         ChatUser user = room.getUser( strPseudo );
159 
160         if ( user == null )
161         {
162             return buildMessage( CMD_KICK, AppPropertiesService.getProperty( MESSAGE_USER_KICKED ) );
163         }
164 
165         if ( user.isKicked(  ) )
166         {
167             String strMessage = user.getKickComment(  );
168             room.removeUser( strPseudo );
169 
170             return buildMessage( CMD_KICK, strMessage );
171         }
172 
173         if ( user.hasNewPseudo(  ) )
174         {
175             room.removeOldPseudo( strPseudo );
176 
177             return buildMessage( CMD_NEW_PSEUDO, user.getNickname(  ) );
178         }
179 
180         // List of the messages
181         Enumeration entries = user.getChatEntries(  );
182 
183         while ( entries.hasMoreElements(  ) )
184         {
185             ChatEntry entry = (ChatEntry) entries.nextElement(  );
186 
187             if ( entry.getTime(  ) < user.getLastAccessTime(  ).getTime(  ) )
188             {
189                 // The message came before the arrival of the user, then go to the next one
190                 continue;
191             }
192 
193             switch ( entry.getType(  ) )
194             {
195                 case ChatEntry.TYPE_MESSAGE:
196                     strData.append( buildMessage( "<" + entry.getNickname(  ) + "> " + entry.getChatMessage(  ) ) );
197 
198                     break;
199 
200                 case ChatEntry.TYPE_NOTIFICATION:
201                     strData.append( buildMessage( entry.getChatMessage(  ) ) );
202 
203                     break;
204 
205                 default:
206 
207                     // No data is appended
208                     break;
209             }
210         }
211 
212         // List of the users
213         Enumeration users = room.getUsers(  );
214 
215         while ( users.hasMoreElements(  ) )
216         {
217             ChatUser u = (ChatUser) users.nextElement(  );
218             strData.append( CMD_ADD_USER );
219 
220             if ( u.getMode(  ) == ChatUser.MODE_OP )
221             {
222                 strData.append( "@" );
223             }
224             else if ( u.getMode(  ) == ChatUser.MODE_VOICE )
225             {
226                 strData.append( "+" );
227             }
228 
229             strData.append( u.getNickname(  ) );
230 
231             if ( strPseudo.equals( u.getNickname(  ) ) )
232             {
233                 strData.append( "*" );
234             }
235 
236             if ( u.isAway(  ) )
237             {
238                 if ( u.getAwayComment(  ) != null )
239                 {
240                     strData.append( " (absent:" + u.getAwayComment(  ) + ")" );
241                 }
242                 else
243                 {
244                     strData.append( " (absent)" );
245                 }
246             }
247 
248             strData.append( SEPARATOR );
249         }
250 
251         // Topic of the room
252         strData.append( buildMessage( CMD_SET_TOPIC, room.getDescription(  ) ) );
253         user.setLastAccessTime( new Date(  ) );
254 
255         return strData.toString(  );
256     }
257 }