Skip to content

Commit

Permalink
sub attrib timestamps (#191)
Browse files Browse the repository at this point in the history
* sub attrib timestamps

* fixed modifiedAt generation for multivalues
  • Loading branch information
ScorpioBroker committed Feb 3, 2021
1 parent a0b7ce7 commit 20360e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,41 @@ private HttpUtils(ContextResolverBasic contextResolver) {
*/
public static HttpUtils getInstance(ContextResolverBasic contextResolver) {
if(contextResolver == null) {
getSystemProxy(NULL_INSTANCE);
return NULL_INSTANCE;
}
if (SINGLETON == null) {
SINGLETON = new HttpUtils(contextResolver);
getSystemProxy(SINGLETON);
}
return SINGLETON;
}
private static void getSystemProxy(HttpUtils instance) {
String httpProxy = null;
String httpsProxy = null;

for(Entry<String, String> entry: System.getenv().entrySet()) {
if(entry.getKey().equalsIgnoreCase("http_proxy")) {
httpProxy = entry.getValue();
}
if(entry.getKey().equalsIgnoreCase("https_proxy")) {
httpsProxy = entry.getValue();
}
}
if(httpsProxy != null) {
try {
setHttpProxy(new URL(httpsProxy), instance);
} catch (MalformedURLException e) {
LOG.error("Your configured https_proxy setting is not valid.", e);
}
}else if(httpProxy != null) {
try {
setHttpProxy(new URL(httpProxy), instance);
} catch (MalformedURLException e) {
LOG.error("Your configured http_proxy setting is not valid.", e);
}
}
}
public static String denormalize(String attrId) {
String result = attrId.replace(":/", "://");
if(result.endsWith("/")) {
Expand Down Expand Up @@ -195,15 +223,15 @@ public String expandPayload(HttpServletRequest request, String payload, int endP
*
* @param httpProxy a URL with the HTTP proxy
*/
public static void setHttpProxy(URL httpProxy) {
public static void setHttpProxy(URL httpProxy, HttpUtils instance) {
if (httpProxy != null) {
int port = httpProxy.getPort();
if (port == -1) {
port = DEFAULT_PROXY_PORT;
}
SINGLETON.httpProxy = new HttpHost(httpProxy.getHost(), port, httpProxy.getProtocol());
instance.httpProxy = new HttpHost(httpProxy.getHost(), port, httpProxy.getProtocol());
} else {
SINGLETON.httpProxy = null;
instance.httpProxy = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,15 @@ private void setTemporalProperties(JsonNode jsonNode, String createdAt, String m
while (iter.hasNext()) {
Map.Entry<String, JsonNode> entry = iter.next();
if (entry.getValue().isArray() && entry.getValue().has(0) && entry.getValue().get(0).isObject()) {
ObjectNode attrObj = (ObjectNode) entry.getValue().get(0);
// add createdAt/modifiedAt only to properties, geoproperties and relationships
if (attrObj.has(NGSIConstants.JSON_LD_TYPE) && attrObj.get(NGSIConstants.JSON_LD_TYPE).isArray()
&& attrObj.get(NGSIConstants.JSON_LD_TYPE).has(0)
&& attrObj.get(NGSIConstants.JSON_LD_TYPE).get(0).asText().matches(regexNgsildAttributeTypes)) {
setTemporalProperties(attrObj, createdAt, modifiedAt, rootOnly);
Iterator<JsonNode> valueIterator = ((ArrayNode) entry.getValue()).iterator();
while (valueIterator.hasNext()) {
ObjectNode attrObj = (ObjectNode) valueIterator.next();
// add createdAt/modifiedAt only to properties, geoproperties and relationships
if (attrObj.has(NGSIConstants.JSON_LD_TYPE) && attrObj.get(NGSIConstants.JSON_LD_TYPE).isArray()
&& attrObj.get(NGSIConstants.JSON_LD_TYPE).has(0) && attrObj.get(NGSIConstants.JSON_LD_TYPE)
.get(0).asText().matches(regexNgsildAttributeTypes)) {
setTemporalProperties(attrObj, createdAt, modifiedAt, rootOnly);
}
}
}
}
Expand Down Expand Up @@ -796,7 +799,7 @@ public UpdateResult updateFields(String originalJsonObject, JsonNode jsonToUpdat
createdAt = ((ObjectNode) ((ObjectNode) originalNode).get(NGSIConstants.NGSI_LD_CREATED_AT)
.get(0)).get(NGSIConstants.JSON_LD_VALUE).asText();
}
setTemporalProperties(attrNode, createdAt, now, true);
setTemporalProperties(attrNode, createdAt, now, false);

// TODO check if this should ever happen. 5.6.4.4 says BadRequest if AttrId is
// present ...
Expand Down Expand Up @@ -860,7 +863,7 @@ public AppendResult appendFields(String originalJsonObject, JsonNode jsonToAppen
// TODO: should we keep the createdAt value if attribute already exists?
// (overwrite operation) => if (objectNode.has(key)) ...
JsonNode attrNode = jsonToAppend.get(key).get(0);
setTemporalProperties(attrNode, now, now, true);
setTemporalProperties(attrNode, now, now, false);
}
objectNode.replace(key, jsonToAppend.get(key));
((ObjectNode) appendResult.getAppendedJsonFields()).set(key, jsonToAppend.get(key));
Expand Down

0 comments on commit 20360e7

Please sign in to comment.