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();
}
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);
}
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);
}
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/"));
}
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();
}
}
Aggregations