3535
3636import java .io .UnsupportedEncodingException ;
3737import java .lang .reflect .InvocationTargetException ;
38+ import java .net .URI ;
39+ import java .net .URISyntaxException ;
3840import java .net .URLEncoder ;
3941import java .text .SimpleDateFormat ;
4042import java .util .ArrayList ;
6163 * Version 1.2 - Topcoder Member Settings Profile - Email Verification v1.0
6264 * - use core api 4.3.1, change to use M2M token
6365 * </p>
64- *
66+ *
6567 * <p>
6668 * Version 1.3 - Topcoder Member Processor ES Response Fixes Code Challenge v1.0
6769 * - change the event message structure for update profile/photo
6870 * </p>
69- *
71+ *
7072 * <p>
7173 * Version 1.4 - Topcoder Member Service - Update the Members Endpoint version 1.0
7274 * - get more profile properties from informix db
7375 * </p>
74- *
76+ *
7577 * <p>
7678 * Version 1.5 - Topcoder Member Service - Update endpoints to read from ES v1.0
7779 * - get the profile from elasticsearch if exists
7880 * </p>
7981 *
8082 * @author TCCoder
81- * @version 1.5
83+ * @version 1.5
8284 *
8385 */
8486public class MemberProfileManager {
@@ -127,22 +129,22 @@ public class MemberProfileManager {
127129 * The email verification config field
128130 */
129131 private final EmailVerificationConfiguration emailVerificationConfig ;
130-
132+
131133 /**
132- * The jestClient field
134+ * The jestClient field
133135 */
134136 private JestClient jestClient ;
135-
137+
136138 /**
137- * The membersIndexName field
139+ * The membersIndexName field
138140 */
139141 private final String membersIndexName ;
140-
142+
141143 /**
142- * The membersTypeName field
144+ * The membersTypeName field
143145 */
144146 private final String membersTypeName ;
145-
147+
146148 /**
147149 * The informixDAO field
148150 */
@@ -167,9 +169,9 @@ public class MemberProfileManager {
167169 * @param m2mAuthConfiguration the m2mAuthConfiguration to use
168170 * @param emailVerificationConfig the emailVerificationConfig to use
169171 */
170- public MemberProfileManager (MemberProfileDAO memberProfileDAO , MemberStatsDAO memberStatsDAO ,
172+ public MemberProfileManager (MemberProfileDAO memberProfileDAO , MemberStatsDAO memberStatsDAO ,
171173 MemberProfileInformixDAO informixDAO , MemberProfileInformixDwDAO informixDwDAO , String photoURLDomain , FileInvocationHandler fileInvocationHandler ,
172- EventBusServiceClient eventBusServiceClient , M2mAuthConfiguration m2mAuthConfiguration , EmailVerificationConfiguration emailVerificationConfig ,
174+ EventBusServiceClient eventBusServiceClient , M2mAuthConfiguration m2mAuthConfiguration , EmailVerificationConfiguration emailVerificationConfig ,
173175 JestClient jestClient , String membersIndexName , String membersTypeName ) {
174176 this .memberProfileDAO = memberProfileDAO ;
175177 this .memberStatsDAO = memberStatsDAO ;
@@ -203,7 +205,7 @@ public MemberProfile getMemberProfile(String handle, String data, AuthUser authU
203205 if (fields .isEmpty ()) {
204206 fields .addAll (Helper .PROFILE_FIELDS );
205207 }
206-
208+
207209 MemberProfile memberProfile = SearchHelper .getMemberProfile (handle , jestClient , this .membersIndexName , this .membersTypeName );
208210 if (memberProfile != null ) {
209211 logger .info ("Get the member profile from the elasticsearch for the user: " + handle );
@@ -218,7 +220,7 @@ public MemberProfile getMemberProfile(String handle, String data, AuthUser authU
218220 memberProfile .setMaxRating (memberStats .getMaxRating ());
219221 }
220222 }
221-
223+
222224 Helper .addMoreProperties (handle , memberProfile , fields , informixDAO , informixDwDAO );
223225
224226 // if user is not logged in
@@ -241,25 +243,46 @@ public MemberProfile getMemberProfile(String handle, String data, AuthUser authU
241243 /**
242244 * Update member profile
243245 *
244- * @param handle Handle of the user
245- * @param authUser Authentication user
246- * @param memberProfile Member profile
247- * @return MemberProfile Member profile
246+ * @param handle the handle to use
247+ * @param successUrl the successUrl to use
248+ * @param failUrl the failUrl to use
249+ * @param authUser the authUser to use
250+ * @param memberProfile the memberProfile to use
248251 * @throws SupplyException if any error occurs
249252 * @throws IllegalAccessException if any error occurs
250- * @throws InvocationTargetException if any error occurs
251- * @throws InstantiationException if any error occurs
252253 * @throws NoSuchMethodException if any error occurs
254+ * @throws InstantiationException if any error occurs
255+ * @throws SupplyException if any error occurs
256+ * @throws InvocationTargetException if any error occurs
257+ * @throws JsonProcessingException if any error occurs
258+ * @return the MemberProfile result
253259 */
254- public MemberProfile updateMemberProfile (String handle , AuthUser authUser , MemberProfile memberProfile ) throws
255- IllegalAccessException , NoSuchMethodException , InstantiationException , SupplyException ,
260+ public MemberProfile updateMemberProfile (String handle , String successUrl , String failUrl , AuthUser authUser , MemberProfile memberProfile )
261+ throws IllegalAccessException , NoSuchMethodException , InstantiationException , SupplyException ,
256262 InvocationTargetException , JsonProcessingException {
257263
258264 MemberProfile existingMemberProfile = memberProfileDAO .validateHandle (handle , authUser , false );
259265
260266 boolean verifyEmail = false ;
261267 if (!existingMemberProfile .getEmail ().equals (memberProfile .getEmail ())) {
262268 verifyEmail = true ;
269+ if (successUrl == null || successUrl .trim ().length () == 0 ) {
270+ throw new SupplyException ("The success redirect url should be provided for the verification of email" , HttpServletResponse .SC_BAD_REQUEST );
271+ }
272+ if (failUrl == null || failUrl .trim ().length () == 0 ) {
273+ throw new SupplyException ("The failUrl redirect url should be provided for the verification of email" , HttpServletResponse .SC_BAD_REQUEST );
274+ }
275+ try {
276+ new URI (successUrl );
277+ } catch (URISyntaxException e ) {
278+ throw new SupplyException ("The successUrl is syntax invalid" , HttpServletResponse .SC_BAD_REQUEST );
279+ }
280+ try {
281+ new URI (failUrl );
282+ } catch (URISyntaxException e ) {
283+ throw new SupplyException ("The failUrl is syntax invalid" , HttpServletResponse .SC_BAD_REQUEST );
284+ }
285+
263286 if (!EmailValidator .getInstance ().isValid ((String ) memberProfile .getEmail ())) {
264287 throw new SupplyException ("The email is invalid:" + memberProfile .getEmail (), HttpServletResponse .SC_BAD_REQUEST );
265288 } else {
@@ -284,11 +307,11 @@ public MemberProfile updateMemberProfile(String handle, AuthUser authUser, Membe
284307 // fire event on to the event bus to update member profile
285308 logger .info ("publish member-profile-update-event" );
286309 Helper .publishEventMessage (eventBusServiceClient , m2mAuthConfiguration ,
287- memberProfile .getUserId (),
310+ memberProfile .getUserId (),
288311 "member.action.profile.update" , existingMemberProfile , memberProfile , logger );
289312
290313 if (verifyEmail ) {
291- this .fireEmailVerificationEvent (authUser , memberProfile );
314+ this .fireEmailVerificationEvent (authUser , memberProfile , successUrl , failUrl );
292315 }
293316
294317 MemberProfile result = memberProfileDAO .getMemberProfile (handle );
@@ -352,9 +375,12 @@ public void verifyUserEmail(String handle, AuthUser authUser, String newEmail, S
352375 * Fire email verification event
353376 *
354377 * @param authUser the authUser to use
355- * @param profile the member profile to use
378+ * @param profile the profile to use
379+ * @param successUrl the successUrl to use
380+ * @param failUrl the failUrl to use
381+ * @throws SupplyException if any error occurs
356382 */
357- private void fireEmailVerificationEvent (AuthUser authUser , MemberProfile profile ) throws SupplyException {
383+ private void fireEmailVerificationEvent (AuthUser authUser , MemberProfile profile , String successUrl , String failUrl ) throws SupplyException {
358384 EventMessage msg = new EventMessage ();
359385 msg .setTopic ("member.action.email.profile.emailchange.verification" ).setMimeType ("application/json" ).setOriginator ("tc-member-profile" ).setTimestamp (new Date ());
360386 Map <String , String > data = new LinkedHashMap <String , String >();
@@ -366,9 +392,16 @@ private void fireEmailVerificationEvent(AuthUser authUser, MemberProfile profile
366392 .replace ("{token}" , profile .getEmailVerifyToken ())
367393 .replace ("{newEmail}" , profile .getNewEmail ())
368394 .replace ("{oldEmail}" , profile .getEmail ())
369- .replace ("{jwtToken}" , authUser .getToken ());
395+ .replace ("{jwtToken}" , authUser .getToken ())
396+ .replace ("{redirectTo}" , successUrl );
370397 String verificationDisagreeUrl = this .emailVerificationConfig .getVerificationDisagreeUrl ()
371- .replace ("{baseUrl}" , this .emailVerificationConfig .getBaseUrl ());
398+ .replace ("{baseUrl}" , this .emailVerificationConfig .getBaseUrl ())
399+ .replace ("{handle}" , profile .getHandle ())
400+ .replace ("{token}" , profile .getEmailVerifyToken ())
401+ .replace ("{newEmail}" , profile .getNewEmail ())
402+ .replace ("{oldEmail}" , profile .getEmail ())
403+ .replace ("{jwtToken}" , authUser .getToken ())
404+ .replace ("{redirectTo}" , failUrl );
372405 data .put ("verificationAgreeUrl" , verificationAgreeUrl );
373406 data .put ("verificationDisagreeUrl" , verificationDisagreeUrl );
374407 data .put ("verificationToken" , profile .getEmailVerifyToken ());
@@ -480,7 +513,7 @@ public String updatePhoto(String handle, AuthUser authUser, PhotoTokenContentTyp
480513
481514 logger .info ("publish member-profile-photo-update-event" );
482515 Helper .publishProfilePhotoEvent (eventBusServiceClient , m2mAuthConfiguration , "member.action.profile.photo.update" , eventObj , logger );
483-
516+
484517 return photoURL ;
485518 }
486519
0 commit comments