Search in sources :

Example 16 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class SingleInputStreamParser method parseInputStream.

/**
 * Parse single InputStream and return SingleStreamRuntime
 *
 * @param inputStream                 single input stream to be parsed
 * @param siddhiAppContext            query to be parsed
 * @param variableExpressionExecutors List to hold VariableExpressionExecutors to update after query parsing
 * @param streamDefinitionMap         Stream Definition Map
 * @param tableDefinitionMap          Table Definition Map
 * @param windowDefinitionMap         window definition map
 * @param aggregationDefinitionMap    aggregation definition map
 * @param tableMap                    Table Map
 * @param metaComplexEvent            MetaComplexEvent
 * @param processStreamReceiver       ProcessStreamReceiver
 * @param supportsBatchProcessing     supports batch processing
 * @param outputExpectsExpiredEvents  is output expects ExpiredEvents
 * @param queryName                   query name of single input stream belongs to.
 *
 * @return SingleStreamRuntime
 */
public static SingleStreamRuntime parseInputStream(SingleInputStream inputStream, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaComplexEvent metaComplexEvent, ProcessStreamReceiver processStreamReceiver, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
    Processor processor = null;
    EntryValveProcessor entryValveProcessor = null;
    boolean first = true;
    MetaStreamEvent metaStreamEvent;
    if (metaComplexEvent instanceof MetaStateEvent) {
        metaStreamEvent = new MetaStreamEvent();
        ((MetaStateEvent) metaComplexEvent).addEvent(metaStreamEvent);
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, metaStreamEvent);
    }
    // A window cannot be defined for a window stream
    if (!inputStream.getStreamHandlers().isEmpty() && windowDefinitionMap != null && windowDefinitionMap.containsKey(inputStream.getStreamId())) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            if (handler instanceof Window) {
                throw new OperationNotSupportedException("Cannot create " + ((Window) handler).getName() + " " + "window for the window stream " + inputStream.getStreamId());
            }
        }
    }
    if (!inputStream.getStreamHandlers().isEmpty()) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            Processor currentProcessor = generateProcessor(handler, metaComplexEvent, variableExpressionExecutors, siddhiAppContext, tableMap, supportsBatchProcessing, outputExpectsExpiredEvents, queryName);
            if (currentProcessor instanceof SchedulingProcessor) {
                if (entryValveProcessor == null) {
                    entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
                    if (first) {
                        processor = entryValveProcessor;
                        first = false;
                    } else {
                        processor.setToLast(entryValveProcessor);
                    }
                }
                Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
                ((SchedulingProcessor) currentProcessor).setScheduler(scheduler);
            }
            if (first) {
                processor = currentProcessor;
                first = false;
            } else {
                processor.setToLast(currentProcessor);
            }
        }
    }
    metaStreamEvent.initializeAfterWindowData();
    return new SingleStreamRuntime(processStreamReceiver, processor, metaComplexEvent);
}
Also used : Window(org.wso2.siddhi.query.api.execution.query.input.handler.Window) OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) FilterProcessor(org.wso2.siddhi.core.query.processor.filter.FilterProcessor) StreamFunctionProcessor(org.wso2.siddhi.core.query.processor.stream.function.StreamFunctionProcessor) Processor(org.wso2.siddhi.core.query.processor.Processor) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) StreamProcessor(org.wso2.siddhi.core.query.processor.stream.StreamProcessor) AbstractStreamProcessor(org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) Scheduler(org.wso2.siddhi.core.util.Scheduler) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamHandler(org.wso2.siddhi.query.api.execution.query.input.handler.StreamHandler) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent)

Example 17 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisValidateDefinitionPost.

/**
 * Validates a provided API definition
 *
 * @param type            API definition type (SWAGGER or WSDL)
 * @param fileInputStream file content stream
 * @param fileDetail      file details
 * @param url             URL of the definition
 * @param request         msf4j request
 * @return API definition validation information
 * @throws NotFoundException
 */
@Override
public Response apisValidateDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, Request request) throws NotFoundException {
    String errorMessage = "Error while validating the definition";
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (StringUtils.isBlank(type)) {
            type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
        }
        Response responseIfParamsInvalid = buildResponseIfParamsInvalid(type, fileInputStream, url);
        if (responseIfParamsInvalid != null) {
            return responseIfParamsInvalid;
        }
        if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
            if (log.isDebugEnabled()) {
                log.debug("Validating a swagger file.");
            }
            // TODO implement swagger validation
            return Response.noContent().build();
        } else {
            // WSDL type
            WSDLProcessor processor = null;
            WSDLInfo info = null;
            if (!StringUtils.isBlank(url)) {
                processor = WSDLProcessFactory.getInstance().getWSDLProcessor(url);
                info = processor.getWsdlInfo();
                if (log.isDebugEnabled()) {
                    log.debug("Successfully validated WSDL URL " + url);
                }
            } else {
                if (fileDetail.getFileName().endsWith(".zip")) {
                    WSDLArchiveInfo archiveInfo = apiPublisher.extractAndValidateWSDLArchive(fileInputStream);
                    info = archiveInfo.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL archive " + fileDetail.getFileName());
                    }
                } else if (fileDetail.getFileName().endsWith(".wsdl")) {
                    byte[] wsdlContent = IOUtils.toByteArray(fileInputStream);
                    processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
                    info = processor.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL file " + fileDetail.getFileName());
                    }
                } else {
                    String msg = "Unsupported extension type of file: " + fileDetail.getFileName();
                    log.error(msg);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
                    return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
                }
            }
            if (info != null) {
                APIDefinitionValidationResponseDTO responseDTO = MappingUtil.toWSDLValidationResponseDTO(info);
                return Response.ok(responseDTO).build();
            }
            APIDefinitionValidationResponseDTO responseDTO = new APIDefinitionValidationResponseDTO();
            responseDTO.isValid(false);
            return Response.ok().entity(responseDTO).build();
        }
    } catch (APIManagementException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (IOException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) WSDLInfo(org.wso2.carbon.apimgt.core.models.WSDLInfo) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) APIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDefinitionValidationResponseDTO)

Example 18 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project carbon-apimgt by wso2.

the class EmitOnStateChange method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor processor, StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {
    while (streamEventChunk.hasNext()) {
        StreamEvent event = streamEventChunk.next();
        Boolean currentThrottleState = (Boolean) isThrottledExpressionExecutor.execute(event);
        String key = (String) keyExpressionExecutor.execute(event);
        Boolean lastThrottleState = (Boolean) throttleStateMap.get(key);
        if (currentThrottleState.equals(lastThrottleState) && !currentThrottleState) {
            streamEventChunk.remove();
        } else {
            throttleStateMap.put(key, currentThrottleState);
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 19 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project carbon-apimgt by wso2.

the class APIPublisherImpl method extractAndValidateWSDLArchive.

@Override
public WSDLArchiveInfo extractAndValidateWSDLArchive(InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    String path = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
    String archivePath = path + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
    String extractedLocation = APIFileUtils.extractUploadedArchive(inputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, path);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
    }
    WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
    if (!processor.canProcess()) {
        throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
    }
    WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(path, APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME);
    archiveInfo.setWsdlInfo(processor.getWsdlInfo());
    return archiveInfo;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)

Example 20 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project carbon-apimgt by wso2.

the class APIPublisherImpl method addAPIFromWSDLFile.

@Override
public String addAPIFromWSDLFile(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
    byte[] wsdlContent;
    try {
        wsdlContent = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new APIMgtWSDLException("Error while converting input stream to byte array", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
    WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
    apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(processor.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
    if (!processor.canProcess()) {
        throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
    }
    String uuid = addAPI(apiBuilder);
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the API. uuid: " + uuid);
    }
    getApiDAO().addOrUpdateWSDL(uuid, wsdlContent, getUsername());
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the WSDL file to database. API uuid: " + uuid);
    }
    if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(processor.getWsdlInfo().getVersion())) {
        log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
    }
    return uuid;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) IOException(java.io.IOException)

Aggregations

StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)15 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)11 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)8 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)6 Processor (org.wso2.siddhi.core.query.processor.Processor)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)5 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)5 Test (org.testng.annotations.Test)4 API (org.wso2.carbon.apimgt.core.models.API)4 Label (org.wso2.carbon.apimgt.core.models.Label)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)4 SiddhiAppRuntimeException (org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)4 WindowProcessor (org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor)4 WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)3 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)3 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)3 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)3 SchedulingProcessor (org.wso2.siddhi.core.query.processor.SchedulingProcessor)3