|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +package org.springframework.data.rest.webmvc.alps; |
| 17 | + |
| 18 | +import tools.jackson.databind.ObjectMapper; |
| 19 | +import tools.jackson.databind.SerializationFeature; |
| 20 | +import tools.jackson.databind.cfg.MapperBuilder; |
| 21 | +import tools.jackson.databind.json.JsonMapper; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +import org.jspecify.annotations.Nullable; |
| 27 | + |
| 28 | +import org.springframework.core.MethodParameter; |
| 29 | +import org.springframework.core.ResolvableType; |
| 30 | +import org.springframework.core.convert.converter.Converter; |
| 31 | +import org.springframework.data.rest.webmvc.RootResourceInformation; |
| 32 | +import org.springframework.hateoas.MediaTypes; |
| 33 | +import org.springframework.hateoas.mediatype.alps.Alps; |
| 34 | +import org.springframework.http.MediaType; |
| 35 | +import org.springframework.http.converter.HttpMessageConverter; |
| 36 | +import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter; |
| 37 | +import org.springframework.http.server.ServerHttpRequest; |
| 38 | +import org.springframework.http.server.ServerHttpResponse; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
| 41 | + |
| 42 | +import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 43 | + |
| 44 | +/** |
| 45 | + * {@link HttpMessageConverter} to render {@link Alps} and {@link RootResourceInformation} instances as |
| 46 | + * {@code application/alps+json}. |
| 47 | + * |
| 48 | + * @author Mark Paluch |
| 49 | + * @author Oliver Gierke |
| 50 | + * @author Greg Turnquist |
| 51 | + * @since 5.0 |
| 52 | + */ |
| 53 | +public class AlpsJackson3JsonHttpMessageConverter extends JacksonJsonHttpMessageConverter |
| 54 | + implements ResponseBodyAdvice<Object> { |
| 55 | + |
| 56 | + private final RootResourceInformationToAlpsDescriptorConverter converter; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link AlpsJackson3JsonHttpMessageConverter} for the given {@link Converter}. |
| 60 | + * |
| 61 | + * @param converter must not be {@literal null}. |
| 62 | + */ |
| 63 | + public AlpsJackson3JsonHttpMessageConverter(RootResourceInformationToAlpsDescriptorConverter converter) { |
| 64 | + this(JsonMapper.builder(), converter); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a new {@link AlpsJackson3JsonHttpMessageConverter} for the given {@link Converter}. |
| 69 | + * |
| 70 | + * @param converter must not be {@literal null}. |
| 71 | + */ |
| 72 | + public AlpsJackson3JsonHttpMessageConverter(ObjectMapper objectMapper, |
| 73 | + RootResourceInformationToAlpsDescriptorConverter converter) { |
| 74 | + this(objectMapper.rebuild(), converter); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new {@link AlpsJackson3JsonHttpMessageConverter} for the given {@link Converter}. |
| 79 | + * |
| 80 | + * @param converter must not be {@literal null}. |
| 81 | + */ |
| 82 | + AlpsJackson3JsonHttpMessageConverter( |
| 83 | + MapperBuilder<? extends ObjectMapper, ? extends MapperBuilder<?, ?>> objectMapper, |
| 84 | + RootResourceInformationToAlpsDescriptorConverter converter) { |
| 85 | + |
| 86 | + super(objectMapper.changeDefaultPropertyInclusion(it -> it.withValueInclusion(Include.NON_EMPTY)) |
| 87 | + .enable(SerializationFeature.INDENT_OUTPUT).build()); |
| 88 | + Assert.notNull(converter, "Converter must not be null"); |
| 89 | + |
| 90 | + this.converter = converter; |
| 91 | + |
| 92 | + setSupportedMediaTypes(Arrays.asList(MediaTypes.ALPS_JSON, MediaType.APPLICATION_JSON, MediaType.ALL)); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean canWrite(Class<?> clazz, MediaType mediaType) { |
| 97 | + return (clazz.isAssignableFrom(Alps.class) || clazz.isAssignableFrom(RootResourceInformation.class)) |
| 98 | + && super.canWrite(clazz, mediaType); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean canRead(ResolvableType type, @Nullable MediaType mediaType) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, |
| 108 | + Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, |
| 109 | + ServerHttpResponse response) { |
| 110 | + |
| 111 | + return body instanceof RootResourceInformation |
| 112 | + ? Collections.singletonMap("alps", converter.convert((RootResourceInformation) body)) |
| 113 | + : body; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { |
| 118 | + return converterType.equals(AlpsJackson3JsonHttpMessageConverter.class); |
| 119 | + } |
| 120 | +} |
0 commit comments