Skip to content

Commit

Permalink
Merge branch 'release/3.21.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Jun 20, 2024
2 parents fb23a38 + d5a96f7 commit 0c51412
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 29 deletions.
2 changes: 1 addition & 1 deletion solarnet/datum-input/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencyManagement {
}

description = 'SolarNet: Datum Input'
version = '1.3.0'
version = '1.3.1'

base {
archivesName = 'solarnet-datum-input'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -117,14 +119,29 @@ public DaoDatumInputEndpointBiz(SolarNodeOwnershipDao nodeOwnershipDao,
.collect(Collectors.toMap(s -> s.getId(), Function.identity()));
}

private static final MimeType JSON_TYPE = MimeType.valueOf("application/json");
private static final MimeType TEXT_TYPE = MimeType.valueOf("text/*");

private static LogEventInfo importErrorEvent(String msg, EndpointConfiguration endpoint,
TransformConfiguration xform, MimeType contentType, Map<String, String> parameters) {
TransformConfiguration xform, MimeType contentType, byte[] content,
Map<String, String> parameters) {
var eventData = new LinkedHashMap<>(8);
eventData.put(ENDPOINT_ID_DATA_KEY, endpoint.getEndpointId());
eventData.put(TRANSFORM_ID_DATA_KEY, endpoint.getTransformId());
eventData.put(TRANSFORM_SERVICE_ID_DATA_KEY, xform.getServiceIdentifier());
eventData.put(CONTENT_TYPE_DATA_KEY, contentType.toString());

if ( content != null && content.length > 0 ) {
String value = null;
if ( JSON_TYPE.isCompatibleWith(contentType) || TEXT_TYPE.isCompatibleWith(contentType) ) {
value = new String(content, contentType.getCharset() != null ? contentType.getCharset()
: StandardCharsets.UTF_8);
} else {
value = Base64.getEncoder().encodeToString(content);
}
eventData.put(CONTENT_DATA_KEY, value);
}

if ( parameters != null ) {
eventData.put(PARAMETERS_DATA_KEY, parameters);
}
Expand Down Expand Up @@ -155,12 +172,16 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
contentType = MimeType.valueOf(endpoint.getRequestContentType());
}

// load data, so can include in error event if needed
final byte[] inputData = FileCopyUtils.copyToByteArray(in);
in = new ByteArrayInputStream(inputData);

if ( !xformService.supportsInput(requireNonNullArgument(in, "in"),
requireNonNullArgument(contentType, "contentType")) ) {
String msg = "Transform service %s does not support input type %s with %s."
.formatted(xformServiceId, contentType, in.getClass().getSimpleName());
// @formatter:off
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, parameters));
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, inputData,parameters));
// @formatter:on
throw new IllegalArgumentException(msg);
}
Expand Down Expand Up @@ -191,12 +212,10 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
if ( nodeId != null && sourceId != null ) {
UserLongStringCompositePK key = new UserLongStringCompositePK(userId, nodeId,
sourceId);
byte[] inputData = FileCopyUtils.copyToByteArray(in);
byte[] previousInput = previousInputDao.getAndPut(key, inputData);
if ( previousInput == null ) {
return Collections.emptyList();
}
in = new ByteArrayInputStream(inputData);
params.put(TransformService.PARAM_PREVIOUS_INPUT,
new ByteArrayInputStream(previousInput));
}
Expand All @@ -209,7 +228,7 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
} catch ( Exception e ) {
String msg = "Error executing transform: " + e.getMessage();
// @formatter:off
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, parameters));
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, inputData,parameters));
// @formatter:on
if ( e instanceof IOException ioe ) {
throw ioe;
Expand Down Expand Up @@ -247,8 +266,8 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
nodeOwnershipDao.ownershipForNodeId(nodeId), nodeId);
if ( !userId.equals(owner.getUserId()) ) {
var ex = new AuthorizationException(Reason.ACCESS_DENIED, nodeId);
addEvent(userEventAppenderBiz, userId,
importErrorEvent(ex.getMessage(), endpoint, xform, contentType, parameters));
addEvent(userEventAppenderBiz, userId, importErrorEvent(ex.getMessage(), endpoint,
xform, contentType, inputData, parameters));
throw ex;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Constants for central datum input (DIN) user events.
*
* @author matt
* @version 1.1
* @version 1.2
*/
public interface CentralDinUserEvents {

Expand Down Expand Up @@ -62,4 +62,16 @@ public interface CentralDinUserEvents {
*/
String PARAMETERS_DATA_KEY = "parameters";

/**
* User event data key for a request content value.
*
* <p>
* If the content is textual, the content will be included as-is. Otherwise
* it will be Base64 encoded.
* </p>
*
* @since 1.2
*/
String CONTENT_DATA_KEY = "content";

}
Loading

0 comments on commit 0c51412

Please sign in to comment.