Commit f3330bd1 authored by Matt Wise's avatar Matt Wise
Browse files

DMS enhancements for genericization

commit a611e240 
Author: Matt Wise <wsmatth@amazon.com> 
Date: Thu Jan 14 2021 09:55:16 GMT-0600 (Central Standard Time) 

    refactor DMS service impl to be more generic
parent 8258398c
......@@ -14,7 +14,6 @@
package org.opengroup.osdu.dataset.di;
import org.opengroup.osdu.dataset.dms.DmsAPIConfig;
import org.opengroup.osdu.dataset.dms.DmsFactory;
import org.opengroup.osdu.dataset.dms.IDmsFactory;
import org.springframework.beans.factory.annotation.Value;
......@@ -34,9 +33,6 @@ public class DmsClientFactory extends AbstractFactoryBean<IDmsFactory> {
@Override
protected IDmsFactory createInstance() throws Exception {
return new DmsFactory(DmsAPIConfig
.builder()
.rootUrl(DMS_API_BASE)
.build());
return new DmsFactory();
}
}
......@@ -21,22 +21,15 @@ import org.opengroup.osdu.core.common.http.HttpClient;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
public class DmsFactory implements IDmsFactory {
private final DmsAPIConfig config;
public DmsFactory(DmsAPIConfig config) {
if (config == null) {
throw new IllegalArgumentException("DmsAPIConfig cannot be empty");
}
this.config = config;
public DmsFactory() {
}
@Override
public IDmsProvider create(DpsHeaders headers, String dmsServiceRoute) {
public IDmsProvider create(DpsHeaders headers, DmsServiceProperties dmsServiceProperties) {
if (headers == null) {
throw new NullPointerException("headers cannot be null");
}
return new DmsService(this.config, dmsServiceRoute, new HttpClient(), headers);
return new DmsService(dmsServiceProperties, new HttpClient(), headers);
}
}
......@@ -17,41 +17,36 @@
package org.opengroup.osdu.dataset.dms;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.http.IHttpClient;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.storage.MultiRecordIds;
import org.opengroup.osdu.core.common.model.storage.MultiRecordInfo;
import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.core.common.model.storage.Schema;
import org.opengroup.osdu.dataset.model.request.GetDatasetRegistryRequest;
import org.opengroup.osdu.dataset.model.response.GetDatasetRetrievalInstructionsResponse;
import org.opengroup.osdu.dataset.model.response.GetDatasetStorageInstructionsResponse;
import org.springframework.http.HttpStatus;
public class DmsService implements IDmsProvider {
private final String rootUrl;
private String dmsServiceRoute;
private String dmsServiceUrl;
private DmsServiceProperties dmsServiceProperties;
private final IHttpClient httpClient;
private final DpsHeaders headers;
public DmsService(DmsAPIConfig config, String dmsServiceRoute, IHttpClient httpClient, DpsHeaders headers) {
public DmsService(DmsServiceProperties dmsServiceProperties, IHttpClient httpClient, DpsHeaders headers) {
this.dmsServiceRoute = dmsServiceRoute;
this.rootUrl = config.getRootUrl();
this.dmsServiceProperties = dmsServiceProperties;
this.dmsServiceUrl = dmsServiceProperties.getDmsServiceBaseUrl();
this.httpClient = httpClient;
this.headers = headers;
if (config.apiKey != null) {
headers.put("AppKey", config.apiKey);
if (dmsServiceProperties.getApiKey() != null) {
headers.put("AppKey", dmsServiceProperties.getApiKey());
}
}
......@@ -76,7 +71,7 @@ public class DmsService implements IDmsProvider {
private String createUrl(String requestPathAndQuery) {
return StringUtils.join(this.rootUrl, this.dmsServiceRoute, requestPathAndQuery);
return StringUtils.join(this.dmsServiceUrl, requestPathAndQuery);
}
private <T> T getResult(HttpResponse result, Class<T> type) throws DmsException {
......
// Copyright © 2020 Amazon Web Services
// Copyright 2017-2019, Schlumberger
// Copyright © 2021 Amazon Web Services
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
......@@ -13,22 +12,35 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//TODO: move to os-core-common
package org.opengroup.osdu.dataset.dms;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Builder
@Data
public class DmsAPIConfig {
@Builder.Default
String rootUrl = "https://osdu-api";
@NoArgsConstructor
@AllArgsConstructor
public class DmsServiceProperties {
//url to dms api base endpoint. ex: https://some-url/api/dms/file/v1/file-collection
private String dmsServiceBaseUrl;
//mark this is as false if the DMS service can only retrieve datasets, but cant store new ones via OSDU
private boolean allowStorage = true;
//optional API key if required for connecting to service
private String apiKey;
String apiKey;
//additional constructors
public DmsServiceProperties(String dmsServiceBaseUrl) {
this.dmsServiceBaseUrl = dmsServiceBaseUrl;
}
public DmsServiceProperties(String dmsServiceBaseUrl, boolean allowStorage) {
this(dmsServiceBaseUrl);
public static DmsAPIConfig Default() {
return DmsAPIConfig.builder().build();
this.allowStorage = allowStorage;
}
}
......@@ -19,5 +19,5 @@ package org.opengroup.osdu.dataset.dms;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
public interface IDmsFactory {
IDmsProvider create(DpsHeaders headers, String dmsServiceRoute);
IDmsProvider create(DpsHeaders headers, DmsServiceProperties dmsServiceRoute);
}
\ No newline at end of file
......@@ -22,4 +22,5 @@ public class DmsValidationDoc {
}
public static final String RESOURCE_TYPE_NOT_REGISTERED_ERROR = "No DMS handler for resource type '%s' is registered";
public static final String DMS_STORAGE_NOT_SUPPORTED_ERROR = "The requested DMS (%s) does not support storage operations";
}
......@@ -16,8 +16,10 @@ package org.opengroup.osdu.dataset.provider.interfaces;
import java.util.Map;
import org.opengroup.osdu.dataset.dms.DmsServiceProperties;
public interface IDatasetDmsServiceMap {
public Map<String, String> getResourceTypeToDmsServiceMap();
public Map<String, DmsServiceProperties> getResourceTypeToDmsServiceMap();
}
......@@ -31,6 +31,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.storage.MultiRecordIds;
import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.dataset.dms.DmsException;
import org.opengroup.osdu.dataset.dms.DmsServiceProperties;
import org.opengroup.osdu.dataset.dms.IDmsFactory;
import org.opengroup.osdu.dataset.dms.IDmsProvider;
import org.opengroup.osdu.dataset.model.request.DmsExceptionResponse;
......@@ -77,19 +78,26 @@ public class DatasetDmsServiceImpl implements DatasetDmsService {
@Override
public GetDatasetStorageInstructionsResponse getStorageInstructions(String resourceType) {
Map<String,String> resourceTypeToDmsServiceMap = dmsServiceMap.getResourceTypeToDmsServiceMap();
Map<String,DmsServiceProperties> resourceTypeToDmsServiceMap = dmsServiceMap.getResourceTypeToDmsServiceMap();
if (!resourceTypeToDmsServiceMap.containsKey(resourceType)) {
DmsServiceProperties dmsServiceProperties = resourceTypeToDmsServiceMap.get(resourceType);
if (dmsServiceProperties == null) {
throw new AppException(HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.getReasonPhrase(),
String.format(DmsValidationDoc.RESOURCE_TYPE_NOT_REGISTERED_ERROR, resourceType));
}
if (!dmsServiceProperties.isAllowStorage()) {
HttpStatus status = HttpStatus.METHOD_NOT_ALLOWED;
throw new AppException(status.value(), "DMS - Storage Not Supported", String.format(DmsValidationDoc.DMS_STORAGE_NOT_SUPPORTED_ERROR, resourceType));
}
GetDatasetStorageInstructionsResponse response = null;
try {
IDmsProvider dmsProvider = dmsFactory.create(headers, resourceTypeToDmsServiceMap.get(resourceType));
IDmsProvider dmsProvider = dmsFactory.create(headers, dmsServiceProperties);
response = dmsProvider.getStorageInstructions();
}
......@@ -118,7 +126,7 @@ public class DatasetDmsServiceImpl implements DatasetDmsService {
@Override
public GetDatasetRetrievalInstructionsResponse getDatasetRetrievalInstructions(List<String> datasetRegistryIds) {
Map<String,String> resourceTypeToDmsServiceMap = dmsServiceMap.getResourceTypeToDmsServiceMap();
Map<String,DmsServiceProperties> resourceTypeToDmsServiceMap = dmsServiceMap.getResourceTypeToDmsServiceMap();
GetRecordsResponse getRecordsResponse = null;
......
......@@ -19,23 +19,37 @@ import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.dataset.dms.DmsServiceProperties;
import org.opengroup.osdu.dataset.provider.interfaces.IDatasetDmsServiceMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class DatasetDmsServiceMapImpl implements IDatasetDmsServiceMap {
private Map<String,String> resourceTypeToDmsServiceMap = new HashMap<>();
@Value("${DMS_API_BASE}")
private String DMS_API_BASE;
private Map<String,DmsServiceProperties> resourceTypeToDmsServiceMap = new HashMap<>();
@PostConstruct
public void init() {
//todo: replace this with service discovery / registered db entries
resourceTypeToDmsServiceMap.put("srn:type:file", "/api/dms/file/v1/file");
resourceTypeToDmsServiceMap.put("srn:type:filecollection", "/api/dms/file/v1/file-collection");
resourceTypeToDmsServiceMap.put(
"srn:type:file",
new DmsServiceProperties(StringUtils.join(DMS_API_BASE, "/api/dms/file/v1/file"))
);
resourceTypeToDmsServiceMap.put(
"srn:type:file-collection",
new DmsServiceProperties(StringUtils.join(DMS_API_BASE, "/api/dms/file/v1/file-collection"))
);
}
@Override
public Map<String, String> getResourceTypeToDmsServiceMap() {
public Map<String, DmsServiceProperties> getResourceTypeToDmsServiceMap() {
return resourceTypeToDmsServiceMap;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment