Skip to content

Commit a82f229

Browse files
committed
introduce "UI" setting for JSF vs SPA #11648
The first use case is to avoid returning a JSF/xhtml URL to the SPA.
1 parent bfe59f0 commit a82f229

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/MailServiceBean.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public Boolean sendNotificationEmail(UserNotification notification, String comme
281281
if (emailAddress != null){
282282
Object objectOfNotification = getObjectOfNotification(notification);
283283
if (objectOfNotification != null){
284-
String messageText = getMessageTextBasedOnNotification(notification, objectOfNotification, comment, requestor);
284+
String messageText = getMessageTextBasedOnNotification(notification, objectOfNotification, comment, requestor, null);
285285
String subjectText = MailUtil.getSubjectTextBasedOnNotification(notification, objectOfNotification);
286286
if (!(StringUtils.isEmpty(messageText) || StringUtils.isEmpty(subjectText))){
287287
retval = sendSystemEmail(emailAddress, subjectText, messageText, isHtmlContent);
@@ -376,10 +376,10 @@ public String getMessageTextBasedOnNotification(UserNotification userNotificatio
376376
}
377377

378378
public String getMessageTextBasedOnNotification(UserNotification userNotification, Object targetObject, String comment) {
379-
return getMessageTextBasedOnNotification(userNotification, targetObject, comment, null);
379+
return getMessageTextBasedOnNotification(userNotification, targetObject, comment, null, null);
380380
}
381381

382-
public String getMessageTextBasedOnNotification(UserNotification userNotification, Object targetObject, String comment, AuthenticatedUser requestor) {
382+
public String getMessageTextBasedOnNotification(UserNotification userNotification, Object targetObject, String comment, AuthenticatedUser requestor, SystemConfig.UI ui) {
383383
String messageText = BundleUtil.getStringFromBundle("notification.email.greeting");
384384
DatasetVersion version;
385385
Dataset dataset;
@@ -597,7 +597,7 @@ public String getMessageTextBasedOnNotification(UserNotification userNotificatio
597597
BrandingUtil.getSupportTeamName(getSystemAddress().orElse(null)),
598598
BrandingUtil.getSupportTeamEmailAddress(getSystemAddress().orElse(null))
599599
));
600-
String optionalConfirmEmailAddon = confirmEmailService.optionalConfirmEmailAddonMsg(userNotification.getUser());
600+
String optionalConfirmEmailAddon = confirmEmailService.optionalConfirmEmailAddonMsg(userNotification.getUser(), ui);
601601
accountCreatedMessage += optionalConfirmEmailAddon;
602602
logger.fine("accountCreatedMessage: " + accountCreatedMessage);
603603
return messageText += accountCreatedMessage;

src/main/java/edu/harvard/iq/dataverse/api/Notifications.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import jakarta.ws.rs.core.Response;
2727

2828
import edu.harvard.iq.dataverse.util.MailUtil;
29+
import edu.harvard.iq.dataverse.util.SystemConfig;
2930
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder;
3031
import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder;
32+
import jakarta.ws.rs.QueryParam;
3133

3234
@Stateless
3335
@Path("notifications")
@@ -39,7 +41,7 @@ public class Notifications extends AbstractApiBean {
3941
@GET
4042
@AuthRequired
4143
@Path("/all")
42-
public Response getAllNotificationsForUser(@Context ContainerRequestContext crc) {
44+
public Response getAllNotificationsForUser(@Context ContainerRequestContext crc, @QueryParam("ui") String uiIn) {
4345
User user = getRequestUser(crc);
4446
if (!(user instanceof AuthenticatedUser)) {
4547
// It's unlikely we'll reach this error. A Guest doesn't have an API token and would have been blocked above.
@@ -66,7 +68,10 @@ public Response getAllNotificationsForUser(@Context ContainerRequestContext crc)
6668
Object objectOfNotification = mailService.getObjectOfNotification(notification);
6769
if (objectOfNotification != null){
6870
String subjectText = MailUtil.getSubjectTextBasedOnNotification(notification, objectOfNotification);
69-
String messageText = mailService.getMessageTextBasedOnNotification(notification, objectOfNotification, null, notification.getRequestor());
71+
System.out.println("uiIn: " + uiIn);
72+
SystemConfig.UI ui = SystemConfig.UI.valueOf(uiIn);
73+
System.out.println("ui: " + ui);
74+
String messageText = mailService.getMessageTextBasedOnNotification(notification, objectOfNotification, null, notification.getRequestor(), ui);
7075
notificationObjectBuilder.add("subjectText", subjectText);
7176
notificationObjectBuilder.add("messageText", messageText);
7277
}

src/main/java/edu/harvard/iq/dataverse/confirmemail/ConfirmEmailServiceBean.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
66
import edu.harvard.iq.dataverse.MailServiceBean;
77
import edu.harvard.iq.dataverse.UserNotification;
8+
import edu.harvard.iq.dataverse.api.AbstractApiBean;
89
import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider;
910
import edu.harvard.iq.dataverse.util.BundleUtil;
1011
import edu.harvard.iq.dataverse.util.MailUtil;
@@ -101,7 +102,8 @@ private ConfirmEmailInitResponse sendConfirm(AuthenticatedUser aUser, boolean se
101102
* ConfirmEmailIT.
102103
*/
103104
em.persist(confirmEmailData);
104-
ConfirmEmailInitResponse confirmEmailInitResponse = new ConfirmEmailInitResponse(true, confirmEmailData, optionalConfirmEmailAddonMsg(aUser));
105+
// TODO: don't hard-code SystemConfig.UI.JSF to JSF. When the SPA is in use, what should the email say?
106+
ConfirmEmailInitResponse confirmEmailInitResponse = new ConfirmEmailInitResponse(true, confirmEmailData, optionalConfirmEmailAddonMsg(aUser, SystemConfig.UI.JSF));
105107
if (sendEmail) {
106108
sendLinkOnEmailChange(aUser, confirmEmailInitResponse.getConfirmUrl());
107109
}
@@ -268,8 +270,13 @@ public ConfirmEmailData createToken(AuthenticatedUser au) {
268270
return confirmEmailData;
269271
}
270272

271-
public String optionalConfirmEmailAddonMsg(AuthenticatedUser user) {
273+
public String optionalConfirmEmailAddonMsg(AuthenticatedUser user, SystemConfig.UI ui) {
272274
final String emptyString = "";
275+
if (SystemConfig.UI.SPA.equals(ui)) {
276+
// Return early to avoid giving the SPA a JSF/xhtml URL.
277+
// TODO: when "confirm email" is implemented by the SPA, return the proper SPA URL.
278+
return emptyString;
279+
}
273280
if (user == null) {
274281
logger.info("Can't return confirm email message. AuthenticatedUser was null!");
275282
return emptyString;

src/main/java/edu/harvard/iq/dataverse/util/SystemConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ public class SystemConfig {
8484

8585
public static final String DEFAULTCURATIONLABELSET = "DEFAULT";
8686
public static final String CURATIONLABELSDISABLED = "DISABLED";
87-
87+
88+
public enum UI {
89+
JSF, SPA
90+
};
91+
8892
public String getVersion() {
8993
return getVersion(false);
9094
}

src/test/java/edu/harvard/iq/dataverse/api/NotificationsIT.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package edu.harvard.iq.dataverse.api;
22

3+
import edu.harvard.iq.dataverse.util.SystemConfig;
4+
import static edu.harvard.iq.dataverse.util.SystemConfig.UI.JSF;
5+
import static edu.harvard.iq.dataverse.util.SystemConfig.UI.SPA;
36
import io.restassured.RestAssured;
47
import io.restassured.path.json.JsonPath;
58
import io.restassured.response.Response;
69
import java.util.logging.Logger;
710
import static jakarta.ws.rs.core.Response.Status.CREATED;
811
import static jakarta.ws.rs.core.Response.Status.OK;
12+
import org.hamcrest.CoreMatchers;
13+
import static org.hamcrest.CoreMatchers.containsString;
914
import static org.hamcrest.CoreMatchers.equalTo;
1015
import org.junit.jupiter.api.BeforeAll;
1116
import org.junit.jupiter.api.Test;
@@ -41,14 +46,24 @@ public void testNotifications() {
4146
createDataset.prettyPrint();
4247
createDataset.then().assertThat()
4348
.statusCode(CREATED.getStatusCode());
44-
Response getNotifications = UtilIT.getNotifications(authorApiToken);
45-
getNotifications.prettyPrint();
46-
getNotifications.then().assertThat()
49+
50+
Response getNotificationsJSF = UtilIT.getNotifications(authorApiToken, JSF);
51+
getNotificationsJSF.prettyPrint();
52+
getNotificationsJSF.then().assertThat()
53+
.body("data.notifications[0].type", equalTo("CREATEACC"))
54+
.body("data.notifications[0].messageText", containsString("xhtml"))
55+
.body("data.notifications[1]", equalTo(null))
56+
.statusCode(OK.getStatusCode());
57+
58+
Response getNotificationsSpa = UtilIT.getNotifications(authorApiToken, SPA);
59+
getNotificationsSpa.prettyPrint();
60+
getNotificationsSpa.then().assertThat()
4761
.body("data.notifications[0].type", equalTo("CREATEACC"))
62+
.body("data.notifications[0].messageText", CoreMatchers.not(containsString("xhtml")))
4863
.body("data.notifications[1]", equalTo(null))
4964
.statusCode(OK.getStatusCode());
5065

51-
long id = JsonPath.from(getNotifications.getBody().asString()).getLong("data.notifications[0].id");
66+
long id = JsonPath.from(getNotificationsSpa.getBody().asString()).getLong("data.notifications[0].id");
5267

5368
Response deleteNotification = UtilIT.deleteNotification(id, authorApiToken);
5469
deleteNotification.prettyPrint();

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import edu.harvard.iq.dataverse.DatasetFieldValue;
5353
import edu.harvard.iq.dataverse.settings.FeatureFlags;
5454
import edu.harvard.iq.dataverse.util.StringUtil;
55+
import edu.harvard.iq.dataverse.util.SystemConfig;
5556

5657
import static org.junit.jupiter.api.Assertions.*;
5758

@@ -1702,12 +1703,21 @@ static Response returnDatasetToAuthor(String datasetPersistentId, JsonObject jso
17021703
}
17031704

17041705
static Response getNotifications(String apiToken) {
1706+
return getNotifications(apiToken, null);
1707+
}
1708+
1709+
static Response getNotifications(String apiToken, SystemConfig.UI ui) {
17051710
RequestSpecification requestSpecification = given();
17061711
if (apiToken != null) {
17071712
requestSpecification = given()
17081713
.header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken);
17091714
}
1710-
return requestSpecification.get("/api/notifications/all");
1715+
String optionalUiParam = "";
1716+
if (ui != null) {
1717+
optionalUiParam = "?ui=" + ui;
1718+
}
1719+
System.out.println("optionalUiParam: " + optionalUiParam);
1720+
return requestSpecification.get("/api/notifications/all" + optionalUiParam);
17111721
}
17121722

17131723
static Response deleteNotification(long id, String apiToken) {

0 commit comments

Comments
 (0)