Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui;
2

    
3
import java.net.URLEncoder;
4
import javax.annotation.Resource;
5
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpServletResponse;
7

    
8
import eu.dnetlib.functionality.modular.ui.users.AuthorizationManager;
9
import eu.dnetlib.functionality.modular.ui.users.User;
10
import eu.dnetlib.functionality.modular.ui.utils.ShutdownUtils;
11
import org.apache.commons.lang3.StringUtils;
12
import org.springframework.beans.factory.BeanNameAware;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Required;
15
import org.springframework.beans.factory.annotation.Value;
16
import org.springframework.ui.ModelMap;
17
import org.springframework.web.servlet.ModelAndView;
18
import org.springframework.web.servlet.mvc.Controller;
19
import org.springframework.web.servlet.view.RedirectView;
20

    
21
public abstract class ModuleEntryPoint extends MenuEntry implements Controller, BeanNameAware {
22

    
23
	@Autowired
24
	protected EntryPointsAggregator aggregator;
25
	@Resource(name = "modularUiAuthorizationManager")
26
	protected AuthorizationManager authorizationManager;
27
	private String beanName;
28
	private boolean validMenuEntry = true;
29
	private String group;
30
	private int groupOrder = 50;
31
	@Value("${dnet.modular.ui.authentication.url}")
32
	private String authenticationUrl;
33
	@Value("${dnet.modular.ui.logout.url}")
34
	private String logoutUrl;
35
	@Value("${dnet.modular.ui.ribbon.environment}")
36
	private String environment;
37
	@Value("${dnet.modular.ui.ribbon.accent}")
38
	private String ribbonAccent;
39
	@Autowired
40
	private ShutdownUtils shutdownUtils;
41

    
42
	@Override
43
	public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
44

    
45
		final User user = this.authorizationManager.obtainUserDetails(request);
46
		if (user != null) {
47
			final ModelAndView mv = new ModelAndView();
48
			final ModelMap map = mv.getModelMap();
49
			map.addAttribute("ui_menu", getMenu());
50
			map.addAttribute("ui_title", getTitle());
51
			map.addAttribute("ui_description", getDescription());
52
			map.addAttribute("ui_group", getGroup());
53
			map.addAttribute("ui_modules", this.aggregator.getMenus(user));
54
			map.addAttribute("environment", this.environment);
55
			map.addAttribute("ribbonAccent", this.ribbonAccent);
56

    
57
			switch (this.shutdownUtils.currentStatus()) {
58
			case STOPPING:
59
				map.addAttribute("ui_navbar_class", "navbar-system-stopping");
60
				map.addAttribute("ui_message", "stopping system");
61
				break;
62
			case STOPPED:
63
				map.addAttribute("ui_navbar_class", "navbar-system-stopped");
64
				map.addAttribute("ui_message", "system stopped");
65
				break;
66
			default:
67
				map.addAttribute("ui_navbar_class", "navbar-inverse");
68
				break;
69
			}
70

    
71
			String baseUrl = "";
72
			for (int i = 1; i < StringUtils.countMatches(this.beanName, "/"); i++) {
73
				baseUrl += "/..";
74
			}
75
			if (baseUrl.length() > 0) {
76
				baseUrl = baseUrl.substring(1);
77
			}
78

    
79
			map.addAttribute("ui_baseUrl", baseUrl);
80

    
81
			if ((this.logoutUrl != null) && !this.logoutUrl.isEmpty()) {
82
				map.addAttribute("ui_logoutUrl", this.logoutUrl);
83
			}
84

    
85
			map.addAttribute("ui_user", user);
86

    
87
			initialize(map, request, response);
88
			return mv;
89
		} else {
90
			final StringBuffer url = request.getRequestURL();
91
			final String queryString = request.getQueryString();
92
			if (queryString != null) {
93
				url.append('?');
94
				url.append(queryString);
95
			}
96
			return new ModelAndView(new RedirectView(this.authenticationUrl + "?url=" + URLEncoder.encode(url.toString(), "UTF-8")));
97
		}
98
	}
99

    
100
	abstract protected void initialize(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception;
101

    
102
	public String getBeanName() {
103
		return this.beanName;
104
	}
105

    
106
	@Override
107
	public void setBeanName(final String beanName) {
108
		this.beanName = beanName;
109
	}
110

    
111
	public String getGroup() {
112
		return this.group;
113
	}
114

    
115
	@Required
116
	public void setGroup(final String group) {
117
		this.group = group;
118
	}
119

    
120
	@Override
121
	public String getRelativeUrl() {
122
		return this.beanName;
123
	}
124

    
125
	public boolean isValidMenuEntry() {
126
		return this.validMenuEntry;
127
	}
128

    
129
	public void setValidMenuEntry(final boolean validMenuEntry) {
130
		this.validMenuEntry = validMenuEntry;
131
	}
132

    
133
	public int getGroupOrder() {
134
		return this.groupOrder;
135
	}
136

    
137
	public void setGroupOrder(final int groupOrder) {
138
		this.groupOrder = groupOrder;
139
	}
140

    
141
}
(11-11/17)