コード例 #1
0
ファイル: MongoDS.java プロジェクト: luckyLz/hutool
  /**
   * 初始化<br>
   * 设定文件中的host和端口有三种形式: --------------------- host = host:port --------------------- host = host
   * port = port --------------------- #此种形式使用MongoDB默认端口 host = host ---------------------
   */
  public synchronized void initSingle() {
    if (setting == null) {
      try {
        setting = new Setting(MONGO_CONFIG_PATH, CharsetUtil.UTF_8, true);
      } catch (Exception e) {
        // 在single模式下,可以没有配置文件。
      }
    }

    String group = StrUtil.EMPTY;
    if (serverAddress == null) {
      if (groups != null && groups.length == 1) {
        group = groups[0];
      }
      serverAddress = createServerAddress(group);
    }
    try {
      mongo = new MongoClient(serverAddress, buildMongoClientOptions(group));
    } catch (Exception e) {
      throw new UtilException(
          StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e);
    }

    log.info("Init MongoDB pool with connection to [{}]", serverAddress);
  }
コード例 #2
0
 /**
  * 根目录
  *
  * @param root 根目录绝对路径
  */
 public static void setRoot(File root) {
   if (root.exists() == false) {
     root.mkdirs();
   } else if (root.isDirectory() == false) {
     throw new ServerSettingException(StrUtil.format("{} is not a directory!", root.getPath()));
   }
   ServerSetting.root = root;
 }
コード例 #3
0
ファイル: Auth.java プロジェクト: HeidCloud/ucloud-java-sdk
 /**
  * 认证字符串
  *
  * @param publicKey 公钥
  * @param signature 签名
  * @return 认证字符串
  */
 public static String authorization(String publicKey, String signature) {
   return StrUtil.builder()
       .append("UCloud ")
       .append(publicKey)
       .append(":")
       .append(signature)
       .toString();
 }
コード例 #4
0
 /**
  * 增加Action类,已有的Action类将被覆盖<br>
  * 自动读取Route的注解来获得Path路径
  *
  * @param action 带注解的Action对象
  */
 public static void setAction(Action action) {
   final Route route = action.getClass().getAnnotation(Route.class);
   if (route != null) {
     final String path = route.value();
     if (StrUtil.isNotBlank(path)) {
       setAction(path, action);
       return;
     }
   }
   throw new ServerSettingException(
       "Can not find Route annotation,please add annotation to Action class!");
 }
コード例 #5
0
ファイル: MongoDS.java プロジェクト: luckyLz/hutool
  /**
   * 创建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));
  }
コード例 #6
0
ファイル: Auth.java プロジェクト: HeidCloud/ucloud-java-sdk
  /**
   * 用于签名的字符串
   *
   * @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();
  }
コード例 #7
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);
  }
コード例 #8
0
ファイル: HttpResponse.java プロジェクト: luckyLz/hutool
  /**
   * 当返回内容为压缩内容时,解压,并去除Gzip头标识
   *
   * @return HttpResponse
   */
  private HttpResponse unzip() {
    String contentEncoding = contentEncoding();

    if (contentEncoding != null && contentEncoding().equals("gzip")) {
      if (body != null) {
        removeHeader(Header.CONTENT_ENCODING);
        try {
          ByteArrayInputStream in = new ByteArrayInputStream(StrUtil.bytes(this.body, charset));
          GZIPInputStream gzipInputStream = new GZIPInputStream(in);

          ByteArrayOutputStream out = new ByteArrayOutputStream();

          IoUtil.copy(gzipInputStream, out);

          this.body = out.toString(charset);
        } catch (IOException ioex) {
          throw new HttpException(ioex);
        }
      }
    }
    return this;
  }
コード例 #9
0
ファイル: Auth.java プロジェクト: HeidCloud/ucloud-java-sdk
  /**
   * 用于签名的标准Ucloud头信息字符串,尾部带换行符
   *
   * @param request Http请求
   * @return 用于签名的Ucloud头信息字符串
   */
  public static String canonicalizedUcloudHeaders(HttpRequest request) {
    Param param = Param.create();

    Map<String, String[]> headers = request.headers();
    String[] value;
    for (Entry<String, String[]> headerEntry : headers.entrySet()) {
      String name = headerEntry.getKey().toLowerCase();
      if (name.startsWith("x-ucloud-")) {
        value = headerEntry.getValue();
        if (value != null && value.length > 0) {
          param.set(name, value[0]);
        }
      }
    }

    StringBuilder builder = StrUtil.builder();
    for (Entry<String, Object> entry : param.entrySet()) {
      builder.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
    }

    return builder.toString();
  }
コード例 #10
0
ファイル: StatefulException.java プロジェクト: luckyLz/hutool
 public StatefulException(String messageTemplate, Object... params) {
   super(StrUtil.format(messageTemplate, params));
 }
コード例 #11
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());
 }
コード例 #12
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());
 }
コード例 #13
0
ファイル: SettingException.java プロジェクト: luckyLz/hutool
 public SettingException(Throwable throwable, String messageTemplate, Object... params) {
   super(StrUtil.format(messageTemplate, params), throwable);
 }
コード例 #14
0
ファイル: HttpResponse.java プロジェクト: luckyLz/hutool
 /**
  * 获取响应流字节码
  *
  * @return byte[]
  */
 public byte[] bodyBytes() {
   if (body == null) {
     return null;
   }
   return StrUtil.bytes(body, charset);
 }
コード例 #15
0
ファイル: MongoDS.java プロジェクト: luckyLz/hutool
  /**
   * 构件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;
  }
コード例 #16
0
ファイル: Auth.java プロジェクト: HeidCloud/ucloud-java-sdk
 /**
  * 用于签名的标准资源字符串
  *
  * @param bucket Bucket
  * @param key 文件的key(在Ufile中的文件名)
  * @return 标准资源字符串
  */
 public static String canonicalizedResource(String bucket, String key) {
   return StrUtil.builder().append("/").append(bucket).append("/").append(key).toString();
 }