Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix frontendproperties readFile() to work with classpath and update download_custom_buttons_json reference #10946

Merged
merged 14 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,9 @@ enable_study_tags=true|false
```

# Add Custom Buttons to data tables
Custom Buttons can be defined which will conditionally appear in all group comparison data tables (with CopyDownloadControls) to launch a custom URL. This can be used, for example, to launch a software application (that is installed on the user's system) with the data. This configuration can also customize new elements on the Visualize page. It points to a JSON file on the classpath. (See [download_custom_buttons reference](download_custom_buttons-Reference.md)).
Custom Buttons can be defined which will conditionally appear in all group comparison data tables (with CopyDownloadControls) to launch a custom URL. This can be used, for example, to launch a software application (that is installed on the user's system) with the data. This configuration can also customize new elements on the Visualize page. It points to a JSON file. (See [download_custom_buttons reference](download_custom_buttons-Reference.md)).


```
download_custom_buttons_json=classpath:/custom_buttons/download_custom_button_avm.json
download_custom_buttons_json=classpath:custom_buttons/download_custom_button_avm.json
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Custom Buttons can be defined which will conditionally appear in all group compa

## Configuration File

The Custom Buttons are defined in a JSON file in the classpath. Set `download_custom_buttons_json` to refer to the file in the
`application.properties` (See [application.properties reference](application.properties-Reference.md#add-custom-buttons-to-data-tables)).
The Custom Buttons are defined in a JSON file on the classpath. Set `download_custom_buttons_json` to refer to the file (see [application.properties reference](application.properties-Reference.md#add-custom-buttons-to-data-tables)).

## JSON format

Expand Down Expand Up @@ -67,6 +66,4 @@ To modify software to leverage this:

- Modify your software to read data from clipboard.
- Modify your software or installer to register a URL protocol to launch.
- Modify your software or installer to install a custom font.


- Modify your software or installer to install a custom font.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -291,18 +295,63 @@ private Map<String,String> cloneProperties() {
);
}

private String readFile(String fileName) {
if (fileName != null && !fileName.isEmpty()) {
try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {
return br.lines().map(String::trim).collect(Collectors.joining(""));
} catch (Exception e) {
log.error("Error reading frontend config file: {}", e.getMessage());
return null;
/**
* Read the file and return the content as a single-line string. This works for:
* 1) loose files given absolute path
* 2) loose files relative to PORTAL_HOME
* 3) "classpath:..."" in the app.jar (relative to PORTAL_HOME)
* @propertiesFileName: the file path
* REF: based on getResourceStream() in WebServletContextListener.java
*/
private String readFile(String propertiesFileName) {
if (propertiesFileName == null || propertiesFileName.isEmpty()) {
return null;
}

InputStream inputStream = null;

// strip off classpath prefix and always check ClassLoader and file system, with latter taking precedence
String filePath = propertiesFileName.startsWith("classpath:")
? propertiesFileName.substring("classpath:".length())
: propertiesFileName;

try {
String home = System.getenv("PORTAL_HOME");

// try absolute or relative to working directory
File file = new File(filePath);
if (file.exists()) {
inputStream = new FileInputStream(file);
} else if (home != null) {
// try relative to PORTAL_HOME
file = new File(Paths.get(home, filePath).toString());
if (file.exists()) {
inputStream = new FileInputStream(file);
}
}

if (inputStream != null) {
log.info("Reading frontend config file: {}", file.getAbsolutePath());
} else {
// try resource (e.g. app.jar)
inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
if (inputStream != null) {
log.info("Reading frontend config resource: {}", filePath);
} else {
throw new Exception("File not found in system or classpath: " + filePath);
}
}

// read the file and return the content as a single-line string
// PRE: inputStream != null
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
return br.lines().map(String::trim).collect(Collectors.joining(""));
pappde marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
log.error("Error reading frontend config file: {}", e.getMessage());
return null;
}
return null;
}

public String getFrontendUrl(String propertyValue) {
String frontendUrlRuntime = env.getProperty("frontend.url.runtime", "");
if (frontendUrlRuntime.length() > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties.EXAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ spring.devtools.restart.enabled=false
#enable_study_tags=true|false

## Custom Buttons
# download_custom_buttons_json=classpath:/custom_buttons/download_custom_button_avm.json
# download_custom_buttons_json=classpath:custom_buttons/download_custom_button_avm.json

# EOL - Do not delete the following lines

Loading