Search in sources :

Example 1 with String8FW

use of io.aklivity.zilla.runtime.binding.http.internal.types.String8FW in project zilla by aklivity.

the class HttpClientFactory method newStream.

@Override
public MessageConsumer newStream(int msgTypeId, DirectBuffer buffer, int index, int length, MessageConsumer application) {
    final BeginFW begin = beginRO.wrap(buffer, index, index + length);
    final long routeId = begin.routeId();
    final long authorization = begin.authorization();
    final HttpBeginExFW beginEx = begin.extension().get(beginExRO::tryWrap);
    final HttpBindingConfig binding = bindings.get(routeId);
    HttpRouteConfig route = null;
    if (binding != null) {
        // TODO: avoid object creation
        final Map<String, String> headers = beginEx != null ? asHeadersMap(beginEx.headers()) : EMPTY_HEADERS;
        route = binding.resolve(authorization, headers::get);
    }
    MessageConsumer newStream = null;
    if (route != null) {
        final long resolvedId = route.id;
        final Map<String8FW, String16FW> overrides = binding.options != null && binding.options.overrides != null ? binding.options.overrides : EMPTY_OVERRIDES;
        final HttpClientPool clientPool = clientPools.computeIfAbsent(resolvedId, HttpClientPool::new);
        newStream = clientPool.newStream(application, begin, overrides);
    }
    return newStream;
}
Also used : MessageConsumer(io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer) BeginFW(io.aklivity.zilla.runtime.binding.http.internal.types.stream.BeginFW) HttpRouteConfig(io.aklivity.zilla.runtime.binding.http.internal.config.HttpRouteConfig) HttpBeginExFW(io.aklivity.zilla.runtime.binding.http.internal.types.stream.HttpBeginExFW) String8FW(io.aklivity.zilla.runtime.binding.http.internal.types.String8FW) HttpBindingConfig(io.aklivity.zilla.runtime.binding.http.internal.config.HttpBindingConfig) String16FW(io.aklivity.zilla.runtime.binding.http.internal.types.String16FW)

Example 2 with String8FW

use of io.aklivity.zilla.runtime.binding.http.internal.types.String8FW in project zilla by aklivity.

the class HttpOptionsConfigAdapterTest method shouldReadOptions.

@Test
public void shouldReadOptions() {
    String text = "{" + "\"versions\":" + "[" + "\"http/1.1\"," + "\"h2\"" + "]," + "\"access-control\":" + "{" + "\"policy\": \"cross-origin\"," + "\"allow\":" + "{" + "\"origins\": [ \"https://example.com:9090\" ]," + "\"methods\": [ \"DELETE\" ]," + "\"headers\": [ \"x-api-key\" ]," + "\"credentials\": true" + "}," + "\"max-age\": 10," + "\"expose\":" + "{" + "\"headers\": [ \"x-custom-header\" ]" + "}" + "}," + "\"overrides\":" + "{" + "\":authority\": \"example.com:443\"" + "}" + "}";
    HttpOptionsConfig options = jsonb.fromJson(text, HttpOptionsConfig.class);
    assertThat(options, not(nullValue()));
    assertThat(options.versions, equalTo(EnumSet.allOf(HttpVersion.class)));
    assertThat(options.access, not(nullValue()));
    assertThat(options.access.allow, not(nullValue()));
    assertThat(options.access.allow.origins, equalTo(singleton("https://example.com:9090")));
    assertThat(options.access.allow.methods, equalTo(singleton("DELETE")));
    assertThat(options.access.allow.headers, equalTo(singleton("x-api-key")));
    assertTrue(options.access.allow.credentials);
    assertThat(options.access.maxAge, equalTo(Duration.ofSeconds(10)));
    assertThat(options.access.expose, not(nullValue()));
    assertThat(options.access.expose.headers, equalTo(singleton("x-custom-header")));
    assertThat(options.overrides, equalTo(singletonMap(new String8FW(":authority"), new String16FW("example.com:443"))));
}
Also used : String8FW(io.aklivity.zilla.runtime.binding.http.internal.types.String8FW) String16FW(io.aklivity.zilla.runtime.binding.http.internal.types.String16FW) Test(org.junit.Test)

Example 3 with String8FW

use of io.aklivity.zilla.runtime.binding.http.internal.types.String8FW in project zilla by aklivity.

the class HttpOptionsConfigAdapter method adaptFromJson.

@Override
public OptionsConfig adaptFromJson(JsonObject object) {
    JsonArray versions = object.containsKey(VERSIONS_NAME) ? object.getJsonArray(VERSIONS_NAME) : null;
    SortedSet<HttpVersion> newVersions = null;
    if (versions != null) {
        SortedSet<HttpVersion> newVersions0 = new TreeSet<HttpVersion>();
        versions.forEach(v -> newVersions0.add(HttpVersion.of(JsonString.class.cast(v).getString())));
        newVersions = newVersions0;
    }
    HttpAccessControlConfig newAccess = null;
    JsonObject access = object.containsKey(ACCESS_CONTROL_NAME) ? object.getJsonObject(ACCESS_CONTROL_NAME) : null;
    if (access != null) {
        String policy = access.containsKey(POLICY_NAME) ? access.getString(POLICY_NAME) : null;
        switch(policy) {
            case POLICY_VALUE_SAME_ORIGIN:
                newAccess = new HttpAccessControlConfig(SAME_ORIGIN);
                break;
            case POLICY_VALUE_CROSS_ORIGIN:
                JsonObject allow = access.containsKey(ALLOW_NAME) ? access.getJsonObject(ALLOW_NAME) : null;
                HttpAllowConfig newAllow = null;
                if (allow != null) {
                    JsonArray origins = allow.containsKey(ALLOW_ORIGINS_NAME) ? allow.getJsonArray(ALLOW_ORIGINS_NAME) : null;
                    Set<String> newOrigins = null;
                    if (origins != null) {
                        Set<String> newOrigins0 = new LinkedHashSet<>();
                        origins.forEach(v -> newOrigins0.add(JsonString.class.cast(v).getString()));
                        newOrigins = newOrigins0;
                    }
                    JsonArray methods = allow.containsKey(ALLOW_METHODS_NAME) ? allow.getJsonArray(ALLOW_METHODS_NAME) : null;
                    Set<String> newMethods = null;
                    if (methods != null) {
                        Set<String> newMethods0 = new LinkedHashSet<>();
                        methods.forEach(v -> newMethods0.add(JsonString.class.cast(v).getString()));
                        newMethods = newMethods0;
                    }
                    JsonArray headers = allow.containsKey(ALLOW_HEADERS_NAME) ? allow.getJsonArray(ALLOW_HEADERS_NAME) : null;
                    Set<String> newHeaders = null;
                    if (headers != null) {
                        Set<String> newHeaders0 = new LinkedHashSet<>();
                        headers.forEach(v -> newHeaders0.add(JsonString.class.cast(v).getString()));
                        newHeaders = newHeaders0;
                    }
                    boolean newCredentials = false;
                    if (allow.containsKey(ALLOW_CREDENTIALS_NAME)) {
                        newCredentials = allow.getBoolean(ALLOW_CREDENTIALS_NAME);
                    }
                    newAllow = new HttpAllowConfig(newOrigins, newMethods, newHeaders, newCredentials);
                }
                Duration newMaxAge = null;
                JsonNumber maxAge = access.containsKey(MAX_AGE_NAME) ? access.getJsonNumber(MAX_AGE_NAME) : null;
                if (maxAge != null) {
                    newMaxAge = Duration.ofSeconds(maxAge.longValue());
                }
                HttpExposeConfig newExpose = null;
                JsonObject expose = access.containsKey(EXPOSE_NAME) ? access.getJsonObject(EXPOSE_NAME) : null;
                if (expose != null) {
                    JsonArray headers = expose.containsKey(ALLOW_HEADERS_NAME) ? expose.getJsonArray(ALLOW_HEADERS_NAME) : null;
                    Set<String> newHeaders = null;
                    if (headers != null) {
                        Set<String> newHeaders0 = new LinkedHashSet<>();
                        headers.forEach(v -> newHeaders0.add(JsonString.class.cast(v).getString()));
                        newHeaders = newHeaders0;
                    }
                    newExpose = new HttpExposeConfig(newHeaders);
                }
                newAccess = new HttpAccessControlConfig(CROSS_ORIGIN, newAllow, newMaxAge, newExpose);
                break;
        }
    }
    JsonObject overrides = object.containsKey(OVERRIDES_NAME) ? object.getJsonObject(OVERRIDES_NAME) : null;
    Map<String8FW, String16FW> newOverrides = null;
    if (overrides != null) {
        Map<String8FW, String16FW> newOverrides0 = new LinkedHashMap<>();
        overrides.forEach((k, v) -> newOverrides0.put(new String8FW(k), new String16FW(JsonString.class.cast(v).getString())));
        newOverrides = newOverrides0;
    }
    return new HttpOptionsConfig(newVersions, newOverrides, newAccess);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpExposeConfig(io.aklivity.zilla.runtime.binding.http.internal.config.HttpAccessControlConfig.HttpExposeConfig) HttpAllowConfig(io.aklivity.zilla.runtime.binding.http.internal.config.HttpAccessControlConfig.HttpAllowConfig) JsonObject(jakarta.json.JsonObject) Duration(java.time.Duration) JsonString(jakarta.json.JsonString) LinkedHashMap(java.util.LinkedHashMap) JsonArray(jakarta.json.JsonArray) String8FW(io.aklivity.zilla.runtime.binding.http.internal.types.String8FW) TreeSet(java.util.TreeSet) JsonNumber(jakarta.json.JsonNumber) String16FW(io.aklivity.zilla.runtime.binding.http.internal.types.String16FW) JsonString(jakarta.json.JsonString)

Aggregations

String16FW (io.aklivity.zilla.runtime.binding.http.internal.types.String16FW)3 String8FW (io.aklivity.zilla.runtime.binding.http.internal.types.String8FW)3 HttpAllowConfig (io.aklivity.zilla.runtime.binding.http.internal.config.HttpAccessControlConfig.HttpAllowConfig)1 HttpExposeConfig (io.aklivity.zilla.runtime.binding.http.internal.config.HttpAccessControlConfig.HttpExposeConfig)1 HttpBindingConfig (io.aklivity.zilla.runtime.binding.http.internal.config.HttpBindingConfig)1 HttpRouteConfig (io.aklivity.zilla.runtime.binding.http.internal.config.HttpRouteConfig)1 BeginFW (io.aklivity.zilla.runtime.binding.http.internal.types.stream.BeginFW)1 HttpBeginExFW (io.aklivity.zilla.runtime.binding.http.internal.types.stream.HttpBeginExFW)1 MessageConsumer (io.aklivity.zilla.runtime.engine.binding.function.MessageConsumer)1 JsonArray (jakarta.json.JsonArray)1 JsonNumber (jakarta.json.JsonNumber)1 JsonObject (jakarta.json.JsonObject)1 JsonString (jakarta.json.JsonString)1 Duration (java.time.Duration)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1