Skip to content

Commit 4a23ba5

Browse files
committed
TEST DO NOT MERGE
1 parent 1b3c5ae commit 4a23ba5

5 files changed

Lines changed: 51 additions & 6 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### Feature Request: API endpoint for analytics.html
22

3-
New API to get the analytics.html from settings for SPA
3+
New API to get the analytics.html from settings for SPA (Also can be used to get homePage, header, footer, style, and logo)
44

55
See also [the guides](https://dataverse-guide--11359.org.readthedocs.build/en/11359/installation/config.html#web-analytics-code), #11448.

doc/sphinx-guides/source/api/native-api.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7707,7 +7707,9 @@ Parameters:
77077707
Customization File Contents
77087708
---------------------------
77097709
7710-
The Customization API is used to retrieve the analytics-code.html as well as other customization file contents. See also :ref:`web-analytics-code` in the Configuration section of the Installation Guide.
7710+
The Customization API is used to retrieve the analytics-code.html as well as other customization file contents.
7711+
7712+
See also :ref:`web-analytics-code` in the Configuration section of the Installation Guide. :ref:`Branding Your Installation`
77117713
77127714
The content is returned in type="text/html; charset=UTF-8"
77137715

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.io.PrintWriter;
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
17+
import java.util.logging.Logger;
18+
1719
import jakarta.servlet.ServletException;
1820
import jakarta.servlet.annotation.WebServlet;
1921
import jakarta.servlet.http.HttpServlet;
@@ -22,14 +24,16 @@
2224
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
2325
import jakarta.ejb.EJB;
2426
import org.apache.commons.io.IOUtils;
27+
import org.apache.tika.Tika;
2528

2629
/**
2730
*
2831
* @author skraffmi
2932
*/
3033
@WebServlet(name = "CustomizationFilesServlet", urlPatterns = {"/CustomizationFilesServlet"})
3134
public class CustomizationFilesServlet extends HttpServlet {
32-
35+
private static final Logger logger = Logger.getLogger(CustomizationFilesServlet.class.getCanonicalName());
36+
3337
@EJB
3438
SettingsServiceBean settingsService;
3539

@@ -45,7 +49,7 @@ public class CustomizationFilesServlet extends HttpServlet {
4549
*/
4650
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
4751
throws ServletException, IOException {
48-
response.setContentType("text/html;charset=UTF-8");
52+
response.setContentType(request.getContentType());
4953

5054
String customFileType = request.getParameter("customFileType");
5155
String filePath = getFilePath(customFileType);
@@ -56,15 +60,24 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
5660
try {
5761
File fileIn = physicalPath.toFile();
5862
if (fileIn != null) {
63+
Tika tika = new Tika();
64+
try {
65+
String mimeType = tika.detect(fileIn);
66+
response.setContentType(mimeType);
67+
} catch (Exception e) {
68+
logger.info("Error getting MIME Type for " + filePath + " : " + e.getMessage());
69+
}
5970
inputStream = new FileInputStream(fileIn);
6071

6172
in = new BufferedReader(new InputStreamReader(inputStream));
6273
String line;
6374

6475
StringBuilder responseData = new StringBuilder();
76+
String currentDirectory = System.getProperty("user.dir");
6577
try (PrintWriter out = response.getWriter()) {
6678

6779
while ((line = in.readLine()) != null) {
80+
line = line.replace("Copyright 2025 Payara Services Ltd and/or its affiliates.",currentDirectory);
6881
responseData.append(line);
6982
out.println(line);
7083
}

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

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

3+
import java.util.List;
4+
import java.util.Set;
35
import java.util.logging.Logger;
46

7+
import io.gdcc.xoai.model.oaipmh.results.record.Header;
58
import jakarta.ws.rs.*;
69
import jakarta.ws.rs.client.Client;
710
import jakarta.ws.rs.client.ClientBuilder;
@@ -136,7 +139,7 @@ public Response getCustomizationFile(@PathParam("customizationFileType") String
136139
Client client = ClientBuilder.newClient();
137140
WebTarget endpoint = client.target("http://localhost:8080/CustomizationFilesServlet");
138141
Response response = endpoint.queryParam("customFileType", customizationFileType)
139-
.request(MediaType.TEXT_HTML)
142+
.request(MediaType.MEDIA_TYPE_WILDCARD)
140143
.get();
141144

142145
if (response.getLength() < 1) {

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package edu.harvard.iq.dataverse.api;
22

33
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
4+
import io.restassured.http.Header;
45
import io.restassured.response.Response;
56
import org.junit.jupiter.api.AfterEach;
67
import org.junit.jupiter.api.Test;
@@ -16,14 +17,17 @@ public void after() {
1617

1718
@Test
1819
public void testGetCustomAnalytics() {
20+
// String setting = "/usr/local/glassfish4/glassfish/domains/domain1/docroot/index.html";
1921
String setting = "./appserver/glassfish/domains/domain1/docroot/index.html";
2022
UtilIT.setSetting(SettingsServiceBean.Key.WebAnalyticsCode, setting).prettyPrint();
2123

2224
Response getResponse = UtilIT.getCustomizationFile("analytics");
2325
getResponse.prettyPrint();
2426
getResponse.then().assertThat()
2527
.statusCode(200)
26-
.body(containsString("<!doctype html>"));
28+
.body(containsString("<!doctype html>"))
29+
.body(containsString("<p class=\"signoff\">/opt/payara</p>"));//TESTING PWD NOT TO BE CHECKED IN
30+
assert (getResponse.getHeaders().get("Content-Type").getValue().startsWith("text/html"));
2731

2832
UtilIT.deleteSetting(SettingsServiceBean.Key.WebAnalyticsCode).prettyPrint();
2933

@@ -33,4 +37,27 @@ public void testGetCustomAnalytics() {
3337
.statusCode(404)
3438
.body(containsString("not found"));
3539
}
40+
41+
@Test
42+
public void testGetCustomLogo() {
43+
// String setting = "/usr/local/glassfish4/glassfish/domains/domain1/docroot/img/logo.png";
44+
String setting = "./appserver/glassfish/domains/domain1/docroot/img/logo.png";
45+
UtilIT.setSetting(SettingsServiceBean.Key.LogoCustomizationFile, setting).prettyPrint();
46+
47+
Response getResponse = UtilIT.getCustomizationFile("logo");
48+
getResponse.prettyPrint();
49+
getResponse.then().assertThat()
50+
.statusCode(200)
51+
.body(containsString("PNG"));
52+
53+
assert (getResponse.getHeaders().get("Content-Type").getValue().startsWith("image/png"));
54+
55+
UtilIT.deleteSetting(SettingsServiceBean.Key.LogoCustomizationFile).prettyPrint();
56+
57+
getResponse = UtilIT.getCustomizationFile("logo");
58+
getResponse.prettyPrint();
59+
getResponse.then().assertThat()
60+
.statusCode(404)
61+
.body(containsString("not found"));
62+
}
3663
}

0 commit comments

Comments
 (0)