Commit 749780c2 authored by ethiraj krishnamanaidu's avatar ethiraj krishnamanaidu
Browse files

OpenDES GA code merge

parent ae115e34
Pipeline #2486 passed with stages
in 4 minutes and 58 seconds
// Copyright 2017-2019, Schlumberger
//
// 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.opengroup.osdu.core.common.crs;
public class CrsConversionServiceErrorMessages {
/**
* Please do not change these error messages as these are included in public interface.
* Or if you do need to change, just be aware those changes can be breaking changes to our consumers.
*/
public static final String MISSING_DATA_BLOCK = "CRS Conversion: DataBlock is missing or empty in this record, no conversion applied.";
public static final String ILLEGAL_METAITEM_ARRAY = "CRS conversion: error when extract metaItem array, error message: %s, no conversion applied.";
public static final String MISSING_META_KIND = "CRS conversion: Required property 'kind' in meta block is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY_NAMES = "CRS conversion: 'propertyNames' in meta block is missing or empty, no conversion applied";
public static final String ILLEGAL_PROPERTY_NAMES = "CRS conversion: 'propertyNames' illegal, no conversion applied.";
public static final String MISSING_REFERENCE = "CRS conversion: 'persistableReference' missing, no conversion applied.";
public static final String MISSING_PROPERTY = "CRS conversion: property '%s' is missing in datablock, no conversion applied to this property and its corresponding pairing property.";
public static final String PROPERTY_VALUE_CAST_ERROR = "CRS conversion: cannot cast the value of property '%s' to double, error message: %s, no conversion applied.";
public static final String ILLEGAL_PROPERTY_VALUE = "CRS conversion: illegal value for property '%s', error message: %s, no conversion applied.";
public static final String INVALID_NESTED_PROPERTY_NAME = "CRS conversion: invalid nested property name: '%s', no conversion applied.";
public static final String MISSING_POINTS_IN_NESTED_PROPERTY = "CRS conversion: missing the property 'points' in nested property, no conversion applied.";
public static final String BAD_REQUEST_FROM_CRS = "CRS conversion: bad request from crs converter, illegal persistable reference, no conversion applied. Response From CRS Converter: %s. Involved properties: %s.";
public static final String CRS_OTHER_ERROR = "Response from CRS converter: %s";
public static final String ILLEGAL_DATA_IN_NESTED_PROPERTY = "CRS conversion: illegal value in nested property '%s', error message: %s, no conversion applied.";
public static final String PAIR_FAILURE = "CRS conversion: Unknown coordinate pair '%s'.";
}
// Copyright 2017-2019, Schlumberger
//
// 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.opengroup.osdu.core.common.crs;
public class CrsConversionServiceErrorMessages {
/**
* Please do not change these error messages as these are included in public interface.
* Or if you do need to change, just be aware those changes can be breaking changes to our consumers.
*/
public static final String MISSING_DATA_BLOCK = "CRS Conversion: DataBlock is missing or empty in this record, no conversion applied.";
public static final String ILLEGAL_METAITEM_ARRAY = "CRS conversion: error when extract metaItem array, error message: %s, no conversion applied.";
public static final String MISSING_META_KIND = "CRS conversion: Required property 'kind' in meta block is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY_NAMES = "CRS conversion: 'propertyNames' in meta block is missing or empty, no conversion applied";
public static final String ILLEGAL_PROPERTY_NAMES = "CRS conversion: 'propertyNames' illegal, no conversion applied.";
public static final String MISSING_REFERENCE = "CRS conversion: 'persistableReference' missing, no conversion applied.";
public static final String MISSING_PROPERTY = "CRS conversion: property '%s' is missing in datablock, no conversion applied to this property and its corresponding pairing property.";
public static final String PROPERTY_VALUE_CAST_ERROR = "CRS conversion: cannot cast the value of property '%s' to double, error message: %s, no conversion applied.";
public static final String ILLEGAL_PROPERTY_VALUE = "CRS conversion: illegal value for property '%s', error message: %s, no conversion applied.";
public static final String INVALID_NESTED_PROPERTY_NAME = "CRS conversion: invalid nested property name: '%s', no conversion applied.";
public static final String MISSING_POINTS_IN_NESTED_PROPERTY = "CRS conversion: missing the property 'points' in nested property, no conversion applied.";
public static final String BAD_REQUEST_FROM_CRS = "CRS conversion: bad request from crs converter, illegal persistable reference, no conversion applied. Response From CRS Converter: %s. Involved properties: %s.";
public static final String CRS_OTHER_ERROR = "Response from CRS converter: %s";
public static final String ILLEGAL_DATA_IN_NESTED_PROPERTY = "CRS conversion: illegal value in nested property '%s', error message: %s, no conversion applied.";
public static final String PAIR_FAILURE = "CRS conversion: Unknown coordinate pair '%s'.";
}
package org.opengroup.osdu.core.common.crs.dates;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import org.opengroup.osdu.core.common.model.crs.ConversionRecord;
import org.opengroup.osdu.core.common.model.crs.ConvertStatus;
import org.opengroup.osdu.core.common.model.units.IDateTime;
import org.opengroup.osdu.core.common.model.units.ReferenceConverter;
import java.time.DateTimeException;
import java.time.format.DateTimeParseException;
import java.util.Iterator;
import java.util.List;
public class DatesConversionImpl {
private static final String KIND = "kind";
private static final String META = "meta";
private static final String DATA = "data";
private static final String DATETIME = "dateTime";
private static final String PROPERTY_NAMES = "propertyNames";
private static final String PERSISTABLE_REFERENCE = "persistableReference";
private static final String INVALID_VALUE_STRING = "Invalid value";
private static final String PARSE_ERROR_STRING = "could not be parsed at index";
public void convertDatesToISO(List<ConversionRecord> conversionRecords) {
for (int i = 0; i < conversionRecords.size(); i++) {
this.convertDateOrDatetimeToISO(conversionRecords.get(i));
}
}
private void convertDateOrDatetimeToISO(ConversionRecord conversionRecord) {
if (conversionRecord == null) {
return;
}
List<String> conversionMessages = conversionRecord.getConversionMessages();
JsonObject record = conversionRecord.getRecordJsonObject();
if (record == null) {
return;
}
JsonObject dataBlock = record.getAsJsonObject(DATA);
if (dataBlock == null) {
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_DATA_BLOCK);
conversionRecord.setConvertStatus(ConvertStatus.ERROR);
conversionRecord.setConversionMessages(conversionMessages);
return;
}
JsonArray metaArray = record.getAsJsonArray(META);
if (metaArray == null) {
conversionRecord.setConvertStatus(ConvertStatus.NO_FRAME_OF_REFERENCE);
return;
}
boolean hasFailure = false;
Iterator<JsonElement> metaIterator = metaArray.iterator();
while (metaIterator.hasNext()) {
JsonObject meta = (JsonObject) metaIterator.next();
if (meta == null) {
return;
}
JsonElement kind = meta.get(KIND);
if (kind == null || kind.getAsString().isEmpty()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_META_KIND);
continue;
}
String type = kind.getAsString();
if (type.equalsIgnoreCase(DATETIME)) {
JsonElement propertyNamesElement = meta.get(PROPERTY_NAMES);
if (propertyNamesElement == null) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_PROPERTY_NAMES);
continue;
}
JsonArray propertyNames = null;
try {
propertyNames = propertyNamesElement.getAsJsonArray();
} catch (IllegalStateException ex) {
propertyNames = null;
}
if (propertyNames == null) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.ILLEGAL_PROPERTY_NAMES);
continue;
}
JsonElement referenceElement = meta.get(PERSISTABLE_REFERENCE);
if (referenceElement == null || referenceElement.getAsString().isEmpty()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_REFERENCE);
continue;
}
String reference = referenceElement.getAsString();
IDateTime dateTime = ReferenceConverter.parseDateTimeReference(reference);
if (dateTime == null || !dateTime.isValid()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.INVALID_REFERENCE);
continue;
}
boolean datesConverted = false;
for (int i = 0; i < propertyNames.size(); i++) {
String name = propertyNames.get(i).getAsString();
String message = null;
try {
JsonElement valueElement = dataBlock.get(name);
if((valueElement == null) || (valueElement instanceof JsonNull)) {
hasFailure = true;
conversionMessages.add(String.format(DatesConversionServiceErrorMessages.MISSING_PROPERTY, name));
continue;
}
String value = dateTime.convertToIsoDateTime(valueElement.getAsString());
if (value == null) {
hasFailure = true;
conversionMessages.add(String.format(DatesConversionServiceErrorMessages.INVALID_REFERENCE));
continue;
}
dataBlock.remove(name);
dataBlock.addProperty(name, value);
datesConverted = true;
} catch(IllegalArgumentException ccEx) {
message = String.format(DatesConversionServiceErrorMessages.INVALID_FORMATTER, ccEx.getMessage());
} catch(DateTimeParseException ccEx) {
String exceptionMessage = ccEx.getMessage();
if (exceptionMessage.contains(PARSE_ERROR_STRING)) {
message = String.format(DatesConversionServiceErrorMessages.MISMATCH_REFERENCE, name);
} else if (exceptionMessage.contains(INVALID_VALUE_STRING)) {
String description = exceptionMessage.substring(exceptionMessage.indexOf(INVALID_VALUE_STRING));
message = String.format(DatesConversionServiceErrorMessages.INVALID_DATETIME_VALUE, ccEx.getParsedString(), name , description);
} else {
message = String.format(DatesConversionServiceErrorMessages.ERROR_PARSING_VALUE, ccEx.getParsedString());
}
} catch(DateTimeException ccEx) {
message = String.format(DatesConversionServiceErrorMessages.INVALID_TIMEZONE, ccEx.getMessage());
}
if(message != null) {
hasFailure = true;
conversionMessages.add(message);
}
}
if (datesConverted && !hasFailure) {
String basePersistableReference = dateTime.getBaseDateTime();
meta.remove(PERSISTABLE_REFERENCE);
meta.addProperty(PERSISTABLE_REFERENCE, basePersistableReference);
}
}
}
if (hasFailure) {
conversionRecord.setConvertStatus(ConvertStatus.ERROR);
}
conversionRecord.setConversionMessages(conversionMessages);
}
}
package org.opengroup.osdu.core.common.crs.dates;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import org.opengroup.osdu.core.common.model.crs.ConversionRecord;
import org.opengroup.osdu.core.common.model.crs.ConvertStatus;
import org.opengroup.osdu.core.common.model.units.IDateTime;
import org.opengroup.osdu.core.common.model.units.ReferenceConverter;
import java.time.DateTimeException;
import java.time.format.DateTimeParseException;
import java.util.Iterator;
import java.util.List;
public class DatesConversionImpl {
private static final String KIND = "kind";
private static final String META = "meta";
private static final String DATA = "data";
private static final String DATETIME = "dateTime";
private static final String PROPERTY_NAMES = "propertyNames";
private static final String PERSISTABLE_REFERENCE = "persistableReference";
private static final String INVALID_VALUE_STRING = "Invalid value";
private static final String PARSE_ERROR_STRING = "could not be parsed at index";
public void convertDatesToISO(List<ConversionRecord> conversionRecords) {
for (int i = 0; i < conversionRecords.size(); i++) {
this.convertDateOrDatetimeToISO(conversionRecords.get(i));
}
}
private void convertDateOrDatetimeToISO(ConversionRecord conversionRecord) {
if (conversionRecord == null) {
return;
}
List<String> conversionMessages = conversionRecord.getConversionMessages();
JsonObject record = conversionRecord.getRecordJsonObject();
if (record == null) {
return;
}
JsonObject dataBlock = record.getAsJsonObject(DATA);
if (dataBlock == null) {
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_DATA_BLOCK);
conversionRecord.setConvertStatus(ConvertStatus.ERROR);
conversionRecord.setConversionMessages(conversionMessages);
return;
}
JsonArray metaArray = record.getAsJsonArray(META);
if (metaArray == null) {
conversionRecord.setConvertStatus(ConvertStatus.NO_FRAME_OF_REFERENCE);
return;
}
boolean hasFailure = false;
Iterator<JsonElement> metaIterator = metaArray.iterator();
while (metaIterator.hasNext()) {
JsonObject meta = (JsonObject) metaIterator.next();
if (meta == null) {
return;
}
JsonElement kind = meta.get(KIND);
if (kind == null || kind.getAsString().isEmpty()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_META_KIND);
continue;
}
String type = kind.getAsString();
if (type.equalsIgnoreCase(DATETIME)) {
JsonElement propertyNamesElement = meta.get(PROPERTY_NAMES);
if (propertyNamesElement == null) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_PROPERTY_NAMES);
continue;
}
JsonArray propertyNames = null;
try {
propertyNames = propertyNamesElement.getAsJsonArray();
} catch (IllegalStateException ex) {
propertyNames = null;
}
if (propertyNames == null) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.ILLEGAL_PROPERTY_NAMES);
continue;
}
JsonElement referenceElement = meta.get(PERSISTABLE_REFERENCE);
if (referenceElement == null || referenceElement.getAsString().isEmpty()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.MISSING_REFERENCE);
continue;
}
String reference = referenceElement.getAsString();
IDateTime dateTime = ReferenceConverter.parseDateTimeReference(reference);
if (dateTime == null || !dateTime.isValid()) {
hasFailure = true;
conversionMessages.add(DatesConversionServiceErrorMessages.INVALID_REFERENCE);
continue;
}
boolean datesConverted = false;
for (int i = 0; i < propertyNames.size(); i++) {
String name = propertyNames.get(i).getAsString();
String message = null;
try {
JsonElement valueElement = dataBlock.get(name);
if((valueElement == null) || (valueElement instanceof JsonNull)) {
hasFailure = true;
conversionMessages.add(String.format(DatesConversionServiceErrorMessages.MISSING_PROPERTY, name));
continue;
}
String value = dateTime.convertToIsoDateTime(valueElement.getAsString());
if (value == null) {
hasFailure = true;
conversionMessages.add(String.format(DatesConversionServiceErrorMessages.INVALID_REFERENCE));
continue;
}
dataBlock.remove(name);
dataBlock.addProperty(name, value);
datesConverted = true;
} catch(IllegalArgumentException ccEx) {
message = String.format(DatesConversionServiceErrorMessages.INVALID_FORMATTER, ccEx.getMessage());
} catch(DateTimeParseException ccEx) {
String exceptionMessage = ccEx.getMessage();
if (exceptionMessage.contains(PARSE_ERROR_STRING)) {
message = String.format(DatesConversionServiceErrorMessages.MISMATCH_REFERENCE, name);
} else if (exceptionMessage.contains(INVALID_VALUE_STRING)) {
String description = exceptionMessage.substring(exceptionMessage.indexOf(INVALID_VALUE_STRING));
message = String.format(DatesConversionServiceErrorMessages.INVALID_DATETIME_VALUE, ccEx.getParsedString(), name , description);
} else {
message = String.format(DatesConversionServiceErrorMessages.ERROR_PARSING_VALUE, ccEx.getParsedString());
}
} catch(DateTimeException ccEx) {
message = String.format(DatesConversionServiceErrorMessages.INVALID_TIMEZONE, ccEx.getMessage());
}
if(message != null) {
hasFailure = true;
conversionMessages.add(message);
}
}
if (datesConverted && !hasFailure) {
String basePersistableReference = dateTime.getBaseDateTime();
meta.remove(PERSISTABLE_REFERENCE);
meta.addProperty(PERSISTABLE_REFERENCE, basePersistableReference);
}
}
}
if (hasFailure) {
conversionRecord.setConvertStatus(ConvertStatus.ERROR);
}
conversionRecord.setConversionMessages(conversionMessages);
}
}
package org.opengroup.osdu.core.common.crs.dates;
public class DatesConversionServiceErrorMessages {
public static final String MISSING_DATA_BLOCK = "DateTime Conversion: DataBlock is missing or empty in this record, no conversion applied.";
public static final String MISSING_META_KIND = "DateTime conversion: Required property 'kind' in meta block is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY_NAMES = "DateTime conversion: 'propertyNames' in meta block is missing or empty, no conversion applied";
public static final String ILLEGAL_PROPERTY_NAMES = "DateTime conversion: 'propertyNames' illegal, no conversion applied.";
public static final String MISSING_REFERENCE = "DateTime conversion: Required property 'persistableReference' is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY = "DateTime conversion: Property %s missing, no conversion applied.";
public static final String INVALID_REFERENCE = "DateTime conversion: 'persistableReference' not valid, no conversion applied.";
public static final String MISMATCH_REFERENCE = "DateTime conversion: Frame of reference does not match given data for property %s, no conversion applied.";
public static final String INVALID_TIMEZONE = "DateTime conversion: %s. No conversion applied.";
public static final String INVALID_FORMATTER = "DateTime conversion: Invalid DateTime format. %s. No conversion applied.";
public static final String INVALID_DATETIME_VALUE = "DateTime conversion: \"%s\" could not be parsed for property %s. %s. No conversion applied.";
public static final String ERROR_PARSING_VALUE = "DateTime conversion: Error parsing \"%s\". No conversion applied.";
}
package org.opengroup.osdu.core.common.crs.dates;
public class DatesConversionServiceErrorMessages {
public static final String MISSING_DATA_BLOCK = "DateTime Conversion: DataBlock is missing or empty in this record, no conversion applied.";
public static final String MISSING_META_KIND = "DateTime conversion: Required property 'kind' in meta block is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY_NAMES = "DateTime conversion: 'propertyNames' in meta block is missing or empty, no conversion applied";
public static final String ILLEGAL_PROPERTY_NAMES = "DateTime conversion: 'propertyNames' illegal, no conversion applied.";
public static final String MISSING_REFERENCE = "DateTime conversion: Required property 'persistableReference' is missing or empty, no conversion applied.";
public static final String MISSING_PROPERTY = "DateTime conversion: Property %s missing, no conversion applied.";
public static final String INVALID_REFERENCE = "DateTime conversion: 'persistableReference' not valid, no conversion applied.";
public static final String MISMATCH_REFERENCE = "DateTime conversion: Frame of reference does not match given data for property %s, no conversion applied.";
public static final String INVALID_TIMEZONE = "DateTime conversion: %s. No conversion applied.";
public static final String INVALID_FORMATTER = "DateTime conversion: Invalid DateTime format. %s. No conversion applied.";
public static final String INVALID_DATETIME_VALUE = "DateTime conversion: \"%s\" could not be parsed for property %s. %s. No conversion applied.";
public static final String ERROR_PARSING_VALUE = "DateTime conversion: Error parsing \"%s\". No conversion applied.";
}
package org.opengroup.osdu.core.common.cryptographic;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class HmacData {
private String expireMillisecond;
private String hashMechanism;
private String endpointUrl;
private String nonce;
package org.opengroup.osdu.core.common.cryptographic;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class HmacData {
private String expireMillisecond;
private String hashMechanism;
private String endpointUrl;
private String nonce;
}
\ No newline at end of file
package org.opengroup.osdu.core.common.cryptographic;
public interface ISignatureService {
String getSignedSignature(String url, String secret) throws SignatureServiceException;
String getSignedSignature(String url, String secret, String expireTime, String nonce) throws SignatureServiceException;
void verifyHmacSignature(String hmac, String secret) throws SignatureServiceException;
package org.opengroup.osdu.core.common.cryptographic;
public interface ISignatureService {
String getSignedSignature(String url, String secret) throws SignatureServiceException;
String getSignedSignature(String url, String secret, String expireTime, String nonce) throws SignatureServiceException;
void verifyHmacSignature(String hmac, String secret) throws SignatureServiceException;
}
\ No newline at end of file
package org.opengroup.osdu.core.common.cryptographic;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
@Component
@RequestScope
public class SignatureService implements ISignatureService {
private static final String HMAC_SHA_256 = "HmacSHA256";
private static final String DATA_FORMAT = "{\"expireMillisecond\": \"%s\",\"hashMechanism\": \"hmacSHA256\",\"endpointUrl\": \"%s\",\"nonce\": \"%s\"}";
private static final String NOTIFICATION_SERVICE = "de-notification-service";
private static final long EXPIRE_DURATION = 30000L;
private static final String INVALID_SIGNATURE = "Invalid signature";
private static final String ERROR_GENERATING_SIGNATURE = "Error generating the signature";
private static final String SIGNATURE_EXPIRED = "Signature is expired";
private static final String MISSING_HMAC_SIGNATURE = "HMAC signature should not be null or empty";
private static final String MISSING_SECRET_VALUE = "Secret should not be null or empty";
private static final String MISSING_ATTRIBUTES_IN_SIGNATURE = "Missing url or nonce or expire time in the signature";
@Override
public String getSignedSignature(String url, String secret) throws SignatureServiceException {
if (Strings.isNullOrEmpty(url) || Strings.isNullOrEmpty(secret)) {
throw new SignatureServiceException(ERROR_GENERATING_SIGNATURE);
}
final long currentTime = System.currentTimeMillis();
final String expireTime = String.valueOf(currentTime + EXPIRE_DURATION);
final String timeStamp = String.valueOf(currentTime);