Search in sources :

Example 1 with ServiceException

use of com.centurylink.mdw.common.service.ServiceException in project mdw-designer by CenturyLinkCloud.

the class MdwAuthenticator method doAuthentication.

/**
 * <p>
 * Takes a cuid and pass combination and authenticates against JWT.
 * </p>
 *
 * @param cuid
 * @param pass
 * @return the JWT access token
 */
public String doAuthentication(String cuid, String pass) throws MdwSecurityException {
    String accessToken = null;
    try {
        if (StringHelper.isEmpty(tokenLocation)) {
            throw new MdwSecurityException("Token location is empty, should point to an JWT token location endpoint." + " Unable to authenticate user " + cuid + " with JWT");
        }
        JSONObject json = new JSONObject();
        json.put("user", cuid);
        json.put("password", pass);
        json.put("appId", appId);
        try {
            HttpHelper helper = new HttpHelper(new URL(tokenLocation));
            Map<String, String> hdrs = new HashMap<>();
            hdrs.put("Content-Type", "application/json; charset=utf-8");
            helper.setHeaders(hdrs);
            String response = helper.post(json.toString());
            JSONObject responseJson = new JSONObject(response);
            accessToken = responseJson.getString("mdwauth");
            if (accessToken == null || accessToken.isEmpty())
                throw new IOException("User authentication failed with response:" + responseJson);
        } catch (IOException ex) {
            throw new ServiceException(ex.getMessage(), ex);
        }
    } catch (Exception ex) {
        String msg = "Unable to authenticate user " + cuid + " with JWT";
        throw new AuthenticationException(msg, ex);
    }
    return accessToken;
}
Also used : JSONObject(org.json.JSONObject) ServiceException(com.centurylink.mdw.common.service.ServiceException) HashMap(java.util.HashMap) AuthenticationException(com.centurylink.mdw.auth.AuthenticationException) IOException(java.io.IOException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException) URL(java.net.URL) ServiceException(com.centurylink.mdw.common.service.ServiceException) AuthenticationException(com.centurylink.mdw.auth.AuthenticationException) IOException(java.io.IOException) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException)

Example 2 with ServiceException

use of com.centurylink.mdw.common.service.ServiceException in project mdw-designer by CenturyLinkCloud.

the class NodeRunner method run.

public void run(final TestCase testCase, DesignerDataAccess dao, boolean verbose, boolean createReplace) throws ServiceException {
    PackageVO pkgVo = null;
    NodeJS nodeJS = NodeJS.createNodeJS();
    try {
        pkgVo = dao.getPackage(NODE_PKG);
    } catch (DataAccessException e) {
        throw new ServiceException(e.getMessage());
    }
    final V8Object fileObj = new V8Object(nodeJS.getRuntime()).add("file", pkgVo.getRuleSet(RUNNER).getRawFile().getAbsolutePath());
    JavaCallback callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            return fileObj;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "getRunner");
    final Result parseResult = new Result();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            V8Object resultObj = parameters.getObject(0);
            parseResult.status = resultObj.getString("status");
            parseResult.message = resultObj.getString("message");
            resultObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setParseResult");
    nodeJS.exec(pkgVo.getRuleSet(PARSER).getRawFile());
    while (nodeJS.isRunning()) {
        nodeJS.handleMessage();
    }
    fileObj.release();
    nodeJS.release();
    if (!parseResult.status.equals("OK"))
        throw new ServiceException(PARSER + parseResult);
    nodeJS = NodeJS.createNodeJS();
    final V8Object testObj = new V8Object(nodeJS.getRuntime());
    testObj.add("file", testCase.getCaseFile().getAbsolutePath());
    V8Array valueFiles = new V8Array(nodeJS.getRuntime());
    // TODO hardcoded
    valueFiles.push("localhost.env");
    testObj.add("valueFiles", valueFiles);
    valueFiles.release();
    testObj.add("resultDir", testCase.getResultDirectory().getAbsolutePath());
    final Map<String, TestCaseItem> testCaseItems = new HashMap<>();
    final V8Array testItems = new V8Array(nodeJS.getRuntime());
    for (TestCaseItem item : testCase.getItems()) {
        String itemId = item.getName();
        V8Object itemObj = new V8Object(nodeJS.getRuntime()).add("name", item.getName());
        try {
            if (item.getObject().has("request")) {
                JSONObject request = item.getObject().getJSONObject("request");
                if (request.has("method")) {
                    itemObj.add("method", request.getString("method"));
                    itemId = request.getString("method") + ":" + itemId;
                }
            }
            JSONObject options = item.getOptions() == null ? new JSONObject() : item.getOptions();
            if (verbose && !options.has("debug"))
                options.put("debug", "true");
            if (createReplace && !options.has("overwriteExpected"))
                options.put("overwriteExpected", "true");
            options.put("qualifyLocations", false);
            if (JSONObject.getNames(options) != null) {
                V8Object json = nodeJS.getRuntime().getObject("JSON");
                V8Array params = new V8Array(nodeJS.getRuntime()).push(options.toString());
                V8Object jsonObj = json.executeObjectFunction("parse", params);
                itemObj.add("options", jsonObj);
                params.release();
                json.release();
                jsonObj.release();
            }
            if (item.getValues() != null) {
                V8Object json = nodeJS.getRuntime().getObject("JSON");
                V8Array params = new V8Array(nodeJS.getRuntime()).push(item.getValues().toString());
                V8Object jsonObj = json.executeObjectFunction("parse", params);
                itemObj.add("values", jsonObj);
                params.release();
                json.release();
                jsonObj.release();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        testItems.push(itemObj);
        testCaseItems.put(itemId, item);
        itemObj.release();
    }
    testObj.add("items", testItems);
    testItems.release();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            return testObj;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "getTestCase");
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            String itemId = parameters.getString(0);
            V8Object resultObj = parameters.getObject(1);
            if (itemId == null) {
                for (TestCaseItem item : testCase.getItems()) {
                    updateItem(item, resultObj);
                }
            }
            TestCaseItem item = testCaseItems.get(itemId);
            if (item != null) {
                updateItem(item, resultObj);
            }
            resultObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setTestResult");
    final V8 v8 = nodeJS.getRuntime();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            String itemId = parameters.getString(0);
            V8Object responseObj = parameters.getObject(1);
            TestCaseItem item = testCaseItems.get(itemId);
            if (item != null) {
                V8Object json = v8.getObject("JSON");
                V8Array params = new V8Array(v8).push(responseObj);
                String jsonStr = json.executeStringFunction("stringify", params);
                params.release();
                json.release();
                try {
                    item.setResponse(new JSONObject(jsonStr));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            responseObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setTestResponse");
    nodeJS.exec(pkgVo.getRuleSet(RUNNER).getRawFile());
    while (nodeJS.isRunning()) {
        nodeJS.handleMessage();
    }
    testObj.release();
    nodeJS.release();
}
Also used : V8(com.eclipsesource.v8.V8) PackageVO(com.centurylink.mdw.model.value.process.PackageVO) HashMap(java.util.HashMap) V8Object(com.eclipsesource.v8.V8Object) JavaCallback(com.eclipsesource.v8.JavaCallback) V8Array(com.eclipsesource.v8.V8Array) JSONException(org.json.JSONException) NodeJS(com.eclipsesource.v8.NodeJS) ServiceException(com.centurylink.mdw.common.service.ServiceException) JSONObject(org.json.JSONObject) V8Object(com.eclipsesource.v8.V8Object) JSONObject(org.json.JSONObject) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException)

Aggregations

ServiceException (com.centurylink.mdw.common.service.ServiceException)2 HashMap (java.util.HashMap)2 JSONObject (org.json.JSONObject)2 AuthenticationException (com.centurylink.mdw.auth.AuthenticationException)1 MdwSecurityException (com.centurylink.mdw.auth.MdwSecurityException)1 DataAccessException (com.centurylink.mdw.common.exception.DataAccessException)1 HttpHelper (com.centurylink.mdw.common.utilities.HttpHelper)1 PackageVO (com.centurylink.mdw.model.value.process.PackageVO)1 JavaCallback (com.eclipsesource.v8.JavaCallback)1 NodeJS (com.eclipsesource.v8.NodeJS)1 V8 (com.eclipsesource.v8.V8)1 V8Array (com.eclipsesource.v8.V8Array)1 V8Object (com.eclipsesource.v8.V8Object)1 IOException (java.io.IOException)1 URL (java.net.URL)1 JSONException (org.json.JSONException)1