// Path: udder/src/main/java/com/coillighting/udder/util/CollectionUtil.java
// public class CollectionUtil {
// 
//     // TODO see if there are already Boon equivalents for these - there likely are.
//     public static <T extends Comparable<? super T>> List<T> sorted(Collection<T> c) {
//         ArrayList<T> list = new ArrayList<T>(c);
//         Collections.sort(list);
//         return list;
//     }
// 
// }
// 
// Path: udder/src/main/java/com/coillighting/udder/util/StringUtil.java
// public class StringUtil {
// 
//     // TODO see if there are already Boon equivalents for these - there likely are.
//     /** String.join is available only with Java 1.8. We support 1.7.
//      *
//      *  Format a list of items as strings, separated by the given conjunction.
//      */
//     static public String join(List<? extends Object> items, String conjunction) {
//         StringBuilder sb = new StringBuilder();
//         boolean first = true;
//         for (Object x : items) {
//             if (first) {
//                 first = false;
//             } else {
//                 sb.append(conjunction);
//             }
//             if(x == null) {
//                 sb.append("<null>");
//             } else {
//                 sb.append(x.toString());
//             }
//         }
//         return sb.toString();
//     }
// 
//     /** Draw an ASCII slider representing the value of x in range [0..1.0]. */
//     public static final String plot1D(double x) {
//         if(x < 0.0) {
//             return "[< MIN ERR ----------------------------------------] " + x;
//         } else if(x > 1.0) {
//             return "[---------------------------------------- ERR MAX >] " + x;
//         } else {
//             if(x <= 0.01) {
//                 x = 0.0;
//             } else if(x >= 0.99) {
//                 x = 1.0;
//             }
//             int xi = (int)(x * 50.999999999);
//             StringBuffer sb = new StringBuffer(80);
//             sb.append('[');
//             for(int i=0; i<xi; i++) {
//                 sb.append('-');
//             }
//             sb.append('|');
//             for(int i=xi+1; i<=50; i++) {
//                 sb.append('-');
//             }
//             sb.append(']').append(' ').append(x);
//             return sb.toString();
//         }
//     }
// 
// }


import java.io.*;
import java.net.URLDecoder;
import java.util.Map;
import java.util.Queue;
import org.boon.json.JsonFactory;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.Path;
import org.simpleframework.http.Query;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.Status;
import static org.boon.Exceptions.SoftenedException;
import com.coillighting.udder.util.CollectionUtil;
import com.coillighting.udder.util.StringUtil;
import static com.coillighting.udder.util.LogUtil.log;
        String responseBody = "TODO GET " + index;
        this.respond(response, responseBody);
    }

    private void handlePost(Request request, Response response) {
        int index = this.requestIndex;
        ++this.requestIndex; // Increment before any possible exception.

        String responseBody;
        try {
            Command command = this.createCommand(request);

            if(command == null) {
                throw new NullPointerException("Unreachable code: comand is null.");
            } else {
                boolean accepted = this.queue.offer(command);
                if(accepted) {
                    response.setStatus(Status.OK);
                    responseBody = "OK " + index;
                    if(this.debug) log(command.toString() + ' ' + responseBody);
                } else {
                    // For some reason, Status doesn't know about RFC 6585.
                    response.setCode(429);
                    response.setDescription("Too Many Requests");
                    responseBody = "DROPPED " + index;
                    if(this.debug) log("Request " + index + " for " + command + " dropped. No room in queue.");
                }
            }

        } catch (RoutingException e) {
// Path: udder/src/main/java/com/coillighting/udder/util/CollectionUtil.java
// public class CollectionUtil {
// 
//     // TODO see if there are already Boon equivalents for these - there likely are.
//     public static <T extends Comparable<? super T>> List<T> sorted(Collection<T> c) {
//         ArrayList<T> list = new ArrayList<T>(c);
//         Collections.sort(list);
//         return list;
//     }
// 
// }
// 
// Path: udder/src/main/java/com/coillighting/udder/util/StringUtil.java
// public class StringUtil {
// 
//     // TODO see if there are already Boon equivalents for these - there likely are.
//     /** String.join is available only with Java 1.8. We support 1.7.
//      *
//      *  Format a list of items as strings, separated by the given conjunction.
//      */
//     static public String join(List<? extends Object> items, String conjunction) {
//         StringBuilder sb = new StringBuilder();
//         boolean first = true;
//         for (Object x : items) {
//             if (first) {
//                 first = false;
//             } else {
//                 sb.append(conjunction);
//             }
//             if(x == null) {
//                 sb.append("<null>");
//             } else {
//                 sb.append(x.toString());
//             }
//         }
//         return sb.toString();
//     }
// 
//     /** Draw an ASCII slider representing the value of x in range [0..1.0]. */
//     public static final String plot1D(double x) {
//         if(x < 0.0) {
//             return "[< MIN ERR ----------------------------------------] " + x;
//         } else if(x > 1.0) {
//             return "[---------------------------------------- ERR MAX >] " + x;
//         } else {
//             if(x <= 0.01) {
//                 x = 0.0;
//             } else if(x >= 0.99) {
//                 x = 1.0;
//             }
//             int xi = (int)(x * 50.999999999);
//             StringBuffer sb = new StringBuffer(80);
//             sb.append('[');
//             for(int i=0; i<xi; i++) {
//                 sb.append('-');
//             }
//             sb.append('|');
//             for(int i=xi+1; i<=50; i++) {
//                 sb.append('-');
//             }
//             sb.append(']').append(' ').append(x);
//             return sb.toString();
//         }
//     }
// 
// }


// Path: udder/src/main/java/com/coillighting/udder/infrastructure/HttpServiceContainer.java
import java.io.*;
import java.net.URLDecoder;
import java.util.Map;
import java.util.Queue;
import org.boon.json.JsonFactory;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.Path;
import org.simpleframework.http.Query;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.Status;
import static org.boon.Exceptions.SoftenedException;
import com.coillighting.udder.util.CollectionUtil;
import com.coillighting.udder.util.StringUtil;
import static com.coillighting.udder.util.LogUtil.log;

        String responseBody = "TODO GET " + index;
        this.respond(response, responseBody);
    }

    private void handlePost(Request request, Response response) {
        int index = this.requestIndex;
        ++this.requestIndex; // Increment before any possible exception.

        String responseBody;
        try {
            Command command = this.createCommand(request);

            if(command == null) {
                throw new NullPointerException("Unreachable code: comand is null.");
            } else {
                boolean accepted = this.queue.offer(command);
                if(accepted) {
                    response.setStatus(Status.OK);
                    responseBody = "OK " + index;
                    if(this.debug) log(command.toString() + ' ' + responseBody);
                } else {
                    // For some reason, Status doesn't know about RFC 6585.
                    response.setCode(429);
                    response.setDescription("Too Many Requests");
                    responseBody = "DROPPED " + index;
                    if(this.debug) log("Request " + index + " for " + command + " dropped. No room in queue.");
                }
            }

        } catch (RoutingException e) {
            String routes = StringUtil.join(CollectionUtil.sorted(this.commandMap.keySet()), "\n    ");