feat: support TagUtils, ContextToHeaderInterceptor in TSF(2024). (#1754)
parent
1c01fccb62
commit
75f72ab023
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 org.springframework.tsf.core.filter;
|
||||
|
||||
/**
|
||||
* Compatible with old versions TSF SDK.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public interface ContextToHeaderInterceptor {
|
||||
|
||||
void beforeContextToHeader();
|
||||
|
||||
void afterContextToHeader();
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 org.springframework.tsf.core.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import com.tencent.cloud.common.util.TsfTagUtils;
|
||||
|
||||
import org.springframework.tsf.core.entity.Metadata;
|
||||
import org.springframework.tsf.core.entity.Tag;
|
||||
|
||||
/**
|
||||
* Compatible with old versions TSF SDK.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public final class TagUtils {
|
||||
|
||||
private TagUtils() {
|
||||
}
|
||||
|
||||
public static <T> String serializeToJson(T object) {
|
||||
return JacksonUtils.serialize2Json(object);
|
||||
}
|
||||
|
||||
public static List<Tag> deserializeTagList(String buffer) {
|
||||
return TsfTagUtils.deserializeTagList(buffer);
|
||||
}
|
||||
|
||||
public static Metadata deserializeMetadata(String buffer) {
|
||||
return TsfTagUtils.deserializeMetadata(buffer);
|
||||
}
|
||||
|
||||
public static List<Tag> getTagListFromTagMap(Map<String, Tag> tagMap) {
|
||||
return new ArrayList<>(tagMap.values());
|
||||
}
|
||||
|
||||
public static Map<String, Tag> buildTagMapFromTagList(List<Tag> tagList) {
|
||||
Map<String, Tag> result = new HashMap<>();
|
||||
for (Tag tag : tagList) {
|
||||
result.put(tag.getKey(), tag);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 org.springframework.tsf.core.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import com.tencent.cloud.common.util.TsfTagUtils;
|
||||
import com.tencent.cloud.common.util.UrlUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.tsf.core.entity.Metadata;
|
||||
import org.springframework.tsf.core.entity.Tag;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link TagUtils}.
|
||||
*/
|
||||
public class TagUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testSerializeDeserialize() {
|
||||
Tag tag = new Tag("key", "value");
|
||||
String json = TagUtils.serializeToJson(Collections.singletonList(tag));
|
||||
|
||||
List<Tag> tagList = TagUtils.deserializeTagList(json);
|
||||
assertThat(tagList).isEqualTo(Collections.singletonList(tag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildTagMapFromTagList() {
|
||||
Tag tag = new Tag("key", "value");
|
||||
List<Tag> tagList = Collections.singletonList(tag);
|
||||
Map<String, Tag> tagMap = TagUtils.buildTagMapFromTagList(tagList);
|
||||
List<Tag> tagList2 = TagUtils.getTagListFromTagMap(tagMap);
|
||||
|
||||
assertThat(tagList2).isEqualTo(tagList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserializeMetadata() {
|
||||
String encodedInput = "%7B%22ai%22%3A%22applicationId%22%2C%22av%22%3A%22applicationVersion%22%2C%22sn%22%3A%22serviceName%22%2C%22ii%22%3A%22instanceId%22%2C%22gi%22%3A%22groupId%22%2C%22li%22%3A%22127.0.0.1%22%2C%22lis%22%3A%22127.0.0.1%22%2C%22ni%22%3A%22namespaceId%22%2C%22pi%22%3Atrue%7D";
|
||||
Metadata expectedMetadata = new Metadata();
|
||||
expectedMetadata.setApplicationId("applicationId");
|
||||
expectedMetadata.setApplicationVersion("applicationVersion");
|
||||
expectedMetadata.setServiceName("serviceName");
|
||||
expectedMetadata.setInstanceId("instanceId");
|
||||
expectedMetadata.setGroupId("groupId");
|
||||
expectedMetadata.setLocalIp("127.0.0.1");
|
||||
expectedMetadata.setLocalIps("127.0.0.1");
|
||||
expectedMetadata.setNamespaceId("namespaceId");
|
||||
expectedMetadata.setPreferIpv6(true);
|
||||
|
||||
String json = JacksonUtils.serialize2Json(expectedMetadata);
|
||||
String buffer = UrlUtils.encode(json);
|
||||
System.out.println(buffer);
|
||||
|
||||
Metadata result = TagUtils.deserializeMetadata(encodedInput);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.toString()).isEqualTo(expectedMetadata.toString());
|
||||
assertThat(result.getApplicationId()).isEqualTo(expectedMetadata.getApplicationId());
|
||||
assertThat(result.getApplicationVersion()).isEqualTo(expectedMetadata.getApplicationVersion());
|
||||
assertThat(result.getServiceName()).isEqualTo(expectedMetadata.getServiceName());
|
||||
assertThat(result.getInstanceId()).isEqualTo(expectedMetadata.getInstanceId());
|
||||
assertThat(result.getGroupId()).isEqualTo(expectedMetadata.getGroupId());
|
||||
assertThat(result.getLocalIp()).isEqualTo(expectedMetadata.getLocalIp());
|
||||
assertThat(result.getLocalIps()).isEqualTo(expectedMetadata.getLocalIps());
|
||||
assertThat(result.getNamespaceId()).isEqualTo(expectedMetadata.getNamespaceId());
|
||||
assertThat(result.isPreferIpv6()).isEqualTo(expectedMetadata.isPreferIpv6());
|
||||
|
||||
assertThat(TsfTagUtils.deserializeMetadata(null)).isNull();
|
||||
assertThat(TsfTagUtils.deserializeMetadata("")).isNull();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.tsf.demo.consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.tsf.core.filter.ContextToHeaderInterceptor;
|
||||
|
||||
@Component
|
||||
public class TestContextToHeaderInterceptor implements ContextToHeaderInterceptor {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TestContextToHeaderInterceptor.class);
|
||||
|
||||
@Override
|
||||
public void beforeContextToHeader() {
|
||||
LOG.info("beforeContextToHeader");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterContextToHeader() {
|
||||
LOG.info("afterContextToHeader");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue