Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the notion of a default SessionProtocol #6128

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.linecorp.armeria.common.SessionProtocol.httpAndHttpsValues;
import static com.linecorp.armeria.internal.client.ClientUtil.UNDEFINED_URI;
import static com.linecorp.armeria.internal.client.SessionProtocolUtil.defaultSessionProtocol;
import static com.linecorp.armeria.internal.client.SessionProtocolUtil.maybeApplyDefaultProtocol;
import static java.util.Objects.requireNonNull;

import java.net.URI;
Expand Down Expand Up @@ -60,6 +62,13 @@ protected AbstractWebClientBuilder(URI uri) {
this(validateUri(uri), null, null, null);
}

/**
* Creates a new instance.
*/
protected AbstractWebClientBuilder(EndpointGroup endpointGroup, @Nullable String path) {
this(defaultSessionProtocol(), requireNonNull(endpointGroup, "endpointGroup"), path);
}

/**
* Creates a new instance.
*
Expand Down Expand Up @@ -87,6 +96,7 @@ protected AbstractWebClientBuilder(@Nullable URI uri, @Nullable Scheme scheme,

private static URI validateUri(URI uri) {
requireNonNull(uri, "uri");
uri = maybeApplyDefaultProtocol(uri);
if (Clients.isUndefinedUri(uri)) {
return uri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.linecorp.armeria.client;

import static com.google.common.base.Preconditions.checkArgument;
import static com.linecorp.armeria.internal.client.SessionProtocolUtil.maybeApplyDefaultProtocol;
import static java.util.Objects.requireNonNull;

import java.net.URI;
Expand Down Expand Up @@ -76,7 +77,7 @@ public final class ClientBuilder extends AbstractClientOptionsBuilder {
private final Scheme scheme;

ClientBuilder(URI uri) {
checkArgument(uri.getScheme() != null, "uri must have scheme: %s", uri);
uri = maybeApplyDefaultProtocol(uri);
checkArgument(uri.getRawAuthority() != null, "uri must have authority: %s", uri);
this.uri = uri;
endpointGroup = null;
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/java/com/linecorp/armeria/client/Clients.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.linecorp.armeria.client;

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

import java.net.URI;
Expand Down Expand Up @@ -72,6 +73,17 @@ public static <T> T newClient(URI uri, Class<T> clientType) {
return builder(uri).build(clientType);
}

/**
* Creates a new client that connects to the specified {@link EndpointGroup} with
* the default {@link SessionProtocol} and {@link ClientFactory}.
*
* @param endpointGroup the server {@link EndpointGroup}
* @param clientType the type of the new client
*/
public static <T> T newClient(EndpointGroup endpointGroup, Class<T> clientType) {
return builder(endpointGroup).build(clientType);
}

/**
* Creates a new client that connects to the specified {@link EndpointGroup} with the specified
* {@code scheme} using the default {@link ClientFactory}.
Expand All @@ -88,6 +100,18 @@ public static <T> T newClient(String scheme, EndpointGroup endpointGroup, Class<
return builder(scheme, endpointGroup).build(clientType);
}

/**
* Creates a new client that connects to the specified {@link EndpointGroup} and
* {@code path} using the default {@link SessionProtocol} and {@link ClientFactory}.
*
* @param endpointGroup the server {@link EndpointGroup}
* @param path the path to the endpoint
* @param clientType the type of the new client
*/
public static <T> T newClient(EndpointGroup endpointGroup, String path, Class<T> clientType) {
return builder(endpointGroup, path).build(clientType);
}

/**
* Creates a new client that connects to the specified {@link EndpointGroup} with the specified
* {@code scheme} and {@code path} using the default {@link ClientFactory}.
Expand Down Expand Up @@ -192,6 +216,14 @@ public static ClientBuilder builder(URI uri) {
return new ClientBuilder(requireNonNull(uri, "uri"));
}

/**
* Returns a new {@link ClientBuilder} that builds the client that connects to the specified
* {@link EndpointGroup} using the default {@link SessionProtocol}.
*/
public static ClientBuilder builder(EndpointGroup endpointGroup) {
return builder(Scheme.of(SerializationFormat.NONE, defaultSessionProtocol()), endpointGroup);
}

/**
* Returns a new {@link ClientBuilder} that builds the client that connects to the specified
* {@link EndpointGroup} with the specified {@code scheme}.
Expand All @@ -202,6 +234,14 @@ public static ClientBuilder builder(String scheme, EndpointGroup endpointGroup)
return builder(Scheme.parse(requireNonNull(scheme, "scheme")), endpointGroup);
}

/**
* Returns a new {@link ClientBuilder} that builds the client that connects to the specified
* {@link EndpointGroup} and {@code path} using the default {@link SessionProtocol}.
*/
public static ClientBuilder builder(EndpointGroup endpointGroup, String path) {
return builder(Scheme.of(SerializationFormat.NONE, defaultSessionProtocol()), endpointGroup, path);
}

/**
* Returns a new {@link ClientBuilder} that builds the client that connects to the specified
* {@link EndpointGroup} with the specified {@code scheme} and {@code path}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.linecorp.armeria.client;

import static com.linecorp.armeria.internal.client.SessionProtocolUtil.defaultSessionProtocol;
import static java.util.Objects.requireNonNull;

import com.google.common.base.Strings;
Expand Down Expand Up @@ -86,17 +87,16 @@ public HttpResponse execute(HttpRequest req, RequestOptions requestOptions) {
scheme = req.scheme();
authority = req.authority();

if (scheme == null || authority == null) {
if (authority == null) {
return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException(
"Scheme and authority must be specified in \":path\" or " +
"in \":scheme\" and \":authority\". :path=" +
originalPath + ", :scheme=" + req.scheme() + ", :authority=" + req.authority()));
"Authority must be specified in \":path\" or " +
"in \":authority\". :path=" + originalPath + ", :authority=" + req.authority()));
}
}

endpointGroup = Endpoint.parse(authority);
try {
protocol = Scheme.parse(scheme).sessionProtocol();
protocol = computeSessionProtocol(scheme);
} catch (Exception e) {
return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException(
"Failed to parse a scheme: " + reqTarget.scheme(), e));
Expand Down Expand Up @@ -126,6 +126,13 @@ public HttpResponse execute(HttpRequest req, RequestOptions requestOptions) {
return ClientUtil.executeWithFallback(preClient, ctx, newReq, errorResponseFactory());
}

private static SessionProtocol computeSessionProtocol(@Nullable String scheme) {
if (scheme == null) {
return defaultSessionProtocol();
}
return Scheme.parse(scheme).sessionProtocol();
}

private static HttpResponse abortRequestAndReturnFailureResponse(
HttpRequest req, IllegalArgumentException cause) {
req.abort(cause);
Expand Down
41 changes: 41 additions & 0 deletions core/src/main/java/com/linecorp/armeria/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.linecorp.armeria.client;

import static com.linecorp.armeria.internal.client.SessionProtocolUtil.defaultSessionProtocol;
import static java.util.Objects.requireNonNull;

import java.net.URI;
Expand Down Expand Up @@ -78,6 +79,16 @@ static RestClient of(URI uri) {
return builder(uri).build();
}

/**
* Returns a new {@link RestClient} that connects to the specified {@link EndpointGroup} with
* the default {@link SessionProtocol}, {@link ClientFactory} and {@link ClientOptions}.
*
* @param endpointGroup the server {@link EndpointGroup}
*/
static RestClient of(EndpointGroup endpointGroup) {
return builder(endpointGroup).build();
}

/**
* Returns a new {@link RestClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code protocol} using the default {@link ClientFactory} and the default
Expand Down Expand Up @@ -110,6 +121,18 @@ static RestClient of(SessionProtocol protocol, EndpointGroup endpointGroup) {
return builder(protocol, endpointGroup).build();
}

/**
* Returns a new {@link RestClient} that connects to the specified {@link EndpointGroup} and
* {@code path} using the default {@link SessionProtocol}, {@link ClientFactory} and
* the {@link ClientOptions}.
*
* @param endpointGroup the server {@link EndpointGroup}
* @param path the path to the endpoint
*/
static RestClient of(EndpointGroup endpointGroup, String path) {
return builder(endpointGroup, path).build();
}

/**
* Returns a new {@link RestClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code protocol} and {@code path} using the default {@link ClientFactory} and
Expand Down Expand Up @@ -173,6 +196,15 @@ static RestClientBuilder builder(URI uri) {
return new RestClientBuilder(uri);
}

/**
* Returns a new {@link RestClientBuilder} created with the default {@link SessionProtocol}
* and base {@link EndpointGroup}.
*/
static RestClientBuilder builder(EndpointGroup endpointGroup) {
requireNonNull(endpointGroup, "endpointGroup");
return builder(defaultSessionProtocol(), endpointGroup);
}

/**
* Returns a new {@link RestClientBuilder} created with the specified {@code protocol}
* and base {@link EndpointGroup}.
Expand All @@ -199,6 +231,15 @@ static RestClientBuilder builder(SessionProtocol protocol, EndpointGroup endpoin
return new RestClientBuilder(protocol, endpointGroup, null);
}

/**
* Returns a new {@link RestClientBuilder} created with the base {@link EndpointGroup} and path.
*/
static RestClientBuilder builder(EndpointGroup endpointGroup, String path) {
requireNonNull(endpointGroup, "endpointGroup");
requireNonNull(path, "path");
return builder(defaultSessionProtocol(), endpointGroup, path);
}

/**
* Returns a new {@link RestClientBuilder} created with the specified {@code protocol}.
* base {@link EndpointGroup} and path.
Expand Down
41 changes: 41 additions & 0 deletions core/src/main/java/com/linecorp/armeria/client/WebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ static WebClient of(URI uri) {
return builder(uri).build();
}

/**
* Returns a new {@link WebClient} that connects to the specified {@link EndpointGroup}
* using the default {@link SessionProtocol}, {@link ClientFactory} and {@link ClientOptions}.
*
* @param endpointGroup the server {@link EndpointGroup}
*/
static WebClient of(EndpointGroup endpointGroup) {
return builder(endpointGroup).build();
}

/**
* Returns a new {@link WebClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code protocol} using the default {@link ClientFactory} and the default
Expand Down Expand Up @@ -111,6 +121,18 @@ static WebClient of(SessionProtocol protocol, EndpointGroup endpointGroup) {
return builder(protocol, endpointGroup).build();
}

/**
* Returns a new {@link WebClient} that connects to the specified {@link EndpointGroup}
* and {@code path} using the default {@link SessionProtocol}, {@link ClientFactory} and
* {@link ClientOptions}.
*
* @param endpointGroup the server {@link EndpointGroup}
* @param path the path to the endpoint
*/
static WebClient of(EndpointGroup endpointGroup, String path) {
return builder(endpointGroup, path).build();
}

/**
* Returns a new {@link WebClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code protocol} and {@code path} using the default {@link ClientFactory} and
Expand Down Expand Up @@ -174,6 +196,15 @@ static WebClientBuilder builder(URI uri) {
return new WebClientBuilder(uri);
}

/**
* Returns a new {@link WebClientBuilder} created with the default {@link SessionProtocol}
* and base {@link EndpointGroup}.
*/
static WebClientBuilder builder(EndpointGroup endpointGroup) {
requireNonNull(endpointGroup, "endpointGroup");
return new WebClientBuilder(endpointGroup, null);
}

/**
* Returns a new {@link WebClientBuilder} created with the specified {@code protocol}
* and base {@link EndpointGroup}.
Expand All @@ -200,6 +231,16 @@ static WebClientBuilder builder(SessionProtocol protocol, EndpointGroup endpoint
return new WebClientBuilder(protocol, endpointGroup, null);
}

/**
* Returns a new {@link WebClientBuilder} created with the default {@link SessionProtocol},
* base {@link EndpointGroup} and path.
*/
static WebClientBuilder builder(EndpointGroup endpointGroup, String path) {
requireNonNull(endpointGroup, "endpointGroup");
requireNonNull(path, "path");
return new WebClientBuilder(endpointGroup, path);
}

/**
* Returns a new {@link WebClientBuilder} created with the specified {@code protocol}.
* base {@link EndpointGroup} and path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public final class WebClientBuilder extends AbstractWebClientBuilder {
super(uri);
}

/**
* Creates a new instance.
*/
WebClientBuilder(EndpointGroup endpointGroup, @Nullable String path) {
super(endpointGroup, path);
}

/**
* Creates a new instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.linecorp.armeria.client.websocket;

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

import java.net.URI;
Expand Down Expand Up @@ -97,6 +98,14 @@ static WebSocketClient of(URI uri) {
return builder(uri).build();
}

/**
* Returns a new {@link WebSocketClient} that connects to the specified {@link EndpointGroup} using
* the default {@link Scheme} and {@link ClientOptions}.
*/
static WebSocketClient of(EndpointGroup endpointGroup) {
return builder(endpointGroup).build();
}

/**
* Returns a new {@link WebSocketClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code scheme} using the default {@link ClientOptions}.
Expand All @@ -121,6 +130,14 @@ static WebSocketClient of(SessionProtocol protocol, EndpointGroup endpointGroup)
return builder(protocol, endpointGroup).build();
}

/**
* Returns a new {@link WebSocketClient} that connects to the specified {@link EndpointGroup} with
* and {@code path} using the default {@link Scheme} and {@link ClientOptions}.
*/
static WebSocketClient of(EndpointGroup endpointGroup, String path) {
return builder(endpointGroup, path).build();
}

/**
* Returns a new {@link WebSocketClient} that connects to the specified {@link EndpointGroup} with
* the specified {@code scheme} and {@code path} using the default {@link ClientOptions}.
Expand Down Expand Up @@ -166,6 +183,14 @@ static WebSocketClientBuilder builder(URI uri) {
return new WebSocketClientBuilder(requireNonNull(uri, "uri"));
}

/**
* Returns a new {@link WebSocketClientBuilder} created with the specified {@link EndpointGroup}
* and the default {@link Scheme}.
*/
static WebSocketClientBuilder builder(EndpointGroup endpointGroup) {
return builder(Scheme.of(SerializationFormat.WS, defaultSessionProtocol()), endpointGroup);
}

/**
* Returns a new {@link WebSocketClientBuilder} created with the specified {@code scheme}
* and the {@link EndpointGroup}.
Expand Down Expand Up @@ -194,6 +219,14 @@ static WebSocketClientBuilder builder(SessionProtocol protocol, EndpointGroup en
return builder(Scheme.of(SerializationFormat.WS, protocol), endpointGroup);
}

/**
* Returns a new {@link WebSocketClientBuilder} created with the specified
* {@link EndpointGroup}, {@code path} and the default {@link Scheme}.
*/
static WebSocketClientBuilder builder(EndpointGroup endpointGroup, String path) {
return builder(Scheme.of(SerializationFormat.WS, defaultSessionProtocol()), endpointGroup, path);
}

/**
* Returns a new {@link WebSocketClientBuilder} created with the specified {@code scheme},
* the {@link EndpointGroup}, and the {@code path}.
Expand Down
Loading
Loading