Skip to content

Commit 19e5267

Browse files
committed
Move constants and path extraction to a central location
1 parent 3b99b30 commit 19e5267

File tree

7 files changed

+54
-20
lines changed

7 files changed

+54
-20
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/channelfactory/NettyChannelFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package net.devh.boot.grpc.client.channelfactory;
1919

2020
import static java.util.Objects.requireNonNull;
21+
import static net.devh.boot.grpc.common.util.GrpcUtils.DOMAIN_SOCKET_ADDRESS_SCHEME;
2122

2223
import java.io.IOException;
2324
import java.io.InputStream;
@@ -39,6 +40,7 @@
3940
import net.devh.boot.grpc.client.config.GrpcChannelsProperties;
4041
import net.devh.boot.grpc.client.config.NegotiationType;
4142
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry;
43+
import net.devh.boot.grpc.common.util.GrpcUtils;
4244

4345
/**
4446
* This channel factory creates and manages netty based {@link GrpcChannelFactory}s.
@@ -74,8 +76,8 @@ protected NettyChannelBuilder newChannelBuilder(final String name) {
7476
if (address == null) {
7577
address = URI.create(name);
7678
}
77-
if (GrpcChannelProperties.DOMAIN_SOCKET_ADDRESS_SCHEME.equals(address.getScheme())) {
78-
String path = address.getSchemeSpecificPart();
79+
if (DOMAIN_SOCKET_ADDRESS_SCHEME.equals(address.getScheme())) {
80+
final String path = GrpcUtils.extractDomainSocketAddressPath(address.toString());
7981
return NettyChannelBuilder.forAddress(new DomainSocketAddress(path))
8082
.channelType(EpollDomainSocketChannel.class)
8183
.eventLoopGroup(new EpollEventLoopGroup());

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/channelfactory/ShadedNettyChannelFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package net.devh.boot.grpc.client.channelfactory;
1919

2020
import static java.util.Objects.requireNonNull;
21+
import static net.devh.boot.grpc.common.util.GrpcUtils.DOMAIN_SOCKET_ADDRESS_SCHEME;
2122

2223
import java.io.IOException;
2324
import java.io.InputStream;
@@ -39,6 +40,7 @@
3940
import net.devh.boot.grpc.client.config.GrpcChannelsProperties;
4041
import net.devh.boot.grpc.client.config.NegotiationType;
4142
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry;
43+
import net.devh.boot.grpc.common.util.GrpcUtils;
4244

4345
/**
4446
* This channel factory creates and manages shaded netty based {@link GrpcChannelFactory}s.
@@ -72,8 +74,8 @@ protected NettyChannelBuilder newChannelBuilder(final String name) {
7274
if (address == null) {
7375
address = URI.create(name);
7476
}
75-
if (GrpcChannelProperties.DOMAIN_SOCKET_ADDRESS_SCHEME.equals(address.getScheme())) {
76-
final String path = address.getSchemeSpecificPart();
77+
if (DOMAIN_SOCKET_ADDRESS_SCHEME.equals(address.getScheme())) {
78+
final String path = GrpcUtils.extractDomainSocketAddressPath(address.toString());
7779
return NettyChannelBuilder.forAddress(new DomainSocketAddress(path))
7880
.channelType(EpollDomainSocketChannel.class)
7981
.eventLoopGroup(new EpollEventLoopGroup());

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
@EqualsAndHashCode
5353
public class GrpcChannelProperties {
5454

55-
/**
56-
* A constant that defines, the scheme of a Unix domain socket address.
57-
*/
58-
public static final String DOMAIN_SOCKET_ADDRESS_SCHEME = "unix";
59-
6055
// --------------------------------------------------
6156
// Target Address
6257
// --------------------------------------------------

grpc-common-spring-boot/src/main/java/net/devh/boot/grpc/common/util/GrpcUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
*/
2727
public final class GrpcUtils {
2828

29+
/**
30+
* A constant that defines, the scheme of a Unix domain socket address.
31+
*/
32+
public static final String DOMAIN_SOCKET_ADDRESS_SCHEME = "unix";
33+
34+
/**
35+
* A constant that defines, the scheme prefix of a Unix domain socket address.
36+
*/
37+
public static final String DOMAIN_SOCKET_ADDRESS_PREFIX = DOMAIN_SOCKET_ADDRESS_SCHEME + ":";
38+
2939
/**
3040
* The cloud discovery metadata key used to identify the grpc port.
3141
*/
@@ -36,6 +46,30 @@ public final class GrpcUtils {
3646
*/
3747
public static final int INTER_PROCESS_DISABLE = -1;
3848

49+
/**
50+
* Extracts the domain socket address specific path from the given full address. The address must fulfill the
51+
* requirements as specified by <a href="https://grpc.github.io/grpc/cpp/md_doc_naming.html">grpc</a>.
52+
*
53+
* @param address The address to extract it from.
54+
* @return The extracted domain socket address specific path.
55+
* @throws IllegalArgumentException If the given address is not a valid address.
56+
*/
57+
public static String extractDomainSocketAddressPath(final String address) {
58+
if (!address.startsWith(DOMAIN_SOCKET_ADDRESS_PREFIX)) {
59+
throw new IllegalArgumentException(address + " is not a valid domain socket address.");
60+
}
61+
String path = address.substring(DOMAIN_SOCKET_ADDRESS_PREFIX.length());
62+
if (path.startsWith("//")) {
63+
path = path.substring(2);
64+
// We don't check this as there is no reliable way to check that it's an absolute path,
65+
// especially when Windows adds support for these in the future
66+
// if (!path.startsWith("/")) {
67+
// throw new IllegalArgumentException("If the path is prefixed with '//', then the path must be absolute");
68+
// }
69+
}
70+
return path;
71+
}
72+
3973
/**
4074
* Extracts the service name from the given method.
4175
*

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/config/GrpcServerProperties.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ public class GrpcServerProperties {
6666
*/
6767
public static final String ANY_IPv6_ADDRESS = "::";
6868

69-
/**
70-
* A constant that defines, the prefix of a Unix domain socket address.
71-
*/
72-
public static final String DOMAIN_SOCKET_ADDRESS_PREFIX = "unix:";
73-
7469
/**
7570
* Bind address for the server. Defaults to {@link #ANY_IP_ADDRESS "*"}. Alternatively you can restrict this to
7671
* {@link #ANY_IPv4_ADDRESS "0.0.0.0"} or {@link #ANY_IPv6_ADDRESS "::"}. Or restrict it to exactly one IP address.

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/NettyGrpcServerFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package net.devh.boot.grpc.server.serverfactory;
1919

2020
import static java.util.Objects.requireNonNull;
21+
import static net.devh.boot.grpc.common.util.GrpcUtils.DOMAIN_SOCKET_ADDRESS_PREFIX;
22+
import static net.devh.boot.grpc.server.config.GrpcServerProperties.ANY_IP_ADDRESS;
2123

2224
import java.io.IOException;
2325
import java.io.InputStream;
@@ -37,6 +39,7 @@
3739
import io.netty.channel.epoll.EpollServerDomainSocketChannel;
3840
import io.netty.channel.unix.DomainSocketAddress;
3941
import io.netty.handler.ssl.SslContextBuilder;
42+
import net.devh.boot.grpc.common.util.GrpcUtils;
4043
import net.devh.boot.grpc.server.config.ClientAuth;
4144
import net.devh.boot.grpc.server.config.GrpcServerProperties;
4245
import net.devh.boot.grpc.server.config.GrpcServerProperties.Security;
@@ -64,13 +67,13 @@ public NettyGrpcServerFactory(final GrpcServerProperties properties,
6467
protected NettyServerBuilder newServerBuilder() {
6568
final String address = getAddress();
6669
final int port = getPort();
67-
if (address.startsWith(GrpcServerProperties.DOMAIN_SOCKET_ADDRESS_PREFIX)) {
68-
String path = address.substring(GrpcServerProperties.DOMAIN_SOCKET_ADDRESS_PREFIX.length());
70+
if (address.startsWith(DOMAIN_SOCKET_ADDRESS_PREFIX)) {
71+
final String path = GrpcUtils.extractDomainSocketAddressPath(address);
6972
return NettyServerBuilder.forAddress(new DomainSocketAddress(path))
7073
.channelType(EpollServerDomainSocketChannel.class)
7174
.bossEventLoopGroup(new EpollEventLoopGroup(1))
7275
.workerEventLoopGroup(new EpollEventLoopGroup());
73-
} else if (GrpcServerProperties.ANY_IP_ADDRESS.equals(address)) {
76+
} else if (ANY_IP_ADDRESS.equals(address)) {
7477
return NettyServerBuilder.forPort(port);
7578
} else {
7679
return NettyServerBuilder.forAddress(new InetSocketAddress(InetAddresses.forString(address), port));

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/ShadedNettyGrpcServerFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package net.devh.boot.grpc.server.serverfactory;
1919

2020
import static java.util.Objects.requireNonNull;
21+
import static net.devh.boot.grpc.common.util.GrpcUtils.DOMAIN_SOCKET_ADDRESS_PREFIX;
22+
import static net.devh.boot.grpc.server.config.GrpcServerProperties.ANY_IP_ADDRESS;
2123

2224
import java.io.IOException;
2325
import java.io.InputStream;
@@ -37,6 +39,7 @@
3739
import io.grpc.netty.shaded.io.netty.channel.epoll.EpollServerDomainSocketChannel;
3840
import io.grpc.netty.shaded.io.netty.channel.unix.DomainSocketAddress;
3941
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
42+
import net.devh.boot.grpc.common.util.GrpcUtils;
4043
import net.devh.boot.grpc.server.config.ClientAuth;
4144
import net.devh.boot.grpc.server.config.GrpcServerProperties;
4245
import net.devh.boot.grpc.server.config.GrpcServerProperties.Security;
@@ -65,13 +68,13 @@ public ShadedNettyGrpcServerFactory(final GrpcServerProperties properties,
6568
protected NettyServerBuilder newServerBuilder() {
6669
final String address = getAddress();
6770
final int port = getPort();
68-
if (address.startsWith(GrpcServerProperties.DOMAIN_SOCKET_ADDRESS_PREFIX)) {
69-
final String path = address.substring(GrpcServerProperties.DOMAIN_SOCKET_ADDRESS_PREFIX.length());
71+
if (address.startsWith(DOMAIN_SOCKET_ADDRESS_PREFIX)) {
72+
final String path = GrpcUtils.extractDomainSocketAddressPath(address);
7073
return NettyServerBuilder.forAddress(new DomainSocketAddress(path))
7174
.channelType(EpollServerDomainSocketChannel.class)
7275
.bossEventLoopGroup(new EpollEventLoopGroup(1))
7376
.workerEventLoopGroup(new EpollEventLoopGroup());
74-
} else if (GrpcServerProperties.ANY_IP_ADDRESS.equals(address)) {
77+
} else if (ANY_IP_ADDRESS.equals(address)) {
7578
return NettyServerBuilder.forPort(port);
7679
} else {
7780
return NettyServerBuilder.forAddress(new InetSocketAddress(InetAddresses.forString(address), port));

0 commit comments

Comments
 (0)