Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for files in payload of an incoming ArtifactRequestMessage #757

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ids.messaging.dispatcher.filter.PreDispatchingFilter;
import ids.messaging.dispatcher.filter.PreDispatchingFilterException;
import ids.messaging.handler.message.MessageAndClaimsHandler;
import ids.messaging.handler.message.MessageFilesAndClaimsHandler;
import ids.messaging.handler.message.MessageHandler;
import ids.messaging.handler.message.MessageHandlerException;
import ids.messaging.handler.message.MessagePayloadInputstream;
Expand All @@ -44,6 +45,9 @@
import io.jsonwebtoken.Jws;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

/**
* The MessageDispatcher takes all incoming Messages, applies all defined PreDispatchingFilters
Expand Down Expand Up @@ -123,6 +127,7 @@ public void registerPreDispatchingAction(
*
* @param header Header of the incoming Message (RequestMessage implementation).
* @param payload Payload of the incoming Message.
* @param files Files payload of the incoming Message
* @param <R> A subtype of RequestMessage.
* @return The {@link MessageResponse} that is returned by the specified {@link MessageHandler}
* for the type of the incoming Message.
Expand All @@ -131,7 +136,7 @@ public void registerPreDispatchingAction(
*/
@SuppressWarnings("unchecked")
public <R extends Message> MessageResponse process(final R header,
final InputStream payload)
final InputStream payload, MultiValueMap<String, MultipartFile> files)
throws PreDispatchingFilterException {
final var connectorId = configContainer.getConnector().getId();
final var modelVersion =
Expand Down Expand Up @@ -213,11 +218,17 @@ public <R extends Message> MessageResponse process(final R header,

// Checks if revolvedHandler is not null
if (resolvedHandler.isPresent()) {
//if an handler exists, let the handle handle the
//if a handler exists, let the handle handle the
// message and return its response
try {
final var handler = (MessageHandler<R>) resolvedHandler.get();
if (handler instanceof MessageAndClaimsHandler) {
if (handler.getClass().getSimpleName().equals("ArtifactRequestHandler") && files.size() != 0){
return((MessageFilesAndClaimsHandler) handler)
.handleMessage(header,
new MessagePayloadInputstream(payload, objectMapper),
optionalClaimsJws, files);
}
else if (handler instanceof MessageAndClaimsHandler) {
//for MessageAndClaims handlers, also pass parsed DAT claims
return ((MessageAndClaimsHandler) handler)
.handleMessage(header,
Expand All @@ -243,7 +254,7 @@ public <R extends Message> MessageResponse process(final R header,
} else {
if (log.isDebugEnabled()) {
log.debug("No message handler exists! [code=(IMSMED0118), type=({})]",
header.getClass());
header.getClass());
}

//If no handler for the type exists, the message type isn't supported
Expand All @@ -255,6 +266,27 @@ public <R extends Message> MessageResponse process(final R header,
}
}

/**
* Apply the preDispatchingFilters to the message. If it wasn't filtered:
* find the {@link MessageHandler} for its type. Let the handler handle the Message and return
* the {@link MessageResponse}.
*
* @param header Header of the incoming Message (RequestMessage implementation).
* @param payload Payload of the incoming Message.
* @param <R> A subtype of RequestMessage.
* @return The {@link MessageResponse} that is returned by the specified {@link MessageHandler}
* for the type of the incoming Message.
* @throws PreDispatchingFilterException If an error occurs
* in a PreDispatchingFilter.
*/
@SuppressWarnings("unchecked")
public <R extends Message> MessageResponse process(final R header,
final InputStream payload)
throws PreDispatchingFilterException {
MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>();
return process(header, payload, files);
}

private <R extends Message> boolean isReferringConnector(final R header,
final Jws<Claims> claims) {
final var datClaim = claims.getBody().get("referringConnector").toString().strip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

/**
* REST controller for handling all incoming IDS multipart Messages.
Expand Down Expand Up @@ -129,6 +130,8 @@ public ResponseEntity<MultiValueMap<String, Object>> handleIDSMessage(
final var payloadPart =
request.getPart(MultipartDatapart.PAYLOAD.toString());

final var filesPart = (new StandardMultipartHttpServletRequest(request)).getMultiFileMap();

if (headerPart == null) {
if (log.isDebugEnabled()) {
log.debug("Header of incoming message were empty! [code=(IMSMED0119)]");
Expand Down Expand Up @@ -189,7 +192,7 @@ public ResponseEntity<MultiValueMap<String, Object>> handleIDSMessage(
final var response = this.messageDispatcher
.process(requestHeader, payloadPart == null
? null
: payloadPart.getInputStream());
: payloadPart.getInputStream(), filesPart);

if (response != null) {
//get Response as MultiValueMap
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Fraunhofer Institute for Software and Systems Engineering
*
* 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.
*
* Contributors:
* sovity GmbH
*
*/
package ids.messaging.handler.message;

import de.fraunhofer.iais.eis.Message;
import ids.messaging.response.MessageResponse;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

/**
* MessageHandler, also passing DAT Claims for additional checks. Supports files in payload.
*
* @param <T> Type of Message accepted by handler
*/
public interface MessageFilesAndClaimsHandler<T extends Message> extends MessageAndClaimsHandler<T> {

/**
* {@inheritDoc}
*/
@Override
default MessageResponse handleMessage(T queryHeader, MessagePayload payload, Optional<Jws<Claims>> optionalClaimsJws)
throws MessageHandlerException {
return handleMessage(queryHeader, payload, optionalClaimsJws, null);
}

MessageResponse handleMessage(T queryHeader, MessagePayload payload,
Optional<Jws<Claims>> optionalClaimsJws, MultiValueMap<String, MultipartFile> files)
throws MessageHandlerException;


}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void testGetValidMultipartReturn() throws Exception {
._senderAgent_(configurationContainer.getConnector().getId())
._modelVersion_(configurationContainer.getConnector().getOutboundModelVersion()).build();

Mockito.when(messageDispatcher.process(Mockito.any(), Mockito.any())).thenReturn(
Mockito.when(messageDispatcher.process(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(
BodyResponse.create(responseMessage, mockResponseBody));

final var header = serializer.serialize(msgHeader);
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</modules>

<properties>
<revision>7.0.1</revision>
<revision>7.0.2</revision>

<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand Down Expand Up @@ -70,7 +70,7 @@
<enabled>false</enabled>
</snapshots>
<id>eis-public-repo</id>
<url>http://maven.iais.fraunhofer.de/artifactory/eis-ids-public</url>
<url>https://maven.iais.fraunhofer.de/artifactory/eis-ids-public</url>
</repository>
<repository>
<id>sovity-public</id>
Expand Down