-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AboutController.java
240 lines (204 loc) · 9.35 KB
/
AboutController.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.rest.catalog;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.NotImplementedException;
import org.geoserver.ManifestLoader;
import org.geoserver.ManifestLoader.AboutModel;
import org.geoserver.ManifestLoader.AboutModel.AboutModelType;
import org.geoserver.ManifestLoader.AboutModel.ManifestModel;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.config.util.XStreamPersister;
import org.geoserver.platform.ModuleStatus;
import org.geoserver.rest.ObjectToMapWrapper;
import org.geoserver.rest.RestBaseController;
import org.geoserver.rest.converters.FreemarkerHTMLMessageConverter;
import org.geoserver.rest.converters.XStreamMessageConverter;
import org.geoserver.rest.wrapper.RestWrapper;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
@RestController
@RequestMapping(path = RestBaseController.ROOT_PATH + "/about", produces = { MediaType.TEXT_HTML_VALUE,
MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ControllerAdvice
public class AboutController extends RestBaseController {
@GetMapping(value = "/manifest")
public RestWrapper<AboutModel> getManifest(
@RequestParam(name = "manifest", required = false) String regex,
@RequestParam(required = false) String from, @RequestParam(required = false) String to,
@RequestParam(required = false) String key,
@RequestParam(required = false) String value) {
return wrapObject(getModel(AboutModelType.RESOURCES, regex, from, to, key, value),
AboutModel.class);
}
@GetMapping(value = "/version")
public RestWrapper<AboutModel> getVersion(
@RequestParam(name = "manifest", required = false) String regex,
@RequestParam(required = false) String from, @RequestParam(required = false) String to,
@RequestParam(required = false) String key,
@RequestParam(required = false) String value) {
return wrapObject(getModel(AboutModelType.VERSIONS, regex, from, to, key, value),
AboutModel.class);
}
protected AboutModel getModel(AboutModelType type, String regex, String from, String to,
String key, String value) {
AboutModel model = null;
// filter name by regex
if (regex != null) {
model = buildAboutModel(type).filterNameByRegex(regex);
}
// filter name by range
if (from != null && to != null) {
if (model != null) {
model = model.filterNameByRange(from, to);
} else {
model = buildAboutModel(type).filterNameByRange(from, to);
}
}
// filter by properties
if (model == null) {
model = buildAboutModel(type);
}
if (key != null && value != null) {
model = model.filterPropertyByKeyValue(value, key);
} else if (key != null) {
model = model.filterPropertyByKey(key);
} else if (value != null) {
model = model.filterPropertyByValue(value);
}
if (model != null) {
return model;
} else {
return buildAboutModel(type);
}
}
private static AboutModel buildAboutModel(AboutModelType type) {
if (type.equals(AboutModelType.RESOURCES)) {
// if request is for resource return the resources
return ManifestLoader.getResources();
} else {
// get the version
return ManifestLoader.getVersions();
}
}
@Override
protected String getTemplateName(Object object) {
if (object instanceof AboutModel) {
return "AboutModel.ftl";
} else {
return null;
}
}
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return AboutModel.class.isAssignableFrom(methodParameter.getParameterType());
}
@Override
protected <T> ObjectWrapper createObjectWrapper(Class<T> clazz) {
if (AboutModel.class.isAssignableFrom(clazz)) {
return new ObjectToMapWrapper<AboutModel>(AboutModel.class) {
@Override
protected void wrapInternal(Map properties, SimpleHash model, AboutModel object) {
final List<Map<String, Object>> manifests = new ArrayList<Map<String, Object>>();
final Iterator<ManifestModel> it = object.getManifests().iterator();
while (it.hasNext()) {
final ManifestModel manifest = it.next();
final Map<String, Object> map = new HashMap<String, Object>();
map.put("name", manifest.getName());
final List<String> props = new ArrayList<String>();
map.put("properties", props);
final List<String> values = new ArrayList<String>();
map.put("valuez", values);
final Iterator<String> innerIt = manifest.getEntries().keySet().iterator();
while (innerIt.hasNext()) {
String key = innerIt.next();
props.add(key);
values.add(manifest.getEntries().get(key));
}
manifests.add(map);
}
properties.put("manifests", manifests);
}
};
} else {
return null;
}
}
public void configurePersister(XStreamPersister persister, XStreamMessageConverter converter) {
XStream xs = persister.getXStream();
// AboutModel
xs.processAnnotations(AboutModel.class);
xs.allowTypes(new Class[] { AboutModel.class });
xs.addImplicitCollection(AboutModel.class, "manifests");
xs.alias("about", AboutModel.class);
// ManifestModel Xstream converter
xs.registerConverter(new Converter() {
@Override
public boolean canConvert(Class type) {
return type.equals(ManifestModel.class);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
ManifestModel model = (ManifestModel) source;
writer.addAttribute("name", model.getName());
for (java.util.Map.Entry<String, String> entry : model.getEntries().entrySet())
context.convertAnother(entry, new Converter() {
@Override
public boolean canConvert(Class type) {
if (java.util.Map.Entry.class.isAssignableFrom(type))
return true;
return false;
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
@SuppressWarnings("unchecked")
Entry<String, String> e = (Entry<String, String>) source;
writer.startNode(e.getKey());
writer.setValue(e.getValue());
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
throw new UnsupportedOperationException("Not implemented");
}
});
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
throw new UnsupportedOperationException("Not implemented");
}
});
xs.alias("resource", ManifestModel.class);
xs.addImplicitCollection(ManifestModel.class, "entries");
xs.useAttributeFor(ManifestModel.class, "name");
xs.alias("property", Entry.class);
xs.autodetectAnnotations(true);
}
}