|
| 1 | +/* |
| 2 | + * Copyright © 2017 Ivar Grimstad ([email protected]) |
| 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.mvcspec.ozark.ext.thymeleaf; |
| 17 | + |
| 18 | +import org.thymeleaf.context.IWebContext; |
| 19 | +import org.thymeleaf.context.LazyContextVariable; |
| 20 | + |
| 21 | +import javax.enterprise.context.spi.CreationalContext; |
| 22 | +import javax.enterprise.inject.Any; |
| 23 | +import javax.enterprise.inject.spi.Bean; |
| 24 | +import javax.enterprise.inject.spi.BeanManager; |
| 25 | +import javax.enterprise.util.AnnotationLiteral; |
| 26 | +import javax.servlet.ServletContext; |
| 27 | +import javax.servlet.http.HttpServletRequest; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | +import javax.servlet.http.HttpSession; |
| 30 | +import java.util.*; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +/** |
| 34 | + * Thymeleaf context for resolving variables from the model or cdi context. In addition to the functions provided by |
| 35 | + * {@link org.thymeleaf.context.WebContext} this allows to resolve beans annotated with {@linkplain javax.inject.Named} |
| 36 | + * from the cdi context. |
| 37 | + * <p> |
| 38 | + * Beans will be resolved in this order: |
| 39 | + * <ol> |
| 40 | + * <li>special variables (like "request")</li> |
| 41 | + * <li>variables from the model</li> |
| 42 | + * <li>named beans from the cdi context</li> |
| 43 | + * </ol> |
| 44 | + * </p> |
| 45 | + * |
| 46 | + * @author Gregor Tudan |
| 47 | + */ |
| 48 | +class CDIWebContext implements IWebContext { |
| 49 | + |
| 50 | + private final HttpServletRequest request; |
| 51 | + private final HttpServletResponse response; |
| 52 | + private final ServletContext context; |
| 53 | + private final Locale locale; |
| 54 | + |
| 55 | + private final BeanManager beanManager; |
| 56 | + private final Set<String> cdiNamedBeans; |
| 57 | + |
| 58 | + /** keeps track of creational contexts, so beans can get disposed by calling {@link #close()} */ |
| 59 | + private final Queue<CreationalContext<?>> contexts = new LinkedList<>(); |
| 60 | + |
| 61 | + private final Map<String, Object> variables = new HashMap<>(); |
| 62 | + |
| 63 | + CDIWebContext(BeanManager beanManager, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, Locale locale) { |
| 64 | + this.beanManager = beanManager; |
| 65 | + this.request = request; |
| 66 | + this.response = response; |
| 67 | + this.context = servletContext; |
| 68 | + this.locale = locale; |
| 69 | + this.cdiNamedBeans = enumerateNamedBeans(); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("serial") |
| 73 | + private Set<String> enumerateNamedBeans() { |
| 74 | + Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() { |
| 75 | + }); |
| 76 | + return beans.stream().map(Bean::getName).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public HttpServletRequest getRequest() { |
| 81 | + return request; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public HttpServletResponse getResponse() { |
| 86 | + return response; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public HttpSession getSession() { |
| 91 | + return request.getSession(false); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public ServletContext getServletContext() { |
| 96 | + return context; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Locale getLocale() { |
| 101 | + return locale; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean containsVariable(String name) { |
| 106 | + Objects.requireNonNull(name, "The variable name must not be null"); |
| 107 | + if (variables.containsKey(name)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + return cdiNamedBeans.contains(name); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Set<String> getVariableNames() { |
| 115 | + final Set<String> variableNames = new HashSet<>(variables.keySet()); |
| 116 | + variableNames.addAll(this.cdiNamedBeans); |
| 117 | + return variableNames; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Object getVariable(String name) { |
| 122 | + Objects.requireNonNull(name, "The variable name must not be null"); |
| 123 | + if (variables.containsKey(name)) { |
| 124 | + return variables.get(name); |
| 125 | + } else if (cdiNamedBeans.contains(name)) { |
| 126 | + return getCdiBean(name); |
| 127 | + } else { |
| 128 | + return null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private Object getCdiBean(String name) { |
| 133 | + return new LazyContextVariable<Object>() { |
| 134 | + @Override |
| 135 | + protected Object loadValue() { |
| 136 | + Bean<?> bean = beanManager.getBeans(name).iterator().next(); |
| 137 | + CreationalContext ctx = beanManager.createCreationalContext(bean); |
| 138 | + // push the context a list so they can be disposed after the template has been processed |
| 139 | + contexts.add(ctx); |
| 140 | + return beanManager.getReference(bean, bean.getBeanClass(), ctx); |
| 141 | + } |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + void setVariables(Map<String, Object> variables) { |
| 146 | + this.variables.clear(); |
| 147 | + if (variables != null) { |
| 148 | + this.variables.putAll(variables); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Dispose all CDI creational contexts to depdenent scoped beans get disposed. |
| 154 | + * <p> |
| 155 | + * MUST be called after the template has been processed |
| 156 | + */ |
| 157 | + void close() { |
| 158 | + for (CreationalContext ctx : contexts) { |
| 159 | + ctx.release(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments