use of io.gravitee.gateway.api.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class DefaultEndpointLifecycleManagerTest method shouldStartEndpoint.
@Test
public void shouldStartEndpoint() throws Exception {
io.gravitee.definition.model.Endpoint endpoint = mock(io.gravitee.definition.model.endpoint.HttpEndpoint.class);
when(endpoint.getName()).thenReturn("endpoint");
when(endpoint.isBackup()).thenReturn(false);
when(endpoint.getType()).thenReturn(EndpointType.HTTP);
when(proxy.getEndpoints()).thenReturn(Collections.singleton(endpoint));
when(applicationContext.getBean(Connector.class, endpoint)).thenReturn(mock(Connector.class));
endpointLifecycleManager.start();
HttpEndpoint httpClientEndpoint = (HttpEndpoint) endpointLifecycleManager.get("endpoint");
assertNotNull(httpClientEndpoint);
verify(applicationContext, times(1)).getBean(eq(Connector.class), any(Endpoint.class));
verify(httpClientEndpoint.connector(), times(1)).start();
assertEquals(httpClientEndpoint, endpointLifecycleManager.get("endpoint"));
assertNull(endpointLifecycleManager.get("unknown"));
assertFalse(endpointLifecycleManager.endpoints().isEmpty());
}
use of io.gravitee.gateway.api.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class MultiTenantAwareEndpointLifecycleManagerTest method shouldStartEndpoint_inTenant.
@Test
public void shouldStartEndpoint_inTenant() throws Exception {
io.gravitee.definition.model.Endpoint endpoint = mock(io.gravitee.definition.model.endpoint.HttpEndpoint.class);
when(endpoint.getName()).thenReturn("endpoint");
when(endpoint.getTenants()).thenReturn(Collections.singletonList("europe"));
when(endpoint.isBackup()).thenReturn(false);
when(endpoint.getType()).thenReturn(EndpointType.HTTP);
when(proxy.getEndpoints()).thenReturn(Collections.singleton(endpoint));
when(applicationContext.getBean(Connector.class, endpoint)).thenReturn(mock(Connector.class));
endpointLifecycleManager.start();
HttpEndpoint httpClientEndpoint = (HttpEndpoint) endpointLifecycleManager.get("endpoint");
assertNotNull(httpClientEndpoint);
verify(applicationContext, times(1)).getBean(eq(Connector.class), any(Endpoint.class));
verify(httpClientEndpoint.connector(), times(1)).start();
assertEquals(httpClientEndpoint, endpointLifecycleManager.get("endpoint"));
assertNull(endpointLifecycleManager.get("unknown"));
assertFalse(endpointLifecycleManager.endpoints().isEmpty());
}
use of io.gravitee.gateway.api.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class DefaultInvoker method setProxyHeaders.
private HttpHeaders setProxyHeaders(HttpHeaders headers, URI requestUri, Endpoint endpoint) {
// Remove hop-by-hop headers.
for (String header : HOP_HEADERS) {
headers.remove(header);
}
// Get HOST header
final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT);
final String host = (port == DEFAULT_HTTP_PORT || port == DEFAULT_HTTPS_PORT) ? requestUri.getHost() : requestUri.getHost() + ':' + port;
headers.set(HttpHeaders.HOST, host);
// Override with default headers defined for endpoint
if (!endpoint.headers().isEmpty()) {
endpoint.headers().forEach(headers::put);
}
return headers;
}
use of io.gravitee.gateway.api.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class DefaultInvoker method selectUserDefinedEndpoint.
/**
* Select an endpoint according to the URI passed in the execution request attribute.
*/
private TargetEndpoint selectUserDefinedEndpoint(Request serverRequest, String target, ExecutionContext executionContext) {
QueryStringDecoder decoder = new QueryStringDecoder(target);
Map<String, List<String>> queryParameters = decoder.parameters();
// Merge query parameters from user target into incoming request query parameters
for (Map.Entry<String, List<String>> param : queryParameters.entrySet()) {
serverRequest.parameters().put(param.getKey(), param.getValue());
}
// Path segments must be encoded to avoid bad URI syntax
String[] segments = decoder.path().split(URI_PATH_SEPARATOR);
StringBuilder builder = new StringBuilder();
for (String pathSeg : segments) {
builder.append(UrlEscapers.urlPathSegmentEscaper().escape(pathSeg)).append(URI_PATH_SEPARATOR);
}
String encodedTarget = builder.substring(0, builder.length() - 1);
// Do we have a single path or a plain URI ?
if (encodedTarget.startsWith("/")) {
Endpoint endpoint = nextEndpoint(serverRequest, executionContext);
return new TargetEndpoint(endpoint, (endpoint != null) ? endpoint.target() + encodedTarget : null);
} else {
Endpoint endpoint = endpointManager.endpoints().stream().filter(endpointEntry -> encodedTarget.startsWith(endpointEntry.target())).findFirst().orElse(endpointManager.endpoints().iterator().next());
return new TargetEndpoint(endpoint, encodedTarget);
}
}
use of io.gravitee.gateway.api.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class RoundRobinLoadBalancer method nextEndpoint.
@Override
public Endpoint nextEndpoint(Request serverRequest, ExecutionContext executionContext) {
List<Endpoint> endpoints = endpoints();
int size = endpoints.size();
if (size == 0) {
return null;
}
return endpoints.get(Math.abs(counter.getAndIncrement() % size));
}
Aggregations