Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-invocation-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-generator-core</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/servicecomb/core/CoreConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private CoreConst() {

public static final String CSE_CONTEXT = "x-cse-context";

public static final String TRANSPORT_NAME = "x-transport-name";

public static final String RESTFUL = "rest";

public static final String HIGHWAY = "highway";
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/org/apache/servicecomb/core/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,22 @@ public String getOperationName() {
return operationMeta.getOperationId();
}

public String getProviderTransportName() {
if (operationMeta.getSwaggerOperation().getExtensions() != null &&
operationMeta.getSwaggerOperation().getExtensions().get(CoreConst.TRANSPORT_NAME) != null) {
return (String) operationMeta.getSwaggerOperation().getExtensions().get(CoreConst.TRANSPORT_NAME);
}
if (schemaMeta.getSwagger().getExtensions() != null &&
schemaMeta.getSwagger().getExtensions().get(CoreConst.TRANSPORT_NAME) != null) {
return (String) schemaMeta.getSwagger().getExtensions().get(CoreConst.TRANSPORT_NAME);
}
return null;
}

public String getConfigTransportName() {
if (getProviderTransportName() != null) {
return getProviderTransportName();
}
return referenceConfig.getTransport();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.servicecomb.core.annotation;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

@Inherited
@Documented
@Retention(RUNTIME)
@Target({TYPE, METHOD, ANNOTATION_TYPE})
@Component
public @interface Transport {
/**
* Transport name. e.g. rest, highway.
*/
String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;

import jakarta.ws.rs.core.Response.Status;

public class ProviderOperationFilter extends AbstractFilter implements ProviderFilter {
public static final String NAME = "producer-operation";
Expand All @@ -46,6 +50,10 @@ public int getOrder() {

@Override
public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode nextNode) {
if (!transportAccessAllowed(invocation)) {
return CompletableFuture.failedFuture(new InvocationException(Status.UNAUTHORIZED,
new CommonExceptionData("transport access not allowed.")));
}
invocation.onBusinessMethodStart();

SwaggerProducerOperation producerOperation = invocation.getOperationMeta().getSwaggerProducerOperation();
Expand All @@ -57,6 +65,13 @@ public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode ne
.whenComplete((response, throwable) -> processMetrics(invocation));
}

private boolean transportAccessAllowed(Invocation invocation) {
if (invocation.getProviderTransportName() == null) {
return true;
}
return invocation.getProviderTransportName().equals(invocation.getTransportName());
}

@SuppressWarnings("unchecked")
protected CompletableFuture<Object> invoke(Invocation invocation, Object instance, Method method, Object[] args) {
ContextUtils.setInvocationContext(invocation);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.servicecomb.core.transport;

import java.lang.reflect.Type;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.core.CoreConst;
import org.apache.servicecomb.core.annotation.Transport;
import org.apache.servicecomb.swagger.generator.ClassAnnotationProcessor;
import org.apache.servicecomb.swagger.generator.SwaggerGenerator;

import io.swagger.v3.oas.models.OpenAPI;

public class TransportClassAnnotationProcessor implements ClassAnnotationProcessor<Transport> {
@Override
public Type getProcessType() {
return Transport.class;
}

@Override
public void process(SwaggerGenerator swaggerGenerator, Transport transport) {
OpenAPI swagger = swaggerGenerator.getOpenAPI();
if (StringUtils.isNotEmpty(transport.name())) {
swagger.addExtension(CoreConst.TRANSPORT_NAME, transport.name());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.servicecomb.core.transport;

import java.lang.reflect.Type;

import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.core.CoreConst;
import org.apache.servicecomb.core.annotation.Transport;
import org.apache.servicecomb.swagger.generator.MethodAnnotationProcessor;
import org.apache.servicecomb.swagger.generator.OperationGenerator;
import org.apache.servicecomb.swagger.generator.SwaggerGenerator;

import io.swagger.v3.oas.models.Operation;

public class TransportMethodAnnotationProcessor implements MethodAnnotationProcessor<Transport> {
@Override
public Type getProcessType() {
return Transport.class;
}

@Override
public void process(SwaggerGenerator swaggerGenerator, OperationGenerator operationGenerator, Transport transport) {
Operation operation = operationGenerator.getOperation();
if (StringUtils.isNotEmpty(transport.name())) {
operation.addExtension(CoreConst.TRANSPORT_NAME, transport.name());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.servicecomb.core.transport.TransportClassAnnotationProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.servicecomb.core.transport.TransportMethodAnnotationProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ public TestDateTimeSchema() {

}

@Override
public void testRestTransport() throws Exception {

}

@Override
public void testHighwayTransport() throws Exception {

}

@Override
public void testAllTransport() throws Exception {
testDateTimeSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,4 @@ private void testUrlNotLongerThan4096() {
TestMgr.check(REQUEST_URI_TOO_LONG.getStatusCode(), e.getStatusCode());
}
}

@Override
public void testHighwayTransport() throws Exception {

}

@Override
public void testAllTransport() throws Exception {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.servicecomb.demo.springmvc.client;

import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.springframework.stereotype.Component;

@Component
public class TestTransportSchema implements CategorizedTestCase {
interface TransportClient {
boolean restTransport();

boolean highwayTransport();
}

@RpcReference(microserviceName = "springmvc", schemaId = "TransportSchema")
private TransportClient transportClient;

@Override
public void testRestTransport() throws Exception {
testTransportSchema();
}

@Override
public void testHighwayTransport() throws Exception {
testTransportSchema();
}

@Override
public void testAllTransport() throws Exception {
testTransportSchema();
}

private void testTransportSchema() {
TestMgr.check(true, transportClient.highwayTransport());
TestMgr.check(true, transportClient.restTransport());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@ public class TestWeakSpringmvc implements CategorizedTestCase {

private RestOperations restTemplate = RestTemplateBuilder.create();

@Override
public void testRestTransport() throws Exception {

}

@Override
public void testHighwayTransport() throws Exception {

}

@Override
public void testAllTransport() throws Exception {
testDiffName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.servicecomb.demo.springmvc.server;

import org.apache.servicecomb.core.CoreConst;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.annotation.Transport;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestSchema(schemaId = "TransportSchema")
@RequestMapping(path = "/transport")
public class TransportSchema {
@GetMapping(path = "/restTransport")
@Transport(name = CoreConst.RESTFUL)
public boolean restTransport(InvocationContext invocation) {
return CoreConst.RESTFUL.equals(((Invocation) invocation).getTransportName());
}

@GetMapping(path = "/highwayTransport")
@Transport(name = CoreConst.HIGHWAY)
public boolean highwayTransport(InvocationContext invocation) {
return CoreConst.HIGHWAY.equals(((Invocation) invocation).getTransportName());
}
}
Loading