Skip to content

Commit bb07875

Browse files
committed
refactored
1 parent 77b7999 commit bb07875

5 files changed

Lines changed: 21 additions & 13 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
6565
String mimeType = tika.detect(fileIn);
6666
response.setContentType(mimeType);
6767
} catch (Exception e) {
68-
String currentDirectory = System.getProperty("user.dir");
69-
response.setHeader("X-cur-dir", currentDirectory);
7068
logger.info("Error getting MIME Type for " + filePath + " : " + e.getMessage());
7169
}
7270
inputStream = new FileInputStream(fileIn);
@@ -75,7 +73,6 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
7573
String line;
7674

7775
StringBuilder responseData = new StringBuilder();
78-
String currentDirectory = System.getProperty("user.dir");
7976
try (PrintWriter out = response.getWriter()) {
8077

8178
while ((line = in.readLine()) != null) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.logging.Logger;
44

5+
import edu.harvard.iq.dataverse.customization.CustomizationConstants;
56
import jakarta.ws.rs.*;
67
import jakarta.ws.rs.client.Client;
78
import jakarta.ws.rs.client.ClientBuilder;
@@ -133,14 +134,18 @@ public Response getExportFormats() {
133134
@GET
134135
@Path("settings/customization/{customizationFileType}")
135136
public Response getCustomizationFile(@PathParam("customizationFileType") String customizationFileType) {
137+
String type = customizationFileType != null ? customizationFileType.toLowerCase() : "";
138+
if (!CustomizationConstants.validTypes.contains(type)) {
139+
return badRequest("Customization type unknown or missing. Must be one of the following: " + CustomizationConstants.validTypes);
140+
}
136141
Client client = ClientBuilder.newClient();
137142
WebTarget endpoint = client.target("http://localhost:8080/CustomizationFilesServlet");
138-
Response response = endpoint.queryParam("customFileType", customizationFileType)
143+
Response response = endpoint.queryParam("customFileType", type)
139144
.request(MediaType.MEDIA_TYPE_WILDCARD)
140145
.get();
141146

142147
if (response.getLength() < 1) {
143-
return notFound(customizationFileType + " not found. " + response.getHeaderString("X-cur-dir"));
148+
return notFound(type + " not found.");
144149
} else {
145150
return response;
146151
}

src/main/java/edu/harvard/iq/dataverse/customization/CustomizationConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package edu.harvard.iq.dataverse.customization;
77

8+
import java.util.List;
9+
810
/**
911
*
1012
* @author rmp553
@@ -23,5 +25,5 @@ public class CustomizationConstants {
2325

2426
public static String fileTypeLogo = "logo";
2527

26-
28+
public static List<String> validTypes = List.of(fileTypeHomePage, fileTypeHeader, fileTypeFooter, fileTypeStyle, fileTypeAnalytics, fileTypeLogo);
2729
} // end CustomizationConstants

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class CustomizationIT {
1616
static String docroot;
1717
@BeforeAll
1818
public static void setup() {
19+
// setup docroot for running test either in docker or in Jenkins
1920
if (Files.exists(Paths.get("docker-dev-volumes"))) {
2021
docroot = "./appserver/glassfish/domains/domain1/docroot/";
2122
} else {
22-
// /usr/local/payara6/glassfish/domains/domain1/config
2323
docroot = "../docroot/";
2424
}
2525
}
@@ -34,17 +34,15 @@ public void testGetCustomAnalytics() {
3434
UtilIT.setSetting(SettingsServiceBean.Key.WebAnalyticsCode, setting).prettyPrint();
3535

3636
Response getResponse = UtilIT.getCustomizationFile("analytics");
37-
getResponse.prettyPrint();
3837
getResponse.then().assertThat()
39-
.body(containsString("<!doctype html>"))
40-
.statusCode(200);
38+
.statusCode(200)
39+
.body(containsString("<!doctype html>"));
4140

4241
assert (getResponse.getHeaders().get("Content-Type").getValue().startsWith("text/html"));
4342

4443
UtilIT.deleteSetting(SettingsServiceBean.Key.WebAnalyticsCode).prettyPrint();
4544

4645
getResponse = UtilIT.getCustomizationFile("analytics");
47-
getResponse.prettyPrint();
4846
getResponse.then().assertThat()
4947
.statusCode(404)
5048
.body(containsString("not found."));
@@ -56,7 +54,6 @@ public void testGetCustomLogo() {
5654
UtilIT.setSetting(SettingsServiceBean.Key.LogoCustomizationFile, setting).prettyPrint();
5755

5856
Response getResponse = UtilIT.getCustomizationFile("logo");
59-
getResponse.prettyPrint();
6057
getResponse.then().assertThat()
6158
.statusCode(200)
6259
.body(containsString("PNG"));
@@ -71,4 +68,12 @@ public void testGetCustomLogo() {
7168
.statusCode(404)
7269
.body(containsString("not found."));
7370
}
71+
72+
@Test
73+
public void testGetCustomUnknown() {
74+
Response getResponse = UtilIT.getCustomizationFile("unknownType");
75+
getResponse.then().assertThat()
76+
.statusCode(400)
77+
.body(containsString("Customization type unknown or missing. Must be one of the following: [homePage, header"));
78+
}
7479
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4871,6 +4871,5 @@ static Response getUserSelectableRoles(String apiToken) {
48714871
.header(API_TOKEN_HTTP_HEADER, apiToken)
48724872
.contentType("application/json")
48734873
.get("/api/roles/userSelectable");
4874-
48754874
}
48764875
}

0 commit comments

Comments
 (0)