Search in sources :

Example 66 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class SecurityUtils method tokenAndExpiration.

public static String[] tokenAndExpiration(Api api, JsonObject entity, Date now) throws ApiServiceExecutionException {
    String thing = salt(api, entity);
    JsonObject auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), Schemes.Token), Api.Spec.Security.Auth);
    if (auth == null) {
        auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), Schemes.Cookie), Api.Spec.Security.Auth);
    }
    String secretsName = Json.getString(auth, ApiSpace.Spec.secrets.class.getSimpleName(), ApiSpace.Secrets.Default);
    // encrypt
    JsonObject secrets;
    try {
        secrets = api.space().getSecrets(secretsName);
    } catch (ApiManagementException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (secrets == null || !secrets.containsKey(ApiSpace.Spec.secrets.Key)) {
        throw new ApiServiceExecutionException("space secrets '" + secretsName + "' not found").status(ApiResponse.SERVICE_UNAVAILABLE);
    }
    Crypto.Algorithm alg = null;
    try {
        alg = Crypto.Algorithm.valueOf(Json.getString(secrets, ApiSpace.Spec.secrets.Algorithm, Crypto.Algorithm.AES.name()).toUpperCase());
    } catch (Exception ex) {
        alg = Crypto.Algorithm.AES;
    }
    long expiresOn = now.getTime() + Json.getLong(secrets, ApiSpace.Spec.secrets.Age, 60) * 60 * 1000;
    String toEncrypt = expiresOn + Lang.SPACE + thing;
    try {
        return new String[] { new String(Lang.encodeHex(Crypto.encrypt(toEncrypt.getBytes(), Json.getString(secrets, ApiSpace.Spec.secrets.Key), alg))), Lang.toUTC(new Date(expiresOn)) };
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
}
Also used : Crypto(com.bluenimble.platform.Crypto) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) Date(java.util.Date)

Example 67 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class OrientDatabase method describe.

@Override
public JsonObject describe() {
    JsonObject describe = new JsonObject();
    describe.set(Describe.Size, db.getSize());
    Collection<OClass> entities = db.getMetadata().getSchema().getClasses();
    if (entities == null || entities.isEmpty()) {
        return describe;
    }
    JsonArray aEntities = new JsonArray();
    describe.set(Describe.Entities, aEntities);
    for (OClass entity : entities) {
        if (SystemEntities.contains(entity.getName())) {
            continue;
        }
        JsonObject oEntity = new JsonObject();
        oEntity.set(Describe.Name, entity.getName());
        oEntity.set(Describe.Count, entity.count());
        aEntities.add(oEntity);
    }
    return describe;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 68 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class Create method main.

public static void main(String[] args) throws DatabaseException {
    Database db = new DatabaseServer().get();
    DatabaseObject employee = db.create("Employees");
    JsonArray names = new JsonArray();
    names.add(new JsonObject().set("number", "4098776623").set("weight", 40));
    employee.set("name", "Simo");
    employee.set("age", 34);
    employee.set("active", true);
    employee.set("salary", 43.98);
    employee.set("names", names);
    employee.save();
    System.out.println(employee.toJson(null));
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 69 with JsonObject

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-1");
    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));
}
Also used : Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 70 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class ResourceTemplateLoader method sourceAt.

@Override
public TemplateSource sourceAt(String name) throws IOException {
    JsonObject runtime = api.getRuntime();
    String templatesPath = Json.getString(runtime, Templates, DefaultTemplatesPath);
    if (templatesPath.startsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(1);
    }
    if (templatesPath.endsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(0, templatesPath.length() - 1);
    }
    String templateExt = Json.getString(runtime, TemplateExtension, DotHtml);
    final String fileName = templatesPath + Lang.SLASH + name + templateExt;
    ApiResource res;
    try {
        res = api.getResourcesManager().get(Lang.split(fileName, Lang.SLASH));
    } catch (ApiResourcesManagerException e) {
        throw new IOException(e.getMessage(), e);
    }
    final ApiResource resource = res;
    return new TemplateSource() {

        @Override
        public long lastModified() {
            return resource.timestamp().getTime();
        }

        @Override
        public String filename() {
            return fileName;
        }

        @Override
        public String content() throws IOException {
            InputStream is = null;
            try {
                is = resource.toInput();
                return IOUtils.toString(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    };
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) TemplateSource(com.github.jknack.handlebars.io.TemplateSource) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)230 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)40 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonArray (com.bluenimble.platform.json.JsonArray)37 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)34 Database (com.bluenimble.platform.db.Database)29 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 File (java.io.File)25 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)23 Map (java.util.Map)22 IOException (java.io.IOException)20 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)17 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 InputStream (java.io.InputStream)14 Date (java.util.Date)14 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)13 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)12 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)11 HashMap (java.util.HashMap)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)9