Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Jan 8, 2025
1 parent 563162c commit 7dccab6
Show file tree
Hide file tree
Showing 66 changed files with 1,614 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.linecorp.armeria.common.SessionProtocol.httpAndHttpsValues;
import static com.linecorp.armeria.internal.client.ClientBuilderParamsUtil.preprocessorToUri;
import static com.linecorp.armeria.internal.client.ClientUtil.UNDEFINED_URI;
import static java.util.Objects.requireNonNull;

Expand All @@ -28,6 +29,7 @@
import com.linecorp.armeria.common.SerializationFormat;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.client.ClientBuilderParamsUtil;

/**
* A skeletal builder implementation for {@link WebClient}.
Expand Down Expand Up @@ -72,6 +74,14 @@ protected AbstractWebClientBuilder(SessionProtocol sessionProtocol, EndpointGrou
requireNonNull(endpointGroup, "endpointGroup"), path);
}

/**
* Creates a new instance.
*/
protected AbstractWebClientBuilder(HttpPreprocessor httpPreprocessor, @Nullable String path) {
this(preprocessorToUri(httpPreprocessor, path), null, null, null);
preprocessor(httpPreprocessor);
}

/**
* Creates a new instance.
*/
Expand All @@ -87,7 +97,7 @@ protected AbstractWebClientBuilder(@Nullable URI uri, @Nullable Scheme scheme,

private static URI validateUri(URI uri) {
requireNonNull(uri, "uri");
if (Clients.isUndefinedUri(uri)) {
if (ClientBuilderParamsUtil.isInternalUri(uri)) {
return uri;
}
final String givenScheme = requireNonNull(uri, "uri").getScheme();
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/com/linecorp/armeria/client/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import com.linecorp.armeria.client.redirect.RedirectConfig;
import com.linecorp.armeria.common.RequestId;
import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SerializationFormat;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.SuccessFunction;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.auth.AuthToken;
import com.linecorp.armeria.common.auth.BasicToken;
import com.linecorp.armeria.common.auth.OAuth1aToken;
import com.linecorp.armeria.common.auth.OAuth2Token;
import com.linecorp.armeria.internal.client.ClientBuilderParamsUtil;

/**
* Creates a new client that connects to the specified {@link URI} using the builder pattern. Use the factory
Expand Down Expand Up @@ -95,6 +98,18 @@ public final class ClientBuilder extends AbstractClientOptionsBuilder {
this.scheme = scheme;
}

ClientBuilder(SerializationFormat serializationFormat,
ClientPreprocessors preprocessors, @Nullable String path) {
checkArgument(!preprocessors.isEmpty(),
"At least one preprocessor must be set in ClientPreprocessors.");
endpointGroup = null;
this.path = path;
scheme = Scheme.of(serializationFormat, SessionProtocol.UNDEFINED);
uri = ClientBuilderParamsUtil.preprocessorToUri(scheme, preprocessors, path);
preprocessors.preprocessors().forEach(this::preprocessor);
preprocessors.rpcPreprocessors().forEach(this::rpcPreprocessor);
}

/**
* Returns a newly-created client which implements the specified {@code clientType}, based on the
* properties of this builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ static ClientBuilderParams of(Scheme scheme, EndpointGroup endpointGroup,
* Returns the options of the client.
*/
ClientOptions options();

/**
* TBU.
*/
default ClientBuilderParamsBuilder paramsBuilder() {
return new ClientBuilderParamsBuilder(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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 com.linecorp.armeria.client;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.linecorp.armeria.internal.client.ClientBuilderParamsUtil.nullOrEmptyToSlash;

import java.net.URI;
import java.net.URISyntaxException;

import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SerializationFormat;
import com.linecorp.armeria.common.annotation.Nullable;

/**
* TBU.
*/
public final class ClientBuilderParamsBuilder {

private final ClientBuilderParams params;

@Nullable
private SerializationFormat serializationFormat;
@Nullable
private String absolutePathRef;
@Nullable
private Class<?> type;
@Nullable
private ClientOptions options;

ClientBuilderParamsBuilder(ClientBuilderParams params) {
this.params = params;
}

/**
* TBU.
*/
public ClientBuilderParamsBuilder serializationFormat(SerializationFormat serializationFormat) {
this.serializationFormat = serializationFormat;
return this;
}

/**
* TBU.
*/
public ClientBuilderParamsBuilder absolutePathRef(String absolutePathRef) {
this.absolutePathRef = absolutePathRef;
return this;
}

/**
* TBU.
*/
public ClientBuilderParamsBuilder clientType(Class<?> type) {
this.type = type;
return this;
}

/**
* TBU.
*/
public ClientBuilderParamsBuilder options(ClientOptions options) {
this.options = options;
return this;
}

/**
* TBU.
*/
public ClientBuilderParams build() {
final Scheme scheme;
if (serializationFormat != null) {
scheme = Scheme.of(serializationFormat, params.scheme().sessionProtocol());
} else {
scheme = params.scheme();
}
final String schemeStr;
if (scheme.serializationFormat() == SerializationFormat.NONE) {
schemeStr = scheme.sessionProtocol().uriText();
} else {
schemeStr = scheme.uriText();
}

final String path;
if (absolutePathRef != null) {
path = nullOrEmptyToSlash(absolutePathRef);
} else {
path = params.absolutePathRef();
}

final URI prevUri = params.uri();
final URI uri;
try {
uri = new URI(schemeStr, prevUri.getRawAuthority(), path,
prevUri.getRawQuery(), prevUri.getRawFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return new DefaultClientBuilderParams(scheme, params.endpointGroup(), uri.getRawPath(),
uri, firstNonNull(type, params.clientType()),
firstNonNull(options, params.options()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.linecorp.armeria.common.util.ReleasableHolder;
import com.linecorp.armeria.common.util.ShutdownHooks;
import com.linecorp.armeria.common.util.Unwrappable;
import com.linecorp.armeria.internal.client.ClientBuilderParamsUtil;

import io.micrometer.core.instrument.MeterRegistry;
import io.netty.channel.EventLoop;
Expand Down Expand Up @@ -291,7 +292,7 @@ default ClientFactory unwrap() {
default URI validateUri(URI uri) {
requireNonNull(uri, "uri");

if (Clients.isUndefinedUri(uri)) {
if (ClientBuilderParamsUtil.isInternalUri(uri)) {
// We use a special singleton marker URI for clients that do not explicitly define a
// host or scheme at construction time.
// As this isn't created by users, we don't need to normalize it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public RpcPreClient rpcDecorate(RpcPreClient execution) {
return execution;
}

boolean isEmpty() {
return preprocessors.isEmpty() && rpcPreprocessors.isEmpty();
}

@Override
public boolean equals(Object object) {
if (this == object) {
Expand Down
72 changes: 66 additions & 6 deletions core/src/main/java/com/linecorp/armeria/client/Clients.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.linecorp.armeria.client;

import static com.linecorp.armeria.internal.client.ClientUtil.UNDEFINED_URI;
import static java.util.Objects.requireNonNull;

import java.net.URI;
Expand All @@ -35,7 +34,9 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.SafeCloseable;
import com.linecorp.armeria.common.util.Unwrappable;
import com.linecorp.armeria.internal.client.ClientBuilderParamsUtil;
import com.linecorp.armeria.internal.client.ClientThreadLocalState;
import com.linecorp.armeria.internal.client.ClientUtil;

/**
* Creates a new client that connects to a specified {@link URI}.
Expand Down Expand Up @@ -172,6 +173,29 @@ public static <T> T newClient(SessionProtocol protocol, EndpointGroup endpointGr
return builder(protocol, endpointGroup, path).build(clientType);
}

/**
* TBU.
*/
public static <T> T newClient(ClientPreprocessors preprocessors, Class<T> clientType) {
return builder(preprocessors).build(clientType);
}

/**
* TBU.
*/
public static <T> T newClient(SerializationFormat serializationFormat, ClientPreprocessors preprocessors,
Class<T> clientType) {
return builder(serializationFormat, preprocessors).build(clientType);
}

/**
* TBU.
*/
public static <T> T newClient(SerializationFormat serializationFormat, ClientPreprocessors preprocessors,
Class<T> clientType, String path) {
return builder(serializationFormat, preprocessors, path).build(clientType);
}

/**
* Returns a new {@link ClientBuilder} that builds the client that connects to the specified {@code uri}.
*
Expand Down Expand Up @@ -252,6 +276,35 @@ public static ClientBuilder builder(Scheme scheme, EndpointGroup endpointGroup,
return new ClientBuilder(scheme, endpointGroup, path);
}

/**
* TBU.
*/
public static ClientBuilder builder(ClientPreprocessors preprocessors) {
requireNonNull(preprocessors, "preprocessors");
return new ClientBuilder(SerializationFormat.NONE, preprocessors, null);
}

/**
* TBU.
*/
public static ClientBuilder builder(SerializationFormat serializationFormat,
ClientPreprocessors preprocessors) {
requireNonNull(serializationFormat, "serializationFormat");
requireNonNull(preprocessors, "preprocessors");
return new ClientBuilder(serializationFormat, preprocessors, null);
}

/**
* TBU.
*/
public static ClientBuilder builder(SerializationFormat serializationFormat,
ClientPreprocessors preprocessors, String path) {
requireNonNull(serializationFormat, "serializationFormat");
requireNonNull(preprocessors, "preprocessors");
requireNonNull(path, "path");
return new ClientBuilder(serializationFormat, preprocessors, path);
}

/**
* Creates a new derived client that connects to the same {@link URI} with the specified {@code client}
* and the specified {@code additionalOptions}.
Expand All @@ -271,12 +324,17 @@ public static <T> T newDerivedClient(T client, ClientOptionValue<?>... additiona
* @see ClientBuilder ClientBuilder, for more information about how the base options and
* additional options are merged when a derived client is created.
*/
@SuppressWarnings("unchecked")
public static <T> T newDerivedClient(T client, Iterable<ClientOptionValue<?>> additionalOptions) {
final ClientBuilderParams params = builderParams(client);
final ClientBuilder builder = newDerivedBuilder(params, true);
builder.options(additionalOptions);

return newDerivedClient(builder, params.clientType());
final ClientOptions newOptions = ClientOptions.builder()
.options(params.options())
.options(additionalOptions)
.build();
final ClientBuilderParams newParams = params.paramsBuilder()
.options(newOptions)
.build();
return (T) newOptions.factory().newClient(newParams);
}

/**
Expand Down Expand Up @@ -604,7 +662,9 @@ public static ClientRequestContextCaptor newContextCaptor() {
* {@code isUndefinedUri(WebClient.of().uri())} will return {@code true}.
*/
public static boolean isUndefinedUri(URI uri) {
return uri == UNDEFINED_URI;
return (uri == ClientUtil.UNDEFINED_URI ||
ClientBuilderParamsUtil.UNDEFINED_URI_AUTHORITY.equals(uri.getAuthority())) &&
uri.getPort() == 1;
}

private Clients() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.linecorp.armeria.client;

import java.net.URI;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,17 +50,13 @@ protected DecoratingClientFactory(ClientFactory delegate) {
* {@link SerializationFormat} are always {@code "/"} and {@link SerializationFormat#NONE}.
*/
protected final HttpClient newHttpClient(ClientBuilderParams params) {
final URI uri = params.uri();
final ClientBuilderParams newParams;
final ClientOptions newOptions = params.options().toBuilder().factory(unwrap()).build();
if (Clients.isUndefinedUri(uri)) {
newParams = ClientBuilderParams.of(uri, HttpClient.class, newOptions);
} else {
final Scheme newScheme = Scheme.of(SerializationFormat.NONE, params.scheme().sessionProtocol());
newParams = ClientBuilderParams.of(newScheme, params.endpointGroup(),
null, HttpClient.class, newOptions);
}

final ClientBuilderParams newParams =
params.paramsBuilder()
.serializationFormat(SerializationFormat.NONE)
.options(newOptions)
.clientType(HttpClient.class)
.build();
return (HttpClient) unwrap().newClient(newParams);
}

Expand Down
Loading

0 comments on commit 7dccab6

Please sign in to comment.