|
23 | 23 | package org.springdoc.core.providers;
|
24 | 24 |
|
25 | 25 | import java.lang.reflect.Field;
|
26 |
| -import java.util.List; |
27 | 26 | import java.util.Map;
|
28 | 27 | import java.util.Optional;
|
29 | 28 |
|
30 | 29 | import org.apache.commons.lang3.reflect.FieldUtils;
|
31 | 30 | import org.slf4j.Logger;
|
32 | 31 | import org.slf4j.LoggerFactory;
|
33 | 32 |
|
| 33 | +import org.springframework.beans.BeansException; |
| 34 | +import org.springframework.beans.factory.InitializingBean; |
| 35 | +import org.springframework.context.ApplicationContext; |
| 36 | +import org.springframework.context.ApplicationContextAware; |
34 | 37 | import org.springframework.core.convert.TypeDescriptor;
|
35 | 38 | import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
|
36 | 39 | import org.springframework.core.convert.support.GenericConversionService;
|
37 | 40 | import org.springframework.format.support.DefaultFormattingConversionService;
|
38 | 41 | import org.springframework.format.support.FormattingConversionService;
|
39 | 42 | import org.springframework.lang.Nullable;
|
| 43 | +import org.springframework.util.ClassUtils; |
40 | 44 |
|
41 | 45 | /**
|
42 | 46 | * The type Web conversion service provider.
|
43 | 47 | * @author bnasslashen
|
44 | 48 | */
|
45 |
| -public class WebConversionServiceProvider { |
46 |
| - |
47 |
| - /** |
48 |
| - * The constant CONVERTERS. |
49 |
| - */ |
50 |
| - private static final String CONVERTERS = "converters"; |
51 |
| - |
52 |
| - /** |
53 |
| - * The constant LOGGER. |
54 |
| - */ |
55 |
| - private static final Logger LOGGER = LoggerFactory.getLogger(WebConversionServiceProvider.class); |
56 |
| - |
57 |
| - /** |
58 |
| - * The Formatting conversion service. |
59 |
| - */ |
60 |
| - private GenericConversionService formattingConversionService; |
61 |
| - |
62 |
| - /** |
63 |
| - * Instantiates a new Web conversion service provider. |
64 |
| - * |
65 |
| - * @param webConversionServiceOptional the web conversion service optional |
66 |
| - */ |
67 |
| - public WebConversionServiceProvider(Optional<List<GenericConversionService>> webConversionServiceOptional) { |
68 |
| - if (webConversionServiceOptional.isPresent()) { |
69 |
| - List<GenericConversionService> conversionServiceList = webConversionServiceOptional.get(); |
70 |
| - for (GenericConversionService genericConversionService : conversionServiceList) { |
71 |
| - if (genericConversionService instanceof FormattingConversionService) { |
72 |
| - this.formattingConversionService = genericConversionService; |
73 |
| - break; |
| 49 | +public class WebConversionServiceProvider implements InitializingBean, ApplicationContextAware { |
| 50 | + |
| 51 | + /** |
| 52 | + * The constant CONVERTERS. |
| 53 | + */ |
| 54 | + private static final String CONVERTERS = "converters"; |
| 55 | + |
| 56 | + /** |
| 57 | + * The constant LOGGER. |
| 58 | + */ |
| 59 | + private static final Logger LOGGER = LoggerFactory.getLogger(WebConversionServiceProvider.class); |
| 60 | + |
| 61 | + /** |
| 62 | + * The Formatting conversion service. |
| 63 | + */ |
| 64 | + private GenericConversionService formattingConversionService; |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * The Application context. |
| 69 | + */ |
| 70 | + private ApplicationContext applicationContext; |
| 71 | + |
| 72 | + /** |
| 73 | + * The constant SERVLET_APPLICATION_CONTEXT_CLASS. |
| 74 | + */ |
| 75 | + private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext"; |
| 76 | + |
| 77 | + /** |
| 78 | + * The constant REACTIVE_APPLICATION_CONTEXT_CLASS. |
| 79 | + */ |
| 80 | + private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext"; |
| 81 | + |
| 82 | + @Override |
| 83 | + public void afterPropertiesSet() { |
| 84 | + if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) { |
| 85 | + this.formattingConversionService = applicationContext.getBean("mvcConversionService", FormattingConversionService.class); |
| 86 | + } |
| 87 | + else if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, this.applicationContext.getClass())) { |
| 88 | + this.formattingConversionService = applicationContext.getBean("webFluxConversionService", FormattingConversionService.class); |
| 89 | + } |
| 90 | + else |
| 91 | + formattingConversionService = new DefaultFormattingConversionService(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Attempts to convert {@code source} into the target type as described by {@code targetTypeDescriptor}. |
| 96 | + * |
| 97 | + * @param source the source |
| 98 | + * @param targetTypeDescriptor the target type descriptor |
| 99 | + * @return the converted source |
| 100 | + */ |
| 101 | + @Nullable |
| 102 | + public Object convert(@Nullable Object source, TypeDescriptor targetTypeDescriptor) { |
| 103 | + return formattingConversionService.convert(source, targetTypeDescriptor); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets spring converted type. |
| 108 | + * |
| 109 | + * @param clazz the clazz |
| 110 | + * @return the spring converted type |
| 111 | + */ |
| 112 | + public Class<?> getSpringConvertedType(Class<?> clazz) { |
| 113 | + Class<?> result = clazz; |
| 114 | + Field convertersField = FieldUtils.getDeclaredField(GenericConversionService.class, CONVERTERS, true); |
| 115 | + if (convertersField != null) { |
| 116 | + Object converters; |
| 117 | + try { |
| 118 | + converters = convertersField.get(formattingConversionService); |
| 119 | + convertersField = FieldUtils.getDeclaredField(converters.getClass(), CONVERTERS, true); |
| 120 | + Map<ConvertiblePair, Object> springConverters = (Map) convertersField.get(converters); |
| 121 | + Optional<ConvertiblePair> convertiblePairOptional = springConverters.keySet().stream().filter(convertiblePair -> convertiblePair.getTargetType().equals(clazz)).findAny(); |
| 122 | + if (convertiblePairOptional.isPresent()) { |
| 123 | + ConvertiblePair convertiblePair = convertiblePairOptional.get(); |
| 124 | + result = convertiblePair.getSourceType(); |
| 125 | + } |
| 126 | + } |
| 127 | + catch (IllegalAccessException e) { |
| 128 | + LOGGER.warn(e.getMessage()); |
74 | 129 | }
|
75 | 130 | }
|
| 131 | + return result; |
76 | 132 | }
|
77 |
| - if (formattingConversionService == null) |
78 |
| - formattingConversionService = new DefaultFormattingConversionService(); |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Attempts to convert {@code source} into the target type as described by {@code targetTypeDescriptor}. |
83 |
| - * |
84 |
| - * @param source the source |
85 |
| - * @param targetTypeDescriptor the target type descriptor |
86 |
| - * @return the converted source |
87 |
| - */ |
88 |
| - @Nullable |
89 |
| - public Object convert(@Nullable Object source, TypeDescriptor targetTypeDescriptor) { |
90 |
| - return formattingConversionService.convert(source, targetTypeDescriptor); |
91 |
| - } |
92 |
| - |
93 |
| - /** |
94 |
| - * Gets spring converted type. |
95 |
| - * |
96 |
| - * @param clazz the clazz |
97 |
| - * @return the spring converted type |
98 |
| - */ |
99 |
| - public Class<?> getSpringConvertedType(Class<?> clazz) { |
100 |
| - Class<?> result = clazz; |
101 |
| - Field convertersField = FieldUtils.getDeclaredField(GenericConversionService.class, CONVERTERS, true); |
102 |
| - if(convertersField!=null) { |
103 |
| - Object converters; |
| 133 | + |
| 134 | + /** |
| 135 | + * Is assignable boolean. |
| 136 | + * |
| 137 | + * @param target the target |
| 138 | + * @param type the type |
| 139 | + * @return the boolean |
| 140 | + */ |
| 141 | + private boolean isAssignable(String target, Class<?> type) { |
104 | 142 | try {
|
105 |
| - converters = convertersField.get(formattingConversionService); |
106 |
| - convertersField = FieldUtils.getDeclaredField(converters.getClass(), CONVERTERS, true); |
107 |
| - Map<ConvertiblePair, Object> springConverters = (Map) convertersField.get(converters); |
108 |
| - Optional<ConvertiblePair> convertiblePairOptional = springConverters.keySet().stream().filter(convertiblePair -> convertiblePair.getTargetType().equals(clazz)).findAny(); |
109 |
| - if (convertiblePairOptional.isPresent()) { |
110 |
| - ConvertiblePair convertiblePair = convertiblePairOptional.get(); |
111 |
| - result = convertiblePair.getSourceType(); |
112 |
| - } |
| 143 | + return ClassUtils.resolveClassName(target, null).isAssignableFrom(type); |
113 | 144 | }
|
114 |
| - catch (IllegalAccessException e) { |
115 |
| - LOGGER.warn(e.getMessage()); |
| 145 | + catch (Throwable ex) { |
| 146 | + return false; |
116 | 147 | }
|
117 | 148 | }
|
118 |
| - return result; |
119 |
| - } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 152 | + this.applicationContext = applicationContext; |
| 153 | + } |
| 154 | + |
120 | 155 | }
|
0 commit comments