Search in sources :

Example 1 with AccessSecretKeysBasedHttpRequestSigner

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);
    }
}
Also used : HttpHeader(com.bluenimble.platform.http.HttpHeader) AccessSecretKeysBasedHttpRequestSigner(com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner) HttpRequestSignerException(com.bluenimble.platform.http.auth.HttpRequestSignerException) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Example 2 with AccessSecretKeysBasedHttpRequestSigner

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;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) StreamPointer(com.bluenimble.platform.cli.command.parser.converters.StreamPointer) URI(java.net.URI) HttpHeader(com.bluenimble.platform.http.HttpHeader) InputStreamHttpMessageBodyPart(com.bluenimble.platform.http.impls.InputStreamHttpMessageBodyPart) AccessSecretKeysBasedHttpRequestSigner(com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner) HttpRequest(com.bluenimble.platform.http.request.HttpRequest) HttpMessageBody(com.bluenimble.platform.http.HttpMessageBody) StringHttpMessageBodyPart(com.bluenimble.platform.http.impls.StringHttpMessageBodyPart) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl) HttpParameter(com.bluenimble.platform.http.HttpParameter) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HttpParameterImpl(com.bluenimble.platform.http.impls.HttpParameterImpl) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) JsonObject(com.bluenimble.platform.json.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) HttpMessageBodyImpl(com.bluenimble.platform.http.impls.HttpMessageBodyImpl)

Aggregations

HttpHeader (com.bluenimble.platform.http.HttpHeader)2 AccessSecretKeysBasedHttpRequestSigner (com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner)2 HttpHeaderImpl (com.bluenimble.platform.http.impls.HttpHeaderImpl)2 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)1 StreamPointer (com.bluenimble.platform.cli.command.parser.converters.StreamPointer)1 YamlObject (com.bluenimble.platform.cli.impls.YamlObject)1 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpMessageBody (com.bluenimble.platform.http.HttpMessageBody)1 HttpParameter (com.bluenimble.platform.http.HttpParameter)1 HttpRequestSignerException (com.bluenimble.platform.http.auth.HttpRequestSignerException)1 HttpMessageBodyImpl (com.bluenimble.platform.http.impls.HttpMessageBodyImpl)1 HttpParameterImpl (com.bluenimble.platform.http.impls.HttpParameterImpl)1 InputStreamHttpMessageBodyPart (com.bluenimble.platform.http.impls.InputStreamHttpMessageBodyPart)1 StringHttpMessageBodyPart (com.bluenimble.platform.http.impls.StringHttpMessageBodyPart)1 HttpRequest (com.bluenimble.platform.http.request.HttpRequest)1 HttpRequestWriteException (com.bluenimble.platform.http.request.HttpRequestWriteException)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1