You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/12/05 06:28:31 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1051]when interface set produces=text/plain;charset=utf-8. and c… (#1015)

This is an automated email from the ASF dual-hosted git repository.

wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 22ff6d6  [SCB-1051]when interface set produces=text/plain;charset=utf-8. and c… (#1015)
22ff6d6 is described below

commit 22ff6d6769f144664f448e43d7d474f7837c6cf8
Author: heyile <25...@qq.com>
AuthorDate: Wed Dec 5 14:28:26 2018 +0800

    [SCB-1051]when interface set produces=text/plain;charset=utf-8. and c… (#1015)
    
    * [SCB-1051]when interface set produces=text/plain;charset=utf-8. and consumers set accept = text/plain,will cause error
    
    * [SCB-1051]when interface set produces=text/plain;charset=utf-8. and consumers set accept = text/plain,will cause error :add it-test
    
    * [SCB-1051]when interface set produces=text/plain;charset=utf-8. and consumers set accept = text/plain,will cause error: fix details
---
 .../common/rest/definition/RestOperationMeta.java  |   3 +
 .../rest/definition/TestRestOperationMeta.java     |  12 +++
 .../org/apache/servicecomb/it/ConsumerMain.java    |   2 +
 .../servicecomb/it/testcase/TestAcceptType.java    | 102 +++++++++++++++++++++
 .../it/schema/AcceptTypeJaxrsSchema.java           |  42 +++++++++
 .../it/schema/AcceptTypeSpringmvcSchema.java       |  36 ++++++++
 6 files changed, 197 insertions(+)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
index ee7c401..36979ea 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java
@@ -173,6 +173,9 @@ public class RestOperationMeta {
       }
     } else {
       for (String produce : produces) {
+        if (produce.contains(";")) {
+          produce = produce.substring(0, produce.indexOf(";"));
+        }
         ProduceProcessor processor = ProduceProcessorManager.INSTANCE.findValue(produce);
         if (processor == null) {
           LOGGER.error("produce {} is not supported", produce);
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java
index 34621d6..6103df9 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java
@@ -148,6 +148,18 @@ public class TestRestOperationMeta {
   }
 
   @Test
+  public void testCreateProduceProcessorsWithSemicolon() {
+    RestOperationMeta operationMeta = new RestOperationMeta();
+    operationMeta.produces = Arrays
+        .asList(MediaType.TEXT_PLAIN + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8");
+    operationMeta.createProduceProcessors();
+    Assert.assertSame(ProduceProcessorManager.PLAIN_PROCESSOR,
+        operationMeta.ensureFindProduceProcessor(MediaType.TEXT_PLAIN));
+    Assert.assertSame(ProduceProcessorManager.JSON_PROCESSOR,
+        operationMeta.ensureFindProduceProcessor(MediaType.APPLICATION_JSON));
+  }
+
+  @Test
   public void testEnsureFindProduceProcessorRequest(@Mocked HttpServletRequestEx requestEx) {
     RestOperationMeta operationMeta = new RestOperationMeta();
     new Expectations() {
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
index 0ef5039..53edb8b 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
@@ -22,6 +22,7 @@ import org.apache.servicecomb.it.deploy.Deploys;
 import org.apache.servicecomb.it.deploy.MicroserviceDeploy;
 import org.apache.servicecomb.it.junit.ITJUnitUtils;
 import org.apache.servicecomb.it.schema.TestApiOperation;
+import org.apache.servicecomb.it.testcase.TestAcceptType;
 import org.apache.servicecomb.it.testcase.TestAnnotatedAttribute;
 import org.apache.servicecomb.it.testcase.TestApiParam;
 import org.apache.servicecomb.it.testcase.TestChangeTransport;
@@ -102,6 +103,7 @@ public class ConsumerMain {
 
     // only rest support default value feature
     ITJUnitUtils.runWithRest(TestDefaultValue.class);
+    ITJUnitUtils.runWithRest(TestAcceptType.class);
 
     ITJUnitUtils.runWithRest(TestDownload.class);
 
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java
new file mode 100644
index 0000000..9b2f2b1
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java
@@ -0,0 +1,102 @@
+/*
+ * 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.it.testcase;
+
+import org.apache.servicecomb.it.Consumers;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+
+public class TestAcceptType {
+  interface AcceptTypeIntf {
+  }
+
+  private static Consumers<AcceptTypeIntf> consumersAcceptTypeSpringmvc = new Consumers<>("acceptTypeSpringmvcSchema",
+      AcceptTypeIntf.class);
+
+  private static Consumers<AcceptTypeIntf> consumersAcceptTypeJaxrs = new Consumers<>("acceptTypeJaxrsSchema",
+      AcceptTypeIntf.class);
+
+  @Test
+  public void testTextPlain_rt() {
+    checkTextPlain(consumersAcceptTypeSpringmvc);
+    checkTextPlain(consumersAcceptTypeJaxrs);
+  }
+
+  private void checkTextPlain(Consumers<AcceptTypeIntf> consumers) {
+    String result = textHeader_rt(consumers, MediaType.TEXT_PLAIN_VALUE);
+    Assert.assertEquals("cse", result);
+    try {
+      textHeader_rt(consumers, MediaType.APPLICATION_JSON_VALUE);
+      Assert.fail("should throw exception");
+    } catch (InvocationException e) {
+      Assert.assertEquals(406, e.getStatusCode());
+      Assert.assertTrue(e.getMessage().contains("Accept application/json is not supported"));
+    } catch (Throwable e) {
+      Assert.fail(" should throw InvocationException");
+    }
+  }
+
+  private String textHeader_rt(Consumers<AcceptTypeIntf> consumers, String type) {
+    HttpHeaders headers = new HttpHeaders();
+    headers.add("accept", type);
+    HttpEntity<?> entity = new HttpEntity<>(headers);
+    ResponseEntity<String> response = consumers.getSCBRestTemplate()
+        .exchange("/sayHi?name=cse",
+            HttpMethod.GET,
+            entity,
+            String.class);
+    return response.getBody();
+  }
+
+  @Test
+  public void testProducerApplicationJson_rt() {
+    checkApplicationJson(consumersAcceptTypeSpringmvc);
+    checkApplicationJson(consumersAcceptTypeJaxrs);
+  }
+
+  private void checkApplicationJson(Consumers<AcceptTypeIntf> consumers) {
+    String result = jsonHeader_rt(consumers, MediaType.APPLICATION_JSON_VALUE);
+    Assert.assertEquals("cse", result);
+    try {
+      jsonHeader_rt(consumersAcceptTypeSpringmvc, MediaType.TEXT_PLAIN_VALUE);
+      Assert.fail("should throw exception");
+    } catch (InvocationException e) {
+      Assert.assertEquals(406, e.getStatusCode());
+      Assert.assertTrue(e.getMessage().contains("Accept text/plain is not supported"));
+    } catch (Throwable e) {
+      Assert.fail("should throw InvocationException");
+    }
+  }
+
+  private String jsonHeader_rt(Consumers<AcceptTypeIntf> consumers, String type) {
+    HttpHeaders headers = new HttpHeaders();
+    headers.add("accept", type);
+    HttpEntity<?> entity = new HttpEntity<>(headers);
+    ResponseEntity<String> response = consumers.getSCBRestTemplate()
+        .exchange("/sayHello?name=cse",
+            HttpMethod.GET,
+            entity,
+            String.class);
+    return response.getBody();
+  }
+}
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java
new file mode 100644
index 0000000..37804d0
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java
@@ -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.it.schema;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+
+@RestSchema(schemaId = "acceptTypeJaxrsSchema")
+@Path("/v1/acceptTypeJaxrsSchema")
+public class AcceptTypeJaxrsSchema {
+  @Path("/sayHi")
+  @GET
+  @Produces("text/plain;charset=UTF-8")
+  public String sayHi(@QueryParam("name") String name) {
+    return name;
+  }
+
+  @Path("/sayHello")
+  @GET
+  @Produces("application/json;charset=UTF-8")
+  public String sayHello(@QueryParam("name") String name) {
+    return name;
+  }
+}
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java
new file mode 100644
index 0000000..f9d72dd
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java
@@ -0,0 +1,36 @@
+/*
+ * 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.it.schema;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@RestSchema(schemaId = "acceptTypeSpringmvcSchema")
+@RequestMapping(path = "/v1/acceptTypeSpringmvcSchema")
+public class AcceptTypeSpringmvcSchema {
+  @RequestMapping(path = "/sayHi", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
+  public String sayHi(@RequestParam(name = "name", defaultValue = "test") String name) {
+    return name;
+  }
+
+  @RequestMapping(path = "/sayHello", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
+  public String sayHello(@RequestParam(name = "name", defaultValue = "test") String name) {
+    return name;
+  }
+}