Search in sources :

Example 1 with ServicePathManager

use of io.servicecomb.common.rest.locator.ServicePathManager in project java-chassis by ServiceComb.

the class AbstractRestServer method findRestOperation.

protected RestOperationMeta findRestOperation(RestServerRequestInternal restRequest) {
    String selfName = RegistryUtils.getMicroservice().getServiceName();
    MicroserviceMeta selfMicroserviceMeta = CseContext.getInstance().getMicroserviceMetaManager().ensureFindValue(selfName);
    ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(selfMicroserviceMeta);
    if (servicePathManager == null) {
        LOGGER.error("No schema in microservice");
        throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase());
    }
    OperationLocator locator = servicePathManager.locateOperation(restRequest.getPath(), restRequest.getMethod());
    restRequest.setPathParamMap(locator.getPathVarMap());
    return locator.getOperation();
}
Also used : OperationLocator(io.servicecomb.common.rest.locator.OperationLocator) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 2 with ServicePathManager

use of io.servicecomb.common.rest.locator.ServicePathManager in project java-chassis by ServiceComb.

the class CseClientHttpRequest method createRequestMeta.

private RequestMeta createRequestMeta(String httpMetod, URI uri) {
    String microserviceName = uri.getAuthority();
    ReferenceConfig referenceConfig = CseContext.getInstance().getConsumerProviderManager().getReferenceConfig(microserviceName);
    MicroserviceMeta microserviceMeta = referenceConfig.getMicroserviceMeta();
    ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta);
    if (servicePathManager == null) {
        throw new Error(String.format("no schema defined for %s:%s", microserviceMeta.getAppId(), microserviceMeta.getName()));
    }
    OperationLocator locator = servicePathManager.locateOperation(uri.getPath(), httpMetod);
    RestOperationMeta swaggerRestOperation = locator.getOperation();
    Map<String, String> pathParams = locator.getPathVarMap();
    return new RequestMeta(referenceConfig, swaggerRestOperation, pathParams);
}
Also used : OperationLocator(io.servicecomb.common.rest.locator.OperationLocator) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) ReferenceConfig(io.servicecomb.core.provider.consumer.ReferenceConfig) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 3 with ServicePathManager

use of io.servicecomb.common.rest.locator.ServicePathManager in project java-chassis by ServiceComb.

the class MockUtil method mockRegisterManager.

public void mockRegisterManager() throws InstantiationException, IllegalAccessException {
    MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta("app:test");
    microserviceMeta.putExtData("RestServicePathManager", new ServicePathManager(microserviceMeta));
    ConsumerSchemaFactory consumerSchemaFactory = new MockUp<ConsumerSchemaFactory>() {

        @Mock
        public MicroserviceMeta getOrCreateConsumer(String microserviceName) {
            return microserviceMeta;
        }
    }.getMockInstance();
    CseContext.getInstance().setConsumerSchemaFactory(consumerSchemaFactory);
}
Also used : MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ConsumerSchemaFactory(io.servicecomb.core.definition.schema.ConsumerSchemaFactory) Mock(mockit.Mock) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Example 4 with ServicePathManager

use of io.servicecomb.common.rest.locator.ServicePathManager in project java-chassis by ServiceComb.

the class TestRestEngineSchemaListener method test.

@Test
public void test() {
    BeanUtils.setContext(Mockito.mock(ApplicationContext.class));
    MicroserviceMeta mm = new MicroserviceMeta("app:ms");
    List<SchemaMeta> smList = new ArrayList<>();
    SwaggerGenerator generator = new SwaggerGenerator(context, Impl.class);
    Swagger swagger = generator.generate();
    SchemaMeta sm1 = new SchemaMeta(swagger, mm, "sid1");
    smList.add(sm1);
    RestEngineSchemaListener listener = new RestEngineSchemaListener();
    SchemaMeta[] smArr = smList.toArray(new SchemaMeta[smList.size()]);
    listener.onSchemaLoaded(smArr);
    // 重复调用,不应该出异常
    listener.onSchemaLoaded(smArr);
    ServicePathManager spm = ServicePathManager.getServicePathManager(mm);
    Assert.assertEquals(mm, spm.getMicroserviceMeta());
    Assert.assertNotNull(spm.getStaticPathOperationMap().get("Impl/add/"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ArrayList(java.util.ArrayList) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Example 5 with ServicePathManager

use of io.servicecomb.common.rest.locator.ServicePathManager in project java-chassis by ServiceComb.

the class RestEngineSchemaListener method onSchemaLoaded.

@Override
public void onSchemaLoaded(SchemaMeta... schemaMetas) {
    // 此时相应的ServicePathManager可能正在被使用,为避免太高的复杂度,使用copy on write逻辑
    Map<String, ServicePathManager> mgrMap = new HashMap<>();
    for (SchemaMeta schemaMeta : schemaMetas) {
        MicroserviceMeta microserviceMeta = schemaMeta.getMicroserviceMeta();
        ServicePathManager mgr = findPathManager(mgrMap, microserviceMeta);
        if (mgr.isSchemaExists(schemaMeta.getSchemaId())) {
            LOGGER.info("on schema loaded, exists schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
            continue;
        }
        LOGGER.info("on schema loaded, new schema. {}:{}", schemaMeta.getMicroserviceName(), schemaMeta.getSchemaId());
        mgr.addSchema(schemaMeta.getSchemaId());
        for (OperationMeta operationMeta : schemaMeta.getOperations()) {
            RestOperationMeta restOperationMeta = new RestOperationMeta();
            restOperationMeta.init(operationMeta);
            operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
            mgr.addResource(restOperationMeta);
        }
    }
    for (ServicePathManager mgr : mgrMap.values()) {
        // 对具有动态path operation进行排序
        mgr.sortPath();
        mgr.printService();
        mgr.saveToMicroserviceMeta();
    }
}
Also used : HashMap(java.util.HashMap) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager)

Aggregations

ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)6 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)5 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 OperationLocator (io.servicecomb.common.rest.locator.OperationLocator)2 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)2 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 ConsumerSchemaFactory (io.servicecomb.core.definition.schema.ConsumerSchemaFactory)1 InvocationException (io.servicecomb.core.exception.InvocationException)1 ReferenceConfig (io.servicecomb.core.provider.consumer.ReferenceConfig)1 SwaggerGenerator (io.servicecomb.swagger.generator.core.SwaggerGenerator)1 Swagger (io.swagger.models.Swagger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Mock (mockit.Mock)1 Test (org.junit.Test)1 ApplicationContext (org.springframework.context.ApplicationContext)1