View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.broadcastproxy.web;
35  
36  import fr.paris.lutece.plugins.broadcastproxy.service.BroadcastService;
37  import javax.servlet.http.HttpServletRequest;
38  
39  import fr.paris.lutece.portal.service.security.LuteceUser;
40  import fr.paris.lutece.portal.service.security.SecurityService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
44  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
45  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
46  import fr.paris.lutece.portal.web.xpages.XPage;
47  import fr.paris.lutece.util.json.ErrorJsonResponse;
48  import fr.paris.lutece.util.json.JsonResponse;
49  import fr.paris.lutece.util.json.JsonUtil;
50  import java.io.BufferedReader;
51  import java.io.IOException;
52  import org.codehaus.plexus.util.StringUtils;
53  
54  /**
55   * MyLuteceParisConnectXPage
56   *
57   */
58  @Controller( xpageName = BroadcastproxyXPage.PAGE_BROADCAST_MYDASHBOARD, pageTitleI18nKey = "broadcastproxy.xpage.pageTitle", pagePathI18nKey = "broadcastproxy.xpage.pagePathLabel" )
59  public class BroadcastproxyXPage extends MVCApplication
60  {
61      /**
62       * Name of this application
63       */
64      public static final String PAGE_BROADCAST_MYDASHBOARD = "broadcastproxyMyDashboard";
65      private static final long serialVersionUID = -4316691400124512414L;
66  
67      private static final String KEY_USER_INFO_MAIL = "broadcastproxy.userInfoKeys.mail";
68  
69      private static final String ACTION_UPDATE_USER_SUBSCRIPTIONS = "updateUserSubscriptions";
70  
71      /**
72       * Check if the current (front) user is authenticated
73       * 
74       * @param request
75       *            The request
76       * @return true if authenticated
77       */
78      private String getMailUserAuthenticated( HttpServletRequest request )
79      {
80          LuteceUser user = null;
81  
82          if ( SecurityService.isAuthenticationEnable( ) )
83          {
84              user = SecurityService.getInstance( ).getRegisteredUser( request );
85              if ( user != null )
86              {
87                  String userMail = user.getEmail( );
88                  if ( StringUtils.isBlank( userMail ) )
89                  {
90                      String mailUserInfoKey = AppPropertiesService.getProperty( KEY_USER_INFO_MAIL );
91                      if ( !StringUtils.isBlank( mailUserInfoKey ) )
92                      {
93                          userMail = user.getUserInfo( mailUserInfoKey );
94                      }
95                  }
96                  return userMail;
97              }
98              else
99              {
100                 return null;
101             }
102         }
103         else
104         {
105             return null;
106         }
107     }
108 
109     /**
110      * Do update user subscriptions using the AJAX mode
111      * 
112      * json data should be like : { "userSubscriptions": {"typeName": "ALERT", "groupName": "Alertes", "subscriptionsList": [ {"id": "", "active": true} ] } }
113      * 
114      * IF there is not "groupName" (exemple for newsletter), do ["groupName": "NONE"]
115      * 
116      * @param request
117      *            The request
118      * @return
119      */
120     @Action( ACTION_UPDATE_USER_SUBSCRIPTIONS )
121     public XPage doUpdateUserSubscriptions( HttpServletRequest request )
122     {
123 
124         String mailUser = getMailUserAuthenticated( request );
125 
126         if ( StringUtils.isBlank( mailUser ) )
127             return responseJSON( JsonUtil.buildJsonResponse( new ErrorJsonResponse( "User not authentified." ) ) );
128 
129         String strJson;
130         try
131         {
132             StringBuilder sb = new StringBuilder( );
133             String line = null;
134 
135             BufferedReader reader = request.getReader( );
136             while ( ( line = reader.readLine( ) ) != null )
137                 sb.append( line );
138 
139             strJson = sb.toString( );
140         }
141         catch( IOException e )
142         {
143             return responseJSON( JsonUtil.buildJsonResponse( new ErrorJsonResponse( "An error occured while receiving the response" ) ) );
144         }
145 
146         if ( updateSubscriptions( strJson, mailUser ) != true )
147         {
148             responseJSON( JsonUtil.buildJsonResponse( new ErrorJsonResponse( "An error occured while receiving the response" ) ) );
149         }
150 
151         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( "ok" ) ) );
152     }
153 
154     /**
155      * update Subscriptions
156      * 
157      * @param jsonResponse
158      * @return true if successful
159      */
160     private boolean updateSubscriptions( String jsonResponse, String userId )
161     {
162         try
163         {
164             // update subscriptions by feed type
165             BroadcastService.getInstance( ).updateSubscribtions( userId, jsonResponse );
166         }
167         catch( Exception e )
168         {
169             AppLogService.error( "An error occured while updating subscriptions : " + e.getMessage( ) );
170             return false;
171         }
172 
173         return true;
174     }
175 
176 }