Search in sources :

Example 1 with ProducerContextArgumentMapperFactory

use of org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory in project java-chassis by ServiceComb.

the class SwaggerEnvironment method createProducer.

public SwaggerProducer createProducer(Object producerInstance, Class<?> schemaInterface, Swagger swagger) {
    Class<?> producerCls = targetSwaggerClass(producerInstance, schemaInterface);
    swagger = checkAndGenerateSwagger(producerCls, swagger);
    Map<Class<?>, ContextArgumentMapperFactory> contextFactories = SPIServiceUtils.getOrLoadSortedService(ProducerContextArgumentMapperFactory.class).stream().collect(Collectors.toMap(ProducerContextArgumentMapperFactory::getContextClass, Function.identity()));
    ResponseMapperFactorys<ProducerResponseMapper> producerResponseMapperFactorys = new ResponseMapperFactorys<>(ProducerResponseMapperFactory.class);
    SwaggerOperations swaggerOperations = new SwaggerOperations(swagger);
    Map<String, Method> visibleProducerMethods = MethodUtils.findSwaggerMethodsMapOfOperationId(producerCls);
    SwaggerProducer producer = new SwaggerProducer();
    producer.setSwagger(swagger);
    producer.setProducerCls(producerCls);
    producer.setProducerInstance(producerInstance);
    for (SwaggerOperation swaggerOperation : swaggerOperations.getOperations().values()) {
        String operationId = swaggerOperation.getOperationId();
        // producer参数不一定等于swagger参数
        Method producerMethod = visibleProducerMethods.getOrDefault(operationId, null);
        if (producerMethod == null) {
            // producer未实现契约,非法
            String msg = String.format("operationId %s not exist in producer %s.", operationId, producerInstance.getClass().getName());
            throw new IllegalStateException(msg);
        }
        ProducerArgumentsMapperCreator creator = new ProducerArgumentsMapperCreator(Json.mapper().getSerializationConfig(), contextFactories, producerCls, producerMethod, swaggerOperation);
        ProducerArgumentsMapper argsMapper = creator.createArgumentsMapper();
        ProducerResponseMapper responseMapper = producerResponseMapperFactorys.createResponseMapper(producerMethod.getGenericReturnType());
        SwaggerProducerOperation op = new SwaggerProducerOperation();
        op.setProducerClass(producerCls);
        op.setProducerInstance(producerInstance);
        op.setProducerMethod(producerMethod);
        op.setSwaggerOperation(swaggerOperation);
        op.setSwaggerParameterTypes(creator.getSwaggerParameterTypes());
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        producer.addOperation(op);
    }
    return producer;
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) ResponseMapperFactorys(org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys) ProducerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper) Method(java.lang.reflect.Method) ProducerArgumentsMapperCreator(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperCreator) ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ConsumerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory) ProducerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory) ContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)

Aggregations

Method (java.lang.reflect.Method)1 SwaggerOperation (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation)1 SwaggerOperations (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations)1 ContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)1 ConsumerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory)1 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)1 ProducerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperCreator)1 ProducerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory)1 ResponseMapperFactorys (org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys)1 ProducerResponseMapper (org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)1