use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class FileSystemApiServer method create.
private ApiSpace create(JsonObject oSpace, boolean save) throws ApiManagementException {
String spaceNs = Json.getString(oSpace, Spec.Namespace);
if (Lang.isNullOrEmpty(spaceNs)) {
throw new ApiManagementException("space namespace not found");
}
if (!InstallUtils.isValidSpaceNs(spaceNs)) {
throw new ApiManagementException("invalid space namespace '" + spaceNs + "'");
}
File spacesHome = new File(runtimeHome, ConfigKeys.Folders.Spaces);
File spaceHome = new File(spacesHome, spaceNs);
if (!spaceHome.exists()) {
spaceHome.mkdir();
}
ApiSpace space = null;
try {
space = new ApiSpaceImpl(this, oSpace, spaceHome);
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
addSpace(space);
if (save) {
try {
Json.store(oSpace, new File(spaceHome, ConfigKeys.Descriptor.Space));
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
}
// notify space creation
try {
if (space.isStarted()) {
getPluginsRegistry().onEvent(Event.Create, space);
}
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
return space;
}
use of com.bluenimble.platform.api.ApiManagementException 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);
}
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ApiSpaceImpl method alter.
@Override
public void alter(String spaceNs, JsonObject change) throws ApiManagementException {
try {
ApiSpaceImpl space = (ApiSpaceImpl) space(spaceNs);
JsonObject descriptor = space.getDescriptor();
descriptor.putAll(change);
} catch (Exception e) {
throw new ApiManagementException(e.getMessage(), e);
}
// save
save();
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ApiSpaceImpl method start.
@Override
public void start(String apiNs) throws ApiManagementException {
ApiImpl api = (ApiImpl) api(apiNs);
if (api == null) {
throw new ApiManagementException("api " + apiNs + " not found");
}
if (ApiStatus.Failed.equals(api.status())) {
throw new ApiManagementException("can't start api " + apiNs + ". Status=" + api.status());
}
if (ApiStatus.Running.equals(api.status())) {
return;
}
if (ApiStatus.Paused.equals(api.status())) {
api.setStatus(ApiStatus.Running, true);
return;
}
// start api
api.start(false);
// notify plugins
try {
server.getPluginsRegistry().onEvent(Event.Start, api);
} catch (PluginRegistryException e) {
throw new ApiManagementException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.api.ApiManagementException in project serverless by bluenimble.
the class ApiSpaceImpl method stop.
@Override
public void stop(String apiNs) throws ApiManagementException {
ApiImpl api = (ApiImpl) api(apiNs);
if (api == null) {
throw new ApiManagementException("api " + apiNs + " not found");
}
if (!ApiStatus.Running.equals(api.status())) {
throw new ApiManagementException("can't stop api " + apiNs + ". Status=" + api.status());
}
// notify plugins
try {
server.getPluginsRegistry().onEvent(Event.Stop, api);
} catch (PluginRegistryException e) {
throw new ApiManagementException(e.getMessage(), e);
}
// stop api
api.stop(true);
}
Aggregations