|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.test.http; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.StreamSupport; |
| 24 | + |
| 25 | +import org.springframework.core.ResolvableType; |
| 26 | +import org.springframework.http.HttpInputMessage; |
| 27 | +import org.springframework.http.MediaType; |
| 28 | +import org.springframework.http.converter.GenericHttpMessageConverter; |
| 29 | +import org.springframework.http.converter.HttpMessageConverter; |
| 30 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 31 | +import org.springframework.http.converter.SmartHttpMessageConverter; |
| 32 | +import org.springframework.mock.http.MockHttpInputMessage; |
| 33 | +import org.springframework.mock.http.MockHttpOutputMessage; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.function.SingletonSupplier; |
| 36 | + |
| 37 | +/** |
| 38 | + * Convert HTTP message content for testing purposes. |
| 39 | + * |
| 40 | + * @author Stephane Nicoll |
| 41 | + * @since 6.2 |
| 42 | + */ |
| 43 | +public class HttpMessageContentConverter { |
| 44 | + |
| 45 | + private static final MediaType JSON = MediaType.APPLICATION_JSON; |
| 46 | + |
| 47 | + private final List<HttpMessageConverter<?>> messageConverters; |
| 48 | + |
| 49 | + HttpMessageContentConverter(Iterable<HttpMessageConverter<?>> messageConverters) { |
| 50 | + this.messageConverters = StreamSupport.stream(messageConverters.spliterator(), false).toList(); |
| 51 | + Assert.notEmpty(this.messageConverters, "At least one message converter needs to be specified"); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Create an instance with an iterable of the candidates to use. |
| 57 | + * @param candidates the candidates |
| 58 | + */ |
| 59 | + public static HttpMessageContentConverter of(Iterable<HttpMessageConverter<?>> candidates) { |
| 60 | + return new HttpMessageContentConverter(candidates); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Create an instance with a vararg of the candidates to use. |
| 65 | + * @param candidates the candidates |
| 66 | + */ |
| 67 | + public static HttpMessageContentConverter of(HttpMessageConverter<?>... candidates) { |
| 68 | + return new HttpMessageContentConverter(Arrays.asList(candidates)); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Convert the given {@link HttpInputMessage} whose content must match the |
| 74 | + * given {@link MediaType} to the requested {@code targetType}. |
| 75 | + * @param message an input message |
| 76 | + * @param mediaType the media type of the input |
| 77 | + * @param targetType the target type |
| 78 | + * @param <T> the converted object type |
| 79 | + * @return a value of the given {@code targetType} |
| 80 | + */ |
| 81 | + @SuppressWarnings("unchecked") |
| 82 | + public <T> T convert(HttpInputMessage message, MediaType mediaType, ResolvableType targetType) |
| 83 | + throws IOException, HttpMessageNotReadableException { |
| 84 | + Class<?> contextClass = targetType.getRawClass(); |
| 85 | + SingletonSupplier<Type> javaType = SingletonSupplier.of(targetType::getType); |
| 86 | + for (HttpMessageConverter<?> messageConverter : this.messageConverters) { |
| 87 | + if (messageConverter instanceof GenericHttpMessageConverter<?> genericMessageConverter) { |
| 88 | + Type type = javaType.obtain(); |
| 89 | + if (genericMessageConverter.canRead(type, contextClass, mediaType)) { |
| 90 | + return (T) genericMessageConverter.read(type, contextClass, message); |
| 91 | + } |
| 92 | + } |
| 93 | + else if (messageConverter instanceof SmartHttpMessageConverter<?> smartMessageConverter) { |
| 94 | + if (smartMessageConverter.canRead(targetType, mediaType)) { |
| 95 | + return (T) smartMessageConverter.read(targetType, message, null); |
| 96 | + } |
| 97 | + } |
| 98 | + else { |
| 99 | + Class<?> targetClass = (contextClass != null ? contextClass : Object.class); |
| 100 | + if (messageConverter.canRead(targetClass, mediaType)) { |
| 101 | + HttpMessageConverter<T> simpleMessageConverter = (HttpMessageConverter<T>) messageConverter; |
| 102 | + Class<? extends T> clazz = (Class<? extends T>) targetClass; |
| 103 | + return simpleMessageConverter.read(clazz, message); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + throw new IllegalStateException("No converter found to read [%s] to [%s]".formatted(mediaType, targetType)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Convert the given raw value to the given {@code targetType} by writing |
| 112 | + * it first to JSON and reading it back. |
| 113 | + * @param value the value to convert |
| 114 | + * @param targetType the target type |
| 115 | + * @param <T> the converted object type |
| 116 | + * @return a value of the given {@code targetType} |
| 117 | + */ |
| 118 | + public <T> T convertViaJson(Object value, ResolvableType targetType) throws IOException { |
| 119 | + MockHttpOutputMessage outputMessage = convertToJson(value, ResolvableType.forInstance(value)); |
| 120 | + return convert(fromHttpOutputMessage(outputMessage), JSON, targetType); |
| 121 | + } |
| 122 | + |
| 123 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 124 | + private MockHttpOutputMessage convertToJson(Object value, ResolvableType valueType) throws IOException { |
| 125 | + MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
| 126 | + Class<?> valueClass = value.getClass(); |
| 127 | + SingletonSupplier<Type> javaType = SingletonSupplier.of(valueType::getType); |
| 128 | + for (HttpMessageConverter<?> messageConverter : this.messageConverters) { |
| 129 | + if (messageConverter instanceof GenericHttpMessageConverter genericMessageConverter) { |
| 130 | + Type type = javaType.obtain(); |
| 131 | + if (genericMessageConverter.canWrite(type, valueClass, JSON)) { |
| 132 | + genericMessageConverter.write(value, type, JSON, outputMessage); |
| 133 | + return outputMessage; |
| 134 | + } |
| 135 | + } |
| 136 | + else if (messageConverter instanceof SmartHttpMessageConverter smartMessageConverter) { |
| 137 | + if (smartMessageConverter.canWrite(valueType, valueClass, JSON)) { |
| 138 | + smartMessageConverter.write(value, valueType, JSON, outputMessage, null); |
| 139 | + return outputMessage; |
| 140 | + } |
| 141 | + } |
| 142 | + else if (messageConverter.canWrite(valueClass, JSON)) { |
| 143 | + ((HttpMessageConverter<Object>) messageConverter).write(value, JSON, outputMessage); |
| 144 | + return outputMessage; |
| 145 | + } |
| 146 | + } |
| 147 | + throw new IllegalStateException("No converter found to convert [%s] to JSON".formatted(valueType)); |
| 148 | + } |
| 149 | + |
| 150 | + private static HttpInputMessage fromHttpOutputMessage(MockHttpOutputMessage message) { |
| 151 | + MockHttpInputMessage inputMessage = new MockHttpInputMessage(message.getBodyAsBytes()); |
| 152 | + inputMessage.getHeaders().addAll(message.getHeaders()); |
| 153 | + return inputMessage; |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments