2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.client.policy;
23 import java.net.MalformedURLException;
26 import java.util.HashMap;
28 import java.util.Map.Entry;
29 import java.util.Optional;
30 import java.util.UUID;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.client.ClientResponseFilter;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.Invocation.Builder;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.GenericType;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.ext.ContextResolver;
44 import org.apache.log4j.Logger;
45 import org.openecomp.mso.client.RestProperties;
46 import org.openecomp.mso.logger.MsoLogger;
47 import org.springframework.stereotype.Service;
49 import com.fasterxml.jackson.databind.ObjectMapper;
52 public abstract class RestClient {
53 protected static final String ECOMP_COMPONENT_NAME = "MSO";
55 private static final int MAX_PAYLOAD_SIZE = 1024 * 1024;
56 private WebTarget webTarget;
58 protected final Map<String, String> headerMap;
59 protected final MsoLogger msoLogger;
61 protected Optional<URI> path;
62 protected Logger logger;
63 protected String accept;
64 protected String contentType;
65 protected UUID requestId;
67 protected RestClient(RestProperties props, UUID requestId, Optional<URI> path) {
68 logger = Logger.getLogger(getClass().getName());
69 msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
70 this.requestId = requestId;
71 headerMap = new HashMap<>();
73 host = props.getEndpoint();
74 } catch (MalformedURLException e) {
75 logger.error("url not valid", e);
76 throw new RuntimeException(e);
80 initializeClient(getClient());
83 protected RestClient(RestProperties props, UUID requestId, Optional<URI> path, String accept, String contentType) {
84 this(props, requestId, path);
86 this.contentType = contentType;
87 this.requestId = requestId;
91 protected RestClient(URL host, UUID requestId, String contentType) {
92 headerMap = new HashMap<>();
93 logger = Logger.getLogger(getClass().getName());
94 msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
95 this.path = Optional.empty();
97 this.contentType = contentType;
98 this.requestId = requestId;
99 initializeClient(getClient());
103 * Override method to return false to disable logging.
105 * @return true - to enable logging, false otherwise
107 protected boolean enableLogging() {
112 * Override method to return custom value for max payload size.
114 * @return Default value for MAX_PAYLOAD_SIZE = 1024 * 1024
116 protected int getMaxPayloadSize()
118 return MAX_PAYLOAD_SIZE;
121 protected Builder getBuilder() {
123 Builder builder = webTarget.request();
124 initializeHeaderMap(headerMap);
126 for (Entry<String, String> entry : headerMap.entrySet()) {
127 builder.header(entry.getKey(), entry.getValue());
132 protected abstract void initializeHeaderMap(Map<String, String> headerMap);
134 protected abstract Optional<ClientResponseFilter> addResponseFilter();
136 public abstract RestClient addRequestId(UUID requestId);
138 protected ContextResolver<ObjectMapper> getMapper() {
139 return new CommonObjectMapperProvider();
142 protected String getAccept() {
146 protected String getContentType() {
150 protected String getMergeContentType() {
151 return "application/merge-patch+json";
154 protected Client getClient() {
155 return ClientBuilder.newBuilder().build();
158 protected UUID getRequestId() {
159 return this.requestId;
161 protected void initializeClient(Client client) {
162 if (this.enableLogging()) {
163 client.register(logger).register(new LoggingFilter(this.getMaxPayloadSize()));
165 client.register(this.getMapper());
166 Optional<ClientResponseFilter> responseFilter = this.addResponseFilter();
167 responseFilter.ifPresent(clientResponseFilter -> client.register(clientResponseFilter));
168 webTarget = path.<WebTarget>map(uri -> client.target(UriBuilder.fromUri(host + uri.toString())))
169 .orElseGet(() -> client.target(host.toString()));
170 this.accept = MediaType.APPLICATION_JSON;
171 this.contentType = MediaType.APPLICATION_JSON;
174 public Response get() {
175 return this.getBuilder().accept(this.getAccept()).get();
178 public Response post(Object obj) {
179 return this.getBuilder().accept(this.getAccept()).post(Entity.entity(obj, this.getContentType()));
182 public Response patch(Object obj) {
183 return this.getBuilder().header("X-HTTP-Method-Override", "PATCH").accept(this.getAccept())
184 .post(Entity.entity(obj, this.getMergeContentType()));
187 public Response put(Object obj) {
188 return this.getBuilder().accept(this.getAccept()).put(Entity.entity(obj, this.getContentType()));
191 public Response delete() {
192 return this.getBuilder().accept(this.getAccept()).delete();
195 public Response delete(Object obj) {
196 return this.getBuilder().header("X-HTTP-Method-Override", "DELETE").accept(this.getAccept())
197 .put(Entity.entity(obj, this.getContentType()));
200 public <T> T get(Class<T> resultClass) {
201 return this.get().readEntity(resultClass);
204 public <T> T get(GenericType<T> resultClass) {
205 return this.get().readEntity(resultClass);
208 public <T> T post(Object obj, Class<T> resultClass) {
209 return this.post(obj).readEntity(resultClass);
212 public <T> T patch(Object obj, Class<T> resultClass) {
213 return this.patch(obj).readEntity(resultClass);
216 public <T> T put(Object obj, Class<T> resultClass) {
217 return this.put(obj).readEntity(resultClass);
220 public <T> T delete(Class<T> resultClass) {
221 return this.delete().readEntity(resultClass);
224 public <T> T delete(Object obj, Class<T> resultClass) {
225 return this.delete(obj).readEntity(resultClass);