1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
7 | import org.openstreetmap.josm.io.remotecontrol.RequestProcessor;
|
---|
8 | import org.openstreetmap.josm.tools.Logging;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Reports available commands, their parameters and examples
|
---|
12 | * @since 6091
|
---|
13 | */
|
---|
14 | public class FeaturesHandler extends RequestHandler {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * The remote control command name used to reply version.
|
---|
18 | */
|
---|
19 | public static final String command = "features";
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | protected void handleRequest() throws RequestHandlerErrorException,
|
---|
23 | RequestHandlerBadRequestException {
|
---|
24 | StringBuilder buf = new StringBuilder();
|
---|
25 | String q = args.get("q");
|
---|
26 | if (q != null) {
|
---|
27 | buf.append('[');
|
---|
28 | boolean first = true;
|
---|
29 | for (String s: q.split("[,\\s]+")) {
|
---|
30 | if (first) {
|
---|
31 | first = false;
|
---|
32 | } else {
|
---|
33 | buf.append(", ");
|
---|
34 | }
|
---|
35 | String info = RequestProcessor.getHandlerInfoAsJSON('/'+s);
|
---|
36 | if (info != null) {
|
---|
37 | buf.append(info);
|
---|
38 | } else {
|
---|
39 | Logging.warn("Unknown handler {0} passed to /features request", s);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | buf.append(']');
|
---|
43 | } else {
|
---|
44 | buf.append(RequestProcessor.getHandlersInfoAsJSON());
|
---|
45 | }
|
---|
46 |
|
---|
47 | content = buf.toString();
|
---|
48 | contentType = "application/json";
|
---|
49 | if (args.containsKey("jsonp")) {
|
---|
50 | content = args.get("jsonp") + " && " + args.get("jsonp") + '(' + content + ')';
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public String getPermissionMessage() {
|
---|
56 | return tr("Remote Control has been asked to report its supported features. This enables web sites to guess a running JOSM version");
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
61 | return PermissionPrefWithDefault.READ_PROTOCOL_VERSION;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public String[] getMandatoryParams() {
|
---|
66 | return new String[0];
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public String[] getOptionalParams() {
|
---|
71 | return new String[]{"jsonp", "q"};
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
76 | // Nothing to do
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String getUsage() {
|
---|
81 | return "reports available commands, their parameters and examples";
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public String[] getUsageExamples() {
|
---|
86 | return new String[] {"/features", "/features?q=import,add_node"};
|
---|
87 | }
|
---|
88 | }
|
---|