diff options
Diffstat (limited to 'blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java')
-rw-r--r-- | blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java new file mode 100644 index 000000000..55cdca40b --- /dev/null +++ b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientAdapterImpl.java @@ -0,0 +1,136 @@ +/*
+ * Copyright © 2017-2018 AT&T Intellectual Property.
+ *
+ * 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.
+ */
+
+package org.onap.ccsdk.config.rest.adaptor.service;
+
+import java.util.Map;
+import java.util.UUID;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
+import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+public class GenericRestClientAdapterImpl extends AbstractConfigRestClientAdapter {
+
+ private static EELFLogger logger = EELFManager.getInstance().getLogger(GenericRestClientAdapterImpl.class);
+ private String baseUrl = "";
+
+ public GenericRestClientAdapterImpl(Map<String, String> properties, String serviceSelector)
+ throws ConfigRestAdaptorException {
+ super(properties, serviceSelector);
+ init(serviceSelector);
+ }
+
+ private void init(String serviceSelector) throws ConfigRestAdaptorException {
+ try {
+ if (isRestClientServiceAdapaterEnabled) {
+ String baseUrlProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_BASEURL_PROPERTY;
+ String userProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_USER_PROPERTY;
+ String passProperty = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_PSSWD_PROPERTY;
+
+ baseUrl = properties.get(baseUrlProperty);
+ String userId = properties.get(userProperty);
+ String pass = properties.get(passProperty);
+
+ initialise(userId, pass);
+ logger.info("Initialised restconf adaptor service for selector ({})", serviceSelector);
+ if (restTemplate == null) {
+ throw new ConfigRestAdaptorException("couldn't initialise rest selector (" + serviceSelector + ")");
+ }
+ } else {
+ throw new ConfigRestAdaptorException("rest selector (" + serviceSelector + ") is not enabled");
+ }
+
+ } catch (Exception e) {
+ throw new ConfigRestAdaptorException("GenericRestClientServiceAdapter : " + e.getMessage(), e);
+ }
+ }
+
+ @Override
+ public <T> T getResource(String path, Class<T> responseType) throws ConfigRestAdaptorException {
+ return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path), responseType);
+ }
+
+ @Override
+ public <T> T postResource(String path, Object request, Class<T> responseType) throws ConfigRestAdaptorException {
+ return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType);
+ }
+
+ @Override
+ public <T> T exchangeResource(String path, Object request, Class<T> responseType, String method)
+ throws ConfigRestAdaptorException {
+ return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, responseType, method);
+ }
+
+ @Override
+ public RestResponse getResource(String path) throws ConfigRestAdaptorException {
+ return super.getResource(formHttpHeaders(), constructUrl(baseUrl, path));
+ }
+
+ @Override
+ public RestResponse postResource(String path, Object request) throws ConfigRestAdaptorException {
+ return super.postResource(formHttpHeaders(), constructUrl(baseUrl, path), request);
+ }
+
+ @Override
+ public RestResponse exchangeResource(String path, Object request, String method) throws ConfigRestAdaptorException {
+ return super.exchangeResource(formHttpHeaders(), constructUrl(baseUrl, path), request, method);
+ }
+
+ private HttpHeaders formHttpHeaders() {
+
+ HttpHeaders headers = new HttpHeaders();
+
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
+ headers.add("X-TransactionId", generateUUID());
+ headers.add("X-ECOMP-RequestID", headers.getFirst("X-TransactionId"));
+
+ String appIDPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_APPID_PROPERTY;
+ String environmentPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_ENV_PROPERTY;
+ String clientAuthPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_CLIENTAUTH_PROPERTY;
+ String authorizationPath = ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
+ + ConfigRestAdaptorConstants.SERVICE_AUTHORIZATION_PROPERTY;
+
+ if (StringUtils.isNotBlank(properties.get(appIDPath))) {
+ headers.add("X-FromAppId", properties.get(appIDPath));
+ }
+ if (StringUtils.isNotBlank(properties.get(clientAuthPath))) {
+ headers.add("ClientAuth", properties.get(clientAuthPath));
+ }
+ if (StringUtils.isNotBlank(properties.get(authorizationPath))) {
+ headers.add("Authorization", properties.get(authorizationPath));
+ }
+ if (StringUtils.isNotBlank(properties.get(environmentPath))) {
+ headers.add("Environment", properties.get(environmentPath));
+ }
+
+ return headers;
+ }
+
+ private synchronized String generateUUID() {
+ return UUID.randomUUID().toString();
+ }
+
+}
|