Ejemplo n.º 1
0
  @RequestMapping(params = "method=listConfigLike", method = RequestMethod.GET)
  public String listConfigLike(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam("dataId") String dataId,
      @RequestParam("group") String group,
      @RequestParam("pageNo") int pageNo,
      @RequestParam("pageSize") int pageSize,
      ModelMap modelMap) {
    if (StringUtils.isBlank(dataId) && StringUtils.isBlank(group)) {
      modelMap.addAttribute("message", "模糊查询请至少设置一个查询参数");
      return "/admin/config/list";
    }
    Page<ConfigInfo> page = this.configService.findConfigInfoLike(pageNo, pageSize, group, dataId);

    String accept = request.getHeader("Accept");
    if (accept != null && accept.indexOf("application/json") >= 0) {
      try {
        String json = JSONUtils.serializeObject(page);
        modelMap.addAttribute("pageJson", json);
      } catch (Exception e) {
        log.error("序列化page对象出错", e);
      }
      return "/admin/config/list_json";
    } else {
      modelMap.addAttribute("page", page);
      modelMap.addAttribute("dataId", dataId);
      modelMap.addAttribute("group", group);
      modelMap.addAttribute("method", "listConfigLike");
      return "/admin/config/list";
    }
  }
Ejemplo n.º 2
0
  @Override
  public BatchHttpResult getConfigureInformationBatch(
      List<String> dataIds, String group, int timeout) {
    if (dataIds == null) {
      log.error("dataId list cannot be null,group=" + group);
      return new BatchHttpResult(HttpStatus.SC_BAD_REQUEST);
    }
    if (group == null) {
      group = Constants.DEFAULT_GROUP;
    }

    StringBuilder dataIdBuilder = new StringBuilder();
    for (String dataId : dataIds) {
      dataIdBuilder.append(dataId).append(Constants.LINE_SEPARATOR);
    }
    String dataIdStr = dataIdBuilder.toString();

    PostMethod post = new PostMethod(Constants.HTTP_URI_FILE_BATCH);
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, timeout);

    BatchHttpResult response = null;
    try {
      NameValuePair dataIdValue = new NameValuePair("dataIds", dataIdStr);
      NameValuePair groupValue = new NameValuePair("group", group);

      post.setRequestBody(new NameValuePair[] {dataIdValue, groupValue});

      httpClient
          .getHostConfiguration()
          .setHost(
              diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
              this.diamondConfigure.getPort());
      int status = httpClient.executeMethod(post);
      String responseMsg = post.getResponseBodyAsString();

      if (status == HttpStatus.SC_OK) {
        String json = null;
        try {
          json = responseMsg;

          List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
          Object resultObj =
              JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {});
          if (!(resultObj instanceof List<?>)) {
            throw new RuntimeException(
                "batch query deserialize type error, not list, json=" + json);
          }
          List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
          for (ConfigInfoEx configInfoEx : resultList) {
            configInfoExList.add(configInfoEx);
          }

          response = new BatchHttpResult(configInfoExList);
          log.info(
              "batch query success,dataIds=" + dataIdStr + ",group=" + group + ",json=" + json);
        } catch (Exception e) {
          response = new BatchHttpResult(Constants.BATCH_OP_ERROR);
          log.error(
              "batch query deserialize error,dataIdStr="
                  + dataIdStr
                  + ",group="
                  + group
                  + ",json="
                  + json,
              e);
        }

      } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
        response = new BatchHttpResult(HttpStatus.SC_REQUEST_TIMEOUT);
        log.error(
            "batch query timeout, socket timeout(ms):"
                + timeout
                + ",dataIds="
                + dataIdStr
                + ",group="
                + group);
      } else {
        response = new BatchHttpResult(status);
        log.error(
            "batch query fail, status:"
                + status
                + ", response:"
                + responseMsg
                + ",dataIds="
                + dataIdStr
                + ",group="
                + group);
      }
    } catch (HttpException e) {
      response = new BatchHttpResult(Constants.BATCH_HTTP_EXCEPTION);
      log.error("batch query http exception,dataIds=" + dataIdStr + ",group=" + group, e);
    } catch (IOException e) {
      response = new BatchHttpResult(Constants.BATCH_IO_EXCEPTION);
      log.error("batch query io exception, dataIds=" + dataIdStr + ",group=" + group, e);
    } finally {
      post.releaseConnection();
    }

    return response;
  }
Ejemplo n.º 3
0
  @RequestMapping(params = "method=batchAddOrUpdate", method = RequestMethod.POST)
  public String batchAddOrUpdate(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam("allDataIdAndContent") String allDataIdAndContent,
      @RequestParam("group") String group,
      ModelMap modelMap) {

    response.setCharacterEncoding(Constants.ENCODE);

    // 这里抛出的异常, 会产生一个500错误, 返回给sdk, sdk会将500错误记录到日志中
    if (StringUtils.isBlank(allDataIdAndContent)) {
      throw new IllegalArgumentException("批量写, allDataIdAndContent不能为空");
    }
    // group对批量操作的每一条数据都相同, 不需要在for循环里面进行判断
    if (StringUtils.isBlank(group) || DiamondUtils.hasInvalidChar(group)) {
      throw new IllegalArgumentException("批量写, group不能为空或者包含非法字符");
    }

    String[] dataIdAndContentArray = allDataIdAndContent.split(Constants.LINE_SEPARATOR);
    group = group.trim();

    List<ConfigInfoEx> configInfoExList = new ArrayList<ConfigInfoEx>();
    for (String dataIdAndContent : dataIdAndContentArray) {
      String dataId =
          dataIdAndContent.substring(0, dataIdAndContent.indexOf(Constants.WORD_SEPARATOR));
      String content =
          dataIdAndContent.substring(dataIdAndContent.indexOf(Constants.WORD_SEPARATOR) + 1);
      ConfigInfoEx configInfoEx = new ConfigInfoEx();
      configInfoEx.setDataId(dataId);
      configInfoEx.setGroup(group);
      configInfoEx.setContent(content);

      try {
        // 判断dataId是否包含非法字符
        if (StringUtils.isBlank(dataId) || DiamondUtils.hasInvalidChar(dataId)) {
          // 这里抛出的异常, 会在下面catch, 然后设置状态, 保证一个dataId的异常不会影响其他dataId
          throw new IllegalArgumentException("批量写, dataId不能包含非法字符");
        }
        // 判断内容是否为空
        if (StringUtils.isBlank(content)) {
          throw new IllegalArgumentException("批量写, 内容不能为空");
        }

        // 查询数据库
        ConfigInfo configInfo = this.configService.findConfigInfo(dataId, group);
        if (configInfo == null) {
          // 数据不存在, 新增
          this.configService.addConfigInfo(dataId, group, content);
          // 新增成功, 设置状态码
          configInfoEx.setStatus(Constants.BATCH_ADD_SUCCESS);
          configInfoEx.setMessage("add success");
        } else {
          // 数据存在, 更新
          this.configService.updateConfigInfo(dataId, group, content);
          // 更新成功, 设置状态码
          configInfoEx.setStatus(Constants.BATCH_UPDATE_SUCCESS);
          configInfoEx.setMessage("update success");
        }
      } catch (Exception e) {
        log.error("批量写这条数据时出错, dataId=" + dataId + ",group=" + group + ",content=" + content, e);
        // 出现异常, 设置异常状态码
        configInfoEx.setStatus(Constants.BATCH_OP_ERROR);
        configInfoEx.setMessage("batch write error: " + e.getMessage());
      }
      configInfoExList.add(configInfoEx);
    }

    String json = null;
    try {
      json = JSONUtils.serializeObject(configInfoExList);
    } catch (Exception e) {
      log.error("批量写, 结果序列化出错, json=" + json, e);
    }
    modelMap.addAttribute("json", json);

    return "/admin/config/batch_result";
  }
Ejemplo n.º 4
0
  @RequestMapping(params = "method=batchQuery", method = RequestMethod.POST)
  public String batchQuery(
      HttpServletRequest request,
      HttpServletResponse response,
      @RequestParam("dataIds") String dataIds,
      @RequestParam("group") String group,
      ModelMap modelMap) {

    response.setCharacterEncoding(Constants.ENCODE);

    // 这里抛出的异常, 会产生一个500错误, 返回给sdk, sdk会将500错误记录到日志中
    if (StringUtils.isBlank(dataIds)) {
      throw new IllegalArgumentException("批量查询, dataIds不能为空");
    }
    // group对批量操作的每一条数据都相同, 不需要在for循环里面进行判断
    if (StringUtils.isBlank(group)) {
      throw new IllegalArgumentException("批量查询, group不能为空或者包含非法字符");
    }

    // 分解dataId
    String[] dataIdArray = dataIds.split(Constants.WORD_SEPARATOR);
    group = group.trim();

    List<ConfigInfoEx> configInfoExList = new ArrayList<ConfigInfoEx>();
    for (String dataId : dataIdArray) {
      ConfigInfoEx configInfoEx = new ConfigInfoEx();
      configInfoEx.setDataId(dataId);
      configInfoEx.setGroup(group);
      configInfoExList.add(configInfoEx);
      try {
        if (StringUtils.isBlank(dataId)) {
          configInfoEx.setStatus(Constants.BATCH_QUERY_NONEXISTS);
          configInfoEx.setMessage("dataId is blank");
          continue;
        }

        // 查询数据库
        ConfigInfo configInfo = this.configService.findConfigInfo(dataId, group);
        if (configInfo == null) {
          // 没有异常, 说明查询成功, 但数据不存在, 设置不存在的状态码
          configInfoEx.setStatus(Constants.BATCH_QUERY_NONEXISTS);
          configInfoEx.setMessage("query data does not exist");
        } else {
          // 没有异常, 说明查询成功, 而且数据存在, 设置存在的状态码
          String content = configInfo.getContent();
          configInfoEx.setContent(content);
          configInfoEx.setStatus(Constants.BATCH_QUERY_EXISTS);
          configInfoEx.setMessage("query success");
        }
      } catch (Exception e) {
        log.error("批量查询, 在查询这个dataId时出错, dataId=" + dataId + ",group=" + group, e);
        // 出现异常, 设置异常状态码
        configInfoEx.setStatus(Constants.BATCH_OP_ERROR);
        configInfoEx.setMessage("query error: " + e.getMessage());
      }
    }

    String json = null;
    try {
      json = JSONUtils.serializeObject(configInfoExList);
    } catch (Exception e) {
      log.error("批量查询结果序列化出错, json=" + json, e);
    }
    modelMap.addAttribute("json", json);

    return "/admin/config/batch_result";
  }