getLocalizedValues(String,List)   F
last analyzed

Complexity

Conditions 9

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
cc 9
rs 3
1
package org.apereo.cas.support.saml.mdui;
2
3
import org.apache.commons.lang3.builder.ToStringBuilder;
4
import org.apereo.cas.services.RegisteredService;
5
import org.apereo.cas.web.flow.services.DefaultRegisteredServiceUserInterfaceInfo;
6
import org.opensaml.core.xml.schema.XSString;
7
import org.opensaml.core.xml.schema.XSURI;
8
import org.opensaml.saml.ext.saml2mdui.UIInfo;
9
10
import javax.annotation.Nullable;
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18
19
import org.opensaml.saml.saml2.metadata.LocalizedName;
20
21
import java.util.regex.Pattern;
22
23
/**
24
 * This is {@link SamlMetadataUIInfo}.
25
 *
26
 * @author Misagh Moayyed
27
 * @since 4.1.0
28
 */
29
public class SamlMetadataUIInfo extends DefaultRegisteredServiceUserInterfaceInfo {
30
31
    private static final long serialVersionUID = -1434801982864628179L;
32
    private static final Logger LOGGER = LoggerFactory.getLogger(SamlMetadataUIInfo.class);
33
34
    private transient UIInfo uiInfo;
35
    private String locale;
36
37
    /**
38
     * Instantiates a new Simple metadata uI info.
39
     *
40
     * @param registeredService the registered service
41
     */
42
    public SamlMetadataUIInfo(final RegisteredService registeredService) {
43
        this(null, registeredService);
44
    }
45
46
    /**
47
     * Instantiates a new Simple metadata uI info.
48
     *
49
     * @param registeredService the registered service
50
     * @param locale browser preferred language
51
     */
52
    public SamlMetadataUIInfo(final RegisteredService registeredService, final String locale) {
53
        this(null, registeredService);
54
        this.locale = locale;
55
    }
56
57
    /**
58
     * Instantiates a new Simple mdui info.
59
     *
60
     * @param uiInfo            the ui info
61
     * @param registeredService the registered service
62
     */
63
    public SamlMetadataUIInfo(@Nullable final UIInfo uiInfo, final RegisteredService registeredService) {
64
        super(registeredService);
65
        this.uiInfo = uiInfo;
66
    }
67
68
    @Override
69
    public Collection<String> getDescriptions() {
70
        if (this.uiInfo != null) {
71
            return getStringValues(this.uiInfo.getDescriptions());
72
        }
73
        return super.getDescriptions();
74
    }
75
76
    @Override
77
    public Collection<String> getDisplayNames() {
78
        if (this.uiInfo != null) {
79
            return getStringValues(this.uiInfo.getDisplayNames());
80
        }
81
        return super.getDescriptions();
82
    }
83
84
85
    @Override
86
    public Collection<String> getInformationURLs() {
87
        if (this.uiInfo != null) {
88
            return getStringValues(this.uiInfo.getInformationURLs());
89
        }
90
        return super.getInformationURLs();
91
    }
92
93
94
    @Override
95
    public Collection<String> getPrivacyStatementURLs() {
96
        if (this.uiInfo != null) {
97
            return getStringValues(this.uiInfo.getPrivacyStatementURLs());
98
        }
99
        return super.getPrivacyStatementURLs();
100
    }
101
102
    /**
103
     * Gets logo urls.
104
     *
105
     * @return the logo urls
106
     */
107
    @Override
108
    public Collection<Logo> getLogoUrls() {
109
        final List<Logo> list = new ArrayList<>();
110
        if (this.uiInfo != null) {
111
            list.addAll(this.uiInfo.getLogos().stream().map(l -> new Logo(l.getURL(), l.getHeight(),
112
                    l.getWidth())).collect(Collectors.toList()));
113
        }
114
        return list;
115
    }
116
117
    /**
118
     * Gets string values from the list of mdui objects.
119
     *
120
     * @param items the items
121
     * @return the string values
122
     */
123
    private static Collection<String> getStringValues(final List<?> items) {
124
        final List<String> list = new ArrayList<>();
125
        items.forEach(d -> {
126
            if (d instanceof XSURI) {
127
                list.add(((XSURI) d).getValue());
128
            } else if (d instanceof XSString) {
129
                list.add(((XSString) d).getValue());
130
            }
131
        });
132
        return list;
133
    }
134
135
    public void setUIInfo(final UIInfo uiInfo) {
136
        this.uiInfo = uiInfo;
137
    }
138
139
    /**
140
     * Gets localized description.
141
     *
142
     * @param locale browser preferred language
143
     * @return the description
144
     */
145
    public String getDescription(final String locale) {
146
        if (this.uiInfo != null) {
147
            final String description = getLocalizedValues(locale, this.uiInfo.getDescriptions());
148
            return (description != null) ? description : super.getDescription();
149
        }
150
        return super.getDescription();
151
    }
152
153
    @Override
154
    public String getDescription() {
155
        return getDescription(this.locale);
156
    }
157
158
    /**
159
     * Gets localized displayName.
160
     *
161
     * @param locale browser preferred language
162
     * @return the displayName
163
     */
164
    public String getDisplayName(final String locale) {
165
        if (this.uiInfo != null) {
166
            final String displayName = getLocalizedValues(locale, this.uiInfo.getDisplayNames());
167
            return (displayName != null) ? displayName : super.getDisplayName();
168
        }
169
        return super.getDisplayName();
170
    }
171
172
    @Override
173
    public String getDisplayName() {
174
        return getDisplayName(this.locale);
175
    }
176
177
    /**
178
     * Gets localized informationURL.
179
     *
180
     * @param locale browser preferred language
181
     * @return the informationURL
182
     */
183
    public String getInformationURL(final String locale) {
184
        if (this.uiInfo != null) {
185
            final String informationUrl = getLocalizedValues(locale, this.uiInfo.getInformationURLs());
186
            return (informationUrl != null) ? informationUrl : super.getInformationURL();
187
        }
188
        return super.getInformationURL();
189
    }
190
191
    @Override
192
    public String getInformationURL() {
193
        return getInformationURL(this.locale);
194
    }
195
196
    /**
197
     * Gets localized privacyStatementURL.
198
     *
199
     * @param locale browser preferred language
200
     * @return the privacyStatementURL
201
     */
202
    public String getPrivacyStatementURL(final String locale) {
203
        if (this.uiInfo != null) {
204
            final String privacyStatementURL = getLocalizedValues(locale, this.uiInfo.getPrivacyStatementURLs());
205
            return (privacyStatementURL != null) ? privacyStatementURL : super.getPrivacyStatementURL();
206
        }
207
        return super.getPrivacyStatementURL();
208
    }
209
210
    @Override
211
    public String getPrivacyStatementURL() {
212
        return getPrivacyStatementURL(this.locale);
213
    }
214
215
    /**
216
     * Gets localized values.
217
     *
218
     * @param locale browser preferred language
219
     * @param items the items
220
     * @return the string value
221
     */
222
    private String getLocalizedValues(final String locale, final List<?> items) {
223
        if (locale != null) {
224
            LOGGER.trace("Looking for locale [{}]", locale);
225
            for (int i = 0; i < items.size(); i++) {
226
                if (items.get(i) instanceof LocalizedName) {
227
                    final Pattern p = Pattern.compile(locale, Pattern.CASE_INSENSITIVE);
228
229
                    if (p.matcher(((LocalizedName) items.get(i)).getXMLLang()).matches()) {
230
                        return ((LocalizedName) items.get(i)).getValue();
231
                    }
232
                }
233
            }
234
            LOGGER.trace("Locale [{}] not found.", locale);
235
        }
236
237
        LOGGER.trace("Looking for locale [en]");
238
        for (int i = 0; i < items.size(); i++) {
239
            if (items.get(i) instanceof LocalizedName) {
240
                final Pattern p = Pattern.compile("en", Pattern.CASE_INSENSITIVE);
241
242
                if (p.matcher(((LocalizedName) items.get(i)).getXMLLang()).matches()) {
243
                    return ((LocalizedName) items.get(i)).getValue();
244
                }
245
            }
246
        }
247
        LOGGER.trace("Locale [en] not found.");
248
249
        if (!items.isEmpty()) {
250
            LOGGER.trace("Loading first available locale [{}]", ((LocalizedName) items.get(0)).getValue());
251
            return ((XSString) items.get(0)).getValue();
252
        }
253
        return null;
254
    }
255
256
    @Override
257
    public String toString() {
258
        return new ToStringBuilder(this)
259
                .appendSuper(super.toString())
260
                .append("displayName", getDisplayName())
261
                .append("description", getDescription())
262
                .append("informationUrl", getInformationURL())
263
                .append("privacyStatementUrl", getPrivacyStatementURL())
264
                .toString();
265
    }
266
}
267