@Override public void updateProperties(RequestContext ctx, ResourceState state, Responder responder) throws Exception { // TODO: put this code in a common location since its also used when creating Object nameProperty = state.getProperty(NAME); if (nameProperty != null) { name = nameProperty.toString(); } else { name = null; } Object descriptionProperty = state.getProperty(DESCRIPTION); if (descriptionProperty != null) { description = descriptionProperty.toString(); } else { description = null; } Object enabledProperty = state.getProperty(ENABELD); if (enabledProperty != null && enabledProperty instanceof Boolean) { enabled = (Boolean) enabledProperty; } else { enabled = false; } libraries.clear(); Object libraries = state.getProperty(LIBRARIES); if (libraries != null && libraries instanceof List) { for (Object entry : (List) libraries) { if (entry instanceof String) { this.libraries.add((String) entry); } } } else if (libraries != null) { throw new InvalidPropertyTypeException(LIBRARIES, List.class); } Object targetProperty = state.getProperty(TARGET_PATH); if (targetProperty != null && targetProperty instanceof String) { target = (String) targetProperty; } else { throw new InvalidPropertyTypeException(TARGET_PATH, String.class); } Object priorityProperty = state.getProperty(PRIORITY); if (priorityProperty != null && priorityProperty instanceof Integer) { priority = (Integer) priorityProperty; } else if (priorityProperty != null) { throw new InvalidPropertyTypeException(PRIORITY, Integer.class); } parent.getResourceInterceptorManager().addResource(this); parent.writeMetadataFile(this.id(), ConversionUtils.convert(state)); responder.resourceUpdated(this); }
@Override public void createMember(RequestContext ctx, ResourceState state, Responder responder) throws Exception { try { ResourceScript resourceScript = new ResourceScript(this, state); scripts.add(resourceScript.getScript()); // Write to the file system ObjectNode objectNode = ConversionUtils.convert(state); writeMetadataFile(resourceScript.id(), objectNode); responder.resourceCreated(resourceScript); } catch (InvalidPropertyTypeException e) { responder.invalidRequest(e.getMessage()); } }
// Read the configuration files from the filesystem [if any] and configure the resource based on // them public void start() throws IOException { // get a reference to where the resource triggred scripts should be held String resourceBasedDir = parent.getScriptDirectory() + File.separator + "/" + RESOURCE_DIRNAME; resourceDirectory = new File(resourceBasedDir); // create the directory if it doesn't already exist if (!resourceDirectory.exists()) { resourceDirectory.mkdirs(); } vertx .fileSystem() .readDir( resourceDirectory.getPath(), (result) -> { if (!result.failed()) { for (String fileName : result.result()) { File scriptDirectory = new File(fileName); String resourceID = scriptDirectory.getName(); try { // read the file containing the metadata for this script File metadataFile = new File( scriptDirectory.getPath() + File.separator + "/" + METADATA_FILENAME); if (metadataFile.exists()) { ObjectNode objectNode = (ObjectNode) ObjectMapperFactory.create().readTree(metadataFile); ResourceState state = ConversionUtils.convert(objectNode); state.id(resourceID); ResourceScript resourceScript = new ResourceScript(this, state); // add this resource to the list of scripts available. this.scripts.add(resourceScript.getScript()); // read the file containing the source for this script [if any] File sourceFile = new File( scriptDirectory.getPath() + File.separator + "/" + SOURCE_FILENAME); if (sourceFile.exists()) { vertx .fileSystem() .readFile( sourceFile.getPath(), (buffer) -> { resourceScript .getScript() .setScriptBuffer(buffer.result().getByteBuf()); }); } } } catch (Exception e) { logger().error("Error reading resource script files.", e); } } } else { logger().error("Error reading resource script files.", result.cause()); } }); }