Beispiel #1
0
  /**
   * 设置Action类,已有的Action类将被覆盖
   *
   * @param path 拦截路径(必须以"/"开头)
   * @param action Action类
   */
  public static void setAction(String path, Action action) {
    if (StrUtil.isBlank(path)) {
      path = StrUtil.SLASH;
    }

    if (null == action) {
      log.warn("Added blank action, pass it.");
      return;
    }
    // 所有路径必须以 "/" 开头,如果没有则补全之
    if (false == path.startsWith(StrUtil.SLASH)) {
      path = StrUtil.SLASH + path;
    }

    ServerSetting.actionMap.put(path, action);
  }
Beispiel #2
0
  /**
   * 创建ServerAddress对象,会读取配置文件中的相关信息
   *
   * @param group 分组,如果为null默认为无分组
   * @return ServerAddress
   */
  private ServerAddress createServerAddress(String group) {
    if (setting == null) {
      throw new UtilException(
          StrUtil.format(
              "Please indicate setting file or create default [{}], and define group [{}]",
              MONGO_CONFIG_PATH,
              group));
    }

    if (group == null) {
      group = StrUtil.EMPTY;
    }

    String tmpHost = setting.getString("host", group);
    if (StrUtil.isBlank(tmpHost)) {
      throw new NotInitedException("Host name is empy of group: " + group);
    }

    final int defaultPort = setting.getInt("port", group, 27017);
    return new ServerAddress(NetUtil.buildInetSocketAddress(tmpHost, defaultPort));
  }
Beispiel #3
0
  /**
   * 用于签名的字符串
   *
   * @param bucket Bucket
   * @param key 文件key
   * @param contentMd5 内容的md5值
   * @param date 日期
   * @param request 请求对象
   * @return 用于签名的字符串
   */
  public static String strToSign(
      String bucket, String key, String contentMd5, String date, HttpRequest request) {
    String contentType = request.contentType();
    if (StrUtil.isBlank(contentType)) {
      contentType = "text/plain";
      request.contentType(contentType);
      log.warn("Content-Type header is empty, use default Content-Type: {}", contentType);
    }

    return StrUtil.builder()
        .append(request.method())
        .append("\n")
        .append(StrUtil.nullToEmpty(contentMd5))
        .append("\n")
        .append(contentType)
        .append("\n")
        .append(StrUtil.nullToEmpty(date))
        .append("\n")
        .append(canonicalizedUcloudHeaders(request))
        // canonicalizedUcloudHeaders尾部带一个换行符
        .append(canonicalizedResource(bucket, key))
        .toString();
  }
Beispiel #4
0
 /**
  * 获得路径对应的Action
  *
  * @param path 路径,为空时将获得 根目录对应的Action
  * @return Action
  */
 public static Action getAction(String path) {
   if (StrUtil.isBlank(path)) {
     path = StrUtil.SLASH;
   }
   return getActionMap().get(path.trim());
 }
Beispiel #5
0
 /**
  * 获得路径对应的Filter
  *
  * @param path 路径,为空时将获得 根目录对应的Action
  * @return Filter
  */
 public static Filter getFilter(String path) {
   if (StrUtil.isBlank(path)) {
     path = StrUtil.SLASH;
   }
   return getFilterMap().get(path.trim());
 }
Beispiel #6
0
  /**
   * 构件MongoDB连接选项<br>
   *
   * @param group 分组,当分组对应的选项不存在时会读取根选项,如果也不存在使用默认值
   * @return Builder
   */
  private Builder buildMongoClientOptions(Builder builder, String group) {
    if (setting == null) {
      return builder;
    }

    if (group == null) {
      group = StrUtil.EMPTY;
    } else {
      group = group + StrUtil.DOT;
    }

    // 每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住
    Integer connectionsPerHost = setting.getInt(group + "connectionsPerHost");
    if (StrUtil.isBlank(group) == false && connectionsPerHost == null) {
      connectionsPerHost = setting.getInt("connectionsPerHost");
    }
    if (connectionsPerHost != null) {
      builder.connectionsPerHost(connectionsPerHost);
      log.debug("MongoDB connectionsPerHost: {}", connectionsPerHost);
    }

    // multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is
    // 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more
    // than that and an exception will be throw --int
    Integer threadsAllowedToBlockForConnectionMultiplier =
        setting.getInt(group + "threadsAllowedToBlockForConnectionMultiplier");
    if (StrUtil.isBlank(group) == false && threadsAllowedToBlockForConnectionMultiplier == null) {
      threadsAllowedToBlockForConnectionMultiplier =
          setting.getInt("threadsAllowedToBlockForConnectionMultiplier");
    }
    if (threadsAllowedToBlockForConnectionMultiplier != null) {
      builder.threadsAllowedToBlockForConnectionMultiplier(
          threadsAllowedToBlockForConnectionMultiplier);
      log.debug(
          "MongoDB threadsAllowedToBlockForConnectionMultiplier: {}",
          threadsAllowedToBlockForConnectionMultiplier);
    }

    // 被阻塞线程从连接池获取连接的最长等待时间(ms) --int
    Integer connectTimeout = setting.getInt(group + "connectTimeout");
    if (StrUtil.isBlank(group) == false && connectTimeout == null) {
      setting.getInt("connectTimeout");
    }
    if (connectTimeout != null) {
      builder.connectTimeout(connectTimeout);
      log.debug("MongoDB connectTimeout: {}", connectTimeout);
    }

    // 套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
    Integer socketTimeout = setting.getInt(group + "socketTimeout");
    if (StrUtil.isBlank(group) == false && socketTimeout == null) {
      setting.getInt("socketTimeout");
    }
    if (socketTimeout != null) {
      builder.socketTimeout(socketTimeout);
      log.debug("MongoDB socketTimeout: {}", socketTimeout);
    }

    // This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE). defaults to
    // false --boolean
    Boolean socketKeepAlive = setting.getBool(group + "socketKeepAlive");
    if (StrUtil.isBlank(group) == false && socketKeepAlive == null) {
      socketKeepAlive = setting.getBool("socketKeepAlive");
    }
    if (socketKeepAlive != null) {
      builder.socketKeepAlive(socketKeepAlive);
      log.debug("MongoDB socketKeepAlive: {}", socketKeepAlive);
    }

    return builder;
  }