-
-
Notifications
You must be signed in to change notification settings - Fork 498
Add metadata editor #9004
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
Draft
Guillaume-d-o
wants to merge
2
commits into
geonetwork:main
Choose a base branch
from
Guillaume-d-o:metadata-editor-integration-
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add metadata editor #9004
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,18 @@ | |
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
// Generated by ChatGPT | ||
// Generated by ChatGPT & Claude3.7 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment necessary? |
||
public class FileUtils { | ||
private static final Path TEMP_DIR; | ||
private static final Path DATAHUB_TEMP_DIR; | ||
private static final Path METADATA_EDITOR_TEMP_DIR; | ||
|
||
static { | ||
try { | ||
TEMP_DIR = Files.createTempDirectory("plugin_cache"); | ||
TEMP_DIR.toFile().deleteOnExit(); // Ensure temp directory is cleaned up | ||
DATAHUB_TEMP_DIR = Files.createTempDirectory("datahub_cache"); | ||
METADATA_EDITOR_TEMP_DIR = Files.createTempDirectory("metadata_editor_cache"); | ||
|
||
DATAHUB_TEMP_DIR.toFile().deleteOnExit(); | ||
METADATA_EDITOR_TEMP_DIR.toFile().deleteOnExit(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
@@ -25,7 +29,17 @@ public static boolean fileExistsInJar(String relativePath) { | |
} | ||
|
||
public static File getFileFromJar(String relativePath) throws IOException { | ||
File tempFile = new File(TEMP_DIR.toFile(), new File(relativePath).getName()); | ||
Path tempDir; | ||
|
||
if (relativePath.startsWith("/datahub/") || relativePath.startsWith("datahub/")) { | ||
tempDir = DATAHUB_TEMP_DIR; | ||
} else if (relativePath.startsWith("/metadata-editor/") || relativePath.startsWith("metadata-editor/")) { | ||
tempDir = METADATA_EDITOR_TEMP_DIR; | ||
} else { | ||
tempDir = DATAHUB_TEMP_DIR; | ||
} | ||
|
||
File tempFile = new File(tempDir.toFile(), new File(relativePath).getName()); | ||
if (tempFile.exists()) { | ||
return tempFile; | ||
} | ||
|
221 changes: 221 additions & 0 deletions
221
...hub-integration/src/main/java/org/fao/geonet/metadataeditor/MetadataEditorController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
package org.fao.geonet.metadataeditor; | ||
|
||
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpHeaders; | ||
import org.fao.geonet.NodeInfo; | ||
import org.fao.geonet.datahub.FileUtils; | ||
import org.fao.geonet.domain.Source; | ||
import org.fao.geonet.domain.SourceType; | ||
import org.fao.geonet.repository.SourceRepository; | ||
import org.fao.geonet.utils.Log; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import static org.fao.geonet.kernel.schema.SchemaPlugin.LOGGER_NAME; | ||
|
||
// FIXME: unit tests missing for this class! | ||
|
||
@RequestMapping(value = {"/{geonetworkPath:[a-zA-Z0-9_\\-]+}"}) | ||
@Controller("metadata-editor") | ||
public class MetadataEditorController { | ||
public static final String INDEX_PATH = "/metadata-editor/index.html"; | ||
public static final String METADATA_EDITOR_FILES_PATH = "/metadata-editor/"; | ||
public static final String DEFAULT_CONFIGURATION_FILE_PATH = METADATA_EDITOR_FILES_PATH + "assets/configuration/default.toml"; | ||
|
||
@Autowired | ||
SourceRepository sourceRepository; | ||
|
||
/*@GetMapping("/metadata-editor/status") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note for me : delete |
||
public ResponseEntity<String> getDatahubStatus() throws IOException { | ||
File configFile = FileUtils.getFileFromJar(DEFAULT_CONFIGURATION_FILE_PATH); | ||
String defaultConfig = FileUtils.readFromInputStream(new FileInputStream(configFile)); | ||
JSONObject body = new JSONObject(); | ||
body.put("defaultConfig", defaultConfig); | ||
File packageJsonFile = FileUtils.getFileFromJar( DATAHUB_FILES_PATH + "package.json"); | ||
String packageJsonContent = FileUtils.readFromInputStream(new FileInputStream(packageJsonFile)); | ||
JSONObject packageJson = new JSONObject(packageJsonContent); | ||
return ResponseEntity | ||
.ok() | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(body.toString()); | ||
}*/ | ||
|
||
@GetMapping("/metadata-editor") | ||
public RedirectView redirectMetadataEditor(HttpServletRequest request, HttpServletResponse response) { | ||
String uri = request.getRequestURI(); | ||
if (!uri.endsWith("/")) { | ||
uri += "/"; | ||
} | ||
return new RedirectView(uri + "index.html"); | ||
} | ||
|
||
@GetMapping("/{locale:[a-z]{2,3}}/metadata-editor") | ||
public RedirectView redirectLocalizedMetadataEditor(HttpServletRequest request, HttpServletResponse response) { | ||
String uri = request.getRequestURI(); | ||
if (!uri.endsWith("/")) { | ||
uri += "/"; | ||
} | ||
return new RedirectView(uri + "index.html"); | ||
} | ||
|
||
@RequestMapping("/metadata-editor/**") | ||
public void handleMetadataEditorWithFilepath(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
handleMetadataEditorRequest(request, response); | ||
} | ||
|
||
@RequestMapping("/{locale:[a-z]{2,3}}/metadata-editor/**") | ||
public void handleLocalizedMetadataEditorWithFilepath(HttpServletRequest request, HttpServletResponse response, | ||
@PathVariable String locale) throws IOException { | ||
handleMetadataEditorRequest(request, response); | ||
} | ||
|
||
void handleMetadataEditorRequest(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
String portalName = getPortalName(request); | ||
if (!isPortalDatahubEnabled(portalName)) { | ||
response.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
return; | ||
} | ||
|
||
File actualFile = getRequestedFile(request); | ||
if (!actualFile.exists()) { | ||
actualFile = getFallbackFile(); | ||
disableCacheForIndex(response); | ||
} | ||
|
||
setResponseHeaders(response, actualFile); | ||
writeResponseContent(request, response, actualFile, portalName); | ||
} | ||
|
||
private String getPortalName(HttpServletRequest request) { | ||
String reqPath = request.getPathInfo(); | ||
String[] parts = reqPath.split("/"); | ||
return FilenameUtils.getName(parts[1]); | ||
} | ||
|
||
private boolean isPortalDatahubEnabled(String portalName) { | ||
if (NodeInfo.DEFAULT_NODE.equals(portalName)) { | ||
return true; | ||
} else if (sourceRepository.existsByUuidAndType(portalName, SourceType.subportal)) { | ||
return Objects.requireNonNull(sourceRepository.findOneByUuid(portalName)).getDatahubEnabled(); | ||
} | ||
return false; | ||
} | ||
|
||
private File getRequestedFile(HttpServletRequest request) { | ||
String reqPath = request.getPathInfo(); | ||
String filePath = Stream.of(reqPath.split("/metadata-editor/")).skip(1).collect(Collectors.joining("/")); | ||
filePath = FilenameUtils.normalize(filePath); | ||
try { | ||
return FileUtils.getFileFromJar(METADATA_EDITOR_FILES_PATH + filePath); | ||
} catch (IOException e) { | ||
return new File(INDEX_PATH);// return file doesn't exist in jar => go back to main menu | ||
} | ||
} | ||
|
||
private File getFallbackFile() { | ||
try { | ||
return FileUtils.getFileFromJar(INDEX_PATH); | ||
} catch (IOException e) { | ||
Log.error(LOGGER_NAME, e.getMessage()); | ||
return new File(INDEX_PATH); | ||
} | ||
} | ||
|
||
private void disableCacheForIndex(HttpServletResponse response) { | ||
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache"); | ||
response.setHeader(HttpHeaders.PRAGMA, "no-cache"); | ||
response.setHeader(HttpHeaders.EXPIRES, "0"); | ||
} | ||
|
||
private void setResponseHeaders(HttpServletResponse response, File actualFile) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
String extension = actualFile.getName().toLowerCase(); | ||
String contentType = extension.equals("js") ? "text/javascript; charset=UTF-8" | ||
: Files.probeContentType(actualFile.toPath()); | ||
response.setContentType(contentType); | ||
} | ||
|
||
void writeResponseContent(HttpServletRequest request, HttpServletResponse response, File actualFile, | ||
String portalName) throws IOException { | ||
InputStream inStream = actualFile.getName().equals("default.toml") ? readConfiguration(portalName) | ||
: new FileInputStream(actualFile); | ||
OutputStream outStream = response.getOutputStream(); | ||
|
||
if (request.getHeader(HttpHeaders.ACCEPT_ENCODING).contains("gzip")) { | ||
response.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); | ||
outStream = new GZIPOutputStream(outStream); | ||
} | ||
|
||
if (actualFile.getName().equals("index.html")) { | ||
// rewrite the base-href attribute to make app routing work | ||
String baseHref = "/geonetwork/" + portalName + "/metadata-editor/"; | ||
String content = IOUtils | ||
.toString(inStream, StandardCharsets.UTF_8) | ||
.replaceAll("<base href=\".*\">", "<base href=\"" + baseHref + "\">"); | ||
outStream.write(content.getBytes()); | ||
} else { | ||
IOUtils.copy(inStream, outStream); | ||
} | ||
|
||
outStream.close(); | ||
} | ||
|
||
InputStream readConfiguration(String portalName) throws IOException { | ||
String configuration = getPortalConfiguration(portalName); | ||
configuration = configuration.replaceAll("\ngeonetwork4_api_url\\s?=.+", "\n") | ||
.replace("[global]", "[global]\ngeonetwork4_api_url = \"/geonetwork/" + portalName + "/api\""); | ||
return new ByteArrayInputStream(configuration.getBytes()); | ||
} | ||
|
||
private String getPortalConfiguration(String portalName) throws IOException { | ||
Source portal = Objects.requireNonNull(sourceRepository.findByType(SourceType.portal, null)).get(0); | ||
if (isNotDefaultPortal(portalName)) { | ||
portal = sourceRepository.findOneByUuid(portalName); | ||
} | ||
|
||
// 1. read for subportal | ||
if (datahubConfigurationExist(portal)) { | ||
return portal.getDatahubConfiguration(); | ||
} | ||
// 2. fallback: read from main portal | ||
else if (isNotDefaultPortal(portalName)) { | ||
return this.getPortalConfiguration(NodeInfo.DEFAULT_NODE); | ||
} | ||
// 3. fallback: read from default.toml file in resource | ||
else { | ||
File defaultConfig = FileUtils.getFileFromJar(DEFAULT_CONFIGURATION_FILE_PATH); | ||
return FileUtils.readFromInputStream(new FileInputStream(defaultConfig)); | ||
} | ||
} | ||
|
||
private boolean isNotDefaultPortal(String portalName) { | ||
return !portalName.equals(NodeInfo.DEFAULT_NODE); | ||
} | ||
|
||
private boolean datahubConfigurationExist(Source portal) { | ||
return portal != null | ||
&& portal.getDatahubConfiguration() != null | ||
&& !portal.getDatahubConfiguration().isEmpty(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note for me : delete and put back : ci --loglevel error