From 8b5de7f66c62073dfa3fac60ca5ea0b7084df00b Mon Sep 17 00:00:00 2001 From: Orez Date: Wed, 23 Jun 2021 14:24:11 +0800 Subject: [PATCH 1/3] use filter to set custom header --- .../filter/ConsumerCustomHeaderFilter.java | 42 +++++++++++++++++++ .../com.alipay.sofa.rpc.filter.Filter | 3 +- .../sofa/rpc/context/RpcInternalContext.java | 29 +++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java diff --git a/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java new file mode 100644 index 000000000..8d74a1c87 --- /dev/null +++ b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java @@ -0,0 +1,42 @@ +/** + * Alipay.com Inc. + * Copyright (c) 2004-2021 All Rights Reserved. + */ +package com.alipay.sofa.rpc.filter; + +import com.alipay.sofa.common.utils.Ordered; +import com.alipay.sofa.rpc.common.utils.CommonUtils; +import com.alipay.sofa.rpc.context.RpcInternalContext; +import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.alipay.sofa.rpc.core.request.SofaRequest; +import com.alipay.sofa.rpc.core.response.SofaResponse; +import com.alipay.sofa.rpc.ext.Extension; + +import java.util.Map; + +/** + * Set customHeader to SofaRequest.requestProps + * + * @author zhaowang + * @version : ConsumerCustomHeaderFilter.java, v 0.1 2021年06月23日 2:05 下午 zhaowang + */ +@AutoActive(consumerSide = true) +@Extension(value = "consumerException",order = Ordered.LOWEST_PRECEDENCE) +public class ConsumerCustomHeaderFilter extends Filter{ + + + @Override + public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { + setCustomHeader(request); + return invoker.invoke(request); + } + + private void setCustomHeader(SofaRequest sofaRequest) { + RpcInternalContext context = RpcInternalContext.getContext(); + Map customHeader = context.getCustomHeader(); + if (CommonUtils.isNotEmpty(customHeader)) { + sofaRequest.addRequestProps(customHeader); + } + context.clearCustomHeader(); + } +} \ No newline at end of file diff --git a/core-impl/filter/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.filter.Filter b/core-impl/filter/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.filter.Filter index 7a7ca5734..b0b7a5616 100644 --- a/core-impl/filter/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.filter.Filter +++ b/core-impl/filter/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.filter.Filter @@ -1,3 +1,4 @@ # name # order com.alipay.sofa.rpc.filter.ProviderExceptionFilter # -20000 -com.alipay.sofa.rpc.filter.ConsumerExceptionFilter # -20000 \ No newline at end of file +com.alipay.sofa.rpc.filter.ConsumerExceptionFilter # -20000 +com.alipay.sofa.rpc.filter.ConsumerCustomHeaderFilter # Ordered.LOWEST_PRECEDENCE \ No newline at end of file diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java b/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java index 431b037d9..67c407942 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java @@ -28,6 +28,7 @@ import java.net.InetSocketAddress; import java.util.ArrayDeque; import java.util.Deque; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -184,6 +185,13 @@ protected RpcInternalContext() { */ private ProviderInfo providerInfo; + + /** + * 自定义 header ,用完一次即删 + */ + protected HashMap customHeader = new HashMap<>(); + + /** * Is provider side. * @@ -499,4 +507,25 @@ static boolean isHiddenParamKey(String key) { char c = key.charAt(0); return c == RpcConstants.HIDE_KEY_PREFIX; } + + + public Map getCustomHeader() { + return new HashMap<>(customHeader); + } + + /** + * 设置请求头,与 RequestBaggage 相比 + * 1. 不受 enable baggage 开关影响,始终生效 + * 2. 仅对一次调用生效,调用完成之会被清空 + * + * @param key header key + * @param value header value + */ + public void putCustomHeader(String key, String value) { + customHeader.put(key, value); + } + + public void clearCustomHeader() { + customHeader.clear(); + } } \ No newline at end of file From a6b1020bed0b694e209051a8cb64469c077234fa Mon Sep 17 00:00:00 2001 From: Orez Date: Wed, 23 Jun 2021 14:37:38 +0800 Subject: [PATCH 2/3] add unit test --- .../filter/ConsumerCustomHeaderFilter.java | 23 +++++-- .../rpc/filter/CustomHeaderFilterTest.java | 63 +++++++++++++++++++ .../sofa/rpc/context/RpcInternalContext.java | 2 +- 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 core-impl/filter/src/test/java/com/alipay/sofa/rpc/filter/CustomHeaderFilterTest.java diff --git a/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java index 8d74a1c87..caa52171a 100644 --- a/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java +++ b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java @@ -1,6 +1,18 @@ -/** - * Alipay.com Inc. - * Copyright (c) 2004-2021 All Rights Reserved. +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.sofa.rpc.filter; @@ -21,9 +33,8 @@ * @version : ConsumerCustomHeaderFilter.java, v 0.1 2021年06月23日 2:05 下午 zhaowang */ @AutoActive(consumerSide = true) -@Extension(value = "consumerException",order = Ordered.LOWEST_PRECEDENCE) -public class ConsumerCustomHeaderFilter extends Filter{ - +@Extension(value = "consumerException", order = Ordered.LOWEST_PRECEDENCE) +public class ConsumerCustomHeaderFilter extends Filter { @Override public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException { diff --git a/core-impl/filter/src/test/java/com/alipay/sofa/rpc/filter/CustomHeaderFilterTest.java b/core-impl/filter/src/test/java/com/alipay/sofa/rpc/filter/CustomHeaderFilterTest.java new file mode 100644 index 000000000..be2fc0597 --- /dev/null +++ b/core-impl/filter/src/test/java/com/alipay/sofa/rpc/filter/CustomHeaderFilterTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.alipay.sofa.rpc.filter; + +import com.alipay.sofa.rpc.config.AbstractInterfaceConfig; +import com.alipay.sofa.rpc.context.RpcInternalContext; +import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.alipay.sofa.rpc.core.request.SofaRequest; +import com.alipay.sofa.rpc.core.response.SofaResponse; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +/** + * @author zhaowang + * @version : CustomHeaderFilterTest.java, v 0.1 2021年06月23日 2:26 下午 zhaowang + */ +public class CustomHeaderFilterTest { + + @Test + public void testCustomFilter() { + RpcInternalContext.getContext().addCustomHeader("a", "b"); + ConsumerCustomHeaderFilter filter = new ConsumerCustomHeaderFilter(); + SofaRequest request = new SofaRequest(); + filter.invoke(new EmptyInvoker(null), request); + Assert.assertTrue(RpcInternalContext.getContext().getCustomHeader().isEmpty()); + Assert.assertEquals("b", request.getRequestProp("a")); + } + + static class EmptyInvoker extends FilterInvoker { + + public Map getMetaHolder() { + return metaHolder; + } + + private Map metaHolder; + + protected EmptyInvoker(AbstractInterfaceConfig config) { + super(config); + } + + @Override + public SofaResponse invoke(SofaRequest request) throws SofaRpcException { + return null; + } + } + +} \ No newline at end of file diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java b/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java index 67c407942..b930ab6ca 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/context/RpcInternalContext.java @@ -521,7 +521,7 @@ public Map getCustomHeader() { * @param key header key * @param value header value */ - public void putCustomHeader(String key, String value) { + public void addCustomHeader(String key, String value) { customHeader.put(key, value); } From cb10a542f9280db8807d4d4c50603660cad0be61 Mon Sep 17 00:00:00 2001 From: Orez Date: Wed, 23 Jun 2021 15:23:16 +0800 Subject: [PATCH 3/3] change filter name --- .../com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java index caa52171a..0222e874c 100644 --- a/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java +++ b/core-impl/filter/src/main/java/com/alipay/sofa/rpc/filter/ConsumerCustomHeaderFilter.java @@ -33,7 +33,7 @@ * @version : ConsumerCustomHeaderFilter.java, v 0.1 2021年06月23日 2:05 下午 zhaowang */ @AutoActive(consumerSide = true) -@Extension(value = "consumerException", order = Ordered.LOWEST_PRECEDENCE) +@Extension(value = "consumerCustomHeader", order = Ordered.LOWEST_PRECEDENCE) public class ConsumerCustomHeaderFilter extends Filter { @Override