use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project servicecomb-java-chassis by apache.
the class GateRestTemplate method getUrlPrefix.
private String getUrlPrefix(String gateName, String producerName, String schemaId) {
MicroserviceVersionRule microserviceVersionRule = DiscoveryManager.INSTANCE.getAppManager().getOrCreateMicroserviceVersionRule(RegistrationManager.INSTANCE.getMicroservice().getAppId(), gateName, DefinitionConst.VERSION_RULE_ALL);
MicroserviceInstance microserviceInstance = microserviceVersionRule.getInstances().values().stream().findFirst().get();
URIEndpointObject edgeAddress = new URIEndpointObject(microserviceInstance.getEndpoints().get(0));
String urlSchema = "http";
if (edgeAddress.isSslEnabled()) {
urlSchema = "https";
}
MicroserviceReferenceConfig microserviceReferenceConfig = SCBEngine.getInstance().createMicroserviceReferenceConfig(producerName);
MicroserviceMeta microserviceMeta = microserviceReferenceConfig.getLatestMicroserviceMeta();
SchemaMeta schemaMeta = microserviceMeta.ensureFindSchemaMeta(schemaId);
return String.format("%s://%s:%d/rest/%s%s", urlSchema, edgeAddress.getHostOrIp(), edgeAddress.getPort(), producerName, schemaMeta.getSwagger().getBasePath());
}
use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.
the class VertxHttpMethod method createRequestPath.
protected String createRequestPath(Invocation invocation, RestOperationMeta swaggerRestOperation) throws Exception {
URIEndpointObject address = (URIEndpointObject) invocation.getEndpoint().getAddress();
String urlPrefix = address.getFirst(Const.URL_PREFIX);
String path = (String) invocation.getHandlerContext().get(RestConst.REST_CLIENT_REQUEST_PATH);
if (path == null) {
path = swaggerRestOperation.getPathBuilder().createRequestPath(invocation.getArgs());
}
if (StringUtils.isEmpty(urlPrefix) || path.startsWith(urlPrefix)) {
return path;
}
return urlPrefix + path;
}
use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.
the class VertxHttpMethod method createRequest.
HttpClientRequest createRequest(HttpClient client, Invocation invocation, IpPort ipPort, String path, AsyncResponse asyncResp) {
URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress();
RequestOptions requestOptions = new RequestOptions();
requestOptions.setHost(ipPort.getHostOrIp()).setPort(ipPort.getPort()).setSsl(endpoint.isSslEnabled()).setURI(path);
HttpMethod method = getMethod(invocation);
LOGGER.debug("Sending request by rest, method={}, qualifiedName={}, path={}, endpoint={}.", method, invocation.getMicroserviceQualifiedName(), path, invocation.getEndpoint().getEndpoint());
HttpClientRequest request = client.request(method, requestOptions, response -> {
handleResponse(invocation, response, asyncResp);
});
return request;
}
use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.
the class TestVertxHttpMethod method testCreateRequest.
@Test
public void testCreateRequest() {
HttpClient client = mock(HttpClient.class);
Invocation invocation = mock(Invocation.class);
OperationMeta operationMeta = mock(OperationMeta.class);
Endpoint endpoint = mock(Endpoint.class);
URIEndpointObject address = mock(URIEndpointObject.class);
when(invocation.getEndpoint()).thenReturn(endpoint);
when(endpoint.getAddress()).thenReturn(address);
when(address.isSslEnabled()).thenReturn(false);
when(invocation.getOperationMeta()).thenReturn(operationMeta);
RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
IpPort ipPort = mock(IpPort.class);
when(ipPort.getPort()).thenReturn(10);
when(ipPort.getHostOrIp()).thenReturn("ever");
AsyncResponse asyncResp = mock(AsyncResponse.class);
List<HttpMethod> methods = new ArrayList<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PATCH));
for (HttpMethod method : methods) {
when(swaggerRestOperation.getHttpMethod()).thenReturn(method.toString());
HttpClientRequest obj = VertxHttpMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", asyncResp);
Assert.assertNull(obj);
}
}
use of org.apache.servicecomb.foundation.common.net.URIEndpointObject in project incubator-servicecomb-java-chassis by apache.
the class TestVertxHttpMethod method testDoMethod.
@Test
public void testDoMethod(@Mocked HttpClient httpClient, @Injectable URIEndpointObject address) throws Exception {
Context context = new MockUp<Context>() {
@Mock
public void runOnContext(Handler<Void> action) {
action.handle(null);
}
}.getMockInstance();
HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
Invocation invocation = mock(Invocation.class);
AsyncResponse asyncResp = mock(AsyncResponse.class);
OperationMeta operationMeta = mock(OperationMeta.class);
RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
Endpoint endpoint = mock(Endpoint.class);
when(invocation.getOperationMeta()).thenReturn(operationMeta);
URLPathBuilder urlPathBuilder = mock(URLPathBuilder.class);
when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
when(invocation.getEndpoint()).thenReturn(endpoint);
when(endpoint.getAddress()).thenReturn(address);
when(request.exceptionHandler(Mockito.any())).then(answer -> null);
Map<String, Object> map = new HashMap<>();
when(invocation.getHandlerContext()).then(answer -> map);
;
this.doMethod(httpClientWithContext, invocation, asyncResp);
Assert.assertTrue(true);
}
Aggregations