oxsean commented on code in PR #14073:
URL: https://github.com/apache/dubbo/pull/14073#discussion_r1578018213
##########
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/DefaultRequestMappingRegistry.java:
##########
@@ -61,8 +61,7 @@ public DefaultRequestMappingRegistry(FrameworkModel
frameworkModel) {
public void register(Invoker<?> invoker) {
Object service = invoker.getUrl().getServiceModel().getProxyObject();
new MethodWalker().walk(service.getClass(), (classes, consumer) -> {
- for (int i = 0, size = resolvers.size(); i < size; i++) {
- RequestMappingResolver resolver = resolvers.get(i);
+ for (RequestMappingResolver resolver : resolvers) {
Review Comment:
The enhanced index loop can avoid creating iterator object. Although it's a
minor optimization, I think it's unnecessary to switch to an iterator loop.
##########
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/filter/CorsHeaderFilterAdapter.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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 org.apache.dubbo.rpc.protocol.tri.rest.filter;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.remoting.http12.HttpRequest;
+import org.apache.dubbo.remoting.http12.HttpResponse;
+import org.apache.dubbo.remoting.http12.HttpResult;
+import org.apache.dubbo.remoting.http12.HttpStatus;
+import org.apache.dubbo.remoting.http12.exception.HttpResultPayloadException;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.rpc.protocol.tri.rest.RestConstants;
+import org.apache.dubbo.rpc.protocol.tri.rest.cors.CorsProcessor;
+import org.apache.dubbo.rpc.protocol.tri.rest.mapping.RequestMapping;
+
+@Activate(group = CommonConstants.PROVIDER, order = 1000)
+public class CorsHeaderFilterAdapter extends RestHeaderFilterAdapter {
Review Comment:
Move into package 'org.apache.dubbo.rpc.protocol.tri.rest.cors' and rename
to CorsHeaderFilter
##########
dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.HeaderFilter:
##########
@@ -0,0 +1 @@
+rest-cors-extension=org.apache.dubbo.rpc.protocol.tri.rest.filter.CorsHeaderFilterAdapter
Review Comment:
rest-cors=...
##########
dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/cors/CorsUtils.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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 org.apache.dubbo.rpc.protocol.tri.rest.cors;
+
+import org.apache.dubbo.common.config.Configuration;
+import org.apache.dubbo.common.config.ConfigurationUtils;
+import org.apache.dubbo.common.lang.Nullable;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.protocol.tri.rest.RestConstants;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CorsUtils {
+ private static CorsMeta globalCorsMeta;
+ public static final String HTTP = "http";
+ public static final String HTTPS = "https";
+ public static final String WS = "ws";
+ public static final String WSS = "wss";
+
+ private CorsUtils() {}
+
+ public static int getPort(String scheme, int port) {
+ if (port == -1) {
+ if (HTTP.equals(scheme) || WS.equals(scheme)) {
+ port = 80;
+ } else if (HTTPS.equals(scheme) || WSS.equals(scheme)) {
+ port = 443;
+ }
+ }
+ return port;
+ }
+
+ public static CorsMeta resolveGlobalMeta(Configuration config) {
+
+ // Get the CORS configuration properties from the configuration object.
+ String allowOrigins = config.getString(RestConstants.ALLOWED_ORIGINS);
+ String allowMethods = config.getString(RestConstants.ALLOWED_METHODS);
+ String allowHeaders = config.getString(RestConstants.ALLOWED_HEADERS);
+ String exposeHeaders = config.getString(RestConstants.EXPOSED_HEADERS);
+ String maxAge = config.getString(RestConstants.MAX_AGE);
+ // Create a new CorsMeta object and set the properties.
+ CorsMeta meta = new CorsMeta();
+ meta.setAllowedOrigins(parseList(allowOrigins));
+ meta.setAllowedMethods(parseList(allowMethods));
+ meta.setAllowedHeaders(parseList(allowHeaders));
+ meta.setExposedHeaders(parseList(exposeHeaders));
+ meta.setMaxAge(maxAge == null ? null : Long.valueOf(maxAge));
+ // Return the CorsMeta object.
+ return meta.applyPermitDefaultValues();
+ }
+
+ @Nullable
+ private static List<String> parseList(@Nullable String value) {
+ if (value == null) {
+ return null;
+ }
+ return
Arrays.stream(value.split(",")).map(String::trim).collect(Collectors.toList());
+ }
+
+ public static CorsMeta getGlobalCorsMeta() {
Review Comment:
If no any global CORS configurations are set, we should consider that there
are no global CORS settings, and CORS function similar to JAX-RS should not be
enabled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]