aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/msb/sdk/httpclient/convert
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/msb/sdk/httpclient/convert')
-rw-r--r--src/main/java/org/onap/msb/sdk/httpclient/convert/IConverterFactoryBuilder.java25
-rw-r--r--src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java90
-rw-r--r--src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactoryBuilder.java50
-rw-r--r--src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonRequestBodyConverter.java51
-rw-r--r--src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonResponseBodyConverter.java64
5 files changed, 280 insertions, 0 deletions
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/IConverterFactoryBuilder.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/IConverterFactoryBuilder.java
new file mode 100644
index 0000000..526392b
--- /dev/null
+++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/IConverterFactoryBuilder.java
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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 org.onap.msb.sdk.httpclient.convert;
+
+import retrofit2.Converter;
+
+/**
+ * @author hu.rui
+ *
+ */
+public interface IConverterFactoryBuilder {
+
+ Converter.Factory buildConverterFactory();
+}
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java
new file mode 100644
index 0000000..4170cbd
--- /dev/null
+++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactory.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.
+ ******************************************************************************/
+/*
+ * Copyright (C) 2015 Square, Inc.
+ *
+ * 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 org.onap.msb.sdk.httpclient.convert.jackson;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+import okhttp3.RequestBody;
+import okhttp3.ResponseBody;
+import retrofit2.Converter;
+import retrofit2.Retrofit;
+
+/**
+ * A {@linkplain Converter.Factory converter} which uses Jackson.
+ * <p>
+ * Because Jackson is so flexible in the types it supports, this converter assumes that it can
+ * handle all types. If you are mixing JSON serialization with something else (such as protocol
+ * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this
+ * instance} last to allow the other converters a chance to see their types.
+ */
+public final class JacksonConverterFactory extends Converter.Factory {
+ /** Create an instance using a default {@link ObjectMapper} instance for conversion. */
+ public static JacksonConverterFactory create() {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+ mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
+ return create(mapper);
+ }
+
+ /** Create an instance using {@code mapper} for conversion. */
+ public static JacksonConverterFactory create(ObjectMapper mapper) {
+ return new JacksonConverterFactory(mapper);
+ }
+
+ private final ObjectMapper mapper;
+
+ private JacksonConverterFactory(ObjectMapper mapper) {
+ if (mapper == null)
+ throw new NullPointerException("mapper == null");
+ this.mapper = mapper;
+ }
+
+ @Override
+ public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
+ Retrofit retrofit) {
+ JavaType javaType = mapper.getTypeFactory().constructType(type);
+ ObjectReader reader = mapper.reader(javaType);
+ return new JacksonResponseBodyConverter<>(reader);
+ }
+
+ @Override
+ public Converter<?, RequestBody> requestBodyConverter(Type type,
+ Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
+ JavaType javaType = mapper.getTypeFactory().constructType(type);
+ ObjectWriter writer = mapper.writerWithType(javaType);
+ return new JacksonRequestBodyConverter<>(writer);
+ }
+}
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactoryBuilder.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactoryBuilder.java
new file mode 100644
index 0000000..e0477ad
--- /dev/null
+++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonConverterFactoryBuilder.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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 org.onap.msb.sdk.httpclient.convert.jackson;
+
+import org.onap.msb.sdk.httpclient.convert.IConverterFactoryBuilder;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+import retrofit2.Converter.Factory;
+
+/**
+ * @author hu.rui
+ *
+ */
+public class JacksonConverterFactoryBuilder implements IConverterFactoryBuilder {
+
+ private static ObjectMapper objectMapper = new ObjectMapper();
+ static {
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.zte.ums.zenap.httpclient.retrofit.convert.IConverterFactoryBuilder#buildConverterFactory()
+ */
+ @Override
+ public Factory buildConverterFactory() {
+ return JacksonConverterFactory.create(objectMapper);
+ }
+
+}
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonRequestBodyConverter.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonRequestBodyConverter.java
new file mode 100644
index 0000000..516da82
--- /dev/null
+++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonRequestBodyConverter.java
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.
+ ******************************************************************************/
+/*
+ * Copyright (C) 2015 Square, Inc.
+ *
+ * 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 org.onap.msb.sdk.httpclient.convert.jackson;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
+import retrofit2.Converter;
+
+final class JacksonRequestBodyConverter<T> implements Converter<T, RequestBody> {
+ private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
+
+ private final ObjectWriter adapter;
+
+ JacksonRequestBodyConverter(ObjectWriter adapter) {
+ this.adapter = adapter;
+ }
+
+ @Override
+ public RequestBody convert(T value) throws IOException {
+ byte[] bytes = adapter.writeValueAsBytes(value);
+ return RequestBody.create(MEDIA_TYPE, bytes);
+ }
+}
diff --git a/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonResponseBodyConverter.java b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonResponseBodyConverter.java
new file mode 100644
index 0000000..29ca49b
--- /dev/null
+++ b/src/main/java/org/onap/msb/sdk/httpclient/convert/jackson/JacksonResponseBodyConverter.java
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.
+ ******************************************************************************/
+/*
+ * Copyright (C) 2015 Square, Inc.
+ *
+ * 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 org.onap.msb.sdk.httpclient.convert.jackson;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.ObjectReader;
+
+import okhttp3.ResponseBody;
+import retrofit2.Converter;
+
+final class JacksonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
+
+ private static final Logger logger = LoggerFactory.getLogger(JacksonResponseBodyConverter.class);
+
+ private final ObjectReader adapter;
+
+
+
+ JacksonResponseBodyConverter(ObjectReader adapter) {
+ this.adapter = adapter;
+ }
+
+ @Override
+ public T convert(ResponseBody value) throws IOException {
+ String src = null;
+ try {
+ src = value.string();
+ return adapter.readValue(src);
+ } catch (IOException e) {
+ logger.error("parse responseBody error,body:" + src);
+ throw e;
+ } finally {
+ value.close();
+ }
+ }
+}