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.ChatRoom;
37  import fr.paris.lutece.plugins.chat.business.RoomList;
38  import fr.paris.lutece.plugins.chat.service.ChatConstantes;
39  import fr.paris.lutece.plugins.chat.service.ChatService;
40  import fr.paris.lutece.portal.service.message.SiteMessageException;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.security.UserNotSignedException;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.xpages.XPage;
46  import fr.paris.lutece.portal.web.xpages.XPageApplication;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  
49  import java.util.Enumeration;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   * This class provides chat pages.
56   */
57  public class ChatApp implements XPageApplication
58  {
59      private static final String TEMPLATE_CHAT_APPLET = "skin/plugins/chat/applet.html";
60      private static final String TEMPLATE_CHAT_ENTER = "skin/plugins/chat/chat_enter.html";
61      private static final String TEMPLATE_ROOM_LIST_ROW = "skin/plugins/chat/room_list_row.html";
62      private static final String BOOKMARK_NICKNAME = "@nickname@";
63      private static final String BOOKMARK_ROOM = "@room@";
64      private static final String BOOKMARK_ROOM_DESCRIPTION = "@room_description@";
65      private static final String BOOKMARK_CHECKED = "@checked@";
66      private static final String BOOKMARK_ROWS = "@rooms_list_row@";
67      private static final String BOOKMARK_GUESTS_COUNT = "@users_count@";
68      private static final String BOOKMARK_BGCOLOR = "@bgcolor@";
69      private static final String BOOKMARK_BTBGCOLOR = "@btbgcolor@";
70      private static final String BOOKMARK_BTFGCOLOR = "@btfgcolor@";
71      private static final String BOOKMARK_FDBGCOLOR = "@fdbgcolor@";
72      private static final String PARAM_ACCESS_CODE = "access_code";
73      private static final String PROPERTY_CHAT_PAGE_TITLE = "chat.page.title";
74      private static final String PROPERTY_CHAT_PAGE_PATH_LABEL = "chat.page.pathLabel";
75      private static final String PROPERTY_CHAT_ERROR_INVALID_ROOM = "chat.msg.invalid.room";
76  
77      /**
78       * Creates a new ChatPage object
79       */
80      public ChatApp(  )
81      {
82      }
83  
84      /**
85       * Returns the content of the xpage according to the parameters of the http request and the mode : <br> This method
86       * implements the XPage interface
87       *
88       * @param request The http request
89       * @param nMode The mode in which the page is called
90       * @param plugin The plugin
91       * @return The html content of the page
92       * @throws SiteMessageException If an exception occurs
93       */
94      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
95          throws SiteMessageException
96      {
97          XPage page = new XPage(  );
98          String strTitle = AppPropertiesService.getProperty( PROPERTY_CHAT_PAGE_TITLE );
99          String strPathLabel = AppPropertiesService.getProperty( PROPERTY_CHAT_PAGE_PATH_LABEL );
100         page.setTitle( strTitle );
101         page.setPathLabel( strPathLabel );
102 
103         String strNickname = request.getParameter( ChatConstantes.PARAM_NICKNAME );
104 
105         // the pseudonym is stored in the http request
106         if ( strNickname != null )
107         {
108             page.setContent( getAppletPage( request, strNickname ) );
109 
110             return page;
111         }
112 
113         // the pseudonym is not stored in the http request
114         String strAccessCode = request.getParameter( PARAM_ACCESS_CODE );
115 
116         // The access code is stored in the http request
117         if ( strAccessCode != null )
118         {
119             page.setContent( getAppletPage( request, strAccessCode ) );
120 
121             return page;
122         }
123 
124         // The access code is not stored in the http request, display the login page
125         page.setContent( getLoginPage(  ) );
126 
127         return page;
128     }
129 
130     /**
131      * Returns the page which contains the chat applet
132      *
133      * @param request The http request which contains the connexion parameters
134      * @param strNickname The connexion name (pseudonym)
135      * @return The html code of the page which contains the chat applet
136      */
137     private String getAppletPage( HttpServletRequest request, String strNickname )
138     {
139         ChatRoom room = ChatService.getRoom( request );
140 
141         if ( room == null )
142         {
143             return AppPropertiesService.getProperty( PROPERTY_CHAT_ERROR_INVALID_ROOM );
144         }
145 
146         String strRoom = request.getParameter( ChatConstantes.PARAM_ROOM );
147         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CHAT_APPLET );
148         template.substitute( BOOKMARK_NICKNAME, strNickname );
149         template.substitute( BOOKMARK_ROOM, strRoom );
150         template.substitute( BOOKMARK_BGCOLOR, room.getBgColor(  ) );
151         template.substitute( BOOKMARK_BTBGCOLOR, room.getButtonBgColor(  ) );
152         template.substitute( BOOKMARK_BTFGCOLOR, room.getButtonFgColor(  ) );
153         template.substitute( BOOKMARK_FDBGCOLOR, room.getFieldBgColor(  ) );
154 
155         return template.getHtml(  );
156     }
157 
158     /**
159      * Returns the page which contains the chat applet (form with access code and password fields or form with
160      * pseudonym filed)
161      *
162      * @return The html content of the access page to the chat
163      */
164     private String getLoginPage(  )
165     {
166         // Choose of the access form : access with authentification or free access
167         String strTemplate = TEMPLATE_CHAT_ENTER;
168         HtmlTemplate template = AppTemplateService.getTemplate( strTemplate );
169 
170         // Scan of the rooms list to create the table
171         RoomList roomList = ChatService.getRoomList(  );
172         Enumeration rooms = roomList.getRooms(  );
173         boolean bFirst = true;
174         StringBuffer strLines = new StringBuffer(  );
175         HtmlTemplate templateLine = AppTemplateService.getTemplate( TEMPLATE_ROOM_LIST_ROW );
176 
177         while ( rooms.hasMoreElements(  ) )
178         {
179             HtmlTemplate t = new HtmlTemplate( templateLine );
180             ChatRoom room = (ChatRoom) rooms.nextElement(  );
181             t.substitute( BOOKMARK_ROOM, room.getName(  ) );
182             t.substitute( BOOKMARK_ROOM_DESCRIPTION, room.getDescription(  ) );
183 
184             // Select the first room by default
185             String strChecked = ( bFirst ) ? "checked=\"checked\"" : "";
186             t.substitute( BOOKMARK_CHECKED, strChecked );
187             bFirst = false;
188             t.substitute( BOOKMARK_GUESTS_COUNT, room.getUserCount(  ) );
189             strLines.append( t.getHtml(  ) );
190         }
191 
192         template.substitute( BOOKMARK_ROWS, strLines.toString(  ) );
193 
194         return template.getHtml(  );
195     }
196 }