diff options
author | Jim Hahn <jrh3@att.com> | 2019-01-21 12:42:10 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-01-23 18:43:43 -0500 |
commit | 18914b033f67d25cc9377b2e381648a9e3184968 (patch) | |
tree | 973bfdd2a119c19d11e3770e2a4a82d898279284 /policy-endpoints/src/main/java/org/onap | |
parent | 9f15e1987dc9fc6bb8196c415b817b3f1cf6bd5f (diff) |
Add gson support to policy-endpoints
Added "provider" property to both http client and server builders.
The provider defaults to jackson, to maintain backward compatibility
until other policy code has been converted to gson.
Removed commented item from pom.
Added some comments and re-arranged a few pieces of code.
Fixed a few typos and removed spacing at the end of some lines.
Reordered imports.
Added comments about limitations when using jersey-media-json-jackson.
Address ridiculous checkstyle complaint.
Support comma-separated list of serialization providers in jersey client.
Disabled metainf discovery from jersey client and server so that the
media-json dependencies could be re-instated in the pom.
Address another ridiculous checkstyle complaint.
Change-Id: Ic5a93b475d0ee9b435352b3516de6b865b00a86a
Issue-ID: POLICY-1428
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-endpoints/src/main/java/org/onap')
9 files changed, 266 insertions, 69 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java index b752d91c..6e3c2632 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Modifications Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,6 +72,7 @@ public class BusTopicParams { private String clientName; private String hostname; private String basePath; + private String serializationProvider; public static TopicParamsBuilder builder() { return new TopicParamsBuilder(); @@ -254,6 +255,10 @@ public class BusTopicParams { return additionalProps != null; } + public String getSerializationProvider() { + return serializationProvider; + } + public static class TopicParamsBuilder { final BusTopicParams params = new BusTopicParams(); @@ -385,6 +390,11 @@ public class BusTopicParams { return this; } + public TopicParamsBuilder serializationProvider(String serializationProvider) { + this.params.serializationProvider = serializationProvider; + return this; + } + } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java index f482eb01..5f4c4c9d 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java @@ -36,7 +36,7 @@ public interface HttpClientFactory { * Build and http client with the following parameters. */ HttpClient build(BusTopicParams busTopicParams) - throws KeyManagementException, NoSuchAlgorithmException; + throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException; /** * Build http client from properties. diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java index 9aef09e4..5cc0071e 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java @@ -49,7 +49,7 @@ class IndexedHttpClientFactory implements HttpClientFactory { @Override public synchronized HttpClient build(BusTopicParams busTopicParams) - throws KeyManagementException, NoSuchAlgorithmException { + throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { if (clients.containsKey(busTopicParams.getClientName())) { return clients.get(busTopicParams.getClientName()); } @@ -109,6 +109,9 @@ class IndexedHttpClientFactory implements HttpClientFactory { String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); + final String classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER); + String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); boolean managed = true; @@ -128,6 +131,7 @@ class IndexedHttpClientFactory implements HttpClientFactory { .userName(userName) .password(password) .managed(managed) + .serializationProvider(classProv) .build()); clientList.add(client); } catch (Exception e) { diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index b55a7bb2..2287486e 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -22,7 +22,6 @@ package org.onap.policy.common.endpoints.http.client.internal; import com.fasterxml.jackson.annotation.JsonIgnore; - import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; @@ -30,7 +29,6 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Map; import java.util.Map.Entry; - import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; @@ -39,7 +37,7 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; - +import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; @@ -48,6 +46,10 @@ import org.slf4j.LoggerFactory; /** * Http Client implementation using a Jersey Client. + * + * <p>Note: the serialization provider will be ignored if the maven artifact, + * <i>jersey-media-json-jackson</i>, is included, regardless of whether it's included + * directly or indirectly. */ public class JerseyClient implements HttpClient { @@ -55,6 +57,9 @@ public class JerseyClient implements HttpClient { * Logger. */ private static Logger logger = LoggerFactory.getLogger(JerseyClient.class); + + protected static final String JERSEY_DEFAULT_SERIALIZATION_PROVIDER = + "com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"; protected final String name; protected final boolean https; @@ -73,14 +78,17 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * <p>name the name https is it https or not selfSignedCerts are there self signed certs hostname - * the hostname port port being used basePath base context userName user password password + * <p>name the name https is it https or not selfSignedCerts are there self signed certs + * hostname the hostname port port being used basePath base context userName user + * password password * * @param busTopicParams Input parameters object * @throws KeyManagementException key exception * @throws NoSuchAlgorithmException no algorithm exception + * @throws ClassNotFoundException if the serialization provider cannot be found */ - public JerseyClient(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException { + public JerseyClient(BusTopicParams busTopicParams) + throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { super(); @@ -147,10 +155,28 @@ public class JerseyClient implements HttpClient { this.client.register(authFeature); } + registerSerProviders(busTopicParams.getSerializationProvider()); + + this.client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + this.baseUrl = tmpBaseUrl.append(this.hostname).append(":").append(this.port).append("/") .append((this.basePath == null) ? "" : this.basePath).toString(); } + /** + * Registers the serialization provider(s) with the client. + * + * @param serializationProvider comma-separated list of serialization providers + * @throws ClassNotFoundException if the serialization provider cannot be found + */ + private void registerSerProviders(String serializationProvider) throws ClassNotFoundException { + String providers = (serializationProvider == null || serializationProvider.isEmpty() + ? JERSEY_DEFAULT_SERIALIZATION_PROVIDER : serializationProvider); + for (String prov : providers.split(",")) { + this.client.register(Class.forName(prov)); + } + } + @Override public Response get(String path) { if (path != null && !path.isEmpty()) { diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java index c4db9fbe..b674e265 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,6 +62,13 @@ public interface HttpServletServer extends Startable { boolean isAaf(); /** + * Sets the serialization provider to be used when classes are added to the service. + * + * @param provider the provider to use for message serialization and de-serialization + */ + void setSerializationProvider(String provider); + + /** * Adds a filter at the specified path. * * @param filterPath filter path diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java index ad8ef99c..b2c49eae 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java @@ -129,6 +129,9 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { final String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX); + final String classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER); + final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); boolean managed = true; @@ -157,9 +160,14 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { aaf = Boolean.parseBoolean(aafString); } + HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed); + if (classProv != null && !classProv.isEmpty()) { + service.setSerializationProvider(classProv); + } + /* authentication method either AAF or HTTP Basic Auth */ if (aaf) { diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/GsonMessageBodyHandler.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/GsonMessageBodyHandler.java new file mode 100644 index 00000000..a29afef4 --- /dev/null +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/GsonMessageBodyHandler.java @@ -0,0 +1,128 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.http.server.internal; + +import com.google.gson.Gson; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +/** + * Provider that serializes and de-serializes JSON via gson. + * + * <p>Note: <i>jersey</i> will ignore this class if the maven artifact, + * <i>jersey-media-json-jackson</i>, is included, regardless of whether it's included + * directly or indirectly. + */ +@Provider +@Consumes(MediaType.WILDCARD) +@Produces(MediaType.WILDCARD) +public class GsonMessageBodyHandler implements MessageBodyReader<Object>, MessageBodyWriter<Object> { + + /** + * Object to be used to serialize and de-serialize. + */ + private Gson gson; + + /** + * Constructs the object, using a plain Gson object. + */ + public GsonMessageBodyHandler() { + this(new Gson()); + } + + /** + * Constructs the object. + * + * @param gson the Gson object to be used to serialize and de-serialize + */ + public GsonMessageBodyHandler(Gson gson) { + this.gson = gson; + } + + @Override + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return canHandle(mediaType); + } + + @Override + public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) + throws IOException, WebApplicationException { + + try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) { + Type jsonType = (type.equals(genericType) ? type : genericType); + gson.toJson(object, jsonType, writer); + } + } + + @Override + public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return canHandle(mediaType); + } + + /** + * Determines if this provider can handle the given media type. + * + * @param mediaType the media type of interest + * @return {@code true} if this provider handles the given media type, {@code false} + * otherwise + */ + private boolean canHandle(MediaType mediaType) { + if (mediaType == null) { + return true; + } + + String subtype = mediaType.getSubtype(); + + return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json") || "javascript".equals(subtype) + || "x-javascript".equals(subtype) || "x-json".equals(subtype); + } + + @Override + public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, String> httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + + try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) { + Type jsonType = (type.equals(genericType) ? type : genericType); + return gson.fromJson(streamReader, jsonType); + } + } +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java index a97a9bf5..22fc9ac3 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * policy-endpoints * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -23,12 +23,23 @@ package org.onap.policy.common.endpoints.http.server.internal; import io.swagger.jersey.config.JerseyJaxrsConfig; import java.util.HashMap; import org.eclipse.jetty.servlet.ServletHolder; +import org.glassfish.jersey.server.ServerProperties; import org.onap.policy.common.utils.network.NetworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * REST Jetty Server that uses Jersey Servlets to support JAX-RS Web Services. + * + * <p>Note: the serialization provider will always be added to the server's class providers, + * as will the swagger providers (assuming swagger has been enabled). This happens whether + * {@link #addServletClass(String, String)} is used or + * {@link #addServletPackage(String, String)} is used. Thus it's possible to have both the + * server's class provider property and the server's package provider property populated. + * + * <p>Also note: the serialization provider will be ignored if the maven artifact, + * <i>jersey-media-json-jackson</i>, is included, regardless of whether it's included + * directly or indirectly. */ public class JettyJerseyServer extends JettyServletServer { @@ -53,26 +64,6 @@ public class JettyJerseyServer extends JettyServletServer { protected static final String SWAGGER_PRETTY_PRINT = "swagger.pretty.print"; /** - * Swagger Packages. - */ - protected static final String SWAGGER_INIT_PACKAGES_PARAM_VALUE = "io.swagger.jaxrs.listing"; - - /** - * Jersey Packages Init Param Name. - */ - protected static final String JERSEY_INIT_PACKAGES_PARAM_NAME = "jersey.config.server.provider.packages"; - - /** - * Jersey Packages Init Param Value. - */ - protected static final String JERSEY_INIT_PACKAGES_PARAM_VALUE = "com.fasterxml.jackson.jaxrs.json"; - - /** - * Jersey Classes Init Param Name. - */ - protected static final String JERSEY_INIT_CLASSNAMES_PARAM_NAME = "jersey.config.server.provider.classnames"; - - /** * Jersey Jackson Classes Init Param Value. */ protected static final String JERSEY_JACKSON_INIT_CLASSNAMES_PARAM_VALUE = @@ -99,15 +90,20 @@ public class JettyJerseyServer extends JettyServletServer { protected String swaggerId = null; /** + * The serialization provider to be used when classes are added to the service. + */ + private String classProvider = JERSEY_JACKSON_INIT_CLASSNAMES_PARAM_VALUE; + + /** * Constructor. - * + * * @param name name * @param https enable https? * @param host host server host * @param port port server port * @param swagger support swagger? * @param contextPath context path - * + * * @throws IllegalArgumentException in invalid arguments are provided */ public JettyJerseyServer(String name, boolean https, String host, int port, String contextPath, boolean swagger) { @@ -145,10 +141,10 @@ public class JettyJerseyServer extends JettyServletServer { /** * Retrieves cached server based on servlet path. - * + * * @param servletPath servlet path * @return the jetty servlet holder - * + * * @throws IllegalArgumentException if invalid arguments are provided */ protected synchronized ServletHolder getServlet(String servletPath) { @@ -176,27 +172,17 @@ public class JettyJerseyServer extends JettyServletServer { ServletHolder jerseyServlet = this.getServlet(servPath); - String initClasses = jerseyServlet.getInitParameter(JERSEY_INIT_CLASSNAMES_PARAM_NAME); - if (initClasses != null && !initClasses.isEmpty()) { - logger.warn("Both packages and classes are used in Jetty+Jersey Configuration: {}", restPackage); - } + initStandardParams(jerseyServlet); - String initPackages = jerseyServlet.getInitParameter(JERSEY_INIT_PACKAGES_PARAM_NAME); + String initPackages = jerseyServlet.getInitParameter(ServerProperties.PROVIDER_PACKAGES); if (initPackages == null) { - if (this.swaggerId != null) { - initPackages = - JERSEY_INIT_PACKAGES_PARAM_VALUE + "," + SWAGGER_INIT_PACKAGES_PARAM_VALUE + "," + restPackage; - - jerseyServlet.setInitParameter(SWAGGER_CONTEXT_ID, swaggerId); - jerseyServlet.setInitParameter(SWAGGER_SCANNER_ID, swaggerId); - } else { - initPackages = JERSEY_INIT_PACKAGES_PARAM_VALUE + "," + restPackage; - } + initPackages = restPackage; + } else { - initPackages = initPackages + "," + restPackage; + initPackages += "," + restPackage; } - jerseyServlet.setInitParameter(JERSEY_INIT_PACKAGES_PARAM_NAME, initPackages); + jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, initPackages); if (logger.isDebugEnabled()) { logger.debug("{}: added REST package: {}", this, jerseyServlet.dump()); @@ -216,33 +202,59 @@ public class JettyJerseyServer extends JettyServletServer { ServletHolder jerseyServlet = this.getServlet(servletPath); - String initPackages = jerseyServlet.getInitParameter(JERSEY_INIT_PACKAGES_PARAM_NAME); - if (initPackages != null && !initPackages.isEmpty()) { - logger.warn("Both classes and packages are used in Jetty+Jersey Configuration: {}", restClass); - } + initStandardParams(jerseyServlet); - String initClasses = jerseyServlet.getInitParameter(JERSEY_INIT_CLASSNAMES_PARAM_NAME); + String initClasses = jerseyServlet.getInitParameter(ServerProperties.PROVIDER_CLASSNAMES); if (initClasses == null) { - if (this.swaggerId != null) { - initClasses = JERSEY_JACKSON_INIT_CLASSNAMES_PARAM_VALUE + "," + SWAGGER_INIT_CLASSNAMES_PARAM_VALUE - + "," + restClass; - - jerseyServlet.setInitParameter(SWAGGER_CONTEXT_ID, swaggerId); - jerseyServlet.setInitParameter(SWAGGER_SCANNER_ID, swaggerId); - } else { - initClasses = JERSEY_JACKSON_INIT_CLASSNAMES_PARAM_VALUE + "," + restClass; - } + initClasses = restClass; + } else { - initClasses = initClasses + "," + restClass; + initClasses += "," + restClass; } - jerseyServlet.setInitParameter(JERSEY_INIT_CLASSNAMES_PARAM_NAME, initClasses); + jerseyServlet.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES, initClasses); if (logger.isDebugEnabled()) { logger.debug("{}: added REST class: {}", this, jerseyServlet.dump()); } } + /** + * Adds "standard" parameters to the initParameter set. Sets swagger parameters, if + * specified, and sets the class provider property. This can be invoked multiple + * times, but only the first actually causes any changes to the parameter set. + * + * @param jerseyServlet servlet into which parameters should be added + */ + private void initStandardParams(ServletHolder jerseyServlet) { + String initClasses = jerseyServlet.getInitParameter(ServerProperties.PROVIDER_CLASSNAMES); + if (initClasses != null) { + return; + } + + initClasses = classProvider; + + if (this.swaggerId != null) { + initClasses += "," + SWAGGER_INIT_CLASSNAMES_PARAM_VALUE; + + jerseyServlet.setInitParameter(SWAGGER_CONTEXT_ID, swaggerId); + jerseyServlet.setInitParameter(SWAGGER_SCANNER_ID, swaggerId); + } + + jerseyServlet.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES, initClasses); + + jerseyServlet.setInitParameter(ServerProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + } + + /** + * Note: this must be invoked <i>before</i> {@link #addServletClass(String, String)} + * or {@link #addServletPackage(String, String)}. + */ + @Override + public void setSerializationProvider(String provider) { + classProvider = provider; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java index 883ba7d2..2b1a9a3f 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/properties/PolicyEndPointProperties.java @@ -94,6 +94,8 @@ public interface PolicyEndPointProperties { String PROPERTY_HTTP_HTTPS_SUFFIX = ".https"; String PROPERTY_HTTP_SWAGGER_SUFFIX = ".swagger"; + String PROPERTY_HTTP_SERIALIZATION_PROVIDER = ".serialization.provider"; + /* HTTP Client Properties */ String PROPERTY_HTTP_CLIENT_SERVICES = "http.client.services"; |