Search in sources :

Example 21 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project ballerina by ballerina-lang.

the class CreateHttpClient method populateSenderConfigurationOptions.

private void populateSenderConfigurationOptions(SenderConfiguration senderConfiguration, Struct clientEndpointConfig) {
    ProxyServerConfiguration proxyServerConfiguration = null;
    boolean followRedirect = false;
    int maxRedirectCount = DEFAULT_MAX_REDIRECT_COUNT;
    Struct followRedirects = clientEndpointConfig.getStructField(HttpConstants.FOLLOW_REDIRECT_STRUCT_REFERENCE);
    if (followRedirects != null) {
        followRedirect = followRedirects.getBooleanField(HttpConstants.FOLLOW_REDIRECT_ENABLED);
        maxRedirectCount = (int) followRedirects.getIntField(HttpConstants.FOLLOW_REDIRECT_MAXCOUNT);
    }
    Struct secureSocket = null;
    Value[] targetServices = clientEndpointConfig.getArrayField(HttpConstants.TARGET_SERVICES);
    for (Value targetService : targetServices) {
        secureSocket = targetService.getStructValue().getStructField(HttpConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
        if (secureSocket != null) {
            Struct trustStore = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_TRUST_STORE);
            Struct keyStore = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_KEY_STORE);
            Struct protocols = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_PROTOCOLS);
            Struct validateCert = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_VALIDATE_CERT);
            List<Parameter> clientParams = new ArrayList<>();
            if (trustStore != null) {
                String trustStoreFile = trustStore.getStringField(HttpConstants.FILE_PATH);
                if (StringUtils.isNotBlank(trustStoreFile)) {
                    senderConfiguration.setTrustStoreFile(trustStoreFile);
                }
                String trustStorePassword = trustStore.getStringField(HttpConstants.PASSWORD);
                if (StringUtils.isNotBlank(trustStorePassword)) {
                    senderConfiguration.setTrustStorePass(trustStorePassword);
                }
            }
            if (keyStore != null) {
                String keyStoreFile = keyStore.getStringField(HttpConstants.FILE_PATH);
                if (StringUtils.isNotBlank(keyStoreFile)) {
                    senderConfiguration.setKeyStoreFile(keyStoreFile);
                }
                String keyStorePassword = keyStore.getStringField(HttpConstants.PASSWORD);
                if (StringUtils.isNotBlank(keyStorePassword)) {
                    senderConfiguration.setKeyStorePassword(keyStorePassword);
                }
            }
            if (protocols != null) {
                String sslEnabledProtocols = protocols.getStringField(HttpConstants.ENABLED_PROTOCOLS);
                if (StringUtils.isNotBlank(sslEnabledProtocols)) {
                    Parameter clientProtocols = new Parameter(HttpConstants.SSL_ENABLED_PROTOCOLS, sslEnabledProtocols);
                    clientParams.add(clientProtocols);
                }
                String sslProtocol = protocols.getStringField(HttpConstants.PROTOCOL_VERSION);
                if (StringUtils.isNotBlank(sslProtocol)) {
                    senderConfiguration.setSSLProtocol(sslProtocol);
                }
            }
            if (validateCert != null) {
                boolean validateCertEnabled = validateCert.getBooleanField(HttpConstants.ENABLE);
                int cacheSize = (int) validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_SIZE);
                int cacheValidityPeriod = (int) validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_VALIDITY_PERIOD);
                senderConfiguration.setValidateCertEnabled(validateCertEnabled);
                if (cacheValidityPeriod != 0) {
                    senderConfiguration.setCacheValidityPeriod(cacheValidityPeriod);
                }
                if (cacheSize != 0) {
                    senderConfiguration.setCacheSize(cacheSize);
                }
            }
            boolean hostNameVerificationEnabled = secureSocket.getBooleanField(HttpConstants.SSL_CONFIG_HOST_NAME_VERIFICATION_ENABLED);
            senderConfiguration.setHostNameVerificationEnabled(hostNameVerificationEnabled);
            String ciphers = secureSocket.getStringField(HttpConstants.SSL_CONFIG_CIPHERS);
            if (StringUtils.isNotBlank(ciphers)) {
                Parameter clientCiphers = new Parameter(HttpConstants.CIPHERS, ciphers);
                clientParams.add(clientCiphers);
            }
            String enableSessionCreation = String.valueOf(secureSocket.getBooleanField(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION));
            Parameter clientEnableSessionCreation = new Parameter(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION, enableSessionCreation);
            clientParams.add(clientEnableSessionCreation);
            if (!clientParams.isEmpty()) {
                senderConfiguration.setParameters(clientParams);
            }
        }
    }
    Struct proxy = clientEndpointConfig.getStructField(HttpConstants.PROXY_STRUCT_REFERENCE);
    if (proxy != null) {
        String proxyHost = proxy.getStringField(HttpConstants.PROXY_HOST);
        int proxyPort = (int) proxy.getIntField(HttpConstants.PROXY_PORT);
        String proxyUserName = proxy.getStringField(HttpConstants.PROXY_USERNAME);
        String proxyPassword = proxy.getStringField(HttpConstants.PROXY_PASSWORD);
        try {
            proxyServerConfiguration = new ProxyServerConfiguration(proxyHost, proxyPort);
        } catch (UnknownHostException e) {
            throw new BallerinaConnectorException("Failed to resolve host" + proxyHost, e);
        }
        if (!proxyUserName.isEmpty()) {
            proxyServerConfiguration.setProxyUsername(proxyUserName);
        }
        if (!proxyPassword.isEmpty()) {
            proxyServerConfiguration.setProxyPassword(proxyPassword);
        }
        senderConfiguration.setProxyServerConfiguration(proxyServerConfiguration);
    }
    senderConfiguration.setFollowRedirect(followRedirect);
    senderConfiguration.setMaxRedirectCount(maxRedirectCount);
    // For the moment we don't have to pass it down to transport as we only support
    // chunking. Once we start supporting gzip, deflate, etc, we need to parse down the config.
    String transferEncoding = clientEndpointConfig.getEnumField(HttpConstants.CLIENT_EP_TRNASFER_ENCODING);
    if (transferEncoding != null && !HttpConstants.ANN_CONFIG_ATTR_CHUNKING.equalsIgnoreCase(transferEncoding)) {
        throw new BallerinaConnectorException("Unsupported configuration found for Transfer-Encoding : " + transferEncoding);
    }
    String chunking = clientEndpointConfig.getEnumField(HttpConstants.CLIENT_EP_CHUNKING);
    senderConfiguration.setChunkingConfig(HttpUtil.getChunkConfig(chunking));
    long endpointTimeout = clientEndpointConfig.getIntField(HttpConstants.CLIENT_EP_ENDPOINT_TIMEOUT);
    if (endpointTimeout < 0 || !isInteger(endpointTimeout)) {
        throw new BallerinaConnectorException("invalid idle timeout: " + endpointTimeout);
    }
    senderConfiguration.setSocketIdleTimeout((int) endpointTimeout);
    boolean isKeepAlive = clientEndpointConfig.getBooleanField(HttpConstants.CLIENT_EP_IS_KEEP_ALIVE);
    senderConfiguration.setKeepAlive(isKeepAlive);
    String httpVersion = clientEndpointConfig.getStringField(HttpConstants.CLIENT_EP_HTTP_VERSION);
    if (httpVersion != null) {
        senderConfiguration.setHttpVersion(httpVersion);
    }
    String forwardedExtension = clientEndpointConfig.getStringField(HttpConstants.CLIENT_EP_FORWARDED);
    senderConfiguration.setForwardedExtensionConfig(HttpUtil.getForwardedExtensionConfig(forwardedExtension));
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) ProxyServerConfiguration(org.wso2.transport.http.netty.common.ProxyServerConfiguration) Value(org.ballerinalang.connector.api.Value) Parameter(org.wso2.transport.http.netty.config.Parameter)

Example 22 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project ballerina by ballerina-lang.

the class InitEndpoint method setSslConfig.

private ListenerConfiguration setSslConfig(Struct sslConfig, ListenerConfiguration listenerConfiguration) {
    listenerConfiguration.setScheme(HttpConstants.PROTOCOL_HTTPS);
    Struct trustStore = sslConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_TRUST_STORE);
    Struct keyStore = sslConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_KEY_STORE);
    Struct protocols = sslConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_PROTOCOLS);
    Struct validateCert = sslConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_VALIDATE_CERT);
    if (keyStore != null) {
        String keyStoreFile = keyStore.getStringField(HttpConstants.FILE_PATH);
        String keyStorePassword = keyStore.getStringField(HttpConstants.PASSWORD);
        if (StringUtils.isBlank(keyStoreFile)) {
            // TODO get from language pack, and add location
            throw new BallerinaConnectorException("Keystore location must be provided for secure connection");
        }
        if (StringUtils.isBlank(keyStorePassword)) {
            // TODO get from language pack, and add location
            throw new BallerinaConnectorException("Keystore password value must be provided for secure connection");
        }
        listenerConfiguration.setKeyStoreFile(keyStoreFile);
        listenerConfiguration.setKeyStorePass(keyStorePassword);
    }
    String sslVerifyClient = sslConfig.getStringField(HttpConstants.SSL_CONFIG_SSL_VERIFY_CLIENT);
    listenerConfiguration.setVerifyClient(sslVerifyClient);
    if (trustStore != null) {
        String trustStoreFile = trustStore.getStringField(HttpConstants.FILE_PATH);
        String trustStorePassword = trustStore.getStringField(HttpConstants.PASSWORD);
        if (StringUtils.isBlank(trustStoreFile) && StringUtils.isNotBlank(sslVerifyClient)) {
            // TODO get from language pack, and add location
            throw new BallerinaException("Truststore location must be provided to enable Mutual SSL");
        }
        if (StringUtils.isBlank(trustStorePassword) && StringUtils.isNotBlank(sslVerifyClient)) {
            // TODO get from language pack, and add location
            throw new BallerinaException("Truststore password value must be provided to enable Mutual SSL");
        }
        listenerConfiguration.setTrustStoreFile(trustStoreFile);
        listenerConfiguration.setTrustStorePass(trustStorePassword);
    }
    List<Parameter> serverParamList = new ArrayList<>();
    Parameter serverParameters;
    if (protocols != null) {
        String sslEnabledProtocols = protocols.getStringField(HttpConstants.ENABLED_PROTOCOLS);
        String sslProtocol = protocols.getStringField(HttpConstants.PROTOCOL_VERSION);
        if (StringUtils.isNotBlank(sslEnabledProtocols)) {
            serverParameters = new Parameter(HttpConstants.ANN_CONFIG_ATTR_SSL_ENABLED_PROTOCOLS, sslEnabledProtocols);
            serverParamList.add(serverParameters);
        }
        if (StringUtils.isNotBlank(sslProtocol)) {
            listenerConfiguration.setSSLProtocol(sslProtocol);
        }
    }
    String cipher = sslConfig.getStringField(HttpConstants.SSL_CONFIG_CIPHERS);
    if (StringUtils.isNotBlank(cipher)) {
        serverParameters = new Parameter(HttpConstants.ANN_CONFIG_ATTR_CIPHERS, cipher);
        serverParamList.add(serverParameters);
    }
    if (validateCert != null) {
        boolean validateCertificateEnabled = validateCert.getBooleanField(HttpConstants.ENABLE);
        long cacheSize = validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_SIZE);
        long cacheValidationPeriod = validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_VALIDITY_PERIOD);
        listenerConfiguration.setValidateCertEnabled(validateCertificateEnabled);
        if (validateCertificateEnabled) {
            if (cacheSize != 0) {
                listenerConfiguration.setCacheSize(Math.toIntExact(cacheSize));
            }
            if (cacheValidationPeriod != 0) {
                listenerConfiguration.setCacheValidityPeriod(Math.toIntExact(cacheValidationPeriod));
            }
        }
    }
    listenerConfiguration.setTLSStoreType(HttpConstants.PKCS_STORE_TYPE);
    String serverEnableSessionCreation = String.valueOf(sslConfig.getBooleanField(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION));
    Parameter enableSessionCreationParam = new Parameter(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION, serverEnableSessionCreation);
    serverParamList.add(enableSessionCreationParam);
    if (!serverParamList.isEmpty()) {
        listenerConfiguration.setParameters(serverParamList);
    }
    listenerConfiguration.setId(HttpUtil.getListenerInterface(listenerConfiguration.getHost(), listenerConfiguration.getPort()));
    return listenerConfiguration;
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) ArrayList(java.util.ArrayList) Parameter(org.wso2.transport.http.netty.config.Parameter) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 23 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project ballerina by ballerina-lang.

the class UriMatrixParametersMatchTest method testErrorReportInURI.

@Test
public void testErrorReportInURI() {
    // encoded URI
    String path = "/hello/t2/john;age;color=white/foo;a=5;b=10";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
    HTTPCarbonMessage response = Services.invokeNew(application, TEST_EP, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(response.getProperty(HttpConstants.HTTP_STATUS_CODE), 500, "Response code mismatch");
    // checking the exception message
    String errorMessage = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertNotNull(errorMessage, "Message body null");
    Assert.assertTrue(errorMessage.contains("found non-matrix parameter"), "Expected error not found.");
}
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) Test(org.testng.annotations.Test)

Example 24 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getSignatureInformation.

/**
 * Get the signature information for the given Ballerina function.
 *
 * @param bInvokableSymbol BLang Invokable symbol
 * @param signatureContext Signature operation context
 * @return {@link SignatureInformation}     Signature information for the function
 */
private static SignatureInformation getSignatureInformation(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    List<ParameterInformation> parameterInformationList = new ArrayList<>();
    SignatureInformation signatureInformation = new SignatureInformation();
    SignatureInfoModel signatureInfoModel = getSignatureInfoModel(bInvokableSymbol, signatureContext);
    String functionName = bInvokableSymbol.getName().getValue();
    // Join the function parameters to generate the function's signature
    String paramsJoined = signatureInfoModel.getParameterInfoModels().stream().map(parameterInfoModel -> {
        // For each of the parameters, create a parameter info instance
        ParameterInformation parameterInformation = new ParameterInformation(parameterInfoModel.paramValue, parameterInfoModel.description);
        parameterInformationList.add(parameterInformation);
        return parameterInfoModel.toString();
    }).collect(Collectors.joining(", "));
    signatureInformation.setLabel(functionName + "(" + paramsJoined + ")");
    signatureInformation.setParameters(parameterInformationList);
    signatureInformation.setDocumentation(signatureInfoModel.signatureDescription);
    return signatureInformation;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) Stack(java.util.Stack) ArrayList(java.util.ArrayList) DocTag(org.ballerinalang.model.elements.DocTag) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) ParameterInformation(org.eclipse.lsp4j.ParameterInformation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) Collectors(java.util.stream.Collectors) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Objects(java.util.Objects) List(java.util.List) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) ArrayList(java.util.ArrayList) ParameterInformation(org.eclipse.lsp4j.ParameterInformation)

Example 25 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project ballerina by ballerina-lang.

the class ParserUtils method createNewFunction.

/**
 * Create new function.
 *
 * @param name         name of the function
 * @param annotations  list of annotations
 * @param params       list of parameters
 * @param returnParams list of return params
 * @return {Function} function
 */
private static Function createNewFunction(String name, List<AnnotationAttachment> annotations, List<Parameter> params, List<Parameter> returnParams, String receiverType, boolean isPublic, String fileName) {
    Function function = new Function();
    function.setName(name);
    function.setAnnotations(annotations);
    function.setParameters(params);
    function.setReturnParams(returnParams);
    function.setReceiverType(receiverType);
    function.setPublic(isPublic);
    function.setFileName(fileName);
    return function;
}
Also used : Function(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Function) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction)

Aggregations

HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)19 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)11 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)11 CharonException (org.wso2.charon3.core.exceptions.CharonException)11 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)11 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)11 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)11 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)11 Test (org.testng.annotations.Test)8 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)8 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)8 List (java.util.List)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)7 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)7 Parameter (org.apache.axis2.description.Parameter)6 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)6