use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class CreateOne2One method main.
public static void main(String[] args) throws DatabaseException {
Database db = new DatabaseServer().get();
// create driver
DatabaseObject driver = db.create("Drivers");
driver.set("name", "One2One-New-2");
driver.set("info", new JsonObject().set("x", "40987").set("y", 76623));
driver.set("salary", 48.50);
// create car
DatabaseObject car = db.create("Cars");
car.set("model", "Honda");
car.set("year", "2040");
driver.set("car", car);
driver.save();
System.out.println(driver.toJson(null));
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class HttpHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("keys name required. ex. use keys your-app-prod");
}
String varOrUrl = args[0];
final Map<String, String> options = new HashMap<String, String>();
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
options.put(String.valueOf(i), args[i]);
}
}
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
JsonObject spec = null;
Object oSpec = vars.get(varOrUrl);
if (oSpec == null) {
spec = (JsonObject) new JsonObject().set(Spec.request.class.getSimpleName(), new JsonObject().set(Spec.request.Service, varOrUrl));
} else {
spec = ((JsonObject) oSpec).duplicate();
}
JsonObject request = Json.getObject(spec, Spec.request.class.getSimpleName());
if (request != null) {
if (!request.containsKey(Spec.request.Sign)) {
request.set(Spec.request.Sign, false);
}
JsonObject headers = Json.getObject(request, Spec.request.Headers);
if (headers == null) {
headers = new JsonObject();
request.set(Spec.request.Headers, headers);
}
if (!headers.containsKey(ApiHeaders.Accept)) {
headers.set(ApiHeaders.Accept, "*/*");
}
}
return RemoteUtils.processRequest(tool, spec, options);
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class LoadFeatureHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("feature file path required. ex. load feature aTempate.json");
}
String path = args[0];
File templateFile = new File(path + ".json");
String name = templateFile.getName();
if (!templateFile.exists() || !templateFile.isFile()) {
throw new CommandExecutionException("invalid file path > " + path);
}
String key = name.substring(0, name.lastIndexOf(Lang.DOT));
JsonObject oFeature;
try {
oFeature = Json.load(templateFile);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
}
String provider = key;
String[] variables = null;
int indexOfDash = key.indexOf(Lang.DASH);
if (indexOfDash > 0) {
provider = key.substring(0, indexOfDash);
variables = Lang.split(key.substring(indexOfDash + 1), "__", true);
}
oFeature.set("name", "default");
oFeature.set("provider", provider);
final Map<String, String> mVariables = new HashMap<String, String>();
if (variables != null && variables.length > 0) {
for (String v : variables) {
String vKey = v.substring(0, v.indexOf(Lang.UNDERSCORE));
String vValue = v.substring(v.indexOf(Lang.UNDERSCORE) + 1);
mVariables.put(vKey, vValue);
}
}
if (mVariables != null) {
oFeature = (JsonObject) Json.resolve(oFeature, ExpressionCompiler, new VariableResolver() {
private static final long serialVersionUID = 1L;
@Override
public Object resolve(String namespace, String... property) {
return mVariables.get(Lang.join(property, Lang.DOT));
}
});
}
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.currentContext().get(ToolContext.VARS);
vars.put(key, oFeature);
return new DefaultCommandResult(CommandResult.OK, oFeature);
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class SecureApiHandler method execute.
@Override
public CommandResult execute(Tool tool, String... args) throws CommandExecutionException {
if (args == null || args.length < 1) {
throw new CommandExecutionException("Command syntax:\nsecure api [api namespace] [+ seperated list of shemes, default to 'cookie+token'] [+ seperated list of methods, default to 'up']\nTry something like secure api token+cookie up+fb");
}
String api = args[0];
String apiPath = Json.getString(Json.getObject(BlueNimble.Config, CliSpec.Config.Apis), api);
if (Lang.isNullOrEmpty(apiPath)) {
throw new CommandExecutionException("api path not found for '" + api + "'");
}
File apiFolder = new File(BlueNimble.Workspace, apiPath);
if (!apiFolder.exists() || !apiFolder.isDirectory()) {
throw new CommandExecutionException("invalid api folder '" + apiPath + "'");
}
// schemes
String[] schemes = getSchemes(args);
for (String s : schemes) {
if (!Schemes.contains(s)) {
throw new CommandExecutionException("unsupported authentication scheme '" + s + "'");
}
}
boolean oauth = false;
// methods
String[] methods = getMethods(args);
if (methods != null) {
for (String m : methods) {
if (!Methods.contains(m)) {
throw new CommandExecutionException("unsupported authentication method '" + m + "'");
} else if (!"up".equals(m)) {
oauth = true;
}
}
}
// load tplApiSpec
JsonObject oTplApi = null;
try {
oTplApi = Json.load(new File(BlueNimble.Home, "templates/security/api.json"));
} catch (Exception ex) {
throw new CommandExecutionException("can't read api template security spec file templates/security/api.json");
}
JsonObject tplSchemes = (JsonObject) Json.find(oTplApi, "security", "schemes");
// read api spec
JsonObject oApi = SpecUtils.read(apiFolder);
// add security schemes
JsonObject oSecurity = Json.getObject(oApi, Api.Spec.Security.class.getSimpleName().toLowerCase());
if (oSecurity == null) {
oSecurity = new JsonObject();
oApi.set(Api.Spec.Security.class.getSimpleName().toLowerCase(), oSecurity);
}
JsonObject oSchemes = Json.getObject(oSecurity, Api.Spec.Security.Schemes);
if (oSchemes == null) {
oSchemes = new JsonObject();
oSecurity.set(Api.Spec.Security.Schemes, oSchemes);
}
for (String s : schemes) {
if (!oSchemes.containsKey(s)) {
oSchemes.set(s, tplSchemes.get(s));
tool.printer().info("security scheme '" + s + "' added to api '" + api + "'");
}
}
SpecUtils.write(apiFolder, oApi);
// add services
if (Lang.existsIn("up", methods)) {
try {
addService(tool, api, apiFolder, "signup");
} catch (Exception ex) {
throw new CommandExecutionException("An error occured when generating code for Signup service. Cause: " + ex.getMessage(), ex);
}
// copy email template
File emailsTplsFolder = new File(apiFolder, "resources/templates/emails");
if (!emailsTplsFolder.exists()) {
emailsTplsFolder.mkdirs();
}
File apiSignupTplFile = new File(emailsTplsFolder, "signup.html");
if (!apiSignupTplFile.exists()) {
File signupTplFile = new File(BlueNimble.Home, "templates/security/templates/emails/signup.html");
Map<String, String> tokens = new HashMap<String, String>();
tokens.put("api", api);
tokens.put("Api", api.substring(0, 1).toUpperCase() + api.substring(1));
CodeGenUtils.writeFile(signupTplFile, apiSignupTplFile, tokens, null);
tool.printer().important("An activation email html file was created! 'templates/emails/" + apiSignupTplFile.getName() + "' It's used by the Signup service" + "\nMake sure that the email feature is added to your space in order to send emails.\nUse command 'add feature' to add an smtp server config");
}
try {
addService(tool, api, apiFolder, "activate");
} catch (Exception ex) {
throw new CommandExecutionException("An error occured when generating code for Activate service. Cause: " + ex.getMessage(), ex);
}
try {
addService(tool, api, apiFolder, "login");
} catch (Exception ex) {
throw new CommandExecutionException("An error occured when generating code for Login service. Cause: " + ex.getMessage(), ex);
}
try {
addService(tool, api, apiFolder, "changePassword");
} catch (Exception ex) {
throw new CommandExecutionException("An error occured when generating code for ChangePassword service. Cause: " + ex.getMessage(), ex);
}
}
if (oauth) {
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
String specLang = (String) vars.get(BlueNimble.DefaultVars.SpecLanguage);
if (Lang.isNullOrEmpty(specLang)) {
specLang = BlueNimble.SpecLangs.Json;
}
try {
addService(tool, api, apiFolder, "oAuth");
tool.printer().important("Make sure that your clientId and sercretId are set in your oauth providers.\nSee service spec file resources/services/security/OAuth." + specLang);
} catch (Exception ex) {
throw new CommandExecutionException("An error occured when generating code for oAuth service. Cause: " + ex.getMessage(), ex);
}
}
return new DefaultCommandResult(CommandResult.OK, null);
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class BuildUtils method generate.
public static JsonArray generate(File apiFolder) throws Exception {
JsonArray dsList = new JsonArray();
File dataSourcesFolder = new File(apiFolder, Resources + Lang.SLASH + DataSources);
if (!dataSourcesFolder.exists() || !dataSourcesFolder.isDirectory()) {
return null;
}
File[] dataSources = dataSourcesFolder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
if (dataSources == null || dataSources.length == 0) {
return null;
}
File javaSrc = new File(apiFolder, JavaSource);
if (!javaSrc.exists()) {
javaSrc.mkdirs();
}
Persistence persistence = new Persistence();
// generate sources
for (File dsf : dataSources) {
DataSource ds = new DataSource(dsf.getName());
dsList.add(ds.getName());
persistence.addDataSource(ds);
loadEntities(ds, dsf, dsf, javaSrc);
}
// compile sources
File javaBin = new File(apiFolder, JavaBinary);
if (!javaBin.exists()) {
javaBin.mkdirs();
}
new SourceCompiler(javaSrc, javaBin).compile();
// create META-INF/persistence.xml
File metaInf = new File(new File(apiFolder, JavaBinary), MetaInf);
if (!metaInf.exists()) {
metaInf.mkdirs();
}
File pXml = new File(metaInf, PersistenceXml);
FileWriter writer = null;
try {
writer = new FileWriter(pXml);
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
writer.write("<persistence xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
writer.write(" xsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\"\n");
writer.write(" version=\"2.0\" xmlns=\"http://java.sun.com/xml/ns/persistence\">\n");
// add persistence units
for (DataSource ds : persistence.getDataSources()) {
writer.write("\t<persistence-unit name=\"" + ds.getName() + "\" transaction-type=\"RESOURCE_LOCAL\">\n");
writer.write("\t\t<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>\n");
// add classes
for (DSEntity entity : ds.getEntities()) {
writer.write("\t\t<class>" + (entity.getPackage() == null ? Lang.BLANK : Lang.replace(entity.getPackage(), Lang.SLASH, Lang.DOT) + Lang.DOT) + entity.getName() + "</class>\n");
}
// add properties
File properties = new File(dataSourcesFolder, ds.getName() + Lang.SLASH + Properties);
if (properties.exists()) {
JsonObject oProperties = Json.load(properties);
if (!Json.isNullOrEmpty(oProperties)) {
writer.write("\t\t<properties>\n");
Iterator<String> keys = oProperties.keys();
while (keys.hasNext()) {
String key = keys.next();
writer.write("\t\t\t<property name=\"" + key + "\" value=\"" + oProperties.get(key) + "\" />\n");
}
writer.write("\t\t</properties>\n");
}
}
writer.write("\t</persistence-unit>\n");
}
writer.write("</persistence>");
} finally {
IOUtils.closeQuietly(writer);
}
// create jar file
File apiLibs = new File(apiFolder, Lib);
if (!apiLibs.exists()) {
apiLibs.mkdirs();
}
ArchiveUtils.compress(javaBin, new File(apiLibs, DataSources + Lang.UUID(6) + JarExt), true, new ArchiveUtils.CompressVisitor() {
@Override
public boolean onAdd(File file) {
if (file.getName().startsWith(Lang.DOT)) {
return false;
}
return true;
}
});
return dsList;
}
Aggregations