-
Notifications
You must be signed in to change notification settings - Fork 90
/
AdminfacesAutoConfiguration.java
140 lines (128 loc) · 5.67 KB
/
AdminfacesAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2016-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joinfaces.autoconfigure.adminfaces;
import javax.faces.application.ViewExpiredException;
import javax.persistence.OptimisticLockException;
import com.github.adminfaces.template.config.AdminConfig;
import com.github.adminfaces.template.exception.AccessDeniedException;
import com.github.adminfaces.template.session.AdminServletContextListener;
import com.github.adminfaces.template.session.AdminSession;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.joinfaces.autoconfigure.primefaces.Primefaces4_0Properties;
import org.joinfaces.autoconfigure.primefaces.Primefaces5_2Properties;
import org.joinfaces.autoconfigure.primefaces.PrimefacesAutoConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
/**
* Spring Boot Auto Configuration of AdminFaces.
*
* @author Marcelo Fernandes
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(AdminfacesProperties.class)
@ComponentScan({"com.github.adminfaces.template.bean",
"com.github.adminfaces.template.config",
"com.github.adminfaces.template.security"})
@ServletComponentScan({"com.github.adminfaces.template.security",
"com.github.adminfaces.template.session"})
@ConditionalOnClass(AdminSession.class)
@AutoConfigureBefore(PrimefacesAutoConfiguration.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class AdminfacesAutoConfiguration {
@Autowired
private AdminfacesProperties adminfacesProperties;
@Bean
public BeanPostProcessor adminfacesPrimeFacesPropertiesPostProcessor() {
PrimeFacesPropertiesPostProcessor result = new PrimeFacesPropertiesPostProcessor();
result.setAdminfacesProperties(this.adminfacesProperties);
return result;
}
// AdminSession does not contain @Named.
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public AdminSession adminSession() {
return new AdminSession();
}
/**
* This {@link WebServerFactoryCustomizer} adds a {@link ServletContextInitializer} to the embedded servlet-container
* which is equivalent to adminfaces's own {@code META-INF/web-fragment.xml}.
* @return adminfaces web server factory customizer
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> adminfacesWebServerFactoryCustomizer() {
return factory -> {
factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403.jsf"),
new ErrorPage(AccessDeniedException.class, "/403.jsf"),
new ErrorPage(HttpStatus.NOT_FOUND, "/404.jsf"),
new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.jsf"),
new ErrorPage(Throwable.class, "/500.jsf"),
new ErrorPage(ViewExpiredException.class, "/expired.jsf"),
new ErrorPage(OptimisticLockException.class, "/optimistic.jsf")
);
factory.addInitializers(servletContext -> {
servletContext.addListener(new AdminServletContextListener());
});
};
}
/**
* Configures Primefaces to use admin theme.
* Apply adminfaces configuration from adminfaces properties.
*
* @author Marcelo Fernandes
*/
static class PrimeFacesPropertiesPostProcessor implements BeanPostProcessor {
@Setter
private AdminfacesProperties adminfacesProperties;
@Override
public Object postProcessBeforeInitialization(@Nullable Object bean, @Nullable String beanName) throws BeansException {
if (bean instanceof Primefaces4_0Properties) {
Primefaces4_0Properties properties = (Primefaces4_0Properties) bean;
log.warn("Changing primefaces theme from '{}' to 'admin'.", properties.getTheme());
properties.setTheme("admin");
}
if (bean instanceof Primefaces5_2Properties) {
Primefaces5_2Properties properties = (Primefaces5_2Properties) bean;
log.warn("Changing primefaces fontAwesome from 'false' to 'true'.");
properties.setFontAwesome(true);
}
if (bean instanceof AdminConfig) {
AdminConfigWrapper adminConfigWrapper = new AdminConfigWrapper();
adminConfigWrapper.setAdminfacesProperties(this.adminfacesProperties);
bean = adminConfigWrapper;
}
return bean;
}
}
}