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:43 UTC
[incubator-servicecomb-java-chassis] 05/10: JAV-591 [WIP] swagger
invocation jaxrs 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 430708c0f5139f80d303dbb347f95c2017175a24
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Dec 24 01:21:54 2017 +0800
JAV-591 [WIP] swagger invocation jaxrs ProducerResponseMapper switch to new mechanism
---
.../swagger/invocation/response/Headers.java | 11 ++++
.../swagger/invocation/response/TestHeaders.java | 10 +++
.../response/JaxrsProducerResponseMapper.java | 10 +--
.../JaxrsProducerResponseMapperFactory.java | 38 +++++++++++
...response.producer.ProducerResponseMapperFactory | 18 ++++++
.../response/TestJaxrsProducerResponseMapper.java | 74 ++++++++++++++++++++++
.../TestJaxrsProducerResponseMapperFactory.java | 43 +++++++++++++
7 files changed, 195 insertions(+), 9 deletions(-)
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/Headers.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/Headers.java
index 118bc0a..f3377b6 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/Headers.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/response/Headers.java
@@ -63,4 +63,15 @@ public class Headers {
return this;
}
+
+ public Headers addHeader(String name, List<Object> value) {
+ if (headerMap == null) {
+ headerMap = new HashMap<>();
+ }
+
+ List<Object> values = headerMap.computeIfAbsent(name, k -> new ArrayList<>());
+ values.addAll(value);
+
+ return this;
+ }
}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/TestHeaders.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/TestHeaders.java
index ed461a3..f9dce01 100644
--- a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/TestHeaders.java
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/response/TestHeaders.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
@@ -53,4 +54,13 @@ public class TestHeaders {
Assert.assertEquals("h1v1", headers.getFirst("h1"));
}
+
+ @Test
+ public void addHeader_list() {
+ Headers headers = new Headers();
+ headers.addHeader("h", Arrays.asList("v1", "v2"));
+ headers.addHeader("h", Arrays.asList("v3"));
+
+ Assert.assertThat(headers.getHeader("h"), Matchers.contains("v1", "v2", "v3"));
+ }
}
diff --git a/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapper.java b/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapper.java
index 890e0e3..715e684 100644
--- a/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapper.java
+++ b/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapper.java
@@ -22,19 +22,11 @@ import java.util.Map.Entry;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.StatusType;
-import org.springframework.stereotype.Component;
-
import io.servicecomb.swagger.invocation.Response;
import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
-@Component
public class JaxrsProducerResponseMapper implements ProducerResponseMapper {
@Override
- public Class<?> getResponseClass() {
- return javax.ws.rs.core.Response.class;
- }
-
- @Override
public Response mapResponse(StatusType status, Object response) {
javax.ws.rs.core.Response jaxrsResponse = (javax.ws.rs.core.Response) response;
@@ -45,7 +37,7 @@ public class JaxrsProducerResponseMapper implements ProducerResponseMapper {
continue;
}
- cseResponse.getHeaders().getHeaderMap().put(entry.getKey(), entry.getValue());
+ cseResponse.getHeaders().addHeader(entry.getKey(), entry.getValue());
}
return cseResponse;
}
diff --git a/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapperFactory.java
new file mode 100644
index 0000000..e6ca7ab
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-jaxrs/src/main/java/io/servicecomb/swagger/invocation/jaxrs/response/JaxrsProducerResponseMapperFactory.java
@@ -0,0 +1,38 @@
+/*
+ * 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.jaxrs.response;
+
+import java.lang.reflect.Type;
+
+import javax.ws.rs.core.Response;
+
+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 JaxrsProducerResponseMapperFactory implements ProducerResponseMapperFactory {
+ @Override
+ public boolean isMatch(Type swaggerType, Type producerType) {
+ return Response.class.equals(producerType);
+ }
+
+ @Override
+ public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys,
+ Type swaggerType, Type producerType) {
+ return new JaxrsProducerResponseMapper();
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-jaxrs/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory b/swagger/swagger-invocation/invocation-jaxrs/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory
new file mode 100644
index 0000000..8396059
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-jaxrs/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.jaxrs.response.JaxrsProducerResponseMapperFactory
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapper.java b/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapper.java
new file mode 100644
index 0000000..4bb7258
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapper.java
@@ -0,0 +1,74 @@
+/*
+ * 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.jaxrs.response;
+
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response.Status;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.swagger.invocation.Response;
+import mockit.Expectations;
+import mockit.Mocked;
+
+public class TestJaxrsProducerResponseMapper {
+ JaxrsProducerResponseMapper mapper = new JaxrsProducerResponseMapper();
+
+ @Mocked
+ javax.ws.rs.core.Response jaxrsResponse;
+
+ @Test
+ public void mapResponse_withoutHeaders() {
+ new Expectations() {
+ {
+ jaxrsResponse.getStatusInfo();
+ result = Status.OK;
+ jaxrsResponse.getEntity();
+ result = "result";
+ }
+ };
+ Response response = mapper.mapResponse(null, jaxrsResponse);
+ Assert.assertEquals(Status.OK, response.getStatus());
+ Assert.assertEquals("result", response.getResult());
+ Assert.assertNull(response.getHeaders().getHeaderMap());
+ }
+
+ @Test
+ public void mapResponse_withHeaders() {
+ MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
+ headers.add("h", "v");
+
+ new Expectations() {
+ {
+ jaxrsResponse.getStatusInfo();
+ result = Status.OK;
+ jaxrsResponse.getEntity();
+ result = "result";
+ jaxrsResponse.getHeaders();
+ result = headers;
+ }
+ };
+ Response response = mapper.mapResponse(null, jaxrsResponse);
+ Assert.assertEquals(Status.OK, response.getStatus());
+ Assert.assertEquals("result", response.getResult());
+ Assert.assertEquals(1, response.getHeaders().getHeaderMap().size());
+ Assert.assertThat(response.getHeaders().getHeader("h"), Matchers.contains("v"));
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapperFactory.java b/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapperFactory.java
new file mode 100644
index 0000000..6ec2827
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-jaxrs/src/test/java/io/servicecomb/swagger/invocation/jaxrs/response/TestJaxrsProducerResponseMapperFactory.java
@@ -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 io.servicecomb.swagger.invocation.jaxrs.response;
+
+import javax.ws.rs.core.Response;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestJaxrsProducerResponseMapperFactory {
+ JaxrsProducerResponseMapperFactory factory = new JaxrsProducerResponseMapperFactory();
+
+ @Test
+ public void isMatch_true() {
+ Assert.assertTrue(factory.isMatch(null, Response.class));
+ }
+
+ @Test
+ public void isMatch_false() {
+ Assert.assertFalse(factory.isMatch(null, String.class));
+ }
+
+ @Test
+ public void createResponseMapper() {
+ Assert.assertThat(factory.createResponseMapper(null, null, null),
+ Matchers.instanceOf(JaxrsProducerResponseMapper.class));
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.