View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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.avatarserver.web;
35  
36  import fr.paris.lutece.plugins.avatarserver.business.Avatar;
37  import fr.paris.lutece.plugins.avatarserver.business.AvatarHome;
38  import fr.paris.lutece.plugins.avatarserver.service.AvatarService;
39  import fr.paris.lutece.plugins.avatarserver.service.HashService;
40  import fr.paris.lutece.portal.service.security.LuteceUser;
41  import fr.paris.lutece.portal.service.security.SecurityService;
42  import fr.paris.lutece.portal.service.security.UserNotSignedException;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
47  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
48  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49  import fr.paris.lutece.portal.web.xpages.XPage;
50  
51  import org.apache.commons.fileupload.FileItem;
52  
53  import java.lang.reflect.InvocationTargetException;
54  
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   * AvatarServer App
61   */
62  @Controller( xpageName = "avatarserver", pageTitleI18nKey = "avatarserver.xpage.pageTitle", pagePathI18nKey = "avatarserver.xpage.pagePath" )
63  public class AvatarServerApp extends MVCApplication
64  {
65      private static final String TEMPLATE_AVATAR = "skin/plugins/avatarserver/update_avatar.html";
66      private static final String MARK_AVATAR = "avatar";
67      private static final String MARK_AVATAR_ID = "avatar_id";
68      private static final String MARK_EMAIL = "email";
69      private static final String MARK_BACK_URL = "url_back";
70  
71      // Views and actions
72      private static final String VIEW_HOME = "home";
73      private static final String ACTION_UPDATE_AVATAR = "updateAvatar";
74  
75      // Parameters
76      private static final String PARAMETER_ID_AVATAR = "id_avatar";
77      private static final String PARAMETER_IMAGE = "avatar_image";
78      private static final String PROPERTY_URL_AFTER_UPDATE = "avatarserver.update_avatar.afterUpdateUrl";
79      private static final String PROPERTY_BACK_URL = "avatarserver.update_avatar.backUrl";
80      private static final String MESSAGE_MISSING_FILE = "avatarserver.xpage.message.missingFile";
81  
82      /**
83       * Update avatar view
84       * 
85       * @param request
86       *            The HTTP request
87       * @return The XPage
88       * @throws UserNotSignedException
89       *             if the user is not signed
90       * @throws IllegalAccessException
91       *             if an error occurs
92       * @throws IllegalArgumentException
93       *             if an error occurs
94       * @throws InvocationTargetException
95       *             if an error occurs
96       */
97      @View( value = VIEW_HOME, defaultView = true )
98      public XPage getUpdateAvatar( HttpServletRequest request ) throws UserNotSignedException, IllegalAccessException, IllegalArgumentException,
99              InvocationTargetException
100     {
101         LuteceUser user = SecurityService.isAuthenticationEnable( ) ? SecurityService.getInstance( ).getRegisteredUser( request ) : null;
102 
103         if ( user == null )
104         {
105             throw new UserNotSignedException( );
106         }
107 
108         String strEmail = user.getEmail( );
109         String strHash = HashService.getHash( strEmail );
110         Avatar avatar = AvatarHome.findByHash( strHash );
111         String strBackUrl = AppPropertiesService.getProperty( PROPERTY_BACK_URL );
112 
113         Map<String, Object> model = getModel( );
114 
115         if ( avatar != null )
116         {
117             model.put( MARK_AVATAR_ID, avatar.getId( ) );
118         }
119 
120         model.put( MARK_AVATAR, avatar );
121         model.put( MARK_EMAIL, strEmail );
122         model.put( MARK_BACK_URL, strBackUrl );
123 
124         return getXPage( TEMPLATE_AVATAR, request.getLocale( ), model );
125     }
126 
127     /**
128      * Do update avatar action
129      * 
130      * @param request
131      *            The HTTP request
132      * @return The next XPage
133      * @throws UserNotSignedException
134      *             if the user is not signed
135      */
136     @Action( ACTION_UPDATE_AVATAR )
137     public XPage doUpdateAvatar( HttpServletRequest request ) throws UserNotSignedException
138     {
139         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
140 
141         if ( user != null )
142         {
143             String strAvatarId = request.getParameter( PARAMETER_ID_AVATAR );
144 
145             MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) request;
146             FileItem imageSource = multiPartRequest.getFile( PARAMETER_IMAGE );
147 
148             if ( imageSource.getSize( ) == 0 )
149             {
150                 addError( MESSAGE_MISSING_FILE, request.getLocale( ) );
151 
152                 return redirectView( request, VIEW_HOME );
153             }
154 
155             boolean bUpdate = ( strAvatarId != null );
156 
157             if ( bUpdate )
158             {
159                 Avatar avatar = AvatarHome.findByPrimaryKey( Integer.parseInt( strAvatarId ) );
160                 avatar.setValue( imageSource.get( ) );
161                 avatar.setMimeType( imageSource.getContentType( ) );
162                 AvatarService.update( avatar );
163             }
164             else
165             {
166                 Avatar avatar = new Avatar( );
167                 avatar.setEmail( user.getEmail( ) );
168                 avatar.setValue( imageSource.get( ) );
169                 avatar.setMimeType( imageSource.getContentType( ) );
170                 AvatarService.create( avatar );
171             }
172         }
173         else
174         {
175             throw new UserNotSignedException( );
176         }
177 
178         String strAfterUpdateUrl = AppPropertiesService.getProperty( PROPERTY_URL_AFTER_UPDATE );
179 
180         return redirect( request, strAfterUpdateUrl );
181     }
182 }