use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class JavascriptEngine method write.
@Override
public void write(ApiConsumer consumer, ApiRequest request, final ApiResponse response, ApiOutput output, final ApiResource template, final JsonObject mediaSpec) throws TemplateEngineException {
try {
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, null);
MapScriptContext vars = new MapScriptContext();
vars.set(Json.getString(features, I18n, I18n), api.i18n(request.getLang()));
vars.set(Json.getString(features, Request, Request), request.toJson());
if (consumer != null) {
vars.set(Json.getString(features, Consumer, Consumer), consumer.toJson());
}
JsonObject jOutput = null;
if (output != null) {
jOutput = output.data();
}
if (output != null) {
vars.set(Json.getString(features, Output, Output), jOutput);
vars.set(Json.getString(features, Meta, Meta), output.meta());
}
vars.set(Json.getString(features, Error, Error), response.getError());
vars.set(Json.getString(features, Response, Response), response);
JsonObject result;
Object oResult = engine.eval(ScriptingEngine.Supported.Javascript, api, template, vars);
if (oResult != null) {
if (oResult instanceof JsonObject) {
result = (JsonObject) oResult;
} else if (oResult instanceof JsonArray) {
result = new JsonObject();
result.set(ApiOutput.Defaults.Items, oResult);
} else {
result = new JsonObject();
result.set(Output, oResult);
}
} else {
result = jOutput;
}
if (response.getError() != null) {
result = response.getError();
}
boolean doNotWrite = Json.getBoolean(jOutput, DoNotWrite, false);
if (!doNotWrite) {
doNotWrite = Json.getBoolean(mediaSpec, DoNotWrite, false);
}
if (result != null && !doNotWrite) {
response.flushHeaders();
result.write(new AbstractEmitter() {
@Override
public JsonEmitter write(String chunk) {
try {
response.write(chunk);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return this;
}
});
}
} catch (Exception e) {
throw new TemplateEngineException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class JsonWriter method write.
@Override
public void write(ApiOutput output, ApiResponse response) throws IOException {
if (output == null) {
response.write(Lang.EMTPY_OBJECT);
return;
}
JsonObject json = output.data();
if (json == null) {
response.write(Lang.EMTPY_OBJECT);
return;
}
json.write(new AbstractEmitter() {
@Override
public JsonEmitter write(String chunk) {
try {
response.write(chunk);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return this;
}
});
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DefaultVariableResolver method resolve.
@Override
public String resolve(String ns, String name) {
if (Lang.isNullOrEmpty(ns) || Namespaces.Request.equals(ns)) {
JsonObject rJson = request.toJson();
Object v = rJson.find(name, Lang.DOT);
if (v != null) {
return String.valueOf(v);
}
return (String) request.get(name, Scope.Parameter, Scope.Header);
} else if (Namespaces.Output.equals(ns) && output != null) {
Object v = output.find(name, Lang.DOT);
if (v == null) {
return null;
}
return String.valueOf(v);
} else if (Namespaces.Extra.equals(ns) && extra != null) {
Object v = extra.find(name, Lang.DOT);
if (v == null) {
return null;
}
return String.valueOf(v);
}
return null;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class MediaRoutingUtils method pickMedia.
public static JsonObject pickMedia(Api api, ApiService service, String contentType) {
JsonObject mediaDef = null;
JsonObject mediaSet = service == null ? null : service.getMedia();
if (mediaSet != null && !mediaSet.isEmpty()) {
mediaDef = Json.getObject(mediaSet, contentType);
}
if (mediaDef == null) {
mediaDef = Json.getObject(mediaSet, Lang.STAR);
}
if (mediaDef == null) {
mediaDef = Json.getObject(api.getMedia(), contentType);
}
if (mediaDef == null) {
mediaDef = Json.getObject(api.getMedia(), Lang.STAR);
}
return mediaDef;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class MediaRoutingUtils method processMedia.
public static JsonObject processMedia(final ApiRequest request, ApiResponse response, Lang.VariableResolver vr, JsonObject mediaDef, Tracer tracer) {
if (mediaDef == null || (!mediaDef.containsKey(Success) && !mediaDef.containsKey(Error))) {
return null;
}
String selectProcessor = (String) request.get(ApiRequest.MediaSelector);
if (Lang.isNullOrEmpty(selectProcessor)) {
selectProcessor = Lang.STAR;
}
JsonObject mediaSelections = null;
if (response.getError() == null) {
mediaSelections = Json.getObject(mediaDef, Success);
} else {
mediaSelections = Json.getObject(mediaDef, Error);
if (mediaSelections == null) {
mediaSelections = Json.getObject(mediaDef, Success);
}
}
JsonObject media = Json.getObject(mediaSelections, selectProcessor);
if (media != null) {
int status = Json.getInteger(media, Status, 0);
if (status > 0) {
response.setStatus(new Status(status, Lang.BLANK));
}
JsonObject headers = Json.getObject(media, Headers);
if (headers != null && !headers.isEmpty()) {
Iterator<String> names = headers.keys();
while (names.hasNext()) {
String name = names.next();
Object hv = headers.get(name);
if (hv == null) {
hv = Lang.BLANK;
}
if (hv instanceof JsonArray) {
JsonArray arr = (JsonArray) hv;
if (arr.isEmpty()) {
continue;
}
List<String> values = new ArrayList<String>();
for (int i = 0; i < arr.count(); i++) {
values.add(Lang.resolve(String.valueOf(arr.get(i)), vr));
}
response.set(name, values);
} else {
hv = Lang.resolve(hv.toString(), vr);
if (XLocation.toLowerCase().equals(name.toLowerCase())) {
if (status <= 0) {
response.setStatus(ApiResponse.MOVED_PERMANENTLY);
}
response.set(ApiHeaders.Location, hv);
}
if (ApiHeaders.Location.toLowerCase().equals(name.toLowerCase())) {
if (status <= 0) {
response.setStatus(ApiResponse.MOVED_PERMANENTLY);
}
response.set(ApiHeaders.Location, ApiUtils.apiEndpoint(request) + hv);
} else {
response.set(name, hv);
}
}
}
}
}
return media;
}
Aggregations