import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
+import lombok.AccessLevel;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Object to be used to serialize and de-serialize.
*/
- @Getter
+ @Getter(AccessLevel.PROTECTED)
private final Gson gson;
/**
* into Integer/Long, where possible.
*/
public GsonMessageBodyHandler() {
- this(new GsonBuilder());
- }
-
- /**
- * Constructs the object, using a Gson object that translates Doubles inside of Maps
- * into Integer/Long, where possible.
- *
- * @param builder builder to use to create the gson object
- */
- public GsonMessageBodyHandler(GsonBuilder builder) {
- this(builder.registerTypeAdapterFactory(new MapDoubleAdapterFactory()).create());
+ this(configBuilder(new GsonBuilder()).create());
}
/**
logger.info("Using GSON for REST calls");
}
+ /**
+ * Configures a builder with the adapters normally used by this handler (e.g., mapper
+ * that converts Double to Integer).
+ *
+ * @param builder builder to be configured
+ * @return the configured builder
+ */
+ public static GsonBuilder configBuilder(GsonBuilder builder) {
+ return builder.registerTypeAdapterFactory(new MapDoubleAdapterFactory());
+ }
+
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return canHandle(mediaType);
package org.onap.policy.common.gson;
+import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* Constructs the object.
*/
public JacksonHandler() {
- this(new GsonBuilder());
+ this(configBuilder(new GsonBuilder()).create());
+ }
+ /**
+ * Constructs the object.
+ *
+ * @param gson the Gson object to be used to serialize and de-serialize
+ */
+ public JacksonHandler(Gson gson) {
+ super(gson);
logger.info("Using GSON with Jackson behaviors for REST calls");
}
/**
- * Constructs the object.
- * @param builder builder to use to create the gson object
+ * Configures a builder with the adapters normally used by this handler (e.g.,
+ * adapters for GsonJsonXxx annotations).
+ *
+ * @param builder builder to be configured
+ * @return the configured builder
*/
- public JacksonHandler(GsonBuilder builder) {
- super(builder
- .registerTypeAdapterFactory(new JacksonFieldAdapterFactory())
+ public static GsonBuilder configBuilder(GsonBuilder builder) {
+ return builder.registerTypeAdapterFactory(new JacksonFieldAdapterFactory())
.registerTypeAdapterFactory(new JacksonMethodAdapterFactory())
- .setExclusionStrategies(new JacksonExclusionStrategy()));
+ .registerTypeAdapterFactory(new MapDoubleAdapterFactory())
+ .setExclusionStrategies(new JacksonExclusionStrategy());
}
-
}
package org.onap.policy.common.endpoints.http.server;
+import com.google.gson.GsonBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
* Translator to be used. We want a GSON object that's configured the same way as it
* is in {@link JacksonHandler}, so just get it from there.
*/
- private static final YamlJsonTranslator TRANS = new YamlJsonTranslator(new JacksonHandler().getGson());
+ private static final YamlJsonTranslator TRANS =
+ new YamlJsonTranslator(JacksonHandler.configBuilder(new GsonBuilder()).create());
/**
* Constructs the object.
package org.onap.policy.common.endpoints.http.server;
+import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.io.InputStream;
* from there.
*/
private static final YamlJsonTranslator DEFAULT_TRANSLATOR =
- new YamlJsonTranslator(new GsonMessageBodyHandler().getGson());
+ new YamlJsonTranslator(GsonMessageBodyHandler.configBuilder(new GsonBuilder()).create());
private final YamlJsonTranslator translator;
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", mediaType);
+ conn.setRequestProperty("Accept", mediaType);
IOUtils.write(post, conn.getOutputStream());
return response(conn);
}
String auth = params.getUserName() + ":" + params.getPassword();
conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(auth.getBytes()));
conn.setRequestProperty("Content-type", mediaType);
+ conn.setRequestProperty("Accept", mediaType);
conn.connect();
try (PrintWriter wtr = new PrintWriter(conn.getOutputStream())) {
gsonBldr = new GsonBuilder();
// register jackson behaviors with the builder
- new JacksonHandler(gsonBldr);
+ JacksonHandler.configBuilder(gsonBldr);
}
/**