Changeset 4834 in josm for trunk/src


Ignore:
Timestamp:
2012-01-21T12:07:57+01:00 (12 years ago)
Author:
simon04
Message:

remotecontrol: primt more helpful Bad Request errors, i.e., list available commands, or show missing parameter

Location:
trunk/src/org/openstreetmap/josm/io/remotecontrol
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java

    r4833 r4834  
    1111import java.io.Writer;
    1212import java.net.Socket;
     13import java.util.Arrays;
    1314import java.util.Date;
    1415import java.util.HashMap;
     16import java.util.Map;
     17import java.util.Map.Entry;
    1518import java.util.StringTokenizer;
     19import java.util.TreeMap;
     20import java.util.TreeSet;
    1621
    1722import org.openstreetmap.josm.io.remotecontrol.handler.AddNodeHandler;
     
    2631import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler.RequestHandlerForbiddenException;
    2732import org.openstreetmap.josm.io.remotecontrol.handler.VersionHandler;
     33import org.openstreetmap.josm.tools.Utils;
    2834
    2935/**
     
    4955     * can extend this list by using @see addRequestHandler
    5056     */
    51     private static HashMap<String, Class<? extends RequestHandler>> handlers = new HashMap<String, Class<? extends RequestHandler>>();
     57    private static Map<String, Class<? extends RequestHandler>> handlers = new TreeMap<String, Class<? extends RequestHandler>>();
    5258
    5359    /**
     
    174180
    175181            // find a handler for this command
    176             Class<? extends RequestHandler> handlerClass = handlers
    177                     .get(command);
     182            Class<? extends RequestHandler> handlerClass = handlers.get(command);
    178183            if (handlerClass == null) {
    179184                // no handler found
    180                 sendBadRequest(out);
     185                StringBuilder usage = new StringBuilder(1024);
     186                for (Entry<String, Class<? extends RequestHandler>> handler : handlers.entrySet()) {
     187                    String[] mandatory = handler.getValue().newInstance().getMandatoryParams();
     188                    usage.append("<li>");
     189                    usage.append(handler.getKey());
     190                    if (mandatory != null) {
     191                        usage.append("<br/>mandatory parameter: ").append(Utils.join(", ", Arrays.asList(mandatory)));
     192                    }
     193                    usage.append("</li>");
     194                }
     195                String help = "No command specified! The following commands are available:<ul>"
     196                        + usage.toString()
     197                        + "</ul>";
     198                sendBadRequest(out, help);
    181199            } else {
    182200                // create handler object
     
    196214                    sendError(out);
    197215                } catch (RequestHandlerBadRequestException ex) {
    198                     sendBadRequest(out);
     216                    sendBadRequest(out, ex.getMessage());
    199217                } catch (RequestHandlerForbiddenException ex) {
    200218                    sendForbidden(out);
     
    282300     *             If the error can not be written
    283301     */
    284     private void sendBadRequest(Writer out) throws IOException {
     302    private void sendBadRequest(Writer out, String help) throws IOException {
    285303        sendHeader(out, "400 Bad Request", "text/html", true);
    286304        out.write("<HTML>\r\n");
     
    289307        out.write("<BODY>");
    290308        out.write("<H1>HTTP Error 400: Bad Request</h2>\r\n");
     309        if (help != null) {
     310            out.write(help);
     311        }
    291312        out.write("</BODY></HTML>\r\n");
    292313        out.flush();
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r4833 r4834  
    2727
    2828    @Override
    29     protected String[] getMandatoryParams()
     29    public String[] getMandatoryParams()
    3030    {
    3131        return new String[] { "lat", "lon" };
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r4833 r4834  
    2222
    2323    @Override
    24     protected String[] getMandatoryParams() {
     24    public String[] getMandatoryParams() {
    2525        return new String[]{"way"};
    2626    }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java

    r3720 r4834  
    2626
    2727    @Override
    28     protected String[] getMandatoryParams()
     28    public String[] getMandatoryParams()
    2929    {
    3030        return new String[] { "url" };
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImportHandler.java

    r3707 r4834  
    3232
    3333    @Override
    34     protected String[] getMandatoryParams()
     34    public String[] getMandatoryParams()
    3535    {
    3636        return new String[] { "url" };
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java

    r4795 r4834  
    5050
    5151    @Override
    52     protected String[] getMandatoryParams()
     52    public String[] getMandatoryParams()
    5353    {
    5454        return new String[] { "bottom", "top", "left", "right" };
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadObjectHandler.java

    r4833 r4834  
    2020
    2121    @Override
    22     protected String[] getMandatoryParams() {
     22    public String[] getMandatoryParams() {
    2323        return new String[]{"objects"};
    2424    }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r4191 r4834  
    55
    66import java.util.HashMap;
     7import java.util.LinkedList;
     8import java.util.List;
    79import java.util.StringTokenizer;
    810
     
    1113import org.openstreetmap.josm.Main;
    1214import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     15import org.openstreetmap.josm.tools.Utils;
    1316
    1417/**
     
    9194    }
    9295
    93     protected String[] getMandatoryParams()
    94     {
     96    public String[] getMandatoryParams() {
    9597        return null;
    9698    }
     
    169171        if(mandatory == null) return;
    170172
     173        List<String> missingKeys = new LinkedList<String>();
    171174        boolean error = false;
    172175        for (int i = 0; i < mandatory.length; ++i) {
     
    176179                error = true;
    177180                System.out.println("'" + myCommand + "' remote control request must have '" + key + "' parameter");
     181                missingKeys.add(key);
    178182            }
    179183        }
    180         if (error)
    181             throw new RequestHandlerBadRequestException();
     184        if (error) {
     185            throw new RequestHandlerBadRequestException(
     186                    "The following keys are mandatory, but have not been provided: "
     187                    + Utils.join(", ", missingKeys));
     188        }
    182189    }
    183190
     
    204211
    205212    public static class RequestHandlerException extends Exception {
     213
     214        public RequestHandlerException(String message) {
     215            super(message);
     216        }
     217
     218        public RequestHandlerException() {
     219        }
    206220    }
    207221
     
    210224
    211225    public static class RequestHandlerBadRequestException extends RequestHandlerException {
     226
     227        public RequestHandlerBadRequestException(String message) {
     228            super(message);
     229        }
    212230    }
    213231   
Note: See TracChangeset for help on using the changeset viewer.