Skip to content

Commit f7e1d87

Browse files
authored
Merge pull request #70 from reactivegroup/feature/rpc_add_header
feat: rpc add accept header
2 parents f007d0e + 88c6956 commit f7e1d87

File tree

14 files changed

+80
-50
lines changed

14 files changed

+80
-50
lines changed

capa-spi-aws-config/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
<artifactId>capa-spi-aws-infrastructure</artifactId>
3535
</dependency>
3636

37-
<dependency>
38-
<groupId>group.rxcloud</groupId>
39-
<artifactId>capa-foundation</artifactId>
40-
</dependency>
41-
4237
<dependency>
4338
<groupId>software.amazon.awssdk</groupId>
4439
<artifactId>appconfig</artifactId>

capa-spi-aws-config/src/test/java/group/rxcloud/capa/spi/aws/config/AwsCapaConfigStoreTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public void setUp() {
6666
Whitebox.setInternalState(ins, "appConfigAsyncClient", client);
6767
PowerMockito.when(client.getConfiguration(ArgumentMatchers.any(GetConfigurationRequest.class))).thenReturn(mockGetConfigurationRespV1());
6868

69-
7069
SerializerProcessor serializerProcessor = PowerMockito.mock(SerializerProcessor.class);
7170
Whitebox.setInternalState(ins, "serializerProcessor", serializerProcessor);
7271
User user = new User();

capa-spi-aws-infrastructure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<artifactId>capa-sdk-spi</artifactId>
3838
</dependency>
3939

40+
<dependency>
41+
<groupId>group.rxcloud</groupId>
42+
<artifactId>capa-foundation</artifactId>
43+
</dependency>
44+
4045
<!-- unit test -->
4146
<dependency>
4247
<groupId>org.junit.jupiter</groupId>

capa-spi-aws-infrastructure/src/main/java/group/rxcloud/capa/spi/aws/infrastructure/AwsCapaEnvironment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package group.rxcloud.capa.spi.aws.infrastructure;
1818

19+
import group.rxcloud.capa.addons.foundation.CapaFoundation;
20+
import group.rxcloud.capa.addons.foundation.FoundationType;
1921
import group.rxcloud.capa.infrastructure.CapaEnvironment;
2022
import group.rxcloud.capa.infrastructure.CapaProperties;
2123

@@ -39,8 +41,8 @@ public String getDeployRegion() {
3941

4042
@Override
4143
public String getDeployEnv() {
42-
String envKey = Settings.getEnvKey();
43-
return System.getProperty(envKey);
44+
// FIXME: 2021/12/15 use trip logic currently
45+
return CapaFoundation.getEnv(FoundationType.TRIP);
4446
}
4547

4648
abstract static class Settings {

capa-spi-aws-log/pom.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<artifactId>capa-spi-aws-log</artifactId>
3030

3131
<properties>
32-
<log4j.version>2.15.0</log4j.version>
32+
<log4j.version>2.16.0</log4j.version>
3333
<logback.version>1.2.3</logback.version>
3434
<gson.version>2.8.0</gson.version>
3535
<sentinel.core.version>1.8.2</sentinel.core.version>
@@ -40,10 +40,7 @@
4040
<groupId>group.rxcloud</groupId>
4141
<artifactId>capa-spi-aws-infrastructure</artifactId>
4242
</dependency>
43-
<dependency>
44-
<groupId>group.rxcloud</groupId>
45-
<artifactId>capa-foundation</artifactId>
46-
</dependency>
43+
4744
<dependency>
4845
<groupId>group.rxcloud</groupId>
4946
<artifactId>capa-id-generator</artifactId>
@@ -80,14 +77,15 @@
8077
<version>${logback.version}</version>
8178
<optional>true</optional>
8279
</dependency>
83-
<!--log4j-->
80+
<!-- log4j -->
8481
<dependency>
8582
<groupId>org.apache.logging.log4j</groupId>
8683
<artifactId>log4j-core</artifactId>
8784
<version>${log4j.version}</version>
8885
<optional>true</optional>
8986
</dependency>
90-
<!--http-->
87+
88+
<!-- http -->
9189
<dependency>
9290
<groupId>org.apache.httpcomponents</groupId>
9391
<artifactId>httpclient</artifactId>
@@ -101,11 +99,12 @@
10199
<artifactId>sentinel-core</artifactId>
102100
<version>${sentinel.core.version}</version>
103101
</dependency>
102+
104103
<dependency>
105104
<groupId>org.projectlombok</groupId>
106105
<artifactId>lombok</artifactId>
107-
<scope>test</scope>
108106
<version>1.18.22</version>
107+
<scope>test</scope>
109108
</dependency>
110109
<!-- unit test -->
111110
<dependency>

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/AwsCapaRpcProperties.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package group.rxcloud.capa.spi.aws.mesh;
1818

19+
import group.rxcloud.capa.addons.foundation.CapaFoundation;
20+
import group.rxcloud.capa.addons.foundation.FoundationType;
1921
import group.rxcloud.capa.infrastructure.CapaProperties;
2022
import group.rxcloud.capa.infrastructure.exceptions.CapaErrorContext;
2123
import group.rxcloud.capa.infrastructure.exceptions.CapaException;
@@ -55,7 +57,8 @@ abstract class Settings {
5557
throw new CapaException(CapaErrorContext.PARAMETER_ERROR, "Rpc Port: " + awsRpcAppMeshPort);
5658
}
5759

58-
rpcAwsAppMeshNamespace = properties.getProperty(RPC_AWS_APP_MESH_NAMESPACE, rpcAwsAppMeshNamespace);
60+
// FIXME: 2021/12/15 use trip logic currently
61+
rpcAwsAppMeshNamespace = CapaFoundation.getNamespace(FoundationType.TRIP);
5962
}
6063

6164
public static Integer getRpcAwsAppMeshPort() {

capa-spi-aws-mesh/src/main/java/group/rxcloud/capa/spi/aws/mesh/http/AwsCapaHttp.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import org.slf4j.LoggerFactory;
3636
import software.amazon.awssdk.utils.StringUtils;
3737

38+
import java.util.ArrayList;
3839
import java.util.List;
3940
import java.util.Map;
4041
import java.util.Objects;
4142
import java.util.concurrent.CompletableFuture;
43+
import java.util.stream.Collectors;
4244

4345

4446
/**
@@ -80,6 +82,30 @@ protected <T> CompletableFuture<HttpResponse<T>> invokeSpiApi(String appId,
8082
(AwsRpcServiceOptions) rpcServiceOptions);
8183
}
8284

85+
private static final String ACCEPT_KEY = "accept";
86+
private static final String ACCEPT_ALL = "*/*";
87+
88+
private void setRequestHeaderOfAccept(Map<String, String> headers, RequestBody body) {
89+
final List<String> accepts = new ArrayList<>(3);
90+
// 1. set user accept header
91+
final String userAcceptValue = headers.get(ACCEPT_KEY);
92+
if (userAcceptValue != null && userAcceptValue.length() > 0) {
93+
accepts.add(userAcceptValue);
94+
}
95+
// 2. set accept header same with content-type
96+
final String contentType = body.contentType().toString();
97+
if (contentType != null && contentType.length() > 0) {
98+
accepts.add(contentType);
99+
}
100+
// 3. add */* at last
101+
accepts.add(ACCEPT_ALL);
102+
103+
final String acceptStr = accepts.stream()
104+
.distinct()
105+
.collect(Collectors.joining(","));
106+
headers.put(ACCEPT_KEY, acceptStr);
107+
}
108+
83109
private interface AwsHttpInvoker {
84110

85111
/**
@@ -198,6 +224,9 @@ private <T> CompletableFuture<HttpResponse<T>> invokeHttp(String url,
198224
TypeRef<T> type) {
199225
// generate http request body
200226
RequestBody body = getRequestBodyWithSerialize(requestData, headers);
227+
228+
setRequestHeaderOfAccept(headers, body);
229+
201230
Headers header = getRequestHeaderWithParams(headers);
202231

203232
// make http request

capa-spi-aws-mesh/src/main/resources/capa-component-rpc-aws.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RPC_AWS_APP_MESH_TEMPLATE=http://{serviceId}.{namespace}.svc.cluster.local:{serv
55
RPC_AWS_APP_MESH_PORT=8080
66
#
77
# rpc aws app mesh namespace
8-
RPC_AWS_APP_MESH_NAMESPACE=FWS
8+
RPC_AWS_APP_MESH_NAMESPACE=meshnamespace
99
#
1010
# rpc aws app mesh serializer
1111
RPC_AWS_APP_MESH_SERIALIZER=baiji

capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/env/AwsRpcEnvironmentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public void testGetSerializer_SuccessWhenDefault() {
3737
@Test
3838
public void testGetNamespace_SuccessWhenDefault() {
3939
String namespace = AwsCapaRpcProperties.AppMeshProperties.Settings.getRpcAwsAppMeshNamespace();
40-
Assertions.assertEquals("FWS", namespace);
40+
Assertions.assertNull(namespace);
4141
}
4242
}

capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/http/AwsCapaHttpTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@ public void setUp() {
5050
@Test
5151
public void testInvokeSpiApi_Success() {
5252
AwsSpiOptionsLoader awsSpiOptionsLoader = new AwsSpiOptionsLoader();
53-
AwsRpcServiceOptions awsRpcServiceOptions = awsSpiOptionsLoader.loadRpcServiceOptions("appId");
5453

55-
CompletableFuture<HttpResponse<String>> responseCompletableFuture = awsCapaHttp.invokeSpiApi("appId",
56-
"method",
57-
"requestData",
58-
"POST",
59-
new HashMap<>(),
60-
new HashMap<>(),
61-
TypeRef.STRING,
62-
awsRpcServiceOptions);
54+
Assertions.assertThrows(NullPointerException.class, () -> {
55+
AwsRpcServiceOptions awsRpcServiceOptions = awsSpiOptionsLoader.loadRpcServiceOptions("appId");
6356

64-
responseCompletableFuture.cancel(true);
57+
CompletableFuture<HttpResponse<String>> responseCompletableFuture = awsCapaHttp.invokeSpiApi("appId",
58+
"method",
59+
"requestData",
60+
"POST",
61+
new HashMap<>(),
62+
new HashMap<>(),
63+
TypeRef.STRING,
64+
awsRpcServiceOptions);
65+
66+
responseCompletableFuture.cancel(true);
67+
});
6568
}
6669

6770
@Test

0 commit comments

Comments
 (0)