-
Notifications
You must be signed in to change notification settings - Fork 12
/
PayloadArgumentResolver.java
42 lines (37 loc) · 1.63 KB
/
PayloadArgumentResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.jashmore.sqs.argument.payload;
import com.jashmore.sqs.QueueProperties;
import com.jashmore.sqs.argument.ArgumentResolutionException;
import com.jashmore.sqs.argument.ArgumentResolver;
import com.jashmore.sqs.argument.MethodParameter;
import com.jashmore.sqs.argument.payload.mapper.PayloadMapper;
import com.jashmore.sqs.argument.payload.mapper.PayloadMappingException;
import com.jashmore.sqs.util.annotation.AnnotationUtils;
import lombok.AllArgsConstructor;
import software.amazon.awssdk.services.sqs.model.Message;
/**
* Argument resolver that is able to create an argument from the body of a message.
*
* <p>This should allow for the serialisation of the body to a specific Java Bean via an implementation of the {@link PayloadMapper}.
*
* @see Message#body() for the payload that will be consumed
*/
@AllArgsConstructor
public class PayloadArgumentResolver implements ArgumentResolver<Object> {
private final PayloadMapper payloadMapper;
@Override
public boolean canResolveParameter(final MethodParameter methodParameter) {
return AnnotationUtils.findParameterAnnotation(methodParameter, Payload.class).isPresent();
}
@Override
public Object resolveArgumentForParameter(
final QueueProperties queueProperties,
final MethodParameter methodParameter,
final Message message
) throws ArgumentResolutionException {
try {
return payloadMapper.map(message, methodParameter.getParameter().getType());
} catch (final PayloadMappingException payloadMappingException) {
throw new ArgumentResolutionException(payloadMappingException);
}
}
}