Search in sources :

Example 46 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project carbon-apimgt by wso2.

the class PolicyDAOImpl method setQueryParameterConditions.

/**
 * Add Query parameter conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
 * provided {@link Condition} array
 *
 * @param pipelineId Id of the pipeline
 * @param conditions condition array to populate
 * @throws SQLException
 */
private void setQueryParameterConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String query = "SELECT " + "PARAMETER_NAME,PARAMETER_VALUE , IS_PARAM_MAPPING " + "FROM " + "AM_QUERY_PARAMETER_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
                queryParameterCondition.setParameter(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_NAME));
                queryParameterCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_VALUE));
                queryParameterCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_PARAM_MAPPING));
                conditions.add(queryParameterCondition);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)

Example 47 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project carbon-apimgt by wso2.

the class DefaultKeyManagerImpl method createApplication.

@Override
public OAuthApplicationInfo createApplication(OAuthAppRequest oauthAppRequest) throws KeyManagementException {
    log.debug("Creating OAuth2 application:{}", oauthAppRequest.toString());
    String applicationName = oauthAppRequest.getClientName();
    String keyType = oauthAppRequest.getKeyType();
    if (keyType != null) {
        // Derive oauth2 app name based on key type and user input for app name
        applicationName = applicationName + '_' + keyType;
    }
    DCRClientInfo dcrClientInfo = new DCRClientInfo();
    dcrClientInfo.setClientName(applicationName);
    dcrClientInfo.setGrantTypes(oauthAppRequest.getGrantTypes());
    if (StringUtils.isNotEmpty(oauthAppRequest.getCallBackURL())) {
        dcrClientInfo.addCallbackUrl(oauthAppRequest.getCallBackURL());
    }
    Response response = dcrmServiceStub.registerApplication(dcrClientInfo);
    if (response == null) {
        throw new KeyManagementException("Error occurred while DCR application creation. Response is null", ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
    }
    if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_201_CREATED) {
        // 201 - Success
        try {
            OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
            // setting original parameter list
            oAuthApplicationInfoResponse.setParameters(oauthAppRequest.getParameters());
            log.debug("OAuth2 application created: {}", oAuthApplicationInfoResponse.toString());
            return oAuthApplicationInfoResponse;
        } catch (IOException e) {
            throw new KeyManagementException("Error occurred while parsing the DCR application creation response " + "message.", e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
        }
    } else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
        // 400 - Known Error
        try {
            DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
            throw new KeyManagementException("Error occurred while DCR application creation. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
        } catch (IOException e) {
            throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
        }
    } else {
        // Unknown Error
        throw new KeyManagementException("Error occurred while DCR application creation. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
    }
}
Also used : OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) Response(feign.Response) DCRError(org.wso2.carbon.apimgt.core.auth.dto.DCRError) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) DCRClientInfo(org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 48 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project carbon-apimgt by wso2.

the class FunctionTriggerTestCase method testCaptureEvent.

@Test(description = "Test method for capturing event")
public void testCaptureEvent() throws APIManagementException, URISyntaxException {
    FunctionDAO functionDAO = Mockito.mock(FunctionDAO.class);
    RestCallUtil restCallUtil = Mockito.mock(RestCallUtil.class);
    FunctionTrigger functionTrigger = new FunctionTrigger(functionDAO, restCallUtil);
    URI testUri = new URI("http://testEndpointUri");
    List<Function> functions = new ArrayList<>();
    Function function = new Function(FUNCTION_NAME, testUri);
    functions.add(function);
    HttpResponse response = new HttpResponse();
    response.setResponseCode(200);
    Event event = Event.API_CREATION;
    ZonedDateTime eventTime = ZonedDateTime.now();
    Mockito.when(functionDAO.getUserFunctionsForEvent(USER_NAME, event)).thenReturn(functions);
    Mockito.when(restCallUtil.postRequest(Mockito.eq(function.getEndpointURI()), Mockito.eq(null), Mockito.eq(null), Mockito.any(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(Collections.emptyMap()))).thenReturn(response);
    functionTrigger.captureEvent(event, USER_NAME, eventTime, new HashMap<>());
    // When the event parameter is null
    try {
        functionTrigger.captureEvent(null, USER_NAME, eventTime, new HashMap<>());
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Event must not be null");
    }
    // When the username parameter is null
    try {
        functionTrigger.captureEvent(event, null, eventTime, new HashMap<>());
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Username must not be null");
    }
    // When the eventTime parameter is null
    try {
        functionTrigger.captureEvent(event, USER_NAME, null, new HashMap<>());
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Event_time must not be null");
    }
    // When the metadata parameter is null
    try {
        functionTrigger.captureEvent(event, USER_NAME, eventTime, null);
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Payload must not be null");
    }
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) ZonedDateTime(java.time.ZonedDateTime) RestCallUtil(org.wso2.carbon.apimgt.core.api.RestCallUtil) ArrayList(java.util.ArrayList) HttpResponse(org.wso2.carbon.apimgt.core.models.HttpResponse) Event(org.wso2.carbon.apimgt.core.models.Event) URI(java.net.URI) FunctionDAO(org.wso2.carbon.apimgt.core.dao.FunctionDAO) Test(org.testng.annotations.Test)

Example 49 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project carbon-apimgt by wso2.

the class RestApiUtil method getErrorDTO.

/**
 * Return errorDTO object. This method accept APIMGTException as a parameter so we can set the e.getMessage
 * directly to the errorDTO.
 *
 * @param errorHandler Error Handler object.
 * @param paramList    Parameter list
 * @param e            APIMGTException object.
 * @return ErrorDTO Object.
 */
public static ErrorDTO getErrorDTO(ErrorHandler errorHandler, HashMap<String, String> paramList, APIManagementException e) {
    ErrorDTO errorDTO = new ErrorDTO();
    errorDTO.setCode(errorHandler.getErrorCode());
    errorDTO.setMoreInfo(paramList);
    if (e.getMessage() == null) {
        errorDTO.setMessage(errorHandler.getErrorMessage());
    } else {
        errorDTO.setMessage(e.getMessage());
    }
    errorDTO.setDescription(errorHandler.getErrorDescription());
    return errorDTO;
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 50 with Parameter

use of org.wso2.siddhi.annotation.Parameter in project carbon-apimgt by wso2.

the class ThrottleStreamProcessor method init.

@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition, ExpressionExecutor[] expressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    if (attributeExpressionExecutors.length == 1) {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("Throttle batch window's 1st parameter attribute should be " + "either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("Throttle batch window 1st parameter needs to be constant " + "parameter attribute but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
    } else if (attributeExpressionExecutors.length == 2) {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("Throttle batch window's 1st parameter attribute should be " + "either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("Throttle batch window 1st parameter needs to be constant " + "attribute but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
            startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else {
            throw new SiddhiAppValidationException("Throttle batch window 2nd parameter needs to be a Long " + "or Int type but found a " + attributeExpressionExecutors[2].getReturnType());
        }
    } else {
        throw new SiddhiAppValidationException("Throttle batch window should only have one/two parameter " + "(<int|long|time> windowTime (and <int|long> startTime), but found " + attributeExpressionExecutors.length + " input attributes");
    }
    List<Attribute> attributeList = new ArrayList<Attribute>();
    attributeList.add(new Attribute(EXPIRY_TIME_STAMP, Attribute.Type.LONG));
    return attributeList;
}
Also used : Attribute(org.wso2.siddhi.query.api.definition.Attribute) ArrayList(java.util.ArrayList) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

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