1 /*
2  * Copyright 2013-2019 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.cloud.aws.messaging.endpoint;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.charset.Charset;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27
28 import org.springframework.cloud.aws.messaging.config.annotation.NotificationMessage;
29 import org.springframework.core.MethodParameter;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpInputMessage;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.converter.HttpMessageConverter;
34 import org.springframework.http.converter.HttpMessageNotReadableException;
35 import org.springframework.http.converter.StringHttpMessageConverter;
36 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
37 import org.springframework.util.StringUtils;
38 import org.springframework.web.util.WebUtils;
39
40 /**
41  * @author Agim Emruli
42  */

43 public class NotificationMessageHandlerMethodArgumentResolver
44         extends AbstractNotificationMessageHandlerMethodArgumentResolver {
45
46     private final List<HttpMessageConverter<?>> messageConverter;
47
48     public NotificationMessageHandlerMethodArgumentResolver() {
49         this(Arrays.asList(new MappingJackson2HttpMessageConverter(),
50                 new StringHttpMessageConverter()));
51     }
52
53     public NotificationMessageHandlerMethodArgumentResolver(
54             List<HttpMessageConverter<?>> messageConverter) {
55         this.messageConverter = messageConverter;
56     }
57
58     private static MediaType getMediaType(JsonNode content) {
59         JsonNode contentTypeNode = content.findPath("MessageAttributes")
60                 .findPath("contentType");
61         if (contentTypeNode.isObject()) {
62             String contentType = contentTypeNode.findPath("Value").asText();
63             if (StringUtils.hasText(contentType)) {
64                 return MediaType.parseMediaType(contentType);
65             }
66         }
67
68         return MediaType.TEXT_PLAIN;
69     }
70
71     @Override
72     public boolean supportsParameter(MethodParameter parameter) {
73         return (parameter.hasParameterAnnotation(NotificationMessage.class));
74     }
75
76     @Override
77     @SuppressWarnings({ "unchecked""rawtypes" })
78     protected Object doResolveArgumentFromNotificationMessage(JsonNode content,
79             HttpInputMessage request, Class<?> parameterType) {
80         if (!"Notification".equals(content.get("Type").asText())) {
81             throw new IllegalArgumentException(
82                     "@NotificationMessage annotated parameters are only allowed for method that receive a notification message.");
83         }
84
85         MediaType mediaType = getMediaType(content);
86         String messageContent = content.findPath("Message").asText();
87
88         for (HttpMessageConverter<?> converter : this.messageConverter) {
89             if (converter.canRead(parameterType, mediaType)) {
90                 try {
91                     return converter.read((Class) parameterType,
92                             new ByteArrayHttpInputMessage(messageContent, mediaType,
93                                     request));
94                 }
95                 catch (Exception e) {
96                     throw new HttpMessageNotReadableException(
97                             "Error converting notification message with payload:"
98                                     + messageContent,
99                             e);
100                 }
101             }
102         }
103
104         throw new HttpMessageNotReadableException(
105                 "Error converting notification message with payload:" + messageContent);
106     }
107
108     private static final class ByteArrayHttpInputMessage implements HttpInputMessage {
109
110         private final String content;
111
112         private final MediaType mediaType;
113
114         private final HttpInputMessage request;
115
116         private ByteArrayHttpInputMessage(String content, MediaType mediaType,
117                 HttpInputMessage request) {
118             this.content = content;
119             this.mediaType = mediaType;
120             this.request = request;
121         }
122
123         @Override
124         public InputStream getBody() throws IOException {
125             return new ByteArrayInputStream(this.content.getBytes(getCharset()));
126         }
127
128         private Charset getCharset() {
129             return this.mediaType.getCharset() != null ? this.mediaType.getCharset()
130                     : Charset.forName(WebUtils.DEFAULT_CHARACTER_ENCODING);
131         }
132
133         @Override
134         public HttpHeaders getHeaders() {
135             return this.request.getHeaders();
136         }
137
138     }
139
140 }
141