diff options
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); + } } |