Search in sources :

Example 6 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getIdOfUser.

@Override
public String getIdOfUser(String userName) throws IdentityProviderException {
    // should not user id outside this domain and should not log that id.
    try {
        userName = userNameMapper.getLoggedInUserIDFromPseudoName(userName);
    } catch (APIManagementException e) {
        throw new IdentityProviderException(e.getMessage(), ExceptionCodes.USER_MAPPING_RETRIEVAL_FAILED);
    }
    Response userResponse = scimServiceStub.searchUsers(FILTER_PREFIX_USER + userName);
    String userId;
    if (userResponse == null) {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    if (userResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        String responseBody = userResponse.body().toString();
        JsonParser parser = new JsonParser();
        JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
        JsonArray user = (JsonArray) parsedResponseBody.get(RESOURCES);
        JsonObject scimUser = (JsonObject) user.get(0);
        userId = scimUser.get(ID).getAsString();
        String message = "Id " + userId + " of user " + scimUser.get(USERNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    } else {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : " + getErrorMessage(userResponse);
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return userId;
}
Also used : Response(feign.Response) JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) JsonObject(com.google.gson.JsonObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) JsonParser(com.google.gson.JsonParser)

Example 7 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getRoleId.

@Override
public String getRoleId(String roleName) throws IdentityProviderException {
    Response roleResponse = scimServiceStub.searchGroups(FILTER_PREFIX_ROLE + roleName);
    String roleId;
    if (roleResponse == null) {
        String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    if (roleResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        String responseBody = roleResponse.body().toString();
        JsonParser parser = new JsonParser();
        JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
        JsonArray role = (JsonArray) parsedResponseBody.get(RESOURCES);
        JsonObject scimGroup = (JsonObject) role.get(0);
        roleId = scimGroup.get(ID).getAsString();
        String message = "Id " + roleId + " of role " + scimGroup.get(GROUPNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    } else {
        String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : " + getErrorMessage(roleResponse);
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return roleId;
}
Also used : Response(feign.Response) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) JsonParser(com.google.gson.JsonParser)

Example 8 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project ballerina by ballerina-lang.

the class LSCustomErrorStrategy method setContextException.

@Override
protected void setContextException(Parser parser) {
    // Here the type of the exception is not important.
    InputMismatchException e = new InputMismatchException(parser);
    ParserRuleContext context = parser.getContext();
    // the run time
    if (context instanceof BallerinaParser.CallableUnitBodyContext) {
        context.exception = null;
        return;
    }
    context.exception = e;
    // We need to set the error for the variable definition as well.
    if (context.getParent() instanceof BallerinaParser.VariableDefinitionStatementContext) {
        context.getParent().exception = e;
        return;
    }
    if (context instanceof BallerinaParser.NameReferenceContext) {
        setContextIfConnectorInit(context, e);
    } else if (context instanceof BallerinaParser.ExpressionContext) {
        setContextIfConditionalStatement(context, e);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)

Example 9 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project ballerina by ballerina-lang.

the class Parser method generateCompilationUnit.

private CompilationUnitNode generateCompilationUnit(PackageSourceEntry sourceEntry) {
    try {
        int prevErrCount = dlog.errorCount;
        BDiagnosticSource diagnosticSrc = getDiagnosticSource(sourceEntry);
        String entryName = sourceEntry.getEntryName();
        BLangCompilationUnit compUnit = (BLangCompilationUnit) TreeBuilder.createCompilationUnit();
        compUnit.setName(sourceEntry.getEntryName());
        compUnit.pos = new DiagnosticPos(diagnosticSrc, 1, 1, 1, 1);
        ANTLRInputStream ais = new ANTLRInputStream(new ByteArrayInputStream(sourceEntry.getCode()));
        ais.name = entryName;
        BallerinaLexer lexer = new BallerinaLexer(ais);
        lexer.removeErrorListeners();
        lexer.addErrorListener(new BallerinaParserErrorListener(context, diagnosticSrc));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        BallerinaParser parser = new BallerinaParser(tokenStream);
        parser.setErrorHandler(getErrorStrategy(diagnosticSrc));
        parser.addParseListener(newListener(tokenStream, compUnit, diagnosticSrc));
        parser.compilationUnit();
        return compUnit;
    } catch (IOException e) {
        throw new RuntimeException("Error in populating package model: " + e.getMessage(), e);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BDiagnosticSource(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnosticSource) IOException(java.io.IOException) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ByteArrayInputStream(java.io.ByteArrayInputStream) BallerinaParserErrorListener(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParserErrorListener) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BallerinaLexer(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 10 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project ballerina by ballerina-lang.

the class BallerinaParserErrorStrategy method reportUnwantedToken.

@Override
public void reportUnwantedToken(Parser parser) {
    if (parser.getContext().exception != null || inErrorRecoveryMode(parser)) {
        return;
    }
    beginErrorCondition(parser);
    setContextException(parser);
    Token token = parser.getCurrentToken();
    DiagnosticPos pos = getPosition(getMissingSymbol(parser));
    dlog.error(pos, DiagnosticCode.EXTRANEOUS_INPUT, getTokenErrorDisplay(token));
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) Token(org.antlr.v4.runtime.Token)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)10 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)10 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)9 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 Token (org.antlr.v4.runtime.Token)4 JsonObject (com.google.gson.JsonObject)3 JsonParser (com.google.gson.JsonParser)3 Response (feign.Response)3 ArrayList (java.util.ArrayList)3 BallerinaParser (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)3 IdentityProviderException (org.wso2.carbon.apimgt.core.exception.IdentityProviderException)3 JsonArray (com.google.gson.JsonArray)2 IOException (java.io.IOException)2 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1