001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.remotecontrol.handler;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
007import org.openstreetmap.josm.io.remotecontrol.RequestProcessor;
008
009/**
010 * Handler for version request.
011 */
012public class VersionHandler extends RequestHandler {
013
014    /**
015     * The remote control command name used to reply version.
016     */
017    public static final String command = "version";
018
019    @Override
020    protected void handleRequest() throws RequestHandlerErrorException,
021            RequestHandlerBadRequestException {
022        content = RequestProcessor.PROTOCOLVERSION;
023        contentType = "application/json";
024        if (args.containsKey("jsonp")) {
025            content = args.get("jsonp") + " && " + args.get("jsonp") + '(' + content + ')';
026        }
027    }
028
029    @Override
030    public String getPermissionMessage() {
031        return tr("Remote Control has been asked to report its protocol version. This enables web sites to detect a running JOSM.");
032    }
033
034    @Override
035    public PermissionPrefWithDefault getPermissionPref() {
036        return PermissionPrefWithDefault.READ_PROTOCOL_VERSION;
037    }
038
039    @Override
040    public String[] getMandatoryParams() {
041        return new String[0];
042    }
043
044    @Override
045    public String[] getOptionalParams() {
046        return new String[]{"jsonp"};
047    }
048
049    @Override
050    protected void validateRequest() throws RequestHandlerBadRequestException {
051        // Nothing to do
052    }
053
054    @Override
055    public String getUsage() {
056        return "returns the current protocol version of the installed JOSM RemoteControl";
057    }
058
059    @Override
060    public String[] getUsageExamples() {
061        return new String[] {"/version", "/version?jsonp=test"};
062    }
063}