/** GET /xxxCommand?xxx=yyyyy HTTP/1.1 */
  protected static CommandRequest parse(String url) throws Exception {

    CommandRequest request = new CommandRequest();

    if (StringUtils.isEmpty(url)) {
      return request;
    }
    int start = url.indexOf('/');
    int ask = url.indexOf('?') == -1 ? url.lastIndexOf(' ') : url.indexOf('?');
    int space = url.lastIndexOf(' ');
    String target = url.substring(start != -1 ? start + 1 : 0, ask != -1 ? ask : url.length());
    request.setCommand(target);
    if (ask == -1 || ask == space) {
      return request;
    }

    String paramStr = url.substring(ask + 1, space != -1 ? space : url.length());

    for (String param : paramStr.split("&")) {
      if (StringUtils.isEmpty(param)) {
        continue;
      }
      String[] kvPair = param.split("=");
      if (kvPair.length != 2) {
        continue;
      }

      String key = StringUtils.trim(kvPair[0]);
      String value = StringUtils.trim(kvPair[1]);
      value = URLDecoder.decode(value, "UTF-8");

      request.addParam(key, value);
    }
    return request;
  }
  public void registerCommand(String command, CommandProcessor processor) {

    if (StringUtils.isEmpty(command)) {
      return;
    }
    processorMap.put(command, processor);
  }
    @Override
    public void run() {

      BufferedReader in = null;
      PrintWriter out = null;

      try {
        in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
        OutputStream outputStream = socket.getOutputStream();
        out = new PrintWriter(outputStream);

        String line = in.readLine();
        CommandRequest request = CommandRequest.parse(line);

        out.print("HTTP/1.1 200 OK\r\n\r\n");
        out.flush();
        if (StringUtils.isEmpty(request.getCommand())) {
          out.println("Command is blank");
          out.flush();
          return;
        }

        CommandProcessor commandProcessor = getCommandProcessor(request.getCommand());
        if (commandProcessor != null) {
          commandProcessor.execute(outputStream, request);
        } else {
          out.println("Can not find the command:[" + request.getCommand() + "]");
        }
        out.flush();

      } catch (Throwable t) {
        LOGGER.error("EventRunnable error", t);

        try {
          if (out != null) {
            out.println("CommandCenter error, message is " + t.getMessage());
            out.flush();
          }
        } catch (Exception e) {
          LOGGER.error("EventRunnable error", t);
        }

      } finally {
        try {
          if (out != null) {
            out.close();
          }
          if (in != null) {
            in.close();
          }
          socket.close();
        } catch (Exception e) {
          LOGGER.error("EventRunnable close resource error", e);
        }
      }
    }
 public String getParam(String key, String defaultValue) {
   if (params != null) {
     String value = params.get(key);
     if (StringUtils.isEmpty(value)) {
       return defaultValue;
     }
     return value;
   }
   return null;
 }
 public static <T> T parseObject(String json, Type type) {
   Object object = null;
   if (StringUtils.isEmpty(json)) {
     throw new JSONException("illegal json: json is empty");
   }
   json = json.trim();
   if (json.startsWith("{")) {
     object = new JSONObject(json);
   } else if (json.startsWith("[")) {
     object = new JSONArray(json);
   } else {
     throw new JSONException("illegal json:" + json);
   }
   return JSONParser.parse(object, type);
 }