|
| 1 | +/* |
| 2 | + * Copyright 2013 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 | + * http://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.hateoas.config; |
| 17 | + |
| 18 | +import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.*; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.beans.BeansException; |
| 23 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinitionHolder; |
| 25 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 26 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 27 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 28 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
| 31 | +import org.springframework.core.type.AnnotationMetadata; |
| 32 | +import org.springframework.hateoas.EntityLinks; |
| 33 | +import org.springframework.hateoas.LinkDiscoverer; |
| 34 | +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; |
| 35 | +import org.springframework.hateoas.core.DefaultLinkDiscoverer; |
| 36 | +import org.springframework.hateoas.hal.HalLinkDiscoverer; |
| 37 | +import org.springframework.hateoas.hal.Jackson1HalModule; |
| 38 | +import org.springframework.hateoas.hal.Jackson2HalModule; |
| 39 | +import org.springframework.util.ClassUtils; |
| 40 | + |
| 41 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 42 | + |
| 43 | +/** |
| 44 | + * {@link ImportBeanDefinitionRegistrar} implementation to activate hypermedia support based on the configured |
| 45 | + * hypermedia type. Activates {@link EntityLinks} support as well (essentially as if {@link EnableEntityLinks} was |
| 46 | + * activated as well). |
| 47 | + * |
| 48 | + * @author Oliver Gierke |
| 49 | + */ |
| 50 | +class HypermediaSupportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { |
| 51 | + |
| 52 | + private static final String LINK_DISCOVERER_BEAN_NAME = "_linkDiscoverer"; |
| 53 | + |
| 54 | + private static final boolean JACKSON1_PRESENT = ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", null); |
| 55 | + private static final boolean JACKSON2_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", |
| 56 | + null); |
| 57 | + |
| 58 | + /* |
| 59 | + * (non-Javadoc) |
| 60 | + * @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry) |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { |
| 64 | + |
| 65 | + new LinkBuilderBeanDefinitionRegistrar().registerBeanDefinitions(importingClassMetadata, registry); |
| 66 | + |
| 67 | + Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(EnableHypermediaSupport.class |
| 68 | + .getName()); |
| 69 | + HypermediaType type = (HypermediaType) attributes.get("type"); |
| 70 | + |
| 71 | + registerBeanDefinition(new BeanDefinitionHolder(getLinkDiscovererBeanDefinition(type), LINK_DISCOVERER_BEAN_NAME), |
| 72 | + registry); |
| 73 | + |
| 74 | + if (type == HypermediaType.HAL) { |
| 75 | + |
| 76 | + if (JACKSON2_PRESENT) { |
| 77 | + registerWithGeneratedName(new RootBeanDefinition(Jackson2ModuleRegisteringBeanPostProcessor.class), registry); |
| 78 | + } |
| 79 | + |
| 80 | + if (JACKSON1_PRESENT) { |
| 81 | + registerWithGeneratedName(new RootBeanDefinition(Jackson1ModuleRegisteringBeanPostProcessor.class), registry); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a {@link LinkDiscoverer} {@link BeanDefinition} suitable for the given {@link HypermediaType}. |
| 88 | + * |
| 89 | + * @param type |
| 90 | + * @return |
| 91 | + */ |
| 92 | + private AbstractBeanDefinition getLinkDiscovererBeanDefinition(HypermediaType type) { |
| 93 | + |
| 94 | + AbstractBeanDefinition definition; |
| 95 | + |
| 96 | + switch (type) { |
| 97 | + case HAL: |
| 98 | + definition = new RootBeanDefinition(HalLinkDiscoverer.class); |
| 99 | + break; |
| 100 | + case DEFAULT: |
| 101 | + default: |
| 102 | + definition = new RootBeanDefinition(DefaultLinkDiscoverer.class); |
| 103 | + } |
| 104 | + |
| 105 | + definition.setSource(this); |
| 106 | + return definition; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@link BeanPostProcessor} to register {@link Jackson2HalModule} with {@link ObjectMapper} instances registered in |
| 111 | + * the {@link ApplicationContext}. |
| 112 | + * |
| 113 | + * @author Oliver Gierke |
| 114 | + */ |
| 115 | + private static class Jackson2ModuleRegisteringBeanPostProcessor implements BeanPostProcessor { |
| 116 | + |
| 117 | + /* |
| 118 | + * (non-Javadoc) |
| 119 | + * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String) |
| 120 | + */ |
| 121 | + @Override |
| 122 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 123 | + return bean; |
| 124 | + } |
| 125 | + |
| 126 | + /* |
| 127 | + * (non-Javadoc) |
| 128 | + * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String) |
| 129 | + */ |
| 130 | + @Override |
| 131 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 132 | + |
| 133 | + if (bean instanceof ObjectMapper) { |
| 134 | + ((ObjectMapper) bean).registerModule(new Jackson2HalModule()); |
| 135 | + } |
| 136 | + |
| 137 | + return bean; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * {@link BeanPostProcessor} to register the {@link Jackson1HalModule} with |
| 143 | + * {@link org.codehaus.jackson.map.ObjectMapper} beans registered in the {@link ApplicationContext}. |
| 144 | + * |
| 145 | + * @author Oliver Gierke |
| 146 | + */ |
| 147 | + private static class Jackson1ModuleRegisteringBeanPostProcessor implements BeanPostProcessor { |
| 148 | + |
| 149 | + /* |
| 150 | + * (non-Javadoc) |
| 151 | + * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String) |
| 152 | + */ |
| 153 | + @Override |
| 154 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 155 | + return bean; |
| 156 | + } |
| 157 | + |
| 158 | + /* |
| 159 | + * (non-Javadoc) |
| 160 | + * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String) |
| 161 | + */ |
| 162 | + @Override |
| 163 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 164 | + |
| 165 | + if (bean instanceof org.codehaus.jackson.map.ObjectMapper) { |
| 166 | + ((org.codehaus.jackson.map.ObjectMapper) bean).registerModule(new Jackson1HalModule()); |
| 167 | + } |
| 168 | + |
| 169 | + return bean; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments