Skip to content

Commit

Permalink
Fixed the services bit
Browse files Browse the repository at this point in the history
refs: #5379
  • Loading branch information
mattjdnv committed Oct 18, 2022
1 parent e64dbc9 commit 6bc7221
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 92 deletions.
6 changes: 2 additions & 4 deletions hoot-services/src/main/java/hoot/services/HootProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public final class HootProperties {
public static final String JS_HEADER_SCRIPT_PATH;
public static final String CHANGESETS_FOLDER;
public static final String SCRIPT_FOLDER;
public static final String DEFAULT_TRANSLATIONS_CONFIG;
public static final String DEFAULT_FOUO_TRANSLATIONS_CONFIG;
public static final String TRANSLATIONS_CONFIG;
public static final String CORE_SCRIPT_PATH;
public static final String ERROR_LOG_PATH;
public static final String TEMP_OUTPUT_PATH;
Expand Down Expand Up @@ -224,8 +223,7 @@ public final class HootProperties {
CONFIG_OPTIONS = getProperty("configJsonPath");
DOC_NAME = getProperty("documentName");
JS_HEADER_SCRIPT_PATH = getProperty("dummyjsHeaderScriptPath");
DEFAULT_TRANSLATIONS_CONFIG = getProperty("defaultTranslationsConfig");
DEFAULT_FOUO_TRANSLATIONS_CONFIG = getProperty("defaultFOUOTranslationsConfig");
TRANSLATIONS_CONFIG = getProperty("translationsConfig");
CORE_SCRIPT_PATH = getProperty("coreScriptPath");
ERROR_LOG_PATH = getProperty("ErrorLogPath");
ELEMENT_MERGE_SERVER_PORT = getProperty("ElementMergeServerPort");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,19 @@
@Transactional
public class CustomScriptResource {
private static final Logger logger = LoggerFactory.getLogger(CustomScriptResource.class);
private static final boolean FOUO_TRANSLATIONS_EXIST;
private static final String HEADER_START = "/*<<<";
private static final String HEADER_END = ">>>*/" + System.lineSeparator();

static {
if ((new File(DEFAULT_FOUO_TRANSLATIONS_CONFIG)).exists()) {
FOUO_TRANSLATIONS_EXIST = true;
logger.info("FOUO translations are present.");
}
else {
FOUO_TRANSLATIONS_EXIST = false;
logger.info("FOUO translations are not present.");
}
}
// static {
// if ((new File(DEFAULT_FOUO_TRANSLATIONS_CONFIG)).exists()) {
// FOUO_TRANSLATIONS_EXIST = true;
// logger.info("FOUO translations are present.");
// }
// else {
// FOUO_TRANSLATIONS_EXIST = false;
// logger.info("FOUO translations are not present.");
// }
// }
/**
* Returns the directory the scripts are stored in
*
Expand Down Expand Up @@ -236,14 +235,7 @@ public Response getScriptsList(@Context HttpServletRequest request) {
JSONArray filesList = new JSONArray();

try {
List<String> configFiles = new ArrayList<>();
configFiles.add(DEFAULT_TRANSLATIONS_CONFIG);

if (FOUO_TRANSLATIONS_EXIST) {
configFiles.add(DEFAULT_FOUO_TRANSLATIONS_CONFIG);
}

filesList.addAll(getDefaultList(configFiles));
filesList.addAll(getDefaultList());

for (Object o : filesList) {
JSONObject cO = (JSONObject) o;
Expand Down Expand Up @@ -319,12 +311,26 @@ public Response getScriptsList(@Context HttpServletRequest request) {
* @return JSONArray of translation objects
* @throws IOException, ParseException
*/
private static JSONArray getDefaultList(List<String> configFiles) throws IOException, ParseException {
private static JSONArray getDefaultList() throws IOException, ParseException {
JSONArray filesList = new JSONArray();
List<File> configFiles = new ArrayList<>();

// List<File> fileList = File.listFiles(HOME_FOLDER,new String[] {},false);
File dir = new File(HOME_FOLDER);
for (File file : dir.listFiles()) {
if (file.isDirectory() && file.getName().startsWith("translations")) {
logger.error("XChecking - got Dir" + file.getName());

File confFile = new File(file,"translationConfig.json");
if (confFile.exists()) {
configFiles.add(confFile);
logger.error("Found: " + confFile.getName());
}
}
}

for (String configFile : configFiles) {
File file = new File(configFile);
if (file.exists()) {
for (File configFile : configFiles) {
if (configFile.exists()) {
try (FileReader reader = new FileReader(configFile)) {
JSONParser jsonParser = new JSONParser();
JSONArray defTranslations = (JSONArray) jsonParser.parse(reader);
Expand Down Expand Up @@ -357,18 +363,7 @@ private static JSONArray getDefaultList(List<String> configFiles) throws IOExcep
if (!defTranslations.isEmpty()) {
for (Object oTrans : defTranslations) {
JSONObject jsTrans = (JSONObject) oTrans;
if (jsTrans.get("fouoPath") != null) {
// See if FOUO folder exists
File fouoPath = new File(HOME_FOLDER + "/" + jsTrans.get("fouoPath"));
if (fouoPath.exists()) {
filesList.add(jsTrans);
}
}
else {
// If there is no fouoPath then assume it is
// not FOUO translation
filesList.add(jsTrans);
}
filesList.add(jsTrans);
}
}
}
Expand Down Expand Up @@ -516,14 +511,7 @@ public Response getScript(@Context HttpServletRequest request, @QueryParam("scri
public Response getDefaultScript(@QueryParam("scriptPath") String scriptPath) {
String script = "";
try {
List<String> configFiles = new ArrayList<>();

configFiles.add(DEFAULT_TRANSLATIONS_CONFIG);
if (FOUO_TRANSLATIONS_EXIST) {
configFiles.add(DEFAULT_FOUO_TRANSLATIONS_CONFIG);
}

JSONArray defList = getDefaultList(configFiles);
JSONArray defList = getDefaultList();

// See Bug #6483 Read vulnerability in services script API
boolean pathValidated = false;
Expand All @@ -536,12 +524,6 @@ public Response getDefaultScript(@QueryParam("scriptPath") String scriptPath) {
break;
}

Object fouoPath = item.get("fouoPath");
if ((fouoPath != null) && scriptPath.equals(fouoPath.toString())) {
pathValidated = true;
break;
}

Object importPath = item.get("importPath");
Object exportPath = item.get("exportPath");
if ((importPath != null) && scriptPath.equals(importPath.toString())) {
Expand Down
7 changes: 2 additions & 5 deletions hoot-services/src/main/resources/conf/hoot-services.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ BasemapRasterExtensions=png,tif
# Catalina log location
ErrorLogPath=/var/log/tomcat8/catalina.out

# Default translations config file
defaultTranslationsConfig=$(homeFolder)/conf/services/DefaultTranslations.json

# Default FOUO translations config file
defaultFOUOTranslationsConfig=$(homeFolder)/translations-local/DefaultTranslationsFOUO.json
# The translations config file name
translationsConfig=translationConfig.json

# Main document name
documentName=HootenannyUserGuide.pdf
Expand Down
90 changes: 56 additions & 34 deletions translations/TranslationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if (typeof hoot === 'undefined') {

// Setup the lists of schema
var availableTrans = {};
var availableTranslations = [];
var schemaMap = {};
var fcodeLookup = {};

Expand All @@ -47,52 +48,73 @@ var translationsMap = {


// Loop through all "translations*" directories and load config files if they exist.

// The structure of the translation config file is very basic json:
// {
// "availableTrans" : {"Smurf": {"isavailable": true}},
// "schemaMap" : {"Smurf":"/Smurf_full_schema.js"},
// "fcodeLookup" : {"Smurf": "eSmurf_osm.js"},
// "translationsMap" : {
// "toogr": {"Smurf": "Smurf_to_OGR.js" },
// "toosm": {"Smurf": "Smurf_to_OSM.js"}
// }
// }
// The structure of the translation config file is very basic json array.
// NOTE: This is also used by the Hoot services for the list of available translations
// [
// {
// "name" : "smurf",
// "description" : "The name of the format",
// "canExport" : true,
// "schema":"smurf_schema.js",
// "path" : "Smurf.js"
// },
// {
// "name" : "anotherSmurf",
// "description" : "The name of the format",
// "canExport" : true,
// "schema":"anotherSmurf_schema.js",
// "importPath" : "import_smurf.js",
// "exportPath" : "export_smurf.js"
// }
// ]

fs.readdirSync(HOOT_HOME,{withFileTypes:true}).filter(file => file.isDirectory() && (file.name.indexOf('translations') == 0)).forEach( function (file){
var tLocal = {};
var dirName = HOOT_HOME + '/' + file.name + '/';
var fullName = HOOT_HOME + '/' + file.name + '/translationServerConfig.json';
if (fs.existsSync(fullName))

// Should this be in a config var?
if (fs.existsSync(dirName + 'translationConfig.json'))
{
try {
tLocal = require(fullName);
Object.keys(tLocal.availableTrans).forEach(k => {availableTrans[k] = tLocal.availableTrans[k]});
Object.keys(tLocal.schemaMap).forEach(k => {schemaMap[k] = require(dirName + tLocal.schemaMap[k].split('/').pop());});

Object.keys(tLocal.fcodeLookup).forEach(k => {fcodeLookup[k] = require(dirName + tLocal.fcodeLookup[k].split('/').pop())});

Object.keys(tLocal.translationsMap.toogr).forEach(k => {
translationsMap.toogr[k] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + tLocal.translationsMap.toogr[k].split('/').pop(),
'schema.translation.direction':'toogr'
});
});

Object.keys(tLocal.translationsMap.toosm).forEach(k => {
translationsMap.toosm[k] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + tLocal.translationsMap.toosm[k].split('/').pop(),
'schema.translation.direction':'toosm'
});
});
tLocal = require(dirName + 'translationConfig.json');

tLocal.forEach(function (fmt) {
// If we don't have a specified schema then we don't support schema switching in the UI
if (fmt.schema)
{
availableTrans[fmt.name] = {"isavailable":true};
availableTranslations.push(fmt.name);
schemaMap[fmt.name] = require(dirName + fmt.schema);

// Either a "path" or "importPath" and "exportPath"
if (fmt.path)
{
fcodeLookup[fmt.name] = require(dirName + fmt.path);
translationsMap.toogr[fmt.name] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + fmt.path,
'schema.translation.direction':'toogr'});
translationsMap.toosm[fmt.name] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + fmt.path,
'schema.translation.direction':'toosm'});
}
else
{
fcodeLookup[fmt.name] = require(dirName + fmt.importPath)
translationsMap.toogr[fmt.name] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + fmt.exportPath,
'schema.translation.direction':'toogr'});
translationsMap.toosm[fmt.name] = new hoot.SchemaTranslationOp({
'schema.translation.script': dirName + fmt.importPath,
'schema.translation.direction':'toosm'});
}
}
});
} catch (e) {
console.log('Translation Config Error: ' + e);
}
}
});

availableTranslations = Object.keys(schemaMap);

if (require.main === module) {
//I'm a running server

Expand Down
94 changes: 94 additions & 0 deletions translations/translationConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[
{
"name" : "TDSv71",
"description" : "NSG Topographic Data Store (TDS) v7.1",
"canExport" : true,
"schema":"tds71_full_schema.js",
"path" : "TDSv71.js"
},
{
"name" : "MGCP",
"description" : "Multinational Geospatial Co-production Program (MGCP) TRD3&4",
"canExport" : true,
"schema":"mgcp_schema.js",
"path" : "MGCP_TRD4.js"
},
{
"name" : "MGCP - Cartographic",
"description" : "MGCP TRD4 For Cartographic Use Only",
"canExport" : true,
"path" : "MGCP_TRD4_Cartographic.js"
},
{
"name" : "GGDMv30",
"description" : "Ground-Warfighter Geospatial Data Model (GGDM) v3.0",
"canExport" : true,
"schema":"ggdm30_schema.js",
"path" : "GGDMv30.js"
},
{
"name" : "TDSv61",
"description" : "NSG Topographic Data Store (TDS) v6.1",
"canExport" : true,
"schema":"tds61_full_schema.js",
"path" : "TDSv61.js"
},
{
"name" : "TDSv40",
"description" : "NSG Topographic Data Store (TDS) v4.0",
"canExport" : true,
"schema" : "tds40_full_schema.js",
"path" : "TDSv40.js"
},
{
"name" : "TDSv70",
"description" : "NSG Topographic Data Store (TDS) v7.0",
"canExport" : true,
"schema":"tds70_full_schema.js",
"path" : "TDSv70.js"
},
{
"name" : "DNC",
"description" : "Digital Nautical Chart",
"canExport" : true,
"path" : "DNC.js"
},
{
"name" : "OSM",
"description" : "OpenStreetMap.org (OSM)",
"canExport" : true,
"importPath" : "OSM_Ingest.js",
"exportPath" : "RenderDb.js"
},
{
"name" : "ENC",
"description" : "S-57 Electronic Navigation Chart V3.1.1",
"canExport" : true,
"importPath" : "ENCv311_to_OSM.js",
"exportPath" : "ENCv311_to_OGR.js"
},
{
"name" : "GIFD D4",
"description" : "Geospatial Intelligence Feature Database (GIFD) D4",
"canExport" : false,
"path" : "GIFD_D4.js"
},
{
"name" : "UFD",
"description" : "Urban Feature Data (UFD) Levels 1-3",
"canExport" : false,
"path" : "UFD_to_OSM.js"
},
{
"name" : "WorldPortIndex",
"description" : "NGA World Port Index",
"canExport" : false,
"path" : "WorldPortIndex.js"
},
{
"name" : "GeoNames",
"description" : "GeoNames.org and geonames.nga.mil",
"canExport" : false,
"path" : "GeoNames_to_OSM.js"
}
]

0 comments on commit 6bc7221

Please sign in to comment.