diff --git a/common/http/src/main/java/io/helidon/common/http/ContextualRegistry.java b/common/http/src/main/java/io/helidon/common/http/ContextualRegistry.java deleted file mode 100644 index 42596c3efd4..00000000000 --- a/common/http/src/main/java/io/helidon/common/http/ContextualRegistry.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.common.http; - -import io.helidon.common.context.Context; - -/** - * A registry for context objects. Enables instance localization between several services / components / ... integrated in - * a particular known scope. ContextualRegistry instance is intended to be associated with a scope aware object such as - * WebServer, ServerRequest or ClientRequest. - * - *

Context contains also a notion of classifiers. Classifier is any object defining additional key for registered - * objects. To obtain such registered object, the same classifier (precisely, any equal object) has to be used. - * - *

Classifiers can be used as follows:

    - *
  1. As an additional identifier for registered objects of common types, like a {@link String}, ...
    - *
    {@code
    - * // User detail provider service
    - * registry.register("NAME_PARAM_ID", "Smith");
    - * registry.register("GENDER_PARAM_ID", "male");
    - * ...
    - * // User consumer service
    - * String name = registry.get("name", String.class);
    - * }
  2. - *
  3. As an access control mechanism where only owners of the classifier can retrieve such contextual instance.
    - *
    {@code
    - * // In some central security service.
    - * registry.register(securityFrameworkInternalInstance, new AuthenticatedInternalIdentity(...));
    - * ...
    - * // In some authorization filter known by a central security service
    - * AuthenticatedInternalIdentity auth = registry.get(securityFrameworkInternalInstance, AuthenticatedInternalIdentity.class);
    - * }
  4. - *
- * - * @deprecated This class will be replaced with {@link io.helidon.common.context.Context} in future Helidon versions - */ -@Deprecated -public interface ContextualRegistry extends Context { - - /** - * Creates a new empty instance. - * - * @return new instance - * @deprecated use {@link io.helidon.common.context.Context#create()} - */ - @Deprecated - static ContextualRegistry create() { - return builder().build(); - } - - /** - * Creates a new empty instance backed by its parent read-through {@link ContextualRegistry}. - * - *

Parent {@code registry} is used only for get methods and only if this registry doesn't have registered required type. - * - * @param parent a parent registry - * @return new instance - * @deprecated use {@link io.helidon.common.context.Context#create(io.helidon.common.context.Context)} - */ - @Deprecated - static ContextualRegistry create(Context parent) { - return builder().parent(parent).build(); - } - - /** - * Fluent API builder for advanced configuration. - * - * @return a new builder - * @deprecated used for backward compatibility only - */ - @Deprecated - static Builder builder() { - return new Builder(); - } - - /** - * Fluent API builder for {@link io.helidon.common.http.ContextualRegistry}. - */ - class Builder implements io.helidon.common.Builder { - private Context parent; - private String id; - - @Override - public ContextualRegistry build() { - return new ListContextualRegistry(this); - } - - /** - * Parent of the new context. - * @param parent parent context - * - * @return updated builder instance - */ - public Builder parent(Context parent) { - this.parent = parent; - return this; - } - - /** - * Identification of the new context, should be unique within this runtime. - * - * @param id context identification - * @return updated builder instance - */ - public Builder id(String id) { - this.id = id; - return this; - } - - Context parent() { - return parent; - } - - String id() { - return id; - } - } -} diff --git a/common/http/src/main/java/io/helidon/common/http/ListContextualRegistry.java b/common/http/src/main/java/io/helidon/common/http/ListContextualRegistry.java deleted file mode 100644 index b94277ebf0b..00000000000 --- a/common/http/src/main/java/io/helidon/common/http/ListContextualRegistry.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.common.http; - -import java.util.Optional; -import java.util.function.Supplier; - -import io.helidon.common.context.Context; - -/** - * A {@link ContextualRegistry} implementation with deque registry. - */ -class ListContextualRegistry implements ContextualRegistry { - private final Context delegate; - - ListContextualRegistry(ContextualRegistry.Builder builder) { - String configuredId = builder.id(); - Context parent = builder.parent(); - - Context.Builder delegateBuilder = Context.builder(); - - if (null != parent) { - if (parent instanceof ListContextualRegistry) { - delegateBuilder.parent(((ListContextualRegistry) parent).delegate); - } else { - delegateBuilder.parent(parent); - } - } - - if (null != configuredId) { - delegateBuilder.id(configuredId); - } - - this.delegate = delegateBuilder.build(); - } - - @Override - public String id() { - return delegate.id(); - } - - @Override - public void register(T instance) { - delegate.register(instance); - } - - @Override - public void supply(Class type, Supplier supplier) { - delegate.supply(type, supplier); - } - - @Override - public Optional get(Class type) { - return delegate.get(type); - } - - @Override - public void register(Object classifier, T instance) { - delegate.register(classifier, instance); - } - - @Override - public void supply(Object classifier, Class type, Supplier supplier) { - delegate.supply(classifier, type, supplier); - } - - @Override - public Optional get(Object classifier, Class type) { - return delegate.get(classifier, type); - } -} diff --git a/common/http/src/test/java/io/helidon/common/http/ListContextTest.java b/common/http/src/test/java/io/helidon/common/http/ListContextTest.java deleted file mode 100644 index 39af8ed1c0b..00000000000 --- a/common/http/src/test/java/io/helidon/common/http/ListContextTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.common.http; - -import java.util.Date; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; - -/** - * Tests {@link ListContextualRegistry} and {@link ContextualRegistry}. - */ -public class ListContextTest { - - @Test - public void create() { - assertThat(ContextualRegistry.create(), notNullValue()); - assertThat(ContextualRegistry.create(null), notNullValue()); - assertThat(ContextualRegistry.create(ContextualRegistry.create()), notNullValue()); - } - - @Test - public void registerAndGetLast() { - ContextualRegistry context = ContextualRegistry.create(); - assertThat(context.get(String.class), is(Optional.empty())); - assertThat(context.get(Integer.class), is(Optional.empty())); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(context.get(Integer.class), is(Optional.empty())); - context.register(1); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(context.get(Integer.class), is(Optional.of(1))); - assertThat(context.get(Object.class), is(Optional.of(1))); - context.register("bbb"); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(context.get(Object.class), is(Optional.of("bbb"))); - } - - @Test - public void registerAndGetLastClassifier() { - ContextualRegistry context = ContextualRegistry.create(); - String classifier = "classifier"; - assertThat(context.get(classifier, String.class), is(Optional.empty())); - assertThat(context.get(classifier, Integer.class), is(Optional.empty())); - context.register(classifier, "aaa"); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(context.get(String.class), is(Optional.empty())); - assertThat(context.get(classifier, Integer.class), is(Optional.empty())); - context.register(classifier, 1); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(context.get(classifier, Integer.class), is(Optional.of(1))); - assertThat(context.get(classifier, Object.class), is(Optional.of(1))); - context.register(classifier, "bbb"); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - context.register("ccc"); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(context.get(classifier, Object.class), is(Optional.of("bbb"))); - assertThat(context.get(String.class), is(Optional.of("ccc"))); - } - - @Test - public void emptyParent() { - ContextualRegistry parent = ContextualRegistry.create(); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(String.class), is(Optional.empty())); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - } - - @Test - public void testParent() { - ContextualRegistry parent = ContextualRegistry.create(); - parent.register("ppp"); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(String.class), is(Optional.of("ppp"))); - context.register(1); - assertThat(context.get(String.class), is(Optional.of("ppp"))); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(parent.get(String.class), is(Optional.of("ppp"))); - } - - @Test - public void testParentWithClassifier() { - String classifier = "classifier"; - ContextualRegistry parent = ContextualRegistry.create(); - parent.register(classifier, "ppp"); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(classifier, String.class), is(Optional.of("ppp"))); - context.register(classifier, 1); - assertThat(context.get(classifier, String.class), is(Optional.of("ppp"))); - context.register(classifier, "aaa"); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(parent.get(classifier, String.class), is(Optional.of("ppp"))); - } - - @Test - public void testSupply() { - AtomicInteger counter = new AtomicInteger(0); - ContextualRegistry context = ContextualRegistry.create(); - context.register(1); - Date date = new Date(); - context.register(date); - context.register("aaa"); - context.supply(String.class, () -> { - counter.incrementAndGet(); - return "bbb"; - }); - context.register(2); - assertThat(context.get(Date.class), is(Optional.of(date))); - assertThat(counter.get(), is(0)); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(Date.class), is(Optional.of(date))); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - } - - @Test - public void testSupplyClassifier() { - String classifier = "classifier"; - AtomicInteger counter = new AtomicInteger(0); - ContextualRegistry context = ContextualRegistry.create(); - context.register(classifier, 1); - Date date = new Date(); - context.register(classifier, date); - context.register(classifier, "aaa"); - context.supply(classifier, String.class, () -> { - counter.incrementAndGet(); - return "bbb"; - }); - context.register(classifier, 2); - assertThat(context.get(classifier, Date.class), is(Optional.of(date))); - assertThat(counter.get(), is(0)); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(classifier, Date.class), is(Optional.of(date))); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - } -} diff --git a/common/http/src/test/java/io/helidon/common/http/ListContextualRegistryTest.java b/common/http/src/test/java/io/helidon/common/http/ListContextualRegistryTest.java deleted file mode 100644 index 442b3dddb38..00000000000 --- a/common/http/src/test/java/io/helidon/common/http/ListContextualRegistryTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.common.http; - -import java.util.Date; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; - -/** - * Tests {@link ListContextualRegistry} and {@link ContextualRegistry}. - */ -public class ListContextualRegistryTest { - - @Test - public void create() { - assertThat(ContextualRegistry.create(), notNullValue()); - assertThat(ContextualRegistry.create(null), notNullValue()); - assertThat(ContextualRegistry.create(ContextualRegistry.create()), notNullValue()); - } - - @Test - public void registerAndGetLast() { - ContextualRegistry context = ContextualRegistry.create(); - assertThat(context.get(String.class), is(Optional.empty())); - assertThat(context.get(Integer.class), is(Optional.empty())); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(context.get(Integer.class), is(Optional.empty())); - context.register(1); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(context.get(Integer.class), is(Optional.of(1))); - assertThat(context.get(Object.class), is(Optional.of(1))); - context.register("bbb"); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(context.get(Object.class), is(Optional.of("bbb"))); - } - - @Test - public void registerAndGetLastClassifier() { - ContextualRegistry context = ContextualRegistry.create(); - String classifier = "classifier"; - assertThat(context.get(classifier, String.class), is(Optional.empty())); - assertThat(context.get(classifier, Integer.class), is(Optional.empty())); - context.register(classifier, "aaa"); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(context.get(String.class), is(Optional.empty())); - assertThat(context.get(classifier, Integer.class), is(Optional.empty())); - context.register(classifier, 1); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(context.get(classifier, Integer.class), is(Optional.of(1))); - assertThat(context.get(classifier, Object.class), is(Optional.of(1))); - context.register(classifier, "bbb"); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - context.register("ccc"); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(context.get(classifier, Object.class), is(Optional.of("bbb"))); - assertThat(context.get(String.class), is(Optional.of("ccc"))); - } - - @Test - public void emptyParent() { - ContextualRegistry parent = ContextualRegistry.create(); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(String.class), is(Optional.empty())); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - } - - @Test - public void testParent() { - ContextualRegistry parent = ContextualRegistry.create(); - parent.register("ppp"); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(String.class), is(Optional.of("ppp"))); - context.register(1); - assertThat(context.get(String.class), is(Optional.of("ppp"))); - context.register("aaa"); - assertThat(context.get(String.class), is(Optional.of("aaa"))); - assertThat(parent.get(String.class), is(Optional.of("ppp"))); - } - - @Test - public void testParentWithClassifier() { - String classifier = "classifier"; - ContextualRegistry parent = ContextualRegistry.create(); - parent.register(classifier, "ppp"); - ContextualRegistry context = ContextualRegistry.create(parent); - assertThat(context.get(classifier, String.class), is(Optional.of("ppp"))); - context.register(classifier, 1); - assertThat(context.get(classifier, String.class), is(Optional.of("ppp"))); - context.register(classifier, "aaa"); - assertThat(context.get(classifier, String.class), is(Optional.of("aaa"))); - assertThat(parent.get(classifier, String.class), is(Optional.of("ppp"))); - } - - @Test - public void testSupply() { - AtomicInteger counter = new AtomicInteger(0); - ContextualRegistry context = ContextualRegistry.create(); - context.register(1); - Date date = new Date(); - context.register(date); - context.register("aaa"); - context.supply(String.class, () -> { - counter.incrementAndGet(); - return "bbb"; - }); - context.register(2); - assertThat(context.get(Date.class), is(Optional.of(date))); - assertThat(counter.get(), is(0)); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(Date.class), is(Optional.of(date))); - assertThat(context.get(String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - } - - @Test - public void testSupplyClassifier() { - String classifier = "classifier"; - AtomicInteger counter = new AtomicInteger(0); - ContextualRegistry context = ContextualRegistry.create(); - context.register(classifier, 1); - Date date = new Date(); - context.register(classifier, date); - context.register(classifier, "aaa"); - context.supply(classifier, String.class, () -> { - counter.incrementAndGet(); - return "bbb"; - }); - context.register(classifier, 2); - assertThat(context.get(classifier, Date.class), is(Optional.of(date))); - assertThat(counter.get(), is(0)); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - assertThat(context.get(classifier, Date.class), is(Optional.of(date))); - assertThat(context.get(classifier, String.class), is(Optional.of("bbb"))); - assertThat(counter.get(), is(1)); - } -} diff --git a/examples/webserver/tutorial/src/main/java/io/helidon/webserver/examples/tutorial/user/UserFilter.java b/examples/webserver/tutorial/src/main/java/io/helidon/webserver/examples/tutorial/user/UserFilter.java index 293c1f414d0..ac75b9c3b23 100644 --- a/examples/webserver/tutorial/src/main/java/io/helidon/webserver/examples/tutorial/user/UserFilter.java +++ b/examples/webserver/tutorial/src/main/java/io/helidon/webserver/examples/tutorial/user/UserFilter.java @@ -16,7 +16,6 @@ package io.helidon.webserver.examples.tutorial.user; -import io.helidon.common.http.ContextualRegistry; import io.helidon.webserver.Handler; import io.helidon.webserver.Routing; import io.helidon.webserver.ServerRequest; @@ -24,7 +23,7 @@ /** * If used as a {@link Routing Routing} {@link Handler} then assign valid {@link User} instance on the request - * {@link ContextualRegistry context}. + * {@link io.helidon.common.context.Context context}. */ public class UserFilter implements Handler { diff --git a/grpc/server/src/test/java/io/helidon/grpc/server/ContextIT.java b/grpc/server/src/test/java/io/helidon/grpc/server/ContextIT.java index 9fd7ccacd4a..41847ef3e5f 100644 --- a/grpc/server/src/test/java/io/helidon/grpc/server/ContextIT.java +++ b/grpc/server/src/test/java/io/helidon/grpc/server/ContextIT.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -120,7 +120,7 @@ private static void startGrpcServer() throws Exception { /** - * A test gRPC service that obtains a value from the {@link io.helidon.common.http.ContextualRegistry}. + * A test gRPC service that obtains a value from the {@link io.helidon.common.context.Context}. */ public static class ContextService implements GrpcService { @@ -156,7 +156,7 @@ public void echo(Echo.EchoRequest request, StreamObserver obs /** - * A test value to register with the {@link io.helidon.common.http.ContextualRegistry}. + * A test value to register with the {@link io.helidon.common.context.Context}. */ private static class TestValue { private final String value; diff --git a/webserver/access-log/src/main/java/io/helidon/webserver/accesslog/SizeLogEntry.java b/webserver/access-log/src/main/java/io/helidon/webserver/accesslog/SizeLogEntry.java index e63dc41bf59..5f8b146bb05 100644 --- a/webserver/access-log/src/main/java/io/helidon/webserver/accesslog/SizeLogEntry.java +++ b/webserver/access-log/src/main/java/io/helidon/webserver/accesslog/SizeLogEntry.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ import java.util.concurrent.Flow; import java.util.concurrent.atomic.LongAdder; -import io.helidon.common.http.ContextualRegistry; +import io.helidon.common.context.Context; import io.helidon.common.http.DataChunk; import io.helidon.webserver.ServerRequest; import io.helidon.webserver.ServerResponse; @@ -52,7 +52,7 @@ public static Builder builder() { @Override public void accept(ServerRequest req, ServerResponse res) { - ContextualRegistry context = req.context(); + Context context = req.context(); res.registerFilter(originalPublisher -> new ByteCountingPublisher(originalPublisher, context)); } @@ -84,9 +84,9 @@ private Builder defaults() { private static final class ByteCountingPublisher implements Flow.Publisher { private final Flow.Publisher originalPublisher; - private final ContextualRegistry context; + private final Context context; - private ByteCountingPublisher(Flow.Publisher originalPublisher, ContextualRegistry context) { + private ByteCountingPublisher(Flow.Publisher originalPublisher, Context context) { this.originalPublisher = originalPublisher; this.context = context; } @@ -99,10 +99,10 @@ public void subscribe(Flow.Subscriber subscriber) { private static final class ByteCountingSubscriber implements Flow.Subscriber { private final Flow.Subscriber subscriber; - private final ContextualRegistry context; + private final Context context; private final LongAdder sizeAdder = new LongAdder(); - private ByteCountingSubscriber(Flow.Subscriber subscriber, ContextualRegistry context) { + private ByteCountingSubscriber(Flow.Subscriber subscriber, Context context) { this.subscriber = subscriber; this.context = context; } diff --git a/webserver/access-log/src/test/java/io/helidon/webserver/accesslog/AccessLogSupportTest.java b/webserver/access-log/src/test/java/io/helidon/webserver/accesslog/AccessLogSupportTest.java index 5645ad1b5c3..1f4d4f14576 100644 --- a/webserver/access-log/src/test/java/io/helidon/webserver/accesslog/AccessLogSupportTest.java +++ b/webserver/access-log/src/test/java/io/helidon/webserver/accesslog/AccessLogSupportTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import java.time.format.DateTimeFormatter; import java.util.Arrays; -import io.helidon.common.http.ContextualRegistry; +import io.helidon.common.context.Context; import io.helidon.common.http.Http; import io.helidon.common.http.HttpRequest; import io.helidon.webserver.RequestHeaders; @@ -55,7 +55,7 @@ void testHelidonFormat() { AccessLogSupport accessLog = AccessLogSupport.create(); ServerRequest request = mock(ServerRequest.class); - ContextualRegistry context = ContextualRegistry.create(); + Context context = Context.create(); when(request.remoteAddress()).thenReturn(REMOTE_IP); when(request.context()).thenReturn(context); when(request.method()).thenReturn(Http.Method.PUT); @@ -93,7 +93,7 @@ void testCommonFormat() { .build(); ServerRequest request = mock(ServerRequest.class); - ContextualRegistry context = ContextualRegistry.create(); + Context context = Context.create(); when(request.remoteAddress()).thenReturn(REMOTE_IP); when(request.context()).thenReturn(context); when(request.method()).thenReturn(Http.Method.PUT); @@ -132,7 +132,7 @@ void testCustomFormat() { .build(); ServerRequest request = mock(ServerRequest.class); - ContextualRegistry context = ContextualRegistry.create(); + Context context = Context.create(); when(request.remoteAddress()).thenReturn(REMOTE_IP); when(request.context()).thenReturn(context); when(request.method()).thenReturn(Http.Method.PUT); diff --git a/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestWebServer.java b/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestWebServer.java index 730e7d211c4..ef1700dd6d1 100644 --- a/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestWebServer.java +++ b/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestWebServer.java @@ -18,8 +18,8 @@ import java.util.concurrent.CompletableFuture; -import io.helidon.common.http.ContextualRegistry; import io.helidon.common.reactive.Single; +import io.helidon.common.context.Context; import io.helidon.media.common.MediaContext; import io.helidon.media.common.MessageBodyReaderContext; import io.helidon.media.common.MessageBodyWriterContext; @@ -35,7 +35,7 @@ class TestWebServer implements WebServer { private final CompletableFuture startFuture = new CompletableFuture<>(); private final CompletableFuture shutdownFuture = new CompletableFuture<>(); - private final ContextualRegistry context = ContextualRegistry.create(); + private final Context context = Context.create(); private final ServerConfiguration configuration = ServerConfiguration.builder().build(); private final MediaContext mediaContext; @@ -82,7 +82,7 @@ public boolean isRunning() { } @Override - public ContextualRegistry context() { + public Context context() { return context; } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/NettyWebServer.java b/webserver/webserver/src/main/java/io/helidon/webserver/NettyWebServer.java index 08ce6da0616..1736f3d0a38 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/NettyWebServer.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/NettyWebServer.java @@ -77,7 +77,7 @@ class NettyWebServer implements WebServer { private final CompletableFuture channelsUpFuture = new CompletableFuture<>(); private final CompletableFuture channelsCloseFuture = new CompletableFuture<>(); private final CompletableFuture threadGroupsShutdownFuture = new CompletableFuture<>(); - private final io.helidon.common.http.ContextualRegistry contextualRegistry; + private final Context contextualRegistry; private final ConcurrentMap channels = new ConcurrentHashMap<>(); private final List initializers = new LinkedList<>(); private final MessageBodyWriterContext writerContext; @@ -105,14 +105,7 @@ class NettyWebServer implements WebServer { HelidonFeatures.print(HelidonFlavor.SE, config.printFeatureDetails()); this.bossGroup = new NioEventLoopGroup(sockets.size()); this.workerGroup = config.workersCount() <= 0 ? new NioEventLoopGroup() : new NioEventLoopGroup(config.workersCount()); - // the contextual registry needs to be created as a different type is expected. Once we remove ContextualRegistry - // we can simply use the one from config - Context context = config.context(); - if (context instanceof io.helidon.common.http.ContextualRegistry) { - this.contextualRegistry = (io.helidon.common.http.ContextualRegistry) context; - } else { - this.contextualRegistry = io.helidon.common.http.ContextualRegistry.create(config.context()); - } + this.contextualRegistry = config.context(); this.configuration = config; this.readerContext = MessageBodyReaderContext.create(readerContext); this.writerContext = MessageBodyWriterContext.create(writerContext); @@ -220,7 +213,7 @@ public synchronized Single start() { throw new IllegalStateException( "no socket configuration found for name: " + name); } - int port = socketConfig.port() <= 0 ? 0 : socketConfig.port(); + int port = Math.max(socketConfig.port(), 0); if (channelsUpFuture.isCompletedExceptionally()) { // break because one of the previous channels already failed break; @@ -397,7 +390,7 @@ public boolean isRunning() { } @Override - public io.helidon.common.http.ContextualRegistry context() { + public Context context() { return contextualRegistry; } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/Request.java b/webserver/webserver/src/main/java/io/helidon/webserver/Request.java index a6a02c6f939..392b93d34d0 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/Request.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/Request.java @@ -27,6 +27,7 @@ import java.util.StringTokenizer; import io.helidon.common.GenericType; +import io.helidon.common.context.Context; import io.helidon.common.http.Http; import io.helidon.common.http.MediaType; import io.helidon.common.http.Parameters; @@ -57,7 +58,7 @@ abstract class Request implements ServerRequest { private final BareRequest bareRequest; private final WebServer webServer; - private final io.helidon.common.http.ContextualRegistry context; + private final Context context; private final Parameters queryParams; private final HashRequestHeaders headers; private final MessageBodyReadableContent content; @@ -73,7 +74,7 @@ abstract class Request implements ServerRequest { this.bareRequest = req; this.webServer = webServer; this.headers = headers; - this.context = io.helidon.common.http.ContextualRegistry.create(webServer.context()); + this.context = Context.create(webServer.context()); this.queryParams = UriComponent.decodeQuery(req.uri().getRawQuery(), true); this.eventListener = new MessageBodyEventListener(); MessageBodyReaderContext readerContext = MessageBodyReaderContext @@ -104,10 +105,10 @@ abstract class Request implements ServerRequest { */ static Charset contentCharset(ServerRequest request) { return request.headers() - .contentType() - .flatMap(MediaType::charset) - .map(Charset::forName) - .orElse(DEFAULT_CHARSET); + .contentType() + .flatMap(MediaType::charset) + .map(Charset::forName) + .orElse(DEFAULT_CHARSET); } @Override @@ -116,8 +117,7 @@ public WebServer webServer() { } @Override - @SuppressWarnings("deprecation") - public io.helidon.common.http.ContextualRegistry context() { + public Context context() { return context; } @@ -195,7 +195,7 @@ private final class MessageBodyEventListener implements MessageBodyContext.Event private Span readSpan; - private Span createReadSpan(GenericType type) { + private Span createReadSpan(GenericType type) { // only create this span if we have a parent span SpanContext parentSpan = spanContext(); if (null == parentSpan) { @@ -226,29 +226,29 @@ private Span createReadSpan(GenericType type) { @Override public void onEvent(MessageBodyContext.Event event) { switch (event.eventType()) { - case BEFORE_ONSUBSCRIBE: - GenericType type = event.entityType().orElse(null); - readSpan = createReadSpan(type); - break; - - case AFTER_ONERROR: - if (readSpan != null) { - Tags.ERROR.set(readSpan, Boolean.TRUE); - Throwable ex = event.asErrorEvent().error(); - readSpan.log(Map.of("event", "error", - "error.kind", "Exception", - "error.object", ex, - "message", ex.toString())); - readSpan.finish(); - } - break; - case AFTER_ONCOMPLETE: - if (readSpan != null) { - readSpan.finish(); - } - break; - default: - // do nothing + case BEFORE_ONSUBSCRIBE: + GenericType type = event.entityType().orElse(null); + readSpan = createReadSpan(type); + break; + + case AFTER_ONERROR: + if (readSpan != null) { + Tags.ERROR.set(readSpan, Boolean.TRUE); + Throwable ex = event.asErrorEvent().error(); + readSpan.log(Map.of("event", "error", + "error.kind", "Exception", + "error.object", ex, + "message", ex.toString())); + readSpan.finish(); + } + break; + case AFTER_ONCOMPLETE: + if (readSpan != null) { + readSpan.finish(); + } + break; + default: + // do nothing } } } @@ -313,7 +313,7 @@ public Path absolute() { return absolutePath == null ? this : absolutePath; } - static Path create(Path contextual, String path, Map params) { + static Path create(Path contextual, String path, Map params) { return create(contextual, path, path, params); } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ServerBasicConfig.java b/webserver/webserver/src/main/java/io/helidon/webserver/ServerBasicConfig.java index 60a7fddb56c..de38dde23de 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ServerBasicConfig.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ServerBasicConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2020 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import javax.net.ssl.SSLContext; import io.helidon.common.context.Context; -import io.helidon.common.http.ContextualRegistry; import io.opentracing.Tracer; @@ -39,7 +38,7 @@ class ServerBasicConfig implements ServerConfiguration { private final Tracer tracer; private final Map socketConfigs; private final ExperimentalConfiguration experimental; - private final ContextualRegistry context; + private final Context context; private final boolean printFeatureDetails; /** @@ -56,7 +55,7 @@ class ServerBasicConfig implements ServerConfiguration { this.printFeatureDetails = builder.printFeatureDetails(); HashMap map = new HashMap<>(builder.sockets()); - map.put(ServerConfiguration.DEFAULT_SOCKET_NAME, this.socketConfig); + map.put(WebServer.DEFAULT_SOCKET_NAME, this.socketConfig); this.socketConfigs = Collections.unmodifiableMap(map); } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/ServerConfiguration.java b/webserver/webserver/src/main/java/io/helidon/webserver/ServerConfiguration.java index 939664777ef..cf829a52dcc 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/ServerConfiguration.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/ServerConfiguration.java @@ -31,7 +31,6 @@ import javax.net.ssl.SSLContext; import io.helidon.common.context.Context; -import io.helidon.common.http.ContextualRegistry; import io.helidon.config.Config; import io.helidon.config.ConfigException; import io.helidon.config.DeprecatedConfig; @@ -48,7 +47,7 @@ public interface ServerConfiguration extends SocketConfiguration { * The default server socket configuration name. All the default server socket * configuration (e.g., {@link #port()} or {@link #backlog()}) is accessible through * {@link #socket(String)} or {@link #sockets()} with this - * {@link #DEFAULT_SOCKET_NAME default socket name}. + * {@link io.helidon.webserver.WebServer#DEFAULT_SOCKET_NAME default socket name}. * * @deprecated since 2.0.0, please use {@link WebServer#DEFAULT_SOCKET_NAME} */ @@ -269,7 +268,7 @@ final class Builder implements SocketConfiguration.SocketConfigurationBuilder