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.Main; 007import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 008import org.openstreetmap.josm.io.remotecontrol.RequestProcessor; 009 010/** 011 * Reports available commands, their parameters and examples 012 * @since 6091 013 */ 014public class FeaturesHandler extends RequestHandler { 015 016 /** 017 * The remote control command name used to reply version. 018 */ 019 public static final String command = "features"; 020 021 @Override 022 protected void handleRequest() throws RequestHandlerErrorException, 023 RequestHandlerBadRequestException { 024 StringBuilder buf = new StringBuilder(); 025 String q = args.get("q"); 026 if (q != null) { 027 buf.append('['); 028 boolean first = true; 029 for (String s: q.split("[,\\s]+")) { 030 if (first) { 031 first = false; 032 } else { 033 buf.append(", "); 034 } 035 String info = RequestProcessor.getHandlerInfoAsJSON('/'+s); 036 if (info != null) { 037 buf.append(info); 038 } else { 039 Main.warn("Unknown handler {0} passed to /features request", s); 040 } 041 } 042 buf.append(']'); 043 } else { 044 buf.append(RequestProcessor.getHandlersInfoAsJSON()); 045 } 046 047 content = buf.toString(); 048 contentType = "application/json"; 049 if (args.containsKey("jsonp")) { 050 content = args.get("jsonp") + " && " + args.get("jsonp") + '(' + content + ')'; 051 } 052 } 053 054 @Override 055 public String getPermissionMessage() { 056 return tr("Remote Control has been asked to report its supported features. This enables web sites to guess a running JOSM version"); 057 } 058 059 @Override 060 public PermissionPrefWithDefault getPermissionPref() { 061 return PermissionPrefWithDefault.READ_PROTOCOL_VERSION; 062 } 063 064 @Override 065 public String[] getMandatoryParams() { 066 return null; 067 } 068 069 @Override 070 public String[] getOptionalParams() { 071 return new String[]{"jsonp", "q"}; 072 } 073 074 @Override 075 protected void validateRequest() throws RequestHandlerBadRequestException { 076 // Nothing to do 077 } 078 079 @Override 080 public String getUsage() { 081 return "reports available commands, their parameters and examples"; 082 } 083 084 @Override 085 public String[] getUsageExamples() { 086 return new String[] {"/features", "/features?q=import,add_node"}; 087 } 088}