1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
72 private static final String VIEW_HOME = "home";
73 private static final String ACTION_UPDATE_AVATAR = "updateAvatar";
74
75
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
84
85
86
87
88
89
90
91
92
93
94
95
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
129
130
131
132
133
134
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 }