-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathWebComponentRegistrar.java
More file actions
83 lines (67 loc) · 3.47 KB
/
WebComponentRegistrar.java
File metadata and controls
83 lines (67 loc) · 3.47 KB
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
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.web;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.EnumSet;
import org.openmrs.contrib.dwr.servlet.EfficientShutdownServletContextAttributeListener;
import org.openmrs.module.web.filter.AdminPageFilter;
import org.openmrs.module.web.filter.ForcePasswordChangeFilter;
import org.openmrs.module.web.filter.RedirectAfterLoginFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
//Added
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class WebComponentRegistrar implements ServletContextAware {
//Added
private static final Logger log = LoggerFactory.getLogger(WebComponentRegistrar.class);
@Override
public void setServletContext(ServletContext servletContext) {
try {
String[] mappings = { "*.htm", "*.form", "*.list", "*.json", "*.field", "*.portlet", "*.page", "*.action" };
ServletRegistration openmrsServletReg = servletContext.getServletRegistration("openmrs");
addMappings(openmrsServletReg, mappings);
addMappings(servletContext.getServletRegistration("jsp"), "*.withjstl");
Dynamic filter = servletContext.addFilter("redirectAfterLoginFilter", new RedirectAfterLoginFilter());
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, mappings);
filter = servletContext.addFilter("forcePasswordChangeFilter", new ForcePasswordChangeFilter());
filter.setInitParameter("changePasswordForm", "/admin/users/changePassword.form");
filter.setInitParameter("excludeURL", "changePasswordForm,csrfguard,logout,.js,.css,.gif,.jpg,.jpeg,.png");
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
filter = servletContext.addFilter("adminPageFilter", new AdminPageFilter());
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/admin");
servletContext.addListener(new SessionListener());
/*
* EfficientShutdownServletContextAttributeListener is used instead of
* EfficientShutdownServletContextListener since the latter implements ServletContextListener,
* which is not supported by ServletContext.addListener.
*/
servletContext.addListener(new EfficientShutdownServletContextAttributeListener());
}
catch (Exception ex) {
//TODO need a work around for: java.lang.IllegalStateException: Started
//Unable to configure mapping for servlet because this servlet context has already been initialized.
//This happens on running openmrs after InitializationFilter or UpdateFilter
//hence requiring a restart to see any page other than index.htm
//After a restart, all mappings will then happen within Listener.contextInitialized()
//Replaced
log.error("Error registering web component", ex);
}
}
private void addMappings(ServletRegistration reg, String... mappings) {
for (String mapping : mappings) {
reg.addMapping(mapping);
}
}
}