Search in sources :

Example 81 with Parameter

use of org.wso2.carbon.apimgt.api.doc.model.Parameter in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method buildResponseIfParamsInvalid.

/**
 * Validate API deefinition import/validate parameters
 *
 * @param type            API definition type (SWAGGER or WSDL)
 * @param fileInputStream file content stream
 * @param url             URL of the definition
 * @return Response if any parameter is invalid. Otherwise returns null.
 */
private Response buildResponseIfParamsInvalid(String type, InputStream fileInputStream, String url) {
    final String SWAGGER = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
    final String WSDL = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.WSDL.toString();
    if (!SWAGGER.equals(type) && !WSDL.equals(type)) {
        String errorMessage = "Unsupported definition type. Only SWAGGER or WSDL is allowed";
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.UNSUPPORTED_API_DEFINITION_TYPE);
        log.error(errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    if (url == null && fileInputStream == null) {
        String msg = "Either 'file' or 'url' should be specified";
        log.error(msg);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    if (fileInputStream != null && url != null) {
        String msg = "Only one of 'file' and 'url' should be specified";
        log.error(msg);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    return null;
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 82 with Parameter

use of org.wso2.carbon.apimgt.api.doc.model.Parameter in project siddhi by wso2.

the class ConvertFunctionExecutor method init.

@Override
public void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    if (attributeExpressionExecutors.length != 2) {
        throw new SiddhiAppValidationException("convert() must have at 2 parameters, attribute and to be " + "converted type");
    }
    inputType = attributeExpressionExecutors[0].getReturnType();
    if (inputType == Attribute.Type.OBJECT) {
        throw new SiddhiAppValidationException("1st parameter of convert() cannot be 'object' as " + "it's not supported, it has to be either of (STRING, " + "INT, LONG, FLOAT, DOUBLE, BOOL), but found " + attributeExpressionExecutors[0].getReturnType());
    }
    if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
        throw new SiddhiAppValidationException("2nd parameter of convert() must be 'string' have constant " + "value either of (STRING, INT, LONG, FLOAT, DOUBLE, " + "BOOL), but found " + attributeExpressionExecutors[0].getReturnType());
    }
    if (!(attributeExpressionExecutors[1] instanceof ConstantExpressionExecutor)) {
        throw new SiddhiAppValidationException("2nd parameter of convert() must have constant value either " + "of (STRING, INT, LONG, FLOAT, DOUBLE, BOOL), but found " + "a variable expression");
    }
    String type = (String) attributeExpressionExecutors[1].execute(null);
    if (Attribute.Type.STRING.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.STRING;
    } else if (Attribute.Type.BOOL.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.BOOL;
    } else if (Attribute.Type.DOUBLE.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.DOUBLE;
    } else if (Attribute.Type.FLOAT.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.FLOAT;
    } else if (Attribute.Type.INT.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.INT;
    } else if (Attribute.Type.LONG.toString().equalsIgnoreCase(type)) {
        returnType = Attribute.Type.LONG;
    } else {
        throw new SiddhiAppValidationException("2nd parameter of convert() must have value either of " + "(STRING, INT, LONG, FLOAT, DOUBLE, BOOL), but found '" + type + "'");
    }
}
Also used : SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 83 with Parameter

use of org.wso2.carbon.apimgt.api.doc.model.Parameter in project siddhi by wso2.

the class ExternalTimeBatchWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
    if (outputExpectsExpiredEvents) {
        this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
        this.storeExpiredEvents = true;
    }
    if (attributeExpressionExecutors.length >= 2 && attributeExpressionExecutors.length <= 5) {
        if (!(attributeExpressionExecutors[0] instanceof VariableExpressionExecutor)) {
            throw new SiddhiAppValidationException("ExternalTime window's 1st parameter timestamp should be a" + " variable, but found " + attributeExpressionExecutors[0].getClass());
        }
        if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG) {
            throw new SiddhiAppValidationException("ExternalTime window's 1st parameter timestamp should be " + "type long, but found " + attributeExpressionExecutors[0].getReturnType());
        }
        timestampExpressionExecutor = (VariableExpressionExecutor) attributeExpressionExecutors[0];
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            timeToKeep = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
            timeToKeep = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        } else {
            throw new SiddhiAppValidationException("ExternalTimeBatch window's 2nd parameter windowTime " + "should be either int or long, but found " + attributeExpressionExecutors[1].getReturnType());
        }
        if (attributeExpressionExecutors.length >= 3) {
            isStartTimeEnabled = true;
            if ((attributeExpressionExecutors[2] instanceof ConstantExpressionExecutor)) {
                if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.INT) {
                    startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
                } else if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.LONG) {
                    startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
                } else {
                    throw new SiddhiAppValidationException("ExternalTimeBatch window's 3rd parameter " + "startTime should either be a constant (of type int or long) or an attribute (of type" + " long), but found " + attributeExpressionExecutors[2].getReturnType());
                }
            } else if (attributeExpressionExecutors[2].getReturnType() != Attribute.Type.LONG) {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 3rd parameter startTime " + "should either be a constant (of type int or long) or an attribute (of type long), but " + "found " + attributeExpressionExecutors[2].getReturnType());
            } else {
                startTimeAsVariable = attributeExpressionExecutors[2];
            }
        }
        if (attributeExpressionExecutors.length >= 4) {
            if (attributeExpressionExecutors[3].getReturnType() == Attribute.Type.INT) {
                schedulerTimeout = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[3]).getValue()));
            } else if (attributeExpressionExecutors[3].getReturnType() == Attribute.Type.LONG) {
                schedulerTimeout = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[3]).getValue()));
            } else {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 4th parameter timeout " + "should be either int or long, but found " + attributeExpressionExecutors[3].getReturnType());
            }
        }
        if (attributeExpressionExecutors.length == 5) {
            if (attributeExpressionExecutors[4].getReturnType() == Attribute.Type.BOOL) {
                replaceTimestampWithBatchEndTime = Boolean.parseBoolean(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[4]).getValue()));
            } else {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 5th parameter " + "replaceTimestampWithBatchEndTime should be bool, but found " + attributeExpressionExecutors[4].getReturnType());
            }
        }
    } else {
        throw new SiddhiAppValidationException("ExternalTimeBatch window should only have two to five " + "parameters (<long> timestamp, <int|long|time> windowTime, <long> startTime, <int|long|time> " + "timeout, <bool> replaceTimestampWithBatchEndTime), but found " + attributeExpressionExecutors.length + " input attributes");
    }
    if (schedulerTimeout > 0) {
        if (expiredEventChunk == null) {
            this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
        }
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 84 with Parameter

use of org.wso2.carbon.apimgt.api.doc.model.Parameter in project siddhi by wso2.

the class SortWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
        lengthToKeep = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue()));
    } else {
        throw new UnsupportedOperationException("The first parameter should be an integer");
    }
    parameterInfo = new ArrayList<Object[]>();
    eventComparator = new EventComparator();
    for (int i = 1, parametersLength = attributeExpressionExecutors.length; i < parametersLength; i++) {
        if (!(attributeExpressionExecutors[i] instanceof VariableExpressionExecutor)) {
            throw new UnsupportedOperationException("Required a variable, but found a string parameter");
        } else {
            ExpressionExecutor variableExpressionExecutor = attributeExpressionExecutors[i];
            int order;
            String nextParameter;
            if (i + 1 < parametersLength && attributeExpressionExecutors[i + 1].getReturnType() == Attribute.Type.STRING) {
                nextParameter = (String) ((ConstantExpressionExecutor) attributeExpressionExecutors[i + 1]).getValue();
                if (nextParameter.equalsIgnoreCase(DESC)) {
                    order = -1;
                    i++;
                } else if (nextParameter.equalsIgnoreCase(ASC)) {
                    order = 1;
                    i++;
                } else {
                    throw new UnsupportedOperationException("Parameter string literals should only be \"asc\" or " + "\"desc\"");
                }
            } else {
                // assigning the default order: "asc"
                order = 1;
            }
            parameterInfo.add(new Object[] { variableExpressionExecutor, order });
        }
    }
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 85 with Parameter

use of org.wso2.carbon.apimgt.api.doc.model.Parameter in project charon by wso2.

the class MeResourceManager method updateWithPUT.

@Override
public SCIMResponse updateWithPUT(String userName, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    // needs to validate the incoming object. eg: id can not be set by the consumer.
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder.
        decoder = getDecoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getUserResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // decode the SCIM User object, encoded in the submitted payload.
        User user = (User) decoder.decodeResource(scimObjectString, schema, new User());
        User updatedUser = null;
        if (userManager != null) {
            // retrieve the old object
            User oldUser = userManager.getMe(userName, ResourceManagerUtil.getAllAttributeURIs(schema));
            if (oldUser != null) {
                User validatedUser = (User) ServerSideValidator.validateUpdatedSCIMObject(oldUser, user, schema);
                updatedUser = userManager.updateMe(validatedUser, requiredAttributes);
            } else {
                String error = "No user exists with the given userName: " + userName;
                throw new NotFoundException(error);
            }
        } else {
            String error = "Provided user manager handler is null.";
            throw new InternalErrorException(error);
        }
        // encode the newly created SCIM user object and add id attribute to Location header.
        String encodedUser;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (updatedUser != null) {
            // create a deep copy of the user object since we are going to change it.
            User copiedUser = (User) CopyUtil.deepCopy(updatedUser);
            // need to remove password before returning
            ServerSideValidator.validateReturnedAttributes(copiedUser, attributes, excludeAttributes);
            encodedUser = encoder.encodeSCIMObject(copiedUser);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + updatedUser.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Updated User resource is null.";
            throw new CharonException(error);
        }
        // put the uri of the User object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedUser, httpHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : User(org.wso2.charon3.core.objects.User) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Aggregations

HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)32 Parameter (org.apache.axis2.description.Parameter)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)13 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 List (java.util.List)10 Map (java.util.Map)10 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)9 IOException (java.io.IOException)8 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 Test (org.testng.annotations.Test)8 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)8