Commit 90eb8011 authored by Ryan Berkheimer's avatar Ryan Berkheimer
Browse files

hardened error messages for and completed key existence and map refinement...

hardened error messages for and completed key existence and map refinement checks for the StandardSession
parent d53ac449
......@@ -6,27 +6,12 @@
"required": true
}
],
"conditions": [
{
"id": "is-relative-path",
"type": "comparison",
"operator": "gov.noaa.messageapi.operators.StringConditionOperator",
"constructor": {
"comparison": "contains"
},
"field": "file-path",
"value": "{}"
}
],
"collections": [
{
"id": "coll-1",
"classifiers": {},
"fields": [
"file-path"
],
"conditions": [
"is-relative-path"
]
}
],
......
......@@ -56,7 +56,8 @@ def "Tests submission of a full file reader task with 1 large input using a Stan
ISession session = new StandardSession(parameterPath)
IRequest request = session.createRequest()
IRecord record = request.createRecord()
String filePath = '{}/resources/test/inputs/file-reader/proc_sm_gtsnp_data_ftp_CF6_cf6_20190506.txt'
//String filePath = '{}/resources/test/inputs/file-reader/proc_sm_gtsnp_data_ftp_CF6_cf6_20190506.txt'
String filePath = '/workspaces/messageapi/build/resources/test/inputs/file-reader/proc_sm_gtsnp_data_ftp_CF6_cf6_20190506.txt'
record.setField('file-path', filePath)
//println record.getField('file-path').getValue()
when: 'We submit the test session and wait for completion'
......
......@@ -53,7 +53,7 @@ import gov.noaa.messageapi.rejections.DefaultRejection;
* <p>
* <p>
* <h3>Implementation</h3>
* The specific API of methods in native code are dependent on the language (e.g, C, Fortran);
* The specific API of methods in native code are dependent on the language (e.g, C, C++, Fortran, Python,...);
* however, every native language will be initially wrapped by a JNI method in a C file that corresponds to the
* 'process' method in this class, and all native processing related to use
* of MessageAPI will operate on the passed jlong and the provided MessageApiEndpoint Library.
......
package gov.noaa.messageapi.sessions;
import static java.lang.String.join;
import java.io.File;
import java.util.Map;
import gov.noaa.messageapi.exceptions.MessageApiException;
import gov.noaa.messageapi.utils.general.JsonUtils;
import gov.noaa.messageapi.interfaces.ISession;
......@@ -20,7 +25,6 @@ public class StandardSession extends DefaultSession {
final static String containerMetadataName = "container.json";
final static String protocolMetadataName = "protocol.json";
/**
* Constructs a new publish session directly from a text map. The text map
* should contain all the properties needed for Session Construction.
......@@ -35,33 +39,93 @@ public class StandardSession extends DefaultSession {
/**
* Accesses the installed template file using an env var, parses it into json,
* and then updates targeted keys to
* and then updates targeted keys to
*/
private static ISession buildStandardSession(final String fqParamSpec) throws Exception {
final Map<String, Object> parameterMap = StandardSession.parseParameterMap(fqParamSpec);
final String sessionTemplateDir = System.getenv("MESSAGEAPI_SESSION_TEMPLATE_DIR");
final String sessionTemplateFQName = String.join("/", sessionTemplateDir, standardSessionName);
final String schemaMetadataFQName = String.join("/", sessionTemplateDir, schemaMetadataName);
final String containerMetadataFQName = String.join("/", sessionTemplateDir,
containerMetadataName);
final String protocolMetadataFQName = String.join("/", sessionTemplateDir, protocolMetadataName);
final Map<String, Object> sessionMap = JsonUtils.convertObject(JsonUtils.parse(sessionTemplateFQName));
StandardSession.modifyMapValue(sessionMap, "schema", "fields", fqParamSpec);
StandardSession.modifyMapValue(sessionMap, "schema", "conditions", fqParamSpec);
StandardSession.modifyMapValue(sessionMap, "schema", "metadata", schemaMetadataFQName);
StandardSession.modifyMapValue(sessionMap, "container", "collections", fqParamSpec);
StandardSession.modifyMapValue(sessionMap, "container", "transformations", fqParamSpec);
StandardSession.modifyMapValue(sessionMap, "container", "metadata", containerMetadataFQName);
StandardSession.modifyMapValue(sessionMap, "protocol", "endpoints", fqParamSpec);
StandardSession.modifyMapValue(sessionMap, "protocol", "metadata", protocolMetadataFQName);
final String sessionTemplateFQName = String.join(File.separator, sessionTemplateDir, standardSessionName);
final Map<String, Object> sessionMap = StandardSession.parseSessionMap(sessionTemplateFQName);
final String schemaMetadataFQName = String.join(File.separator, sessionTemplateDir, schemaMetadataName);
final String containerMetadataFQName = join(File.separator, sessionTemplateDir, containerMetadataName);
final String protocolMetadataFQName = String.join(File.separator, sessionTemplateDir, protocolMetadataName);
if (parameterMap.containsKey("fields")) {
StandardSession.setMapValue(sessionMap, "schema", "fields", fqParamSpec);
} else {
throw new MessageApiException(StandardSession.getMissingKeyErrorMessage("fields"));
}
if (parameterMap.containsKey("collections")) {
StandardSession.setMapValue(sessionMap, "container", "collections", fqParamSpec);
} else {
throw new MessageApiException(StandardSession.getMissingKeyErrorMessage("collections"));
}
if (parameterMap.containsKey("endpoints")) {
StandardSession.setMapValue(sessionMap, "protocol", "endpoints", fqParamSpec);
} else {
throw new MessageApiException(StandardSession.getMissingKeyErrorMessage("endpoints"));
}
if (parameterMap.containsKey("conditions")) {
StandardSession.setMapValue(sessionMap, "schema", "conditions", fqParamSpec);
} else {
StandardSession.removeMapKey(sessionMap, "schema", "conditions");
}
if (parameterMap.containsKey("transformations")) {
StandardSession.setMapValue(sessionMap, "container", "transformations", fqParamSpec);
} else {
StandardSession.removeMapKey(sessionMap, "container", "transformations");
}
StandardSession.setMapValue(sessionMap, "schema", "metadata", schemaMetadataFQName);
StandardSession.setMapValue(sessionMap, "container", "metadata", containerMetadataFQName);
StandardSession.setMapValue(sessionMap, "protocol", "metadata", protocolMetadataFQName);
return new DefaultSession(sessionMap);
}
/**
* Parses the session map and checks for installation errors.
*/
private static Map<String, Object> parseSessionMap(String sessionTemplateFQName) throws Exception {
try {
return JsonUtils.convertObject(JsonUtils.parse(sessionTemplateFQName));
} catch (Exception e) {
throw new MessageApiException("Failed trying to parse the standard session. Check your MessageAPI installation.");
}
}
/**
* Parses the parameter map and checks for configuration errors.
*/
private static Map<String, Object> parseParameterMap(String fqParamSpec) throws Exception {
try {
return JsonUtils.convertObject(JsonUtils.parse(fqParamSpec));
} catch (Exception e) {
throw new MessageApiException("Failed initial configuration parsing check. Double check your JSON structure.");
}
}
/**
* Sets a map value for a key on a layer (e.g., schema/container/protocol and a component)
*/
@SuppressWarnings("unchecked")
private static void modifyMapValue(final Map<String, Object> map, final String layer, final String component,
private static void setMapValue(final Map<String, Object> map, final String layer, final String component,
final String newValue) {
((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) map.get("constructor"))
.get(layer)).get("constructor")).put(component, newValue);
((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) map.get("constructor")).get(layer))
.get("constructor")).put(component, newValue);
}
/**
* Removes a component map key attached to a given layer
*/
@SuppressWarnings("unchecked")
private static void removeMapKey(final Map<String, Object> map, final String layer, final String component) {
((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) map.get("constructor")).get(layer))
.get("constructor")).keySet().remove(component);
}
/**
* Builds a missing required parameter key error message for the specified component.
*/
private static String getMissingKeyErrorMessage(final String key) {
return String.format("Your parameter map is missing the required '%s' key. Check your configuration. Stopping session building now.", key);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment