Skip to content

Commit 118e176

Browse files
andreilisadsyer
authored andcommitted
#8: Codec support
1 parent c07d4de commit 118e176

File tree

5 files changed

+145
-24
lines changed

5 files changed

+145
-24
lines changed

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcChannelFactoryConfigurations.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,11 @@ public GrpcChannelConfigurer secureChannelConfigurer(GrpcClientProperties channe
7676
}
7777

7878
private static io.grpc.netty.shaded.io.grpc.netty.NegotiationType of(final NegotiationType negotiationType) {
79-
switch (negotiationType) {
80-
case PLAINTEXT:
81-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT;
82-
case PLAINTEXT_UPGRADE:
83-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
84-
case TLS:
85-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.TLS;
86-
default:
87-
throw new IllegalArgumentException("Unsupported NegotiationType: " + negotiationType);
88-
}
79+
return switch (negotiationType) {
80+
case PLAINTEXT -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT;
81+
case PLAINTEXT_UPGRADE -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
82+
case TLS -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.TLS;
83+
};
8984
}
9085

9186
}
@@ -126,16 +121,11 @@ public GrpcChannelConfigurer secureChannelConfigurer(GrpcClientProperties channe
126121
}
127122

128123
private static io.grpc.netty.NegotiationType of(final NegotiationType negotiationType) {
129-
switch (negotiationType) {
130-
case PLAINTEXT:
131-
return io.grpc.netty.NegotiationType.PLAINTEXT;
132-
case PLAINTEXT_UPGRADE:
133-
return io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
134-
case TLS:
135-
return io.grpc.netty.NegotiationType.TLS;
136-
default:
137-
throw new IllegalArgumentException("Unsupported NegotiationType: " + negotiationType);
138-
}
124+
return switch (negotiationType) {
125+
case PLAINTEXT -> io.grpc.netty.NegotiationType.PLAINTEXT;
126+
case PLAINTEXT_UPGRADE -> io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
127+
case TLS -> io.grpc.netty.NegotiationType.TLS;
128+
};
139129
}
140130

141131
}
@@ -151,8 +141,7 @@ static class NamedChannelVirtualTargets implements VirtualTargets {
151141
@Override
152142
public String getTarget(String authority) {
153143
NamedChannel channel = this.channels.getChannel(authority);
154-
String address = channels.getTarget(channel.getAddress());
155-
return address;
144+
return channels.getTarget(channel.getAddress());
156145
}
157146

158147
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientAutoConfiguration.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717

1818
import java.util.concurrent.TimeUnit;
1919

20+
import io.grpc.CompressorRegistry;
21+
import io.grpc.DecompressorRegistry;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2023
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2124
import org.springframework.boot.ssl.SslBundle;
2225
import org.springframework.boot.ssl.SslBundles;
2326
import org.springframework.context.annotation.Bean;
2427
import org.springframework.context.annotation.Configuration;
2528
import org.springframework.context.annotation.Import;
2629
import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.NamedChannel;
30+
import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration;
2731
import org.springframework.grpc.client.GrpcChannelConfigurer;
2832

2933
@Configuration(proxyBeanMethods = false)
3034
@EnableConfigurationProperties(GrpcClientProperties.class)
3135
@Import({ GrpcChannelFactoryConfigurations.ShadedNettyChannelFactoryConfiguration.class,
32-
GrpcChannelFactoryConfigurations.NettyChannelFactoryConfiguration.class })
36+
GrpcChannelFactoryConfigurations.NettyChannelFactoryConfiguration.class, GrpcCodecConfiguration.class })
3337
public class GrpcClientAutoConfiguration {
3438

3539
@Bean
@@ -80,4 +84,16 @@ else if (ShadedNettyChannelFactoryHelper.isAvailable()) {
8084
};
8185
}
8286

87+
@ConditionalOnBean(CompressorRegistry.class)
88+
@Bean
89+
GrpcChannelConfigurer compressionClientConfigurer(CompressorRegistry registry) {
90+
return (name, builder) -> builder.compressorRegistry(registry);
91+
}
92+
93+
@ConditionalOnBean(DecompressorRegistry.class)
94+
@Bean
95+
GrpcChannelConfigurer decompressionClientConfigurer(DecompressorRegistry registry) {
96+
return (name, builder) -> builder.decompressorRegistry(registry);
97+
}
98+
8399
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.autoconfigure.common.codec;
18+
19+
import io.grpc.Codec;
20+
import io.grpc.Compressor;
21+
import io.grpc.Decompressor;
22+
import io.grpc.DecompressorRegistry;
23+
import io.grpc.CompressorRegistry;
24+
import org.springframework.beans.factory.ObjectProvider;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
/**
30+
* The configuration that contains all codec related beans for clients/servers.
31+
*
32+
* @author Andrei Lisa
33+
*/
34+
@Configuration(proxyBeanMethods = false)
35+
@ConditionalOnClass(Codec.class)
36+
public class GrpcCodecConfiguration {
37+
38+
@Bean
39+
CompressorRegistry compressorRegistry(ObjectProvider<Compressor> compressors) {
40+
CompressorRegistry registry = CompressorRegistry.getDefaultInstance();
41+
compressors.orderedStream().forEachOrdered(registry::register);
42+
return registry;
43+
}
44+
45+
@Bean
46+
DecompressorRegistry decompressorRegistry(ObjectProvider<Decompressor> decompressors) {
47+
DecompressorRegistry registry = DecompressorRegistry.getDefaultInstance();
48+
decompressors.orderedStream().forEachOrdered(decompressor -> registry.with(decompressor, false));
49+
return registry;
50+
}
51+
52+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import io.grpc.BindableService;
1919

20+
import io.grpc.CompressorRegistry;
21+
import io.grpc.DecompressorRegistry;
22+
import io.grpc.netty.NettyServerBuilder;
2023
import org.springframework.beans.factory.ObjectProvider;
2124
import org.springframework.boot.autoconfigure.AutoConfiguration;
2225
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
@@ -30,6 +33,7 @@
3033
import org.springframework.context.annotation.Conditional;
3134
import org.springframework.context.annotation.Import;
3235
import org.springframework.core.Ordered;
36+
import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration;
3337
import org.springframework.grpc.server.GrpcServerFactory;
3438
import org.springframework.grpc.server.ServerBuilderCustomizer;
3539
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
@@ -49,7 +53,7 @@
4953
@ConditionalOnBean(BindableService.class)
5054
@EnableConfigurationProperties(GrpcServerProperties.class)
5155
@Import({ GrpcServerFactoryConfigurations.ShadedNettyServerFactoryConfiguration.class,
52-
GrpcServerFactoryConfigurations.NettyServerFactoryConfiguration.class })
56+
GrpcServerFactoryConfigurations.NettyServerFactoryConfiguration.class, GrpcCodecConfiguration.class })
5357
public class GrpcServerAutoConfiguration {
5458

5559
private final GrpcServerProperties properties;
@@ -71,4 +75,16 @@ ServerBuilderCustomizers serverBuilderCustomizers(ObjectProvider<ServerBuilderCu
7175
return new ServerBuilderCustomizers(customizers.orderedStream().toList());
7276
}
7377

78+
@ConditionalOnBean(CompressorRegistry.class)
79+
@Bean
80+
ServerBuilderCustomizer<NettyServerBuilder> compressionServerConfigurer(CompressorRegistry registry) {
81+
return builder -> builder.compressorRegistry(registry);
82+
}
83+
84+
@ConditionalOnBean(DecompressorRegistry.class)
85+
@Bean
86+
ServerBuilderCustomizer<NettyServerBuilder> decompressionServerConfigurer(DecompressorRegistry registry) {
87+
return builder -> builder.decompressorRegistry(registry);
88+
}
89+
7490
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.autoconfigure.common;
18+
19+
import io.grpc.CompressorRegistry;
20+
import io.grpc.DecompressorRegistry;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link GrpcCodecConfiguration}.
30+
*
31+
* @author Andrei Lisa
32+
*/
33+
public class GrpcCodecConfigurationTest {
34+
35+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
36+
.withConfiguration(AutoConfigurations.of(GrpcCodecConfiguration.class));
37+
38+
@Test
39+
void testCompressorRegistryBean() {
40+
contextRunner.run(context -> assertThat(context).hasSingleBean(CompressorRegistry.class));
41+
}
42+
43+
@Test
44+
void testDecompressorRegistryBean() {
45+
contextRunner.run(context -> assertThat(context).hasSingleBean(DecompressorRegistry.class));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)