use of CCDD.CcddClassesDataTable.CCDDException in project CCDD by nasa.
the class CcddWebDataAccessHandler method getQueryResults.
/**
********************************************************************************************
* Process the web query and return the results encoded as a JSON string. The query is in the
* form [server]:[port]/[component]?[attribute][=name]
*
* @param component
* component for which to request data
*
* @param item
* item in the component
*
* @return Query results encoded as a JSON string
********************************************************************************************
*/
private String getQueryResults(String component, String item) {
String response = null;
// Log the web server request. Mask the password for authentication requests (match an
// expanded range in case the user mistypes the command)
eventLog.logEvent(EventLogMessageType.SERVER_MSG, "Request component '" + component + "' item '" + (component.contains("authentic") ? item.replaceFirst("=.*(?:;|$)", "=*****") : item) + "'");
try {
String[] separators = null;
// Separate the component/attribute/name from the other flag(s) (if present)
String[] itemAndOther = getParts(item, ";", 2, false);
// Set the default macro and variable path flag values
isReplaceMacro = true;
isIncludePath = false;
// Step through the macro and/or variable path flags, if present
for (String macroAndPath : getParts(itemAndOther[1], ";", 2, false)) {
// Split the macro/path option from any parameter values
String[] parts = getParts(macroAndPath, ",", 2, false);
switch(parts[0].toLowerCase()) {
// Display macro names command
case "macro":
case "macros":
// Set the flag so that the macro names (in place of macro values) are
// displayed
isReplaceMacro = false;
break;
// Include variable paths command
case "path":
case "paths":
// Set the flag to include the variable paths and parse the variable path
// separators
isIncludePath = true;
separators = getVariablePathSeparators(parts[1]);
break;
}
}
// Extract the item's attribute and name
String[] attributeAndName = getParts(itemAndOther[0], "=", 2, false);
// Check if this is a table-related request
if (component.equals("table") || component.equals("proto_table") || component.equals("root_table") || component.equals("instance_table")) {
// Set the tree type (instance, prototype, or both) based on the command
tableTreeType = component.equals("table") ? TableTreeType.TABLES : (component.equals("proto_table") ? TableTreeType.PROTOTYPE_TABLES : TableTreeType.INSTANCE_TABLES);
// Set the maximum level flag if only root table information is requested
isMaxLevel = component.equals("root_table");
// Use the attribute to determine the request
switch(attributeAndName[0]) {
case "all":
case "":
// Get the name, type, description, data, and data fields for the specified
// table (or all tables if no table name is specified)
response = getTableInformation(attributeAndName[1], separators);
break;
case "data":
// Get the data for the specified table (or all tables if no table name is
// specified)
response = getTableData(attributeAndName[1], true, separators);
break;
case "description":
// Get the description for the specified table (or all tables if no table
// name is specified)
response = getTableDescription(attributeAndName[1]);
break;
case "fields":
// Get a data field information for the specified table (or all tables if
// no table name is specified)
response = getTableFields(attributeAndName[1], true);
break;
case "names":
// Get the names of the data tables of the specified type (or all tables if
// no table name is specified)
response = getTableNames(attributeAndName[1]);
break;
case "size":
// Get the size of the specified structure data table (or all structure
// tables if no table name is specified)
response = getStructureSize(attributeAndName[1]);
break;
case "search":
// Get the results of the specified table search
response = getSearchResults(attributeAndName[1]);
break;
default:
throw new CCDDException("unrecognized table attribute '" + attributeAndName[0] + "'");
}
} else // Check if this is a group or application related request
if (component.equals("group") || component.equals("application")) {
// Set the flag to true if this request only applies to groups that represent an
// application
boolean applicationOnly = component.equals("application");
// Set the name based on if a group or application is requested
String name = applicationOnly ? "application" : "group";
// Use the attribute to determine the request
switch(attributeAndName[0]) {
case "all":
case "":
// Get the name, application status, description, and data fields for the
// specified group (or all groups if no group name is specified)
response = getGroupInformation(attributeAndName[1], applicationOnly, new CcddGroupHandler(ccddMain, null, ccddMain.getMainFrame()));
break;
case "tables":
// Get the tables for the specified group (or all groups if no group name
// is specified)
response = getGroupTables(attributeAndName[1], applicationOnly, true, new CcddGroupHandler(ccddMain, null, ccddMain.getMainFrame()));
break;
case "description":
// Get the description for the specified group (or all groups if no group
// name is specified)
response = getGroupDescription(attributeAndName[1], applicationOnly, true, new CcddGroupHandler(ccddMain, null, ccddMain.getMainFrame()));
break;
case "fields":
// Get a data field information for the specified group (or all groups if
// no group name is specified)
response = getGroupFields(attributeAndName[1], applicationOnly, true, new CcddGroupHandler(ccddMain, null, ccddMain.getMainFrame()));
break;
case "names":
// Get all group names
response = getGroupNames(applicationOnly, new CcddGroupHandler(ccddMain, null, ccddMain.getMainFrame()));
break;
default:
throw new CCDDException("unrecognized " + name + " attribute '" + attributeAndName[0] + "'");
}
} else // Check if this is a telemetry or application scheduler request
if (component.equals("scheduler")) {
// Use the attribute to determine the request
switch(attributeAndName[0]) {
case "telemetry":
// Get the telemetry scheduler copy table
response = getTelemetrySchedulerData(attributeAndName[1]);
break;
case "application":
// Get the application scheduler table
response = getApplicationSchedulerData();
break;
default:
throw new CCDDException("unrecognized scheduler attribute '" + attributeAndName[0] + "'");
}
} else // Check if this is a variable names request
if (component.equals("variable")) {
// Get the variable names
response = getVariableNames(attributeAndName[0], attributeAndName[1]);
} else // Check if this is a telemetry parameter request
if (component.equals("telemetry")) {
// Get the telemetered variable information
response = getTelemetryInformation(attributeAndName[0]);
} else // Check if this is a command parameter request
if (component.equals("command")) {
// Get the command information
response = getCommandInformation(attributeAndName[0]);
} else // Check if this is a table type definition request
if (component.equals("table_type")) {
// Get the table type definitions
response = getTableTypeDefinitions();
} else // Check if this is a data type definition request
if (component.equals("data_type")) {
// Get the data type definitions
response = getDataTypeDefinitions();
} else // Check if this is a macro definition request
if (component.equals("macro")) {
// Get the macro definitions
response = getMacroDefinitions();
} else // Check if this is a message ID and name request
if (component.equals("message_id")) {
// Get the message IDs and names
response = getMessageIDAndNames();
} else // Check if this is a user authentication request
if (component.equals("authenticate")) {
// Authenticate the user credentials
response = authenticateUser(attributeAndName[0], attributeAndName[1]);
} else // Check if this is the project information request
if (component.equals("project_info")) {
// Get the project information (name, description, etc.)
response = getProjectInformation();
} else // Check if this is a web server shutdown request
if (component.equals("shutdown")) {
// Execute the command to exit the application on a separate thread so that the
// thread it's closing isn't the one that's running
new Thread() {
/**
****************************************************************************
* Exit the application
****************************************************************************
*/
@Override
public void run() {
System.exit(0);
}
}.start();
} else // The request type is unrecognized
{
throw new CCDDException("unrecognized request component '" + component + "'");
}
} catch (CCDDException ce) {
// Inform the user that the web server request is invalid
eventLog.logFailEvent(ccddMain.getMainFrame(), "Web Server Error", "Invalid web server request; cause '" + ce.getMessage() + "'", "<html><b>Invalid web server request");
} catch (Exception e) {
// Display a dialog providing details on the unanticipated error
CcddUtilities.displayException(e, ccddMain.getMainFrame());
}
// Check if a response to the request was made
if (response != null) {
// Remove the extraneous escape (\) characters that the JSON encoder inserts into the
// string
response = response.replaceAll("\\\\\\\\", "\\\\").replaceAll("\\\\/", "/");
}
return response;
}
use of CCDD.CcddClassesDataTable.CCDDException in project CCDD by nasa.
the class CcddWebDataAccessHandler method getTableData.
/**
********************************************************************************************
* Get the data for the specified data table, or for all data tables if no table name is
* provided
*
* @param tableName
* table name and path in the format rootTable[,dataType1.variable1[,...]]. Blank to
* return the data for all tables
*
* @param getDescription
* true to get the table description when loading the table data
*
* @param separators
* string array containing the variable path separator character(s), show/hide data
* types flag ('true' or 'false'), and data type/variable name separator
* character(s)
*
* @return JSON encoded string containing the specified table cell data; null if a table name
* is specified and the table doesn't exist or if no data tables exist in the project
* database, or blank if the specified table has no data or none of the tables have
* data. Empty cells are included
*
* @throws CCDDException
* If an error occurs while parsing the table data
********************************************************************************************
*/
@SuppressWarnings("unchecked")
private String getTableData(String tableName, boolean getDescription, String[] separators) throws CCDDException {
String response = null;
// Check if no table name is specified
if (tableName.isEmpty()) {
// Get the list of all data table names
List<String> tableNameList = getTableList();
// Check that at least one table exists in the project database
if (!tableNameList.isEmpty()) {
JSONArray responseJA = new JSONArray();
JSONParser parser = new JSONParser();
response = "";
// Step through each table name
for (String name : tableNameList) {
try {
// Get the data for this table as a JSON string, then format it as a JSON
// object so that is can be added to the response array. This is needed to
// get the brackets and commas in the JSON formatted string correct
responseJA.add(parser.parse(getTableData(name, true, separators)));
} catch (ParseException pe) {
throw new CCDDException("error parsing table data");
}
}
// Convert the response array to a JSON string
response = responseJA.toString();
}
} else // A table name is provided
{
// Get the table data
JSONObject tableNameAndData = jsonHandler.getTableData(tableName, getDescription, isReplaceMacro, isIncludePath, variableHandler, separators, new JSONObject());
// Check if the table data loaded successfully
if (tableNameAndData != null) {
// Add the table name. If the table has no data then the table data shows empty
tableNameAndData.put(JSONTags.TABLE_NAME.getTag(), tableName);
response = tableNameAndData.toString();
}
}
return response;
}
use of CCDD.CcddClassesDataTable.CCDDException in project CCDD by nasa.
the class CcddWebDataAccessHandler method getGroupFields.
/**
********************************************************************************************
* Get the data field information for the specified group or application, or for all
* groups/applications if no group name is provided
*
* @param groupName
* group name. If blank then every data table's data fields are returned
*
* @param applicationOnly
* true if only groups that represent applications should be processed
*
* @param includeNameTag
* true to include the group name item and data field tag
*
* @param groupHandler
* group handler
*
* @return JSON encoded string containing the specified group's data fields; null if the group
* doesn't exist or if the project database contains no groups, or blank if the group
* contains no data fields
*
* @throws CCDDException
* If an error occurs while parsing the group data fields
********************************************************************************************
*/
@SuppressWarnings("unchecked")
private String getGroupFields(String groupName, boolean applicationOnly, boolean includeNameTag, CcddGroupHandler groupHandler) throws CCDDException {
String response = null;
// Check if no group name is provided (i.e., get the fields for all groups/applications)
if (groupName.isEmpty()) {
// Get an array containing all group/application names
String[] groupNames = groupHandler.getGroupNames(applicationOnly);
// Check if any groups/applications exist
if (groupNames.length != 0) {
JSONArray responseJA = new JSONArray();
JSONParser parser = new JSONParser();
// Step through each group/application name
for (String name : groupNames) {
try {
// Get the fields for this group as a JSON string, then format it as a JSON
// object so that is can be added to the response array. This is needed to
// get the brackets and commas in the JSON formatted string correct
responseJA.add(parser.parse(getGroupFields(name, applicationOnly, true, groupHandler)));
} catch (ParseException pe) {
throw new CCDDException("error parsing " + (applicationOnly ? "application" : "group") + " data fields");
}
}
// Add the table fields to the response
response = responseJA.toString();
}
} else // A group name is provided
{
// Get the group information for the specified group
GroupInformation groupInfo = groupHandler.getGroupInformationByName(groupName);
// application is requested and this group represents an application
if (groupInfo != null && (!applicationOnly || groupInfo.isApplication())) {
JSONArray groupFieldsJA = new JSONArray();
// Build the field information list for this group
fieldHandler.buildFieldInformation(CcddFieldHandler.getFieldGroupName(groupName));
// Check if the group has any fields
if (!fieldHandler.getFieldInformation().isEmpty()) {
// Get the group data fields (extract the data field array from the table field
// tag)
JSONObject fieldsJO = jsonHandler.getDataFields(CcddFieldHandler.getFieldGroupName(groupName), JSONTags.GROUP_FIELD.getTag(), new JSONObject());
groupFieldsJA = (JSONArray) fieldsJO.get(JSONTags.GROUP_FIELD.getTag());
}
// Check if the name tag is to be included
if (includeNameTag) {
// Add the group name and group data fields to the output
JSONObject groupNameAndFields = new JSONObject();
groupNameAndFields.put((applicationOnly ? JSONTags.APPLICATION_NAME.getTag() : JSONTags.GROUP_NAME.getTag()), groupName);
groupNameAndFields.put((applicationOnly ? JSONTags.APPLICATION_FIELD.getTag() : JSONTags.GROUP_FIELD.getTag()), groupFieldsJA);
response = groupNameAndFields.toString();
} else // Don't include the name and field tags
{
// Add the data fields to the output
response = groupFieldsJA.toString();
}
}
}
return response;
}
use of CCDD.CcddClassesDataTable.CCDDException in project CCDD by nasa.
the class CcddWebDataAccessHandler method getGroupInformation.
/**
********************************************************************************************
* Get the description, associated table(s), and data fields for the specified group or
* application
*
* @param groupName
* group name. If blank then every data table's data fields are returned
*
* @param applicationOnly
* true if only groups that represent applications should be processed
*
* @param groupHandler
* group handler
*
* @return JSON encoded string containing the specified group/application information; null if
* a group name is specified and the group/application doesn't exist or if no
* groups/applications exist in the project database
*
* @throws CCDDException
* If an error occurs while parsing the group information
********************************************************************************************
*/
@SuppressWarnings("unchecked")
private String getGroupInformation(String groupName, boolean applicationOnly, CcddGroupHandler groupHandler) throws CCDDException {
JSONArray responseJA = new JSONArray();
JSONParser parser = new JSONParser();
String response = null;
String groupType;
String nameTag;
String descriptionTag;
String dataFieldTag;
String tableTag;
// Check if only groups that represent applications are to be processed
if (applicationOnly) {
groupType = "application";
nameTag = JSONTags.APPLICATION_NAME.getTag();
descriptionTag = JSONTags.APPLICATION_DESCRIPTION.getTag();
dataFieldTag = JSONTags.APPLICATION_FIELD.getTag();
tableTag = JSONTags.APPLICATION_TABLE.getTag();
} else // Process groups of any type
{
groupType = "group";
nameTag = JSONTags.GROUP_NAME.getTag();
descriptionTag = JSONTags.GROUP_DESCRIPTION.getTag();
dataFieldTag = JSONTags.GROUP_FIELD.getTag();
tableTag = JSONTags.GROUP_TABLE.getTag();
}
// Check if no group name is provided (i.e., get the fields for all groups/applications)
if (groupName.isEmpty()) {
// Get an array containing all group/application names
String[] groupNames = groupHandler.getGroupNames(applicationOnly);
// Check if any groups/applications exist
if (groupNames.length != 0) {
response = "";
// Step though each group/application
for (String name : groupNames) {
try {
// Get the fields for this group as a JSON string, then format it as a JSON
// object so that is can be added to the response array. This is needed to
// get the brackets and commas in the JSON formatted string correct
responseJA.add(parser.parse(getGroupInformation(name, applicationOnly, groupHandler)));
} catch (ParseException pe) {
throw new CCDDException("error parsing " + groupType + " information");
}
}
// Convert the response array to a JSON string
response = responseJA.toString();
}
} else // A group name is provided
{
// Get the group information for the specified group
GroupInformation groupInfo = groupHandler.getGroupInformationByName(groupName);
// application is requested and this group represents an application
if (groupInfo != null && (!applicationOnly || groupInfo.isApplication())) {
try {
// Store the group's name, description, tables, and data fields
JSONObject groupInfoJO = new JSONObject();
groupInfoJO.put(nameTag, groupName);
groupInfoJO.put(descriptionTag, getGroupDescription(groupName, applicationOnly, false, groupHandler));
groupInfoJO.put(tableTag, parser.parse(getGroupTables(groupName, applicationOnly, false, groupHandler)));
groupInfoJO.put(dataFieldTag, parser.parse(getGroupFields(groupName, applicationOnly, false, groupHandler)));
// Convert the response object to a JSON string
response = groupInfoJO.toString();
} catch (ParseException pe) {
pe.printStackTrace();
throw new CCDDException("error parsing " + groupType + " information");
}
}
}
return response;
}
Aggregations