示例#1
0
  /**
   * Runs the command and builds the appropriate response
   *
   * @param context - the context to use for this command
   * @throws CommandSpecException
   */
  @Override
  protected void runInternal(CommandContext context) {
    final Context geogig = this.getCommandLocator(context);

    ConfigOp command = geogig.command(ConfigOp.class).setScope(ConfigScope.LOCAL);

    final ConfigAction action;
    if (context.getMethod() == Method.POST) {
      checkArgument(name != null, "You must specify the key when setting a config key.");
      checkArgument(value != null, "You must specify the value when setting a config key.");
      action = ConfigAction.CONFIG_SET;
      command.setName(name);
      command.setValue(value);
    } else {
      if (name == null) {
        action = ConfigAction.CONFIG_LIST;
      } else {
        action = ConfigAction.CONFIG_GET;
        command.setName(name);
      }
    }

    command.setAction(action);

    final Optional<Map<String, String>> results = command.call();
    context.setResponseContent(
        new CommandResponse() {
          @Override
          public void write(ResponseWriter out) throws Exception {
            out.start();
            if (results.isPresent()) {
              if (action == ConfigAction.CONFIG_LIST) {
                Iterator<Map.Entry<String, String>> it = results.get().entrySet().iterator();
                while (it.hasNext()) {
                  Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
                  out.getWriter().writeStartElement("config");
                  out.writeElement("name", pairs.getKey());
                  out.writeElement("value", pairs.getValue());
                  out.getWriter().writeEndElement();
                }
              } else if (action == ConfigAction.CONFIG_GET) {
                out.writeElement("value", results.get().get(name));
              }
            }
            out.finish();
          }
        });
  }