You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2017/12/26 07:18:45 UTC
[incubator-servicecomb-java-chassis] 07/10: JAV-591 [WIP] swagger
invocation springmvc ProducerResponseMapper switch to new mechanism
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 64ed2dfdb0f084cf43644d26023293437ef98fcd
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Dec 24 02:47:31 2017 +0800
JAV-591 [WIP] swagger invocation springmvc ProducerResponseMapper switch to new mechanism
---
.../response/SpringmvcProducerResponseMapper.java | 20 +++--
.../SpringmvcProducerResponseMapperFactory.java | 45 +++++++++++
...response.producer.ProducerResponseMapperFactory | 18 +++++
.../TestSpringmvcProducerResponseMapper.java | 91 ++++++++++++++++++++++
...TestSpringmvcProducerResponseMapperFactory.java | 81 +++++++++++++++++++
5 files changed, 248 insertions(+), 7 deletions(-)
diff --git a/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapper.java b/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapper.java
index b5c74f0..3beb211 100644
--- a/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapper.java
+++ b/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapper.java
@@ -23,18 +23,17 @@ import javax.ws.rs.core.Response.StatusType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Component;
import io.servicecomb.swagger.invocation.Response;
import io.servicecomb.swagger.invocation.context.HttpStatus;
import io.servicecomb.swagger.invocation.response.Headers;
import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
-@Component
public class SpringmvcProducerResponseMapper implements ProducerResponseMapper {
- @Override
- public Class<?> getResponseClass() {
- return ResponseEntity.class;
+ private ProducerResponseMapper realMapper;
+
+ public SpringmvcProducerResponseMapper(ProducerResponseMapper realMapper) {
+ this.realMapper = realMapper;
}
@SuppressWarnings("unchecked")
@@ -44,9 +43,16 @@ public class SpringmvcProducerResponseMapper implements ProducerResponseMapper {
StatusType responseStatus = new HttpStatus(springmvcResponse.getStatusCode().value(),
springmvcResponse.getStatusCode().getReasonPhrase());
- Response cseResponse = Response.status(responseStatus).entity(springmvcResponse.getBody());
- HttpHeaders headers = springmvcResponse.getHeaders();
+ Response cseResponse = null;
+ if (HttpStatus.isSuccess(responseStatus)) {
+ cseResponse = realMapper.mapResponse(responseStatus, springmvcResponse.getBody());
+ } else {
+ // not support fail response mapper now
+ cseResponse = Response.status(responseStatus).entity(springmvcResponse.getBody());
+ }
+
+ HttpHeaders headers = springmvcResponse.getHeaders();
Headers cseHeaders = cseResponse.getHeaders();
for (Entry<String, List<String>> entry : headers.entrySet()) {
if (entry.getValue() == null || entry.getValue().isEmpty()) {
diff --git a/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapperFactory.java
new file mode 100644
index 0000000..a405a9b
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-springmvc/src/main/java/io/servicecomb/swagger/invocation/springmvc/response/SpringmvcProducerResponseMapperFactory.java
@@ -0,0 +1,45 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.springmvc.response;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import org.springframework.http.ResponseEntity;
+
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
+import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory;
+
+public class SpringmvcProducerResponseMapperFactory implements ProducerResponseMapperFactory {
+ @Override
+ public boolean isMatch(Type swaggerType, Type producerType) {
+ if (!ParameterizedType.class.isAssignableFrom(producerType.getClass())) {
+ return false;
+ }
+
+ return ((ParameterizedType) producerType).getRawType().equals(ResponseEntity.class);
+ }
+
+ @Override
+ public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys,
+ Type swaggerType, Type producerType) {
+ Type realProducerType = ((ParameterizedType) producerType).getActualTypeArguments()[0];
+ ProducerResponseMapper realMapper = factorys.createResponseMapper(swaggerType, realProducerType);
+ return new SpringmvcProducerResponseMapper(realMapper);
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-springmvc/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory b/swagger/swagger-invocation/invocation-springmvc/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
new file mode 100644
index 0000000..13ec935
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-springmvc/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
@@ -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.
+#
+
+io.servicecomb.swagger.invocation.springmvc.response.SpringmvcProducerResponseMapperFactory
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapper.java b/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapper.java
new file mode 100644
index 0000000..9e359b5
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapper.java
@@ -0,0 +1,91 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.springmvc.response;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+
+public class TestSpringmvcProducerResponseMapper {
+ @Mocked
+ ProducerResponseMapper realMapper;
+
+ SpringmvcProducerResponseMapper mapper;
+
+ String[] arrResult = new String[] {"a", "b"};
+
+ @Before
+ public void setup() {
+ mapper = new SpringmvcProducerResponseMapper(realMapper);
+
+ new MockUp<ProducerResponseMapper>(realMapper) {
+ @Mock
+ Response mapResponse(StatusType status, Object response) {
+ if (io.servicecomb.foundation.common.http.HttpStatus.isSuccess(status.getStatusCode())) {
+ return Response.ok(Arrays.asList(arrResult));
+ }
+
+ return null;
+ }
+ };
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void mapResponse_withoutHeader_sucess() {
+ ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, HttpStatus.OK);
+ Response response = mapper.mapResponse(null, responseEntity);
+ Assert.assertThat((List<String>) response.getResult(), Matchers.contains("a", "b"));
+ Assert.assertEquals(Status.OK, response.getStatus());
+ }
+
+ @Test
+ public void mapResponse_withoutHeader_fail() {
+ ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, HttpStatus.BAD_REQUEST);
+ Response response = mapper.mapResponse(null, responseEntity);
+ Assert.assertSame(arrResult, response.getResult());
+ Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus().getStatusCode());
+ }
+
+ @Test
+ public void mapResponse_withHeader() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("h", "v");
+
+ ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, headers, HttpStatus.OK);
+ Response response = mapper.mapResponse(null, responseEntity);
+
+ List<Object> hv = response.getHeaders().getHeader("h");
+ Assert.assertThat(hv, Matchers.contains("v"));
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapperFactory.java
new file mode 100644
index 0000000..2c4cbcf
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-springmvc/src/test/java/io/servicecomb/swagger/invocation/springmvc/response/TestSpringmvcProducerResponseMapperFactory.java
@@ -0,0 +1,81 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.springmvc.response;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import io.servicecomb.foundation.common.utils.ReflectUtils;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.converter.ConverterMgr;
+import io.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
+import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
+import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory;
+
+public class TestSpringmvcProducerResponseMapperFactory {
+ SpringmvcProducerResponseMapperFactory factory = new SpringmvcProducerResponseMapperFactory();
+
+ ConverterMgr converterMgr = new ConverterMgr();
+
+ ResponseMapperFactorys<ProducerResponseMapper> factorys =
+ new ResponseMapperFactorys<>(ProducerResponseMapperFactory.class, converterMgr);
+
+ public ResponseEntity<String[]> responseEntity() {
+ return null;
+ }
+
+ public List<String> list() {
+ return null;
+ }
+
+ @Test
+ public void isMatch_true() {
+ Method method = ReflectUtils.findMethod(this.getClass(), "responseEntity");
+ Assert.assertTrue(factory.isMatch(null, method.getGenericReturnType()));
+ }
+
+ @Test
+ public void isMatch_Parameterized_false() {
+ Method method = ReflectUtils.findMethod(this.getClass(), "list");
+ Assert.assertFalse(factory.isMatch(null, method.getGenericReturnType()));
+ }
+
+ @Test
+ public void isMatch_false() {
+ Assert.assertFalse(factory.isMatch(null, String.class));
+ }
+
+ @Test
+ public void createResponseMapper() {
+ Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "responseEntity");
+ Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
+
+ ProducerResponseMapper mapper = factory
+ .createResponseMapper(factorys, listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
+ Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcProducerResponseMapper.class));
+
+ ResponseEntity<String[]> responseEntity = new ResponseEntity<String[]>(new String[] {"a", "b"}, HttpStatus.OK);
+ Response response = mapper.mapResponse(null, responseEntity);
+ Assert.assertThat(response.getResult(), Matchers.contains("a", "b"));
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.