diff options
author | 2017-11-06 15:01:36 -0500 | |
---|---|---|
committer | 2017-11-07 08:42:12 -0500 | |
commit | 13728948b328e856c8892d2f6e47f076df09c757 (patch) | |
tree | cbd810749db590e9e1c94e0bd2cf6ac4096b9a1a /bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java | |
parent | 134be34c844a78f0bb7f6f597417547e7678eee1 (diff) |
modified sample SDNO code for SO
Issue-Id: SO-309
Change-Id: I998d5573d60d99987bb99133967ca012ef8dba6f
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
modified RestClient to accept rest properties
Issue-Id: SO-309
Change-Id: I68bd88f9f942573dc470dda2eb60dc747e782768
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
moved SDNO beans to their own package
Issue-Id: SO-309
Change-Id: I0b35edfe0ac3ad336320d8bc62d2efd98fff1b0b
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
added enum for ServiceType in policy
Issue-Id: SO-309
Change-Id: Icdc95b237a427bbacac4cf5d815fc71cebb91675
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
dmaap rest client implementation
Issue-Id: SO-309
Change-Id: I1bf8b85820524adca462d7d96c4c0beef5892864
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
dmaap client interface for SO component
Issue-Id: SO-309
Change-Id: Ieccfb22678c52af7ffcf8750a38462ba32486182
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
added required guava dependency to pom.xml
Issue-Id: SO-309
Change-Id: I3096bf54888e4960e60bb73b13d8cd3223217271
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
Diffstat (limited to 'bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java')
-rw-r--r-- | bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java | 181 |
1 files changed, 165 insertions, 16 deletions
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java index 81c072ff33..74b1c3f802 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/policy/RestClient.java @@ -20,51 +20,104 @@ package org.openecomp.mso.client.policy; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import java.util.logging.Logger; +import java.util.Optional; +import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.ext.ContextResolver; - -import org.openecomp.mso.bpmn.core.PropertyConfiguration; +import org.apache.log4j.Logger; +import org.openecomp.mso.client.RestProperties; import org.openecomp.mso.logger.MsoLogger; import org.springframework.stereotype.Service; +import com.fasterxml.jackson.databind.ObjectMapper; + @Service public abstract class RestClient { protected static final String ECOMP_COMPONENT_NAME = "MSO"; - + + private static final int MAX_PAYLOAD_SIZE = 1024 * 1024; private WebTarget webTarget; protected final Map<String, String> headerMap; protected final MsoLogger msoLogger; - protected Map<String, String> properties; - protected String host; + protected URL host; + protected Optional<URI> path; + protected Logger logger; + protected String accept; + protected String contentType; - protected RestClient(String endpointKey) { - Logger logger = Logger.getLogger(getClass().getName()); + protected RestClient(RestProperties props, Optional<URI> path) { + logger = Logger.getLogger(getClass().getName()); msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL); - - properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties"); + headerMap = new HashMap<>(); - initializeHeaderMap(headerMap); + try { + host = props.getEndpoint(); + } catch (MalformedURLException e) { + logger.error("url not valid", e); + throw new RuntimeException(e); + } + + this.path = path; + initializeClient(getClient()); + } - host = this.getHost(endpointKey); + protected RestClient(RestProperties props, Optional<URI> path, String accept, String contentType) { + this(props, path); + this.accept = accept; + this.contentType = contentType; - webTarget = ClientBuilder.newClient().register(logger).register(new LoggingFilter()) - .register(new CommonObjectMapperProvider()).target(host); } - private String getHost(String key) { - return properties.get(key); + protected RestClient(URL host, String contentType) { + headerMap = new HashMap<>(); + logger = Logger.getLogger(getClass().getName()); + msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL); + this.path = Optional.empty(); + this.host = host; + this.contentType = contentType; + initializeClient(getClient()); + } + + /** + * Override method to return false to disable logging. + * + * @return true - to enable logging, false otherwise + */ + protected boolean enableLogging() { + return true; + } + + /** + * Override method to return custom value for max payload size. + * + * @return Default value for MAX_PAYLOAD_SIZE = 1024 * 1024 + */ + protected int getMaxPayloadSize() + { + return MAX_PAYLOAD_SIZE; } protected Builder getBuilder() { + Builder builder = webTarget.request(); + initializeHeaderMap(headerMap); for (Entry<String, String> entry : headerMap.entrySet()) { builder.header(entry.getKey(), entry.getValue()); @@ -73,4 +126,100 @@ public abstract class RestClient { } protected abstract void initializeHeaderMap(Map<String, String> headerMap); + + protected abstract Optional<ClientResponseFilter> addResponseFilter(); + + public abstract RestClient addRequestId(String requestId); + + protected ContextResolver<ObjectMapper> getMapper() { + return new CommonObjectMapperProvider(); + } + + protected String getAccept() { + return accept; + } + + protected String getContentType() { + return contentType; + } + + protected String getMergeContentType() { + return "application/merge-patch+json"; + } + + protected Client getClient() { + return ClientBuilder.newBuilder().build(); + } + + protected void initializeClient(Client client) { + if (this.enableLogging()) { + client.register(logger).register(new LoggingFilter(this.getMaxPayloadSize())); + } + client.register(this.getMapper()); + Optional<ClientResponseFilter> responseFilter = this.addResponseFilter(); + if (responseFilter.isPresent()) { + client.register(responseFilter.get()); + } + if (!path.isPresent()) { + webTarget = client.target(host.toString()); + } else { + webTarget = client.target(UriBuilder.fromUri(host + path.get().toString())); + } + this.accept = MediaType.APPLICATION_JSON; + this.contentType = MediaType.APPLICATION_JSON; + } + + public Response get() { + return this.getBuilder().accept(this.getAccept()).get(); + } + + public Response post(Object obj) { + return this.getBuilder().accept(this.getAccept()).post(Entity.entity(obj, this.getContentType())); + } + + public Response patch(Object obj) { + return this.getBuilder().header("X-HTTP-Method-Override", "PATCH").accept(this.getAccept()) + .post(Entity.entity(obj, this.getMergeContentType())); + } + + public Response put(Object obj) { + return this.getBuilder().accept(this.getAccept()).put(Entity.entity(obj, this.getContentType())); + } + + public Response delete() { + return this.getBuilder().accept(this.getAccept()).delete(); + } + + public Response delete(Object obj) { + return this.getBuilder().header("X-HTTP-Method-Override", "DELETE").accept(this.getAccept()) + .put(Entity.entity(obj, this.getContentType())); + } + + public <T> T get(Class<T> resultClass) { + return this.get().readEntity(resultClass); + } + + public <T> T get(GenericType<T> resultClass) { + return this.get().readEntity(resultClass); + } + + public <T> T post(Object obj, Class<T> resultClass) { + return this.post(obj).readEntity(resultClass); + } + + public <T> T patch(Object obj, Class<T> resultClass) { + return this.patch(obj).readEntity(resultClass); + } + + public <T> T put(Object obj, Class<T> resultClass) { + return this.put(obj).readEntity(resultClass); + } + + public <T> T delete(Class<T> resultClass) { + return this.delete().readEntity(resultClass); + } + + public <T> T delete(Object obj, Class<T> resultClass) { + return this.delete(obj).readEntity(resultClass); + } } |