Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/JAV-402_add_rest_model_field_ign…
Browse files Browse the repository at this point in the history
…ore_demo' into JAV-402_add_rest_model_field_ignore_demo
  • Loading branch information
zhengyangyong committed Oct 12, 2017
2 parents 141a971 + ff63972 commit 6d37e90
Show file tree
Hide file tree
Showing 49 changed files with 1,816 additions and 100 deletions.
Expand Up @@ -62,16 +62,12 @@ public void setHttpServerFilters(List<HttpServerFilter> httpServerFilters) {
this.httpServerFilters = httpServerFilters;
}

protected boolean initProduceProcessor() {
protected void initProduceProcessor() {
produceProcessor = restOperationMeta.ensureFindProduceProcessor(requestEx);
if (produceProcessor == null) {
String msg = String.format("Accept %s is not supported", requestEx.getHeader(HttpHeaders.ACCEPT));
InvocationException exception = new InvocationException(Status.NOT_ACCEPTABLE, msg);
sendFailResponse(exception);
return false;
throw new InvocationException(Status.NOT_ACCEPTABLE, msg);
}

return true;
}

protected void setContext() throws Exception {
Expand All @@ -86,26 +82,31 @@ protected void setContext() throws Exception {
invocation.setContext(cseContext);
}

protected void invoke() {
public void invoke() {
try {
this.initProduceProcessor();
this.setContext();
invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx);

for (HttpServerFilter filter : httpServerFilters) {
Response response = filter.afterReceiveRequest(invocation, requestEx);
if (response != null) {
sendResponseQuietly(response);
return;
}
}
prepareInvoke();

doInvoke();
} catch (Throwable e) {
sendFailResponse(e);
}
}

protected void prepareInvoke() throws Throwable {
this.initProduceProcessor();

this.setContext();
invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx);

for (HttpServerFilter filter : httpServerFilters) {
Response response = filter.afterReceiveRequest(invocation, requestEx);
if (response != null) {
sendResponseQuietly(response);
return;
}
}
}

protected abstract void doInvoke() throws Throwable;

public void sendFailResponse(Throwable throwable) {
Expand All @@ -131,15 +132,15 @@ protected void sendResponseQuietly(Response response) {

@SuppressWarnings("deprecation")
protected void sendResponse(Response response) throws Exception {
responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
responseEx.setContentType(produceProcessor.getName());
if (response.getHeaders().getHeaderMap() != null) {
for (Entry<String, List<Object>> entry : response.getHeaders().getHeaderMap().entrySet()) {
for (Object value : entry.getValue()) {
responseEx.addHeader(entry.getKey(), String.valueOf(value));
}
}
}
responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
responseEx.setContentType(produceProcessor.getName());

Object body = response.getResult();
if (response.isFailed()) {
Expand Down
Expand Up @@ -41,6 +41,7 @@
import io.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import io.servicecomb.foundation.vertx.http.HttpServletResponseEx;
import io.servicecomb.swagger.invocation.Response;
import io.servicecomb.swagger.invocation.exception.CommonExceptionData;
import io.servicecomb.swagger.invocation.exception.InvocationException;
import io.servicecomb.swagger.invocation.response.Headers;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -121,7 +122,13 @@ public void sendFailResponse(Throwable throwable) {
};
initRestInvocation();

Assert.assertFalse(restInvocation.initProduceProcessor());
try {
restInvocation.initProduceProcessor();
Assert.fail("must throw exception");
} catch (InvocationException e) {
Assert.assertEquals(Status.NOT_ACCEPTABLE, e.getStatus());
Assert.assertEquals("Accept null is not supported", ((CommonExceptionData) e.getErrorData()).getMessage());
}
}

@Test
Expand All @@ -133,7 +140,8 @@ public void initProduceProcessorNormal() {
}
};

Assert.assertTrue(restInvocation.initProduceProcessor());
// not throw exception
restInvocation.initProduceProcessor();
}

@Test
Expand Down Expand Up @@ -359,14 +367,14 @@ protected void sendResponse(Response response) throws Exception {
}

@Test
public void sendResponseStatusAndContentType(@Mocked Response response) throws Exception {
public void sendResponseStatusAndContentTypeAndHeader(@Mocked Response response) throws Exception {
new Expectations() {
{
response.getStatusCode();
result = 123;
response.getReasonPhrase();
result = "reason";
response.getHeaders();
response.getResult();
result = new Error("stop");
}
};
Expand Down
25 changes: 4 additions & 21 deletions core/src/main/java/io/servicecomb/core/definition/SchemaUtils.java
Expand Up @@ -18,13 +18,9 @@

import java.net.URL;

import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.servicecomb.swagger.SwaggerUtils;
import io.servicecomb.swagger.generator.core.utils.ClassUtils;
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;

public final class SchemaUtils {

Expand All @@ -39,27 +35,14 @@ public static String generatePackageName(MicroserviceMeta microserviceMeta, Stri
}

public static String swaggerToString(Swagger swagger) {
try {
return Yaml.mapper().writeValueAsString(swagger);
} catch (JsonProcessingException e) {
throw new Error(e);
}
return SwaggerUtils.swaggerToString(swagger);
}

public static Swagger parseSwagger(URL url) {
try {
String swaggerContext = IOUtils.toString(url);
return Yaml.mapper().readValue(swaggerContext, Swagger.class);
} catch (Throwable e) {
throw new Error(e);
}
return SwaggerUtils.parseSwagger(url);
}

public static Swagger parseSwagger(String swaggerContent) {
try {
return Yaml.mapper().readValue(swaggerContent, Swagger.class);
} catch (Throwable e) {
throw new Error(e);
}
return SwaggerUtils.parseSwagger(swaggerContent);
}
}
Expand Up @@ -16,12 +16,14 @@

package io.servicecomb.core.provider.consumer;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.netflix.config.DynamicPropertyFactory;
Expand All @@ -33,8 +35,8 @@

@Component
public class ConsumerProviderManager {
@Inject
private List<ConsumerProvider> consumerProviderList;
@Autowired(required = false)
private List<ConsumerProvider> consumerProviderList = Collections.emptyList();

@Inject
private ConsumerSchemaFactory consumerSchemaFactory;
Expand Down Expand Up @@ -65,12 +67,16 @@ public ReferenceConfig getReferenceConfig(String microserviceName) {
if (config == null) {
String key = "cse.references." + microserviceName;
DynamicStringProperty versionRule = DynamicPropertyFactory.getInstance()
.getStringProperty(key + ".version-rule", DynamicPropertyFactory.getInstance()
.getStringProperty("cse.references.version-rule", Const.VERSION_RULE_LATEST).getValue());
.getStringProperty(key + ".version-rule",
DynamicPropertyFactory.getInstance()
.getStringProperty("cse.references.version-rule", Const.VERSION_RULE_LATEST)
.getValue());
DynamicStringProperty transport =
DynamicPropertyFactory.getInstance().getStringProperty(key + ".transport",
DynamicPropertyFactory.getInstance().getStringProperty("cse.references.transport",
Const.ANY_TRANSPORT).getValue());
DynamicPropertyFactory.getInstance()
.getStringProperty("cse.references.transport",
Const.ANY_TRANSPORT)
.getValue());

config = new ReferenceConfig(consumerSchemaFactory, microserviceName, versionRule.getValue(),
transport.getValue());
Expand Down
Expand Up @@ -16,10 +16,12 @@

package io.servicecomb.core.provider.producer;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.servicecomb.core.ProducerProvider;
Expand All @@ -32,8 +34,8 @@

@Component
public class ProducerProviderManager {
@Inject
private List<ProducerProvider> producerProviderList;
@Autowired(required = false)
private List<ProducerProvider> producerProviderList = Collections.emptyList();

@Inject
private MicroserviceMetaManager microserviceMetaManager;
Expand Down
Expand Up @@ -21,7 +21,7 @@

public class TestSchemaUtils {
@Test
public void testSchemaUtils() {
public void generatePackageName() {
MicroserviceMeta microserviceMeta = new MicroserviceMeta("app:ms");
Assert.assertEquals("cse.gen.app.ms.schemaId", SchemaUtils.generatePackageName(microserviceMeta, "schemaId"));
}
Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.core.provider.consumer;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import io.servicecomb.core.definition.schema.ConsumerSchemaFactory;
import mockit.Mocked;

public class TestConsumerProviderManager {
@Test
public void allowedNoProvider(@Mocked ConsumerSchemaFactory consumerSchemaFactory) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerSingleton(consumerSchemaFactory.getClass().getName(), consumerSchemaFactory);
context.register(ConsumerProviderManager.class);
// must not throw exception
context.refresh();

context.close();
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.core.provider.producer;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import io.servicecomb.core.definition.MicroserviceMetaManager;
import mockit.Mocked;

public class TestProducerProviderManager {
@Test
public void allowedNoProvider(@Mocked MicroserviceMetaManager microserviceMetaManager) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerSingleton(microserviceMetaManager.getClass().getName(), microserviceMetaManager);
context.register(ProducerProviderManager.class);
// must not throw exception
context.refresh();

context.close();
}
}
37 changes: 37 additions & 0 deletions demo/demo-edge/edge-service/pom.xml
@@ -0,0 +1,37 @@
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.servicecomb.demo</groupId>
<artifactId>demo-edge</artifactId>
<version>0.4.0-SNAPSHOT</version>
</parent>
<artifactId>edge-service</artifactId>

<properties>
<demo.main>io.servicecomb.demo.edge.service.EdgeMain</demo.main>
</properties>

<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
</dependencies>
</project>

0 comments on commit 6d37e90

Please sign in to comment.