use of com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner in project serverless by bluenimble.
the class BlueNimbleHttpRequestVisitor method visit.
@Override
public void visit(HttpRequest request, HttpURLConnection connection) throws HttpRequestWriteException {
// sign request
List<HttpHeader> headers = request.getHeaders();
if (headers == null) {
headers = new ArrayList<HttpHeader>();
request.setHeaders(headers);
}
AccessSecretKeysBasedHttpRequestSigner signer = new AccessSecretKeysBasedHttpRequestSigner("m>h>p>d>k>t", "Bearer", accessKey, secretKey);
String timestamp = Lang.utc();
headers.add(new HttpHeaderImpl(ApiHeaders.Timestamp, timestamp));
signer.getData().put('t', timestamp);
try {
signer.sign(request);
} catch (HttpRequestSignerException e) {
throw new HttpRequestWriteException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner in project serverless by bluenimble.
the class RemoteUtils method request.
@SuppressWarnings("unchecked")
private static HttpRequest request(JsonObject oKeys, JsonObject spec, Tool tool, JsonObject config, Map<String, String> options, List<Object> streams) throws Exception {
Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
String service = (String) eval(Json.getString(spec, Spec.request.Service), vars, config, options, oKeys);
HttpRequest request = HttpRequests.get(Json.getString(spec, Spec.request.Method, HttpMethods.GET).toUpperCase()).getConstructor(new Class[] { HttpEndpoint.class }).newInstance(HttpUtils.createEndpoint(new URI(service)));
String contentType = Json.getString(spec, Spec.request.ContentType);
// set headers
List<HttpHeader> headers = request.getHeaders();
if (headers == null) {
headers = new ArrayList<HttpHeader>();
request.setHeaders(headers);
}
JsonObject oHeaders = Json.getObject(spec, Spec.request.Headers);
// add default Accept header application/json
if (oHeaders == null || !oHeaders.containsKey(ApiHeaders.Accept)) {
String accept = (String) vars.get(DefaultVars.RemoteHeadersAccept);
if (Lang.isNullOrEmpty(accept)) {
accept = ApiContentTypes.Json;
}
headers.add(new HttpHeaderImpl(ApiHeaders.Accept, accept));
}
if (oHeaders != null && !oHeaders.isEmpty()) {
Iterator<String> hNames = oHeaders.keys();
while (hNames.hasNext()) {
String hName = hNames.next();
headers.add(new HttpHeaderImpl(hName, String.valueOf(eval(Json.getString(oHeaders, hName), vars, config, options, oKeys))));
}
}
// set parameters
JsonObject oParameters = Json.getObject(spec, Spec.request.Parameters);
if (oParameters != null && !oParameters.isEmpty()) {
List<HttpParameter> parameters = request.getParameters();
if (parameters == null) {
parameters = new ArrayList<HttpParameter>();
request.setParameters(parameters);
}
Iterator<String> pNames = oParameters.keys();
while (pNames.hasNext()) {
String pName = pNames.next();
parameters.add(new HttpParameterImpl(pName, String.valueOf(eval(Json.getString(oParameters, pName), vars, config, options, oKeys))));
}
}
// set body
JsonObject oBody = Json.getObject(spec, Spec.request.Body);
if (oBody != null && !oBody.isEmpty()) {
HttpMessageBody body = new HttpMessageBodyImpl();
request.setBody(body);
Iterator<String> bNames = oBody.keys();
while (bNames.hasNext()) {
String bName = bNames.next();
Object value = eval(Json.getString(oBody, bName), vars, config, options, oKeys);
if (value == null) {
continue;
}
if (ContentTypes.Json.equals(contentType)) {
Object ov = null;
String varName = String.valueOf(value);
if (EmptyPayload.equals(varName)) {
ov = JsonObject.Blank;
} else {
ov = ((Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS)).get(varName);
if (ov == null) {
throw new Exception("variable " + value + " not found");
}
if (!(ov instanceof JsonObject)) {
throw new Exception(value + " isn't a valid json object");
}
}
body.add(new StringHttpMessageBodyPart(ov.toString()));
continue;
}
String fileName = null;
InputStream stream = null;
if (value instanceof File) {
File file = (File) value;
fileName = file.getName();
stream = new FileInputStream(file);
streams.add(stream);
} else if (value instanceof StreamPointer) {
StreamPointer sp = (StreamPointer) value;
fileName = sp.getPointer().getName();
stream = sp.getStream();
streams.add(sp);
} else if (value instanceof String) {
tool.printer().info("Api File/Folder ==> " + value);
File file = new File((String) value);
if (!file.exists()) {
continue;
// throw new Exception ("file or folder '" + value + "' doesn't exist");
}
fileName = file.getName();
if (file.isFile()) {
stream = new FileInputStream(file);
streams.add(stream);
} else if (file.isDirectory()) {
fileName += ".api";
File zipFile = new File(BlueNimble.Work, Lang.UUID(20) + ".api");
ArchiveUtils.compress(file, zipFile, true, new ArchiveUtils.CompressVisitor() {
@Override
public boolean onAdd(File file) {
if (file.getName().startsWith(Lang.DOT)) {
return false;
}
return true;
}
});
StreamPointer sp = new StreamPointer(zipFile, true);
streams.add(sp);
stream = sp.getStream();
}
}
body.add(new InputStreamHttpMessageBodyPart(bName, fileName, stream));
}
}
if (request.getBody() != null && request.getBody().count() == 0) {
request.setBody(null);
}
// set content type
if (!Lang.isNullOrEmpty(contentType)) {
if (ApiContentTypes.Multipart.equals(contentType)) {
if (request.getBody() != null) {
request.setContentType(contentType);
}
} else {
request.setContentType(contentType);
}
}
// sign request
boolean sign = Json.getBoolean(spec, Spec.request.Sign, true);
if (sign) {
String space = Json.getString(oKeys, Keys.Spec.Space);
String accessKey = Json.getString(oKeys, KeyPair.Fields.AccessKey);
String secretKey = Json.getString(oKeys, KeyPair.Fields.SecretKey);
AccessSecretKeysBasedHttpRequestSigner signer = new AccessSecretKeysBasedHttpRequestSigner("m>h>p>d>k>t", "Bearer", space == null ? accessKey : space + Lang.DOT + accessKey, secretKey);
String timestamp = Lang.utc();
headers.add(new HttpHeaderImpl(ApiHeaders.Timestamp, timestamp));
signer.getData().put('t', timestamp);
signer.sign(request);
}
if (Lang.isDebugMode()) {
tool.printer().content("Http Request", request.toString());
}
return request;
}
Aggregations