-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNavLinks.java
More file actions
116 lines (101 loc) · 3.42 KB
/
NavLinks.java
File metadata and controls
116 lines (101 loc) · 3.42 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
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
/**
* Copyright (c) 2022-2025 Contributors to the OpenNTF Home App Project
*
* 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 bean;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import model.home.ConfigEntry;
import model.home.NavLink;
@RequestScoped
public class NavLinks {
@Inject
private ApplicationConfig applicationConfig;
private Map<Pattern, String> mappings;
/**
* Load URL mappings from the /urlmap.properties classpath resource.
*
* <p>This file contains known mapping regex patterns from the XPages application to their
* webapp equivalents.</p>
*/
@PostConstruct
public void loadMappings() {
Properties props = new Properties();
try(InputStream is = getClass().getResourceAsStream("/urlmap.properties")) {
try(Reader r = new InputStreamReader(is, StandardCharsets.UTF_8)) {
props.load(r);
}
} catch(IOException e) {
throw new UncheckedIOException(e);
}
this.mappings = props.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> Pattern.compile((String)entry.getKey()),
entry -> (String)entry.getValue()
));
}
@Produces @Named("NavLinks")
public List<NavLink> getLinks() {
return getLinks("links"); //$NON-NLS-1$
}
private List<NavLink> getLinks(String key) {
ConfigEntry links = applicationConfig.getConfigEntry(key)
.orElseThrow(() -> new IllegalStateException(MessageFormat.format("Unable to find setting for {0}", key)));
List<String> value1 = links.getValue1();
List<String> value2 = links.getValue2();
List<NavLink> result = new ArrayList<>(value1.size());
for(int i = 0; i < value1.size(); i++) {
String label = value1.get(i);
String value = value2.get(i);
result.add(toLink(label, value));
}
return result;
}
private NavLink toLink(String label, String value) {
if(value != null && value.startsWith("[") && value.endsWith("]")) { //$NON-NLS-1$ //$NON-NLS-2$
List<NavLink> children = getLinks(value.substring(1, value.length()-1));
return new NavLink(label, null, children);
} else {
return new NavLink(label, mapUrl(value), Collections.emptyList());
}
}
private String mapUrl(String url) {
for(Map.Entry<Pattern, String> entry : mappings.entrySet()) {
Matcher m = entry.getKey().matcher(url);
if(m.matches()) {
return url.replaceAll(entry.getKey().pattern(), entry.getValue());
}
}
return url;
}
}