com.sap.cloud.sdk.cloudplatform.connectivity.EnvironmentVariableDestinationParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectivity Show documentation
Show all versions of connectivity Show documentation
Cloud platform abstraction for general-purpose connectivity.
The newest version!
/*
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
*/
package com.sap.cloud.sdk.cloudplatform.connectivity;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import lombok.AccessLevel;
import lombok.Getter;
class EnvironmentVariableDestinationParser extends AbstractDestinationParser
{
private static final String PROPERTIES = "properties";
private static final String PROPERTY_KEY = "key";
private static final String PROPERTY_VALUE = "value";
@Getter( AccessLevel.PACKAGE )
private final JsonObject destinationAsJson;
EnvironmentVariableDestinationParser( final JsonObject destinationAsJson )
{
this.destinationAsJson = destinationAsJson;
}
private Map getStandardProperties()
{
final Map properties = new HashMap<>();
for( final Map.Entry entry : destinationAsJson.entrySet() ) {
final String key = entry.getKey();
final JsonElement jsonElement = destinationAsJson.get(key);
if( jsonElement == null || !jsonElement.isJsonPrimitive() ) {
continue;
}
// for compatibility, add property keys with their standard casing
if( PROPERTY_URI.equalsIgnoreCase(key) || "uri".equalsIgnoreCase(key) ) {
properties.put(PROPERTY_URI, jsonElement.getAsString());
} else if( PROPERTY_USERNAME.equalsIgnoreCase(key) || "username".equalsIgnoreCase(key) ) {
properties.put(PROPERTY_USERNAME, jsonElement.getAsString());
} else {
for( final String propertyKey : standardPropertyKeys ) {
if( propertyKey.equalsIgnoreCase(key) ) {
properties.put(propertyKey, jsonElement.getAsString());
}
}
}
}
return properties;
}
@Override
Map getProperties()
throws DestinationAccessException
{
final Map properties = getStandardProperties();
final JsonElement jsonElement = destinationAsJson.get(PROPERTIES);
if( jsonElement != null ) {
if( !jsonElement.isJsonArray() ) {
throw new DestinationAccessException("No valid JSON array '" + PROPERTIES + "' defined.");
}
final JsonArray jsonArray = jsonElement.getAsJsonArray();
for( final JsonElement nestedElement : jsonArray ) {
if( nestedElement == null || !nestedElement.isJsonObject() ) {
throw new DestinationAccessException("Destination property is not a JSON object.");
}
final JsonObject jsonObject = nestedElement.getAsJsonObject();
final JsonElement key = jsonObject.get(PROPERTY_KEY);
final JsonElement value = jsonObject.get(PROPERTY_VALUE);
if( key != null && key.isJsonPrimitive() && value != null && value.isJsonPrimitive() ) {
properties.put(key.getAsString(), value.getAsString());
} else {
throw new DestinationAccessException(
"No valid JSON primitives '"
+ PROPERTY_KEY
+ "' and '"
+ PROPERTY_VALUE
+ "' defined for destination property.");
}
}
}
return properties;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy