Search in sources :

Example 26 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for functions.
 * @param functionNode ballerina function node.
 * @return documentation for functions.
 */
public static FunctionDoc createDocForNode(BLangFunction functionNode) {
    String functionName = functionNode.getName().value;
    List<Variable> parameters = new ArrayList<>();
    List<Variable> returnParams = new ArrayList<>();
    // Iterate through the parameters
    if (functionNode.getParameters().size() > 0) {
        for (BLangVariable param : functionNode.getParameters()) {
            String dataType = type(param);
            String desc = paramAnnotation(functionNode, param);
            Variable variable = new Variable(param.getName().value, dataType, desc);
            parameters.add(variable);
        }
    }
    // Iterate through the return types
    if (functionNode.getReturnParameters().size() > 0) {
        for (int i = 0; i < functionNode.getReturnParameters().size(); i++) {
            BLangVariable returnParam = functionNode.getReturnParameters().get(i);
            String dataType = type(returnParam);
            String desc = returnParamAnnotation(functionNode, i);
            Variable variable = new Variable(returnParam.getName().value, dataType, desc);
            returnParams.add(variable);
        }
    }
    return new FunctionDoc(functionName, description(functionNode), new ArrayList<>(), parameters, returnParams);
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Variable(org.ballerinalang.docgen.model.Variable) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) ArrayList(java.util.ArrayList) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 27 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class ProducesConsumesAnnotationTest method testWithoutProducesConsumeAnnotation.

@Test(description = "Test without Pro-Con annotation with URL. /echo67/echo1 ")
public void testWithoutProducesConsumeAnnotation() {
    String path = "/echo67/echo1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
    cMsg.setHeader(HttpHeaderNames.CONTENT_TYPE.toString(), "text/plain; charset=ISO-8859-4");
    cMsg.setHeader(HttpHeaderNames.ACCEPT.toString(), "text/*;q=0.3, text/html;Level=1;q=0.7");
    HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("echo33").asText(), "echo1", "No media types");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 28 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.

the class ProducesConsumesAnnotationTest method testProducesConsumeAnnotation.

@Test(description = "Test Produces and Consumes with URL. /echo66/test3 ")
public void testProducesConsumeAnnotation() {
    String path = "/echo66/test3";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", "Test");
    cMsg.setHeader(HttpHeaderNames.CONTENT_TYPE.toString(), "text/plain; charset=ISO-8859-4");
    cMsg.setHeader(HttpHeaderNames.ACCEPT.toString(), "text/*;q=0.3, text/html;Level=1;q=0.7");
    HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    BJSON bJson = new BJSON(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(bJson.value().get("msg").asText(), "wso222", "media types matched");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 29 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project jaggery by wso2.

the class RegistryHostObject method jsFunction_get.

public static Scriptable jsFunction_get(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
    RegistryHostObject rho = (RegistryHostObject) thisObj;
    if (arguments.length == 1) {
        if (arguments[0] instanceof String) {
            try {
                Scriptable hostObject;
                Resource resource = rho.registry.get((String) arguments[0]);
                if (resource instanceof Collection) {
                    hostObject = cx.newObject(rho, "Collection", new Object[] { resource });
                } else {
                    hostObject = cx.newObject(rho, "Resource", new Object[] { resource });
                }
                return hostObject;
            } catch (RegistryException e) {
                throw new ScriptException("Registry error occurred while executing get() operation", e);
            }
        } else {
            throw new ScriptException("Path argument of method get() should be a string");
        }
    } else if (arguments.length == 3) {
        if (arguments[0] instanceof String && arguments[1] instanceof Number && arguments[2] instanceof Number) {
            try {
                Collection collection = rho.registry.get((String) arguments[0], ((Number) arguments[1]).intValue(), ((Number) arguments[2]).intValue());
                CollectionHostObject cho = (CollectionHostObject) cx.newObject(rho, "Collection", new Object[] { collection });
                return cho;
            } catch (RegistryException e) {
                throw new ScriptException("Registry error occurred while executing get() operation", e);
            }
        } else {
            throw new ScriptException("Invalid argument types for get() method");
        }
    } else {
        throw new ScriptException("Invalid no. of arguments for get() method");
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 30 with Types

use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project carbon-apimgt by wso2.

the class AbstractKeyManager method buildFromJSON.

/**
 * This method will accept json String and will do the json parse will set oAuth application properties to OAuthApplicationInfo object.
 *
 * @param jsonInput this jsonInput will contain set of oAuth application properties.
 * @return OAuthApplicationInfo object will be return.
 * @throws APIManagementException
 */
public OAuthApplicationInfo buildFromJSON(OAuthApplicationInfo oAuthApplicationInfo, String jsonInput) throws APIManagementException {
    // initiate json parser.
    JSONParser parser = new JSONParser();
    JSONObject jsonObject;
    try {
        // parse json String
        jsonObject = (JSONObject) parser.parse(jsonInput);
        if (jsonObject != null) {
            // create a map to hold json parsed objects.
            Map<String, Object> params = (Map) jsonObject;
            if (params.get(APIConstants.JSON_CALLBACK_URL) != null) {
                oAuthApplicationInfo.setCallBackURL((String) params.get(APIConstants.JSON_CALLBACK_URL));
            }
            if (params.get(APIConstants.JSON_GRANT_TYPES) != null) {
                String grantTypeString = params.get(APIConstants.JSON_GRANT_TYPES).toString();
                if (StringUtils.isEmpty(oAuthApplicationInfo.getCallBackURL()) && (grantTypeString.contains("implicit") || grantTypeString.contains("authorization_code"))) {
                    throw new EmptyCallbackURLForCodeGrantsException("The callback url must have at least one URI " + "value when using Authorization code or implicit grant types.");
                }
            }
            // set client Id
            if (params.get(APIConstants.JSON_CLIENT_ID) != null) {
                oAuthApplicationInfo.setClientId((String) params.get(APIConstants.JSON_CLIENT_ID));
            }
            // set client secret
            if (params.get(APIConstants.JSON_CLIENT_SECRET) != null) {
                oAuthApplicationInfo.setClientSecret((String) params.get(APIConstants.JSON_CLIENT_SECRET));
            }
            // copy all params map in to OAuthApplicationInfo's Map object.
            oAuthApplicationInfo.putAll(params);
            validateOAuthAppCreationProperties(oAuthApplicationInfo);
            return oAuthApplicationInfo;
        }
    } catch (ParseException e) {
        handleException("Error occurred while parsing JSON String", e);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) EmptyCallbackURLForCodeGrantsException(org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException)

Aggregations

ArrayList (java.util.ArrayList)20 Test (org.testng.annotations.Test)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)13 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)12 HashMap (java.util.HashMap)11 Map (java.util.Map)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 List (java.util.List)8 JsonObject (com.google.gson.JsonObject)5 Test (org.junit.Test)5 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)5 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4 JSONObject (org.json.simple.JSONObject)4 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 JsonElement (com.google.gson.JsonElement)3 Paths (java.nio.file.Paths)3 TokenStream (org.antlr.v4.runtime.TokenStream)3 StringUtils (org.apache.commons.lang3.StringUtils)3