1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.user2;
20
21 import java.net.URLEncoder;
22 import java.nio.charset.StandardCharsets;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.Map.Entry;
26
27 import org.mycore.common.MCRConstants;
28 import org.mycore.common.MCRSessionMgr;
29 import org.mycore.common.config.MCRConfiguration2;
30
31
32
33
34
35
36
37
38
39 public class MCRRealm {
40
41 private String id;
42
43
44 private HashMap<String, String> labels = new HashMap<>();
45
46
47 private String passwordChangeURL;
48
49
50 private String loginURL;
51
52
53 private String createURL;
54
55 private String redirectParameter;
56
57 private String realmParameter;
58
59 private static String DEFAULT_LANG = MCRConfiguration2.getString("MCR.Metadata.DefaultLang")
60 .orElse(MCRConstants.DEFAULT_LANG);
61
62 public static final String USER_INFORMATION_ATTR = "realmId";
63
64
65
66
67
68
69 MCRRealm(String id) {
70 this.id = id;
71 }
72
73
74
75
76
77
78 public String getID() {
79 return id;
80 }
81
82
83
84
85 public String getLabel() {
86 String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage();
87 String label = labels.get(lang);
88 if (label != null) {
89 return label;
90 }
91 label = labels.get(DEFAULT_LANG);
92 if (label != null) {
93 return label;
94 }
95 return id;
96 }
97
98
99
100
101 void setLabel(String lang, String label) {
102 labels.put(lang, label);
103 }
104
105
106
107
108 public String getPasswordChangeURL() {
109 return passwordChangeURL;
110 }
111
112
113
114
115 void setPasswordChangeURL(String url) {
116 this.passwordChangeURL = url;
117 }
118
119
120
121
122 public String getLoginURL() {
123 return loginURL;
124 }
125
126
127
128
129 void setLoginURL(String url) {
130 this.loginURL = url;
131 }
132
133
134
135
136 public String getCreateURL() {
137 return createURL;
138 }
139
140
141
142
143 void setCreateURL(String createURL) {
144 this.createURL = createURL;
145 }
146
147 @Override
148 public boolean equals(Object obj) {
149 if (obj instanceof MCRRealm) {
150 return ((MCRRealm) obj).id.equals(id);
151 } else {
152 return false;
153 }
154 }
155
156 @Override
157 public int hashCode() {
158 return id.hashCode();
159 }
160
161
162
163
164 @Override
165 public String toString() {
166 return "id=" + id + ", " + (loginURL != null ? "loginURL=" + loginURL : "");
167 }
168
169
170
171
172
173
174
175
176 public String getLoginURL(String redirectURL) {
177 LinkedHashMap<String, String> parameter = new LinkedHashMap<>();
178 String redirect = getRedirectParameter();
179 if (redirect != null && redirectURL != null) {
180 parameter.put(redirect, redirectURL);
181 }
182 String realmParameter = getRealmParameter();
183 if (realmParameter != null) {
184 parameter.put(realmParameter, getID());
185 }
186 if (parameter.isEmpty()) {
187 return getLoginURL();
188 }
189 StringBuilder loginURL = new StringBuilder(getLoginURL());
190 boolean firstParameter = !getLoginURL().contains("?");
191 for (Entry<String, String> entry : parameter.entrySet()) {
192 if (firstParameter) {
193 loginURL.append('?');
194 firstParameter = false;
195 } else {
196 loginURL.append('&');
197 }
198 loginURL.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(),
199 StandardCharsets.UTF_8));
200 }
201 return loginURL.toString();
202 }
203
204
205
206
207 String getRedirectParameter() {
208 return redirectParameter;
209 }
210
211
212
213
214 void setRedirectParameter(String redirectParameter) {
215 this.redirectParameter = redirectParameter;
216 }
217
218 public String getRealmParameter() {
219 return realmParameter;
220 }
221
222 public void setRealmParameter(String realmParameter) {
223 this.realmParameter = realmParameter;
224 }
225
226 }