Skip to content

Commit 6b2b76f

Browse files
committed
Merge remote-tracking branch 'IQSS/develop' into RoleAccessHistory
2 parents a348b98 + b3e1c78 commit 6b2b76f

13 files changed

Lines changed: 444 additions & 226 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Feature Request: API endpoint for analytics.html
2+
3+
New API to get the analytics.html from settings for SPA (Also can be used to get homePage, header, footer, style, and logo)
4+
5+
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6037,6 +6037,34 @@ The fully expanded example above (without environment variables) looks like this
60376037
60386038
curl "https://demo.dataverse.org/api/info/exportFormats"
60396039
6040+
Get Customization File Contents
6041+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6042+
6043+
The Customization API is used to retrieve the analytics-code.html as well as other customization file contents.
6044+
6045+
See also :ref:`web-analytics-code` in the Configuration section of the Installation Guide and :ref:`Branding Your Installation`
6046+
6047+
The Content-Type returned in the header is based on the media type of the file being returned (example: analytics-code.html returns "text/html; charset=UTF-8"
6048+
6049+
Valid types are "homePage", "header", "footer", "style", "analytics", and "logo".
6050+
6051+
A curl example getting the analytics-code
6052+
6053+
.. code-block:: bash
6054+
6055+
export SERVER_URL=https://demo.dataverse.org
6056+
export TYPE=analytics
6057+
6058+
curl -X GET "$SERVER_URL/api/info/settings/customization/$TYPE"
6059+
6060+
The fully expanded example above (without environment variables) looks like this:
6061+
6062+
.. code-block:: bash
6063+
6064+
curl -X GET "https://demo.dataverse.org/api/info/settings/customization/analytics"
6065+
6066+
.. _customization-analytics:
6067+
60406068
.. _metadata-blocks-api:
60416069
60426070
Metadata Blocks

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

Lines changed: 13 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,6 +60,13 @@ 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));

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

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

3-
import java.io.FileInputStream;
4-
import java.io.InputStream;
5-
import java.net.URL;
6-
import java.nio.charset.StandardCharsets;
7-
import java.util.Arrays;
8-
import java.util.List;
9-
import java.util.logging.Level;
103
import java.util.logging.Logger;
114

12-
import jakarta.ws.rs.Produces;
13-
import org.apache.commons.io.IOUtils;
5+
import edu.harvard.iq.dataverse.customization.CustomizationConstants;
6+
import jakarta.ws.rs.*;
7+
import jakarta.ws.rs.client.Client;
8+
import jakarta.ws.rs.client.ClientBuilder;
9+
import jakarta.ws.rs.client.WebTarget;
1410

1511
import edu.harvard.iq.dataverse.export.ExportService;
1612
import edu.harvard.iq.dataverse.settings.JvmSettings;
1713
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
18-
import edu.harvard.iq.dataverse.util.BundleUtil;
1914
import edu.harvard.iq.dataverse.util.SystemConfig;
2015
import io.gdcc.spi.export.Exporter;
2116
import io.gdcc.spi.export.ExportException;
@@ -24,9 +19,6 @@
2419
import jakarta.json.Json;
2520
import jakarta.json.JsonObjectBuilder;
2621
import jakarta.json.JsonValue;
27-
import jakarta.ws.rs.GET;
28-
import jakarta.ws.rs.Path;
29-
import jakarta.ws.rs.QueryParam;
3022
import jakarta.ws.rs.core.MediaType;
3123
import jakarta.ws.rs.core.Response;
3224
import org.eclipse.microprofile.openapi.annotations.Operation;
@@ -139,6 +131,26 @@ public Response getExportFormats() {
139131
return ok(responseModel);
140132
}
141133

134+
@GET
135+
@Path("settings/customization/{customizationFileType}")
136+
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+
}
141+
Client client = ClientBuilder.newClient();
142+
WebTarget endpoint = client.target("http://localhost:8080/CustomizationFilesServlet");
143+
Response response = endpoint.queryParam("customFileType", type)
144+
.request(MediaType.MEDIA_TYPE_WILDCARD)
145+
.get();
146+
147+
if (response.getLength() < 1) {
148+
return notFound(type + " not found.");
149+
} else {
150+
return response;
151+
}
152+
}
153+
142154
private Response getSettingResponseByKey(SettingsServiceBean.Key key) {
143155
String setting = settingsService.getValueForKey(key);
144156
if (setting != null) {

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

0 commit comments

Comments
 (0)