-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginForm.java
149 lines (136 loc) · 5.89 KB
/
LoginForm.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
141
142
143
144
145
146
147
148
149
/*
* Copyright 2011-2021 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.appng.application.authentication.webform;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.Scope;
import org.appng.api.SiteProperties;
import org.appng.api.model.Application;
import org.appng.api.model.Properties;
import org.appng.api.model.Site;
import org.appng.api.model.Subject;
import org.appng.api.support.SelectionFactory;
import org.appng.api.support.environment.DefaultEnvironment;
import org.appng.application.authentication.AbstractLogon;
import org.appng.core.domain.SubjectImpl;
import org.appng.xml.platform.Selection;
import org.appng.xml.platform.SelectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class LoginForm implements DataProvider {
private static final String PARAM_LANG = "lang";
private static final String PARAM_ACTION = "action";
private static final String SLASH = "/";
@Autowired
SelectionFactory selectionFactory;
public DataContainer getData(Site site, Application application, Environment environment, Options options,
Request request, FieldProcessor fieldProcessor) {
DataContainer dataContainer = new DataContainer(fieldProcessor);
Selection langSelection = processLanguage(site, environment, request);
if (null != langSelection) {
dataContainer.getSelections().add(langSelection);
}
dataContainer.setItem(new LoginData());
return dataContainer;
}
private Selection processLanguage(Site site, Environment environment, Request request) {
HttpServletRequest httpServletRequest = ((DefaultEnvironment) environment).getServletRequest();
Properties properties = site.getProperties();
if (null == environment.getAttribute(Scope.SESSION, AbstractLogon.PRE_LOGIN_PATH)) {
String logoutRef = properties.getString(SiteProperties.AUTH_LOGOUT_REF);
String authApplication = properties.getString(SiteProperties.AUTH_APPLICATION);
String managerPath = properties.getString(SiteProperties.MANAGER_PATH);
String queryString = httpServletRequest.getQueryString();
String siteRootPath = managerPath + SLASH + site.getName();
String requestedUrl = httpServletRequest.getServletPath() + (null == queryString ? "" : "?" + queryString);
boolean isRoot = requestedUrl.equals(siteRootPath);
boolean isManager = requestedUrl.equals(managerPath);
boolean isLogout = requestedUrl.equals(managerPath + SLASH + logoutRef);
boolean isAuthentication = requestedUrl.startsWith(siteRootPath + SLASH + authApplication);
boolean hasAction = requestedUrl.startsWith(siteRootPath + SLASH + "?" + PARAM_ACTION + "=");
if (!(isManager || isRoot || isLogout || hasAction || isAuthentication)) {
environment.setAttribute(Scope.SESSION, AbstractLogon.PRE_LOGIN_PATH, requestedUrl);
}
}
List<String> languages = properties.getList(SiteProperties.SUPPORTED_LANGUAGES, ",");
debug("site {} supported languages: {}", site.getName(), StringUtils.join(languages, ","));
debug("language from request: {}", request.getParameter(PARAM_LANG));
String language = null;
if (languages.size() >= 1) {
language = request.getParameter(PARAM_LANG);
boolean mustSetLanguage = true;
if (null == language || !languages.contains(language)) {
Subject subject = environment.getSubject();
if (null == subject) {
Enumeration<Locale> locales = httpServletRequest.getLocales();
while (locales.hasMoreElements()) {
Locale locale = locales.nextElement();
if (languages.contains(locale.getLanguage())) {
language = locale.getLanguage();
debug("no subject present, retrieved language from request.getLocales(): {}", language);
break;
}
}
} else {
language = subject.getLanguage();
debug("retrieved language from subject#{}: {}", subject.hashCode(), language);
mustSetLanguage = false;
}
}
if (null == language) {
language = languages.get(0);
debug("no language set, using {}", language);
}
if (mustSetLanguage) {
SubjectImpl subject = new SubjectImpl();
subject.setLanguage(language);
debug("created new subject#{} with language {}", subject.hashCode(), language);
((DefaultEnvironment) environment).setSubject(subject);
String path = httpServletRequest.getServletPath();
String action = request.getParameter(PARAM_ACTION);
if (null != action) {
String completePath = path + "?" + PARAM_ACTION + "=" + action;
debug("user must set language, redirecting to {}", completePath);
site.sendRedirect(environment, completePath);
}
} else {
language = environment.getLocale().getLanguage();
debug("using language from environment: {}", language);
}
}
String[] values = languages.toArray(new String[languages.size()]);
Selection selection = selectionFactory.fromObjects(PARAM_LANG, "lang", values, language);
selection.setType(SelectionType.SELECT);
return selection;
}
void debug(String message, Object... args) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(message, args);
}
}
}