Authored by qinchao

去掉不用代码

Showing 52 changed files with 0 additions and 4765 deletions

Too many changes to show.

To preserve performance only 52 of 52+ files are displayed.

package com.monitor.qcloudtools.constant;
/**
* 常量
* Created by hui.xu on 2017/5/8.
*/
public class QcloudConstant {
/**
* 用于标识 API 调用者身份
*/
public static final String QCLOUD_SECRETID = "AKIDKXZR8e2JNGhy9AogEdZKuJXaSAot7X7W";
/**
* 用于加密签名字符串和服务器端验证签名字符串的密钥
*/
public static final String QCLOUD_SECRETKEY = "Mhuw11qY5KPK5fBYxDHAsQnzzpU4Xqzx";
/**
* 实例所在的区域
* bj:北京
* gz:广州
* sh:上海
* hk:香港
* ca:北美
* sg:新加坡
* shjr:上海金融
* szjr:深圳金融
* gzopen:广州open专区
*/
public static final String QCLOUD_REGION = "bj";
/**
* 查询伸缩组列表URL
*/
public static final String QCLOUD_API_URL_SCALING = "https://scaling.api.qcloud.com/v2/index.php";
/**
* 查询伸缩组列表 - 函数名称
*/
public static final String QCLOUD_API_SCALING_GROUP_FUN = "DescribeScalingGroup";
/**
* 查询伸缩组绑定的云服务器 - 函数名称
*/
public static final String QCLOUD_API_SCALING_INSTANCE_FUN = "DescribeScalingInstance";
/**
* 查看实例列表 - 函数名称
*/
public static final String QCLOUD_API_INSTANCE_FUN = "DescribeInstances";
/**
* 修改伸缩组信息 - 函数名称
*/
public static final String QCLOUD_API_MODIFY_SCALING_GROUP_FUN = "ModifyScalingGroup";
/**
* 查询伸缩活动 - 函數名稱
*/
public static final String QCLOUD_API_DESCRIBE_SCALING_ACTIVITY_FUN = "DescribeScalingActivity";
/**
* 查询负载均衡实例列表
*/
public static final String QCLOUD_API_DESCRIBE_LOAD_BALANCERS = "DescribeLoadBalancers";
/**
* 获取负载均衡监听器列表
*/
public static final String QCLOUD_API_Describe_Load_BalancerListeners = "DescribeLoadBalancerListeners";
/**
* 获取负载均后台绑定的实例列表
*/
public static final String QCLOUD_API_Describe_Load_BalancerBackends = "DescribeLoadBalancerBackends";
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingGroup
* 伸缩组Data json
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudAutoScalingData {
/**
* 伸缩组信息的集合
*/
private List<QcloudAutoScalingGroup> scalingGroupSet;
/**
* 伸缩组数量
*/
private int totalCount;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingGroup
* 伸缩组信息
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudAutoScalingGroup {
/**
* 查询到的伸缩组ID
*/
private String scalingGroupId;
/**
* 查询到的伸缩组名称
*/
private String scalingGroupName;
/**
* 该伸缩组对应的伸缩配置ID
*/
private String scalingConfigurationId;
/**
* 该伸缩组对应的伸缩配置名称
*/
private String scalingConfigurationName;
/**
* 该伸缩组所设的最小伸缩数,即该伸缩组中CVM实例的最小值
*/
private int minSize;
/**
* 该伸缩组所设的最大伸缩数,即该伸缩组中CVM实例的最大值
*/
private int maxSize;
/**
* 该伸缩组的创建时间
*/
private String createTime;
/**
* 该伸缩组现有CVM实例的数量
*/
private int instanceNum;
/**
* 该伸缩组的移除策略
* 1. 若为RemoveOldestInstance,表示移除最旧策略,即伸缩组缩容时,移除最早加入的CVM实例
* 2. 若为RemoveNewestInstance,表示移除最新策略,即伸缩组缩容时,移除最晚加入的CVM实例
*/
private String removePolicy;
/**
* 与该伸缩组绑定的各负载均衡信息
*/
private List<QcloudAutoScalingLoadBalancer> loadBalancerIdSet;
/**
* 伸缩组所属的私有网络ID
*/
private String vpcId;
/**
* 伸缩组的子网信息
*/
private List<QcloudAutoScalingSubNetId> subnetIdSet;
/**
* 伸缩组的所在地域信息
*/
private List<QcloudAutoScalingZoneId> zoneIdSet;
/**
* 伸缩组所属项目ID
*/
private int projectId;
/**
* 伸缩组是否在伸缩活动中(0:不处于伸缩活动中,1:处于伸缩活动中)
* 扩容、缩容、绑定和解绑云服务器都会触发伸缩活动,伸缩组同时只能有一个伸缩活动在进行
*/
private int bInScalingActivity;
/**
* 期望容量
*/
private int desiredCapacity;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeScalingGroup
* 伸缩组的负载均衡信息,其结构如下。若为空,表示未使用负载均衡
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudAutoScalingLoadBalancer {
/**
* 负载均衡状态
*/
private int status;
/**
* 负载均衡ID
*/
private String loadBalancerId;
/**
* 负载均衡所有者帐号
*/
private String owner;
/**
* 负载均衡所在地域
*/
private int zoneId;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeScalingGroup
* 伸缩组的子网信息,其结构如下。若为空,表示未使用子网
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudAutoScalingSubNetId {
/**
* 子网状态
*/
private int status;
/**
* 子网ID
*/
private String subnetId;
/**
* 子网所有者帐号
*/
private String owner;
/**
* 子网所在地域
*/
private int zoneId;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeScalingGroup
* 伸缩组的地域信息
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudAutoScalingZoneId {
/**
* 状态信息
*/
private int status;
/**
* 所有者帐号
*/
private String owner;
/**
* 所在地域
*/
private int zoneId;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingActivity
* 查询伸缩活动
* 注释:伸缩活动返回的参数太多,目前暂时只要必要参数
* Created by xh on 2017/5/11.
*/
@Data
public class QcloudDescribeScalingActivity {
/**
* 目前:是通过此字段判断扩容和缩容是否完毕
* 在执行过程中此字段是空,执行完毕后是success
* 原意:是想通过code来判断,可惜这个字段一直都是有值且是一直为0,没办法知道过程是否已经完毕
*/
private String msg;
/**
* 暂时放着,没什么用
*/
private String code;
/**
* 说明
*/
private String desciption;
/**
* 新扩容的实例的instanceId,用于查询实例的基本信息用
*/
private List<String> succInsList;
/**
* 活动创建时间
*/
private String createTime;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingActivity
* 伸缩组Data json
* Created by xh on 2017/5/11.
*/
@Data
public class QcloudDescribeScalingActivitySet {
/**
* 具体信息
*/
private List<QcloudDescribeScalingActivity> scalingActivitySet;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeInstances
* Created by xh on 2017/5/9.
*/
@Data
public class QcloudInstanceDiskInfo {
/**
* 硬盘ID
*/
private String storageId;
/**
* 硬盘类型
* 1.本地盘
* 2.云硬盘
* 3.SSD本地盘
* 4.SSD云盘
*/
private int storageType;
/**
* 数据盘大小(GB)
*/
private int storageSize;
/**
* 系统盘ID
*/
private String rootId;
/**
* 系统盘大小(GB)
*/
private int rootSize;
/**
* 系统盘类型
* 1.本地盘
* 2.云硬盘
* 3.SSD本地盘
* 4.SSD云盘
*/
private int rootType;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeInstances
* Created by xh on 2017/5/9.
*/
@Data
public class QcloudInstanceSet {
/**
* 实例名称
*/
private String instanceName;
/**
* 实例ID,命名格式为“ins-xxxxxxxx”
*/
private String unInstanceId;
/**
* 实例主网卡的内网IP
*/
private String lanIp;
/**
* 公网IP列表(包括实例创建时自动分配的IP和弹性IP)
*/
private String[] wanIpSet;
/**
* CPU核数
*/
private int cpu;
/**
* 内存大小(GB)
*/
private int mem;
/**
* 带宽大小(Mbps)
*/
private int bandwidth;
/**
* 镜像ID,命名格式为“img-xxxxxxxx”
*/
private String unImgId;
/**
* 当前状态
*/
private int status;
/**
* 所属地域
*/
private String Region;
/**
* 创建时间
*/
private String createTime;
/**
* 到期时间。如果是按量计费实例,则为“0000-00-00 00:00”
*/
private String deadlineTime;
/**
* 自动续费标识。
* 0:不自动续费
* 1:自动续费
* 2:不再续费
*/
private int autoRenew;
/**
* 项目ID
*/
private int projectId;
/**
* 操作系统名称
*/
private String os;
/**
* 计费模式
* 0:按月结算的后付费
* 1:包年包月
* 2:按量计费
*/
private String cvmPayMode;
/**
* 网络计费模式
* 0:按月结算的后付费
* 1:包年包月
* 2:按流量
* 3:按带宽
*/
private String networkPayMode;
/**
* 可用区ID
*/
private int zoneId;
/**
* 可用区名称
*/
private String zoneName;
/**
* 私有网络ID
*/
private String vpcId;
/**
* 子网ID
*/
private String subnetId;
/**
* 是否是VPC的网关。
* 0:否
* 1:是
*/
private int isVpcGateway;
/**
* 包含了硬盘信息的对象
*/
private List<QcloudInstanceDiskInfo> diskInfo;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* clb DescribeLoadBalancerBackends接口返回数据
* Created by xh on 2017/6/14.
*/
@Data
public class QcloudLbBackends {
/**
* 云服务器实例ID
*/
private String instanceId;
/**
* 云服务器实例ID,可以代替 instanceId 作任何操作,建议使用此ID
*/
private String unInstanceId;
/**
* 云服务器权重
*/
private int weight;
/**
* 云服务器的名称
*/
private String instanceName;
/**
* 云服务器的内网IP
*/
private String lanIp;
/**
* 云服务器的外网IP
*/
private List<String> wanIpSet;
/**
* 云服务器状态
* 1:故障,2:运行中,3:创建中,4:已关机,5:已退还,6:退还中,
* 7:重启中,8:开机中,9:关机中,10:密码重置中,11:格式化中,
* 12:镜像制作中,13:带宽设置中,14:重装系统中,19:升级中,21:热迁移中
*/
private int instanceStatus;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* clb DescribeLoadBalancers接口返回数据
* Created by xh on 2017/6/13.
*/
@Data
public class QcloudLbInstances {
/**
* 负载均衡实例的唯一ID
*/
private String loadBalancerId;
/**
* 负载均衡实例的统一唯一ID
*/
private String unLoadBalancerId;
/**
* 负载均衡实例的名称
*/
private String loadBalancerName;
/**
* 负载均衡实例的类型
1:公网(无日租), 2:公网(有日租), 3:内网
*/
private int loadBalancerType;
/**
* 负载均衡实例的域名,内网类型负载均衡实例没有域名
*/
private String domain;
/**
* 负载均衡实例的 VIP 列表
*/
private List<String> loadBalancerVips;
/**
* 负载均衡实例的状态,包括
* 0:创建中,1:正常运行
*/
private int status;
/**
* 负载均衡实例的创建时间
*/
private String createTime;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* clb DescribeLoadBalancerListeners接口返回数据
* Created by xh on 2017/6/14.
*/
@Data
public class QcloudLbListeners {
/**
* 负载均衡监听器ID
*/
private String unListenerId;
/**
* 负载均衡器监听端口
*/
private int loadBalancerPort;
/**
* 监听器后端转发端口
*/
private int instancePort;
/**
* 监听器协议类型
* 1:HTTP,2:TCP,3:UDP,4:HTTPS
*/
private int protocol;
/**
* 会话保持时间
*/
private int sessionExpire;
/**
* 是否开启了检查
* 1(开启)、0(关闭)
*/
private int healthSwitch;
/**
* 响应超时时间
*/
private int timeOut;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 腾讯云api通用返回对象
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudResponse {
/**
* 公共错误码, 0表示成功,其他值表示失败
*/
private int code;
/**
* 业务侧错误码。成功时返回Success
*/
private String codeDesc;
/**
* 模块错误信息描述,与接口相关
*/
private String message;
/**
* 输出结果,查询到的伸缩组列表信息
*/
private String data;
/**
* 接口:DescribeInstances 专用
* 符合条件的实例数量
*/
private int totalCount;
/**
* 接口:DescribeInstances 专用
* 实例信息列表
*/
private List<QcloudInstanceSet> instanceSet;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeScalingActivity
* 查询伸缩活动
* Created by xh on 2017/5/11.
*/
@Data
public class QcloudScalingActivitySet {
private String status;
private String code;
private String autoScalingGroupId;
private String cause;
private String hostIndex;
private String desciption;
private String startTime;
private String hostIp;
private String msg;
private String scalingPolicyId;
private String scalingActivityId;
private String endTime;
private String createTime;
private String scalingGroupId;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingGroup
* 伸缩组Data json
* Created by hui.xu on 2017/5/8.
*/
@Data
public class QcloudScalingActivitySetData {
/**
* 扩展活动组集合
*/
private List<QcloudScalingActivitySet> scalingActivitySet;
/**
* 扩展活动数量
*/
private int totalCount;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
import java.util.List;
/**
* 接口:DescribeScalingInstance
* 伸缩组实例Data json
* Created by xh on 2017/5/9.
*/
@Data
public class QcloudScalingInstancesData {
/**
* 云服务器实例信息的集合
*/
private List<QcloudScalingInstancesSet> scalingInstancesSet;
/**
* 查询到的云服务器实例数量
*/
private int totalCount;
}
\ No newline at end of file
package com.monitor.qcloudtools.model;
import lombok.Data;
/**
* 接口:DescribeScalingInstance
* 查询伸缩组绑定的云服务器
* Created by xh on 2017/5/9.
*/
@Data
public class QcloudScalingInstancesSet {
/**
* CVM实例的ID
*/
private String instanceId;
/**
* 实例健康状态。若为Healthy,表示健康;若为UnHealthy,表示不健康
*/
private String healthStatus;
/**
* 实例类型。若为Auto,表示是伸缩组自动创建的实例;若为Manual,表示是用户手动创建的实例
*/
private String creationType;
/**
* 实例在伸缩组中的生命周期状态,有以下几种情况:
* Creating:创建中
* InService:运行中
* Removing:移除中
* Attaching:绑定中
* Detaching:解绑中
* Backuping:备份中
* UnBackuping:解除备份中
* AttachLb:绑定LB中
* DetachLb:解绑LB中
* Preheating:预热中
*/
private String lifeCycleState;
/**
* 移除保护标志位。1:处于移除保护中,0:没有处于移除保护中
*/
private int protectedFromScaleIn;
/**
* 该实例加入伸缩组的时间
*/
private String addTime;
}
\ No newline at end of file
package com.monitor.qcloudtools.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.monitor.qcloudtools.constant.QcloudConstant;
import com.monitor.qcloudtools.model.QcloudResponse;
import com.qcloud.Module.Base;
import com.qcloud.QcloudApiModuleCenter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.TreeMap;
/**
* Created by hui.xu on 2017/5/8.
*/
public class QcloudSdkUtil {
public static final Logger logger = LoggerFactory.getLogger("qcloudAutoScalingLogger");
/**
* 获取腾讯云通用参数
* @return
*/
public static TreeMap<String, Object> getCommonConfig(String requestType){
TreeMap<String, Object> config = new TreeMap<>();
config.put("SecretId", QcloudConstant.QCLOUD_SECRETID);
config.put("SecretKey", QcloudConstant.QCLOUD_SECRETKEY);
config.put("RequestMethod", requestType);
config.put("DefaultRegion", QcloudConstant.QCLOUD_REGION);
return config;
}
/**
* 获取腾讯云通用数据json对象
* @param baseModule 接口实例
* @param requestType 请求类型 GET | POST
* @param actionType 接口名称
* @param params 接口请求参数
* @param isChooseRoot true:返回根节点 false:返回data节点
* 由于某些接口返回没有data属性具体参考文档
* 例如:DescribeInstances
* ModifyScalingGroup
* @return
*/
public static String getCommonData(Base baseModule, String requestType, String actionType, TreeMap<String, Object> params, boolean isChooseRoot){
QcloudResponse response;
String dataJson = null;
try{
TreeMap<String, Object> config = getCommonConfig(requestType);
QcloudApiModuleCenter module = new QcloudApiModuleCenter(baseModule, config);
String returnJson = module.call(actionType, params);
if(StringUtils.isBlank(returnJson)){
return null;
}
if(isChooseRoot){
return returnJson;
}
response = JSON.parseObject(returnJson, QcloudResponse.class);
if((null == response) || (0 != response.getCode())){
logger.error(String.format(" - QcloudSdkUtil - getCommonData - failure - code:%s - message:%s", response.getCode(), response.getMessage()));
return null;
}
dataJson = response.getData();
}catch (Exception e){
logger.error(" - QcloudSdkUtil - getCommonData - error", e);
}
return dataJson;
}
public static String getCommonData(Base baseModule, String requestType, String actionType, TreeMap<String, Object> params, String dataValue){
String dataJson = null;
String response = null;
try{
TreeMap<String, Object> config = getCommonConfig(requestType);
QcloudApiModuleCenter module = new QcloudApiModuleCenter(baseModule, config);
String returnJson = module.call(actionType, params);
if(StringUtils.isBlank(returnJson)){
return null;
}
JSONObject responseJSON = JSONObject.parseObject(returnJson);
response = responseJSON.getString(dataValue);
if(StringUtils.isBlank(response)){
logger.error(String.format(" - QcloudSdkUtil - getCommonData - failure - code:%s - respones:%s", responseJSON.getString("code"), returnJson));
return null;
}
}catch (Exception e){
logger.error(" - QcloudSdkUtil - getCommonData - error", e);
}
return response;
}
}
\ No newline at end of file
package com.monitor.qcloudtools.util;
import com.monitor.qcloudtools.constant.QcloudConstant;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Random;
/**
* 腾讯云签名工具类
* Created by hui.xu on 2017/5/8.
*/
public class SignatureUtil {
public static final Logger logger = LoggerFactory.getLogger("qcloudAutoScalingLogger");
/**
* 公共请求参数
* @param actionName 请求的api的函数名称
* @return
*/
public static String getCommReqParms(String actionName){
String returnValue = null;
try{
//实例所在区域
String region = QcloudConstant.QCLOUD_REGION;
//当前时间戳
long timestamp = System.currentTimeMillis() / 1000;
//随机的正整数
int nonce = (new Random()).nextInt();
//密钥Id
String secretId = QcloudConstant.QCLOUD_SECRETID;
//请求签名
String Signature = getSignature(actionName);
if(StringUtils.isBlank(Signature)){
return null;
}
//签名方式
String signatureMethod = "HmacSHA256";
//2.2:拼接请求字符串
returnValue = String.format("Action=%s&SecretId=%s&Region=%s&Timestamp=%s&Nonce=%s&Signature=%s&SignatureMethod=%s",
actionName, secretId, region, timestamp, nonce, Signature, signatureMethod);
}catch(Exception e){
logger.error(" - SignatureUtil - getCommReqParms - eror", e);
}
return returnValue;
}
/**
* 获取签名凭证
* @param actionName 请求的api的函数名称
* @return
*/
public static String getSignature(String actionName){
String returnValue = null;
try{
if(StringUtils.isBlank(actionName)){
return null;
}
//2.1:对参数排序组装
//密钥Id
String secretId = QcloudConstant.QCLOUD_SECRETID;
//用于加密签名字符串和服务器端验证签名字符串的密钥
String secretKey = QcloudConstant.QCLOUD_SECRETKEY;
//当前时间戳
long timestamp = System.currentTimeMillis() / 1000;
//随机的正整数
int nonce = (new Random()).nextInt();
//实例所在区域
String region = QcloudConstant.QCLOUD_REGION;
//签名方式
String signatureMethod = "HmacSHA256";
//待查询的实例ID
String instanceIds = "ins-09dx96dg";
//偏移量
int offset = 0;
//最大允许输出
int limit = 20;
//2.2:拼接请求字符串,字典序升序排列
String urlParms = String.format("Action=%s&Nonce=%s&Region=%s&SecretId=%s&SignatureMethod=%s&Timestamp=%s&instanceIds.0=%s&limit=%s&offset=%s",
actionName, nonce, region, secretId, signatureMethod, timestamp, instanceIds, limit, offset);
//2.3:拼接签名原文字符串,签名原文串的拼接规则为:请求方法 + 请求主机 +请求路径 + ? + 请求字符串
String url = String.format("GETcvm.api.qcloud.com/v2/index.php?%s",urlParms);
System.out.println("***:" + url);
//2.4:生成签名串
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec("Gu5t9xGARNpq86cd98joQYCN3Cozk1qA".getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(urlParms.getBytes()));
System.out.println("---:" + hash);
//3:签名串编码
//hash = "PSWUwVv4TfW2sQP1FT9KbbLjIZ9bmK8f1cL6fbpf3KI=";
returnValue = URLEncoder.encode(hash, "GBK");
System.out.println("~~~:" + returnValue);
}catch (Exception e){
logger.error(" - SignatureUtil - getSignature - eror", e);
}
return returnValue;
}
public static void main(String[] args) {
SignatureUtil.getSignature("DescribeInstances");
}
}
\ No newline at end of file
package com.qcloud.Common;
import com.qcloud.Utilities.MD5;
import org.springframework.util.StringUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
/**
* @brief 请求调用类
* @author robinslsun
*/
public class Request {
protected static String requestUrl = "";
protected static String rawResponse = "";
protected static String version = "SDK_JAVA_1.3";
protected static int timeOut = 1000;//设置连接主机的超时时间,单位:毫秒,可以根据实际需求合理更改 timeOut 的值。
public static String getRequestUrl() {
return requestUrl;
}
public static String getRawResponse() {
return rawResponse;
}
public static String generateUrl(TreeMap<String, Object> params,
String secretId, String secretKey, String requestMethod,
String requestHost, String requestPath) {
if (!params.containsKey("SecretId"))
params.put("SecretId", secretId);
if (!params.containsKey("Nonce"))
params.put("Nonce",
new Random().nextInt(Integer.MAX_VALUE));
if (!params.containsKey("Timestamp"))
params.put("Timestamp", System.currentTimeMillis() / 1000);
params.put("RequestClient", version);
String plainText = Sign.makeSignPlainText(params, requestMethod,
requestHost, requestPath);
String signatureMethod = "HmacSHA1";
if(params.containsKey("SignatureMethod") && params.get("SignatureMethod").toString().equals("HmacSHA256"))
{
signatureMethod = "HmacSHA256";
}
try {
params.put("Signature", Sign.sign(plainText, secretKey, signatureMethod));
} catch (Exception e) {
e.printStackTrace();
}
if (params.get("Action").toString().equals("MultipartUploadVodFile")) {
String url = "http://" + requestHost + requestPath;
url += Sign.buildParamStr1(params,requestMethod);
return url;
}
String url = "https://" + requestHost + requestPath;
if (requestMethod.equals("GET")) {
url += Sign.buildParamStr1(params,requestMethod);
}
return url;
}
public static String send(TreeMap<String, Object> params, String secretId,
String secretKey, String requestMethod, String requestHost,
String requestPath, String fileName) {
if (!params.containsKey("SecretId"))
params.put("SecretId", secretId);
if (!params.containsKey("Nonce"))
params.put("Nonce",
new Random().nextInt(Integer.MAX_VALUE));
if (!params.containsKey("Timestamp"))
params.put("Timestamp", System.currentTimeMillis() / 1000);
params.put("RequestClient", version);
params.remove("Signature");
String plainText = Sign.makeSignPlainText(params, requestMethod,
requestHost, requestPath);
String signatureMethod = "HmacSHA1";
if(params.containsKey("SignatureMethod") && params.get("SignatureMethod").toString().equals("HmacSHA256"))
{
signatureMethod = "HmacSHA256";
}
try {
params.put("Signature", Sign.sign(plainText, secretKey, signatureMethod));
} catch (Exception e) {
e.printStackTrace();
}
if (params.get("Action").toString().equals("MultipartUploadVodFile")) {
String url = "http://" + requestHost + requestPath;
return sendMultipartUploadVodFileRequest(url, params,
requestMethod, fileName);
}
String url = "https://" + requestHost + requestPath;
return sendRequest(url, params, requestMethod, fileName);
}
public static String sendRequest(String url,
Map<String, Object> requestParams, String requestMethod,
String fileName) {
String result = "";
BufferedReader in = null;
String paramStr = "";
for (String key : requestParams.keySet()) {
if(requestParams.get(key) == null){
continue;
}
if (!paramStr.isEmpty()) {
paramStr += '&';
}
try {
paramStr += key + '='
+ URLEncoder.encode(requestParams.get(key).toString(),"utf-8");
} catch (UnsupportedEncodingException e) {
result = "{\"code\":-2300,\"location\":\"com.qcloud.Common.Request:129\",\"message\":\"api sdk throw exception! "
+ e.toString() + "\"}";
}
}
try {
if (requestMethod.equals("GET")) {
if (url.indexOf('?') > 0) {
url += '&' + paramStr;
} else {
url += '?' + paramStr;
}
}
requestUrl = url;
String BOUNDARY = "---------------------------"
+ MD5.stringToMD5(
String.valueOf(System.currentTimeMillis()))
.substring(0, 15);
URL realUrl = new URL(url);
URLConnection connection = null;
if (url.toLowerCase().startsWith("https")) {
HttpsURLConnection httpsConn = (HttpsURLConnection) realUrl
.openConnection();
httpsConn.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
connection = httpsConn;
} else {
connection = realUrl.openConnection();
}
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 设置链接主机超时时间
connection.setConnectTimeout(timeOut);
if (requestMethod.equals("POST")) {
((HttpURLConnection) connection).setRequestMethod("POST");
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(
connection.getOutputStream());
StringBuffer strBuf = new StringBuffer();
for (String key : requestParams.keySet()) {
strBuf.append("\r\n").append("--").append(BOUNDARY)
.append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\""
+ key + "\"\r\n\r\n");
strBuf.append(requestParams.get(key));
}
out.write(strBuf.toString().getBytes());
if (fileName != null) {
File file = new File(fileName);
String filename = file.getName();
String contentType = URLConnection.getFileNameMap()
.getContentTypeFor(fileName);
strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(BOUNDARY)
.append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"entityFile\"; filename=\""
+ filename + "\"\r\n");
strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
out.write(strBuf.toString().getBytes());
DataInputStream ins = new DataInputStream(
new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = ins.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
ins.close();
}
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();
}
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
result = "{\"code\":-2700,\"location\":\"com.qcloud.Common.Request:225\",\"message\":\"api sdk throw exception! "
+ e.toString() + "\"}";
} finally {
// 使用finally块来关闭输入流
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
result = "{\"code\":-2800,\"location\":\"com.qcloud.Common.Request:234\",\"message\":\"api sdk throw exception! "
+ e2.toString() + "\"}";
}
}
rawResponse = result;
return result;
}
public static String sendMultipartUploadVodFileRequest(String url,
Map<String, Object> requestParams, String requestMethod,
String fileName) {
String result = "";
BufferedReader in = null;
String paramStr = "";
for (String key : requestParams.keySet()) {
if (!paramStr.isEmpty()) {
paramStr += '&';
}
try {
paramStr += key + '='
+ URLEncoder.encode(requestParams.get(key).toString(),"utf-8");
} catch (UnsupportedEncodingException e) {
result = "{\"code\":-2400,\"location\":\"com.qcloud.Common.Request:263\",\"message\":\"api sdk throw exception! "
+ e.toString() + "\"}";
}
}
try {
if (url.indexOf('?') > 0) {
url += '&' + paramStr;
} else {
url += '?' + paramStr;
}
System.out.println(url);
requestUrl = url;
// String BOUNDARY = "---------------------------" +
// MD5.stringToMD5(String.valueOf(System.currentTimeMillis())).substring(0,15);
URL realUrl = new URL(url);
URLConnection connection = null;
if (url.toLowerCase().startsWith("https")) {
HttpsURLConnection httpsConn = (HttpsURLConnection) realUrl
.openConnection();
httpsConn.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
connection = httpsConn;
} else {
connection = realUrl.openConnection();
}
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
// 设置链接主机超时时间
connection.setConnectTimeout(timeOut);
File file = new File(fileName);
long file_length = (Long) requestParams.get("fileSize");
OutputStream out = new DataOutputStream(
connection.getOutputStream());
DataInputStream ins = new DataInputStream(new FileInputStream(file));
int offset = ((Integer) requestParams.get("offset")).intValue();
int dataSize = ((Integer) requestParams.get("dataSize")).intValue();
if (offset >= file_length) {
return "{\"code\":-3001,\"location\":\"com.qcloud.Common.Request:303\",\"message\":\"api sdk throw exception! offset larger than the size of file\"}";
}
int skipBytes = ins.skipBytes(offset);
int page = dataSize / 1024;
int remainder = dataSize % 1024;
int bytes = 0;
byte[] bufferOut = new byte[1024];
byte[] bufferOut2 = new byte[remainder];
while (page != 0) {
if ((bytes = ins.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
page = page - 1;
}
if ((bytes = ins.read(bufferOut2)) != -1) {
out.write(bufferOut2, 0, bytes);
}
ins.close();
out.flush();
out.close();
// 建立实际的连接
connection.connect();
try {
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
} catch (Exception e) {
result = "{\"code\":-3002,\"location\":\"com.qcloud.Common.Request:331\",\"message\":\"api sdk throw exception! protocol doesn't support input or the character Encoding is not supported."
+ "details: " + e.toString() + "\"}";
if (in != null) {
in.close();
}
rawResponse = result;
return result;
}
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
result = "{\"code\":-3000,\"location\":\"com.qcloud.Common.Request:345\",\"message\":\"api sdk throw exception! "
+ e.toString() + "\"}";
} finally {
// 使用finally块来关闭输入流
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
result = "{\"code\":-3003,\"location\":\"com.qcloud.Common.Request:354\",\"message\":\"api sdk throw exception! "
+ e2.toString() + "\"}";
}
}
rawResponse = result;
return result;
}
}
package com.qcloud.Common;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
//import sun.misc.BASE64Encoder;
//import org.apache.commons.codec.binary.Base64;
import com.qcloud.Utilities.Base64;
import com.sun.corba.se.impl.protocol.giopmsgheaders.RequestMessage;
import org.apache.commons.lang3.StringUtils;
public class Sign {
// 编码方式
private static final String CONTENT_CHARSET = "UTF-8";
// HMAC算法
private static final String HMAC_ALGORITHM = "HmacSHA1";
/**
* @brief 签名
* @author cicerochen@tencent.com
* @date 2017-03-15 18:00:00
*
* @param signStr 被加密串
* @param secret 加密密钥
* @param signatureMethod 签名算法
*
* @return
*/
public static String sign(String signStr, String secret, String signatureMethod)
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException
{
String sig = null;
Mac mac1 = Mac.getInstance("HmacSHA1");
Mac mac2 = Mac.getInstance("HmacSHA256");
byte[] hash;
if (signatureMethod == "HmacSHA256"){
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac2.getAlgorithm());
mac2.init(secretKey);
hash = mac2.doFinal(signStr.getBytes(CONTENT_CHARSET));
}
else{
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac1.getAlgorithm());
mac1.init(secretKey);
hash = mac1.doFinal(signStr.getBytes(CONTENT_CHARSET));
}
// base64
//sig = new String(new BASE64Encoder().encode(hash).getBytes());
//sig = new String(Base64.encodeBase64(hash));
sig = new String(Base64.encode(hash));
return sig;
}
public static String makeSignPlainText(TreeMap<String, Object> requestParams, String requestMethod, String requestHost, String requestPath) {
String retStr = "";
retStr += requestMethod;
retStr += requestHost;
retStr += requestPath;
retStr += buildParamStr1(requestParams, requestMethod);
return retStr;
}
protected static String buildParamStr1(TreeMap<String, Object> requestParams, String requestMethod) {
return buildParamStr(requestParams, requestMethod);
}
protected static String buildParamStr(TreeMap<String, Object> requestParams, String requestMethod) {
String retStr = "";
for(String key: requestParams.keySet()) {
if(null == requestParams.get(key)){
continue;
}
//排除上传文件的参数
if(requestMethod == "POST" && requestParams.get(key).toString().substring(0, 1).equals("@")){
continue;
}
if (retStr.length()==0) {
retStr += '?';
} else {
retStr += '&';
}
retStr += key.replace("_", ".") + '=' + requestParams.get(key).toString();
}
return retStr;
}
}
package com.qcloud.Module;
public class Account extends Base{
public Account(){
serverHost = "account.api.qcloud.com";
}
}
package com.qcloud.Module;
import com.qcloud.Common.*;
import java.util.TreeMap;
public abstract class Base {
protected String serverHost = "";
protected String serverUri = "/v2/index.php";
protected String secretId = "";
protected String secretKey = "";
protected String defaultRegion = "";
protected String requestMethod = "GET";
public void setConfig(TreeMap<String, Object> config) {
if (config == null)
return;
for (String key : config.keySet()) {
if(key.equals("SecretId")){
setConfigSecretId(config.get(key).toString());
}
else if(key.equals("SecretKey")){
setConfigSecretKey(config.get(key).toString());
}
else if(key.equals("DefaultRegion")){
setConfigDefaultRegion(config.get(key).toString());
}
else if(key.equals("RequestMethod")){
setConfigRequestMethod(config.get(key).toString());
}
}
}
public void setConfigSecretId(String secretId) {
this.secretId = secretId;
}
public void setConfigSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public void setConfigDefaultRegion(String region) {
this.defaultRegion = region;
}
public void setConfigRequestMethod(String method) {
this.requestMethod = method;
}
public String getLastRequest() {
return Request.getRequestUrl();
}
public String getLastResponse() {
return Request.getRawResponse();
}
private String ucFirst(String word){
return word.replaceFirst(word.substring(0, 1),
word.substring(0, 1).toUpperCase());
}
public String generateUrl(String actionName, TreeMap<String, Object> params) {
actionName = ucFirst(actionName);
if(params == null)
params = new TreeMap<String, Object>();
params.put("Action", actionName);
if (!params.containsKey("Region")) {
params.put("Region", defaultRegion);
}
return Request.generateUrl(params, secretId, secretKey, requestMethod,
serverHost, serverUri);
}
public String call(String actionName, TreeMap<String, Object> params){
return call(actionName, params, null);
}
public String call(String actionName, TreeMap<String, Object> params, String fileName){
actionName = ucFirst(actionName);
if(params == null)
params = new TreeMap<String, Object>();
params.put("Action", actionName);
if (!params.containsKey("Region")) {
params.put("Region", defaultRegion);
}
String response = Request.send(params, secretId, secretKey, requestMethod, serverHost, serverUri, fileName);
return response;
}
}
package com.qcloud.Module;
public class Bill extends Base{
public Bill(){
serverHost = "bill.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Cbs extends Base {
public Cbs(){
serverHost = "cbs.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Cdb extends Base {
public Cdb(){
serverHost = "cdb.api.qcloud.com";
}
}
package com.qcloud.Module;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;
import com.qcloud.Utilities.MD5;
public class Cdn extends Base {
public Cdn(){
serverHost = "cdn.api.qcloud.com";
}
public String UploadCdnEntity(TreeMap<String, Object> params) throws NoSuchAlgorithmException, IOException {
String actionName = "UploadCdnEntity";
String entityFile = params.get("entityFile").toString();
params.remove("entityFile");
File file = new File(entityFile);
if (!file.exists()) {
throw new FileNotFoundException();
}
if (!params.containsKey("entityFileMd5")) {
params.put("entityFileMd5", MD5.fileNameToMD5(entityFile));
}
return call(actionName, params, entityFile);
}
}
\ No newline at end of file
package com.qcloud.Module;
public class Cmem extends Base {
//protected String serverHost = "cmem.api.qcloud.com";
public Cmem(){
serverHost = "cmem.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Cns extends Base {
public Cns(){
serverHost = "cns.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Cvm extends Base {
//protected String serverHost = "cvm.api.qcloud.com";
public Cvm(){
serverHost = "cvm.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Eip extends Base {
public Eip(){
serverHost = "eip.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Image extends Base {
public Image(){
serverHost = "image.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Lb extends Base {
public Lb(){
serverHost = "lb.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Live extends Base {
public Live(){
serverHost = "live.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Market extends Base {
public Market(){
serverHost = "market.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Monitor extends Base {
public Monitor(){
serverHost = "monitor.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Scaling extends Base {
public Scaling(){
serverHost = "scaling.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Sec extends Base {
public Sec(){
serverHost = "csec.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Snapshot extends Base {
public Snapshot(){
serverHost = "snapshot.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Tdsql extends Base {
//protected String serverHost = "tdsql.api.qcloud.com";
public Tdsql(){
serverHost = "tdsql.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Trade extends Base {
public Trade(){
serverHost = "trade.api.qcloud.com";
}
}
package com.qcloud.Module;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;
import com.qcloud.Utilities.SHA1;
public class Vod extends Base {
public Vod(){
serverHost = "vod.api.qcloud.com";
}
public String MultipartUploadVodFile(TreeMap<String, Object> params) throws NoSuchAlgorithmException, IOException {
serverHost = "vod.qcloud.com";
String actionName = "MultipartUploadVodFile";
String fileName = params.get("file").toString();
params.remove("file");
File f= new File(fileName);
if (!params.containsKey("fileSize")){
params.put("fileSize", f.length());
}
if (!params.containsKey("fileSha")){
params.put("fileSha", SHA1.fileNameToSHA(fileName));
}
return call(actionName, params, fileName);
}
}
package com.qcloud.Module;
public class Vpc extends Base {
public Vpc(){
serverHost = "vpc.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Wenzhi extends Base {
public Wenzhi(){
serverHost = "wenzhi.api.qcloud.com";
}
}
package com.qcloud.Module;
public class Yunsou extends Base {
public Yunsou(){
serverHost = "yunsou.api.qcloud.com";
}
}
package com.qcloud;
import java.lang.reflect.Method;
import java.util.TreeMap;
import com.qcloud.Module.Base;
/**
* @brief 模块调用类
* @author robinslsun
*
*/
public class QcloudApiModuleCenter {
private Base module;
/**
* 构造模块调用类
* @param module 实际模块实例
* @param config 模块配置参数
*/
public QcloudApiModuleCenter(Base module, TreeMap<String, Object> config){
this.module = module;
this.module.setConfig(config);
}
/**
* 生成Api调用地址
* @param actionName 模块动作名称
* @param params 模块请求参数
* @return Api调用地址
*/
public String generateUrl(String actionName, TreeMap<String, Object> params){
return module.generateUrl(actionName, params);
}
/**
* Api调用
* @param actionName 模块动作名称
* @param params 模块请求参数
* @return json字符串
* @throws Exception
*/
public String call(String actionName, TreeMap<String, Object> params) throws Exception
{
for(Method method : module.getClass().getMethods()){
if(method.getName().equals(actionName)){
try {
return (String) method.invoke(module, params);
} catch (Exception e) {
throw e;
}
}
}
return module.call(actionName, params);
}
public void setConfigSecretId(String secretId) {
module.setConfigSecretId(secretId);
}
public void setConfigSecretKey(String secretKey) {
module.setConfigSecretKey(secretKey);
}
public void setConfigDefaultRegion(String region) {
module.setConfigDefaultRegion(region);
}
public void setConfigRequestMethod(String method) {
module.setConfigRequestMethod(method);
}
}
package com.qcloud.Utilities;
import java.io.UnsupportedEncodingException;
public class Base64 {
private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };
private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1,
-1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
-1, -1 };
public static String encode(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = data.length;
int i = 0;
int b1, b2, b3;
while (i < len) {
b1 = data[i++] & 0xff;
if (i == len) {
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
sb.append("==");
break;
}
b2 = data[i++] & 0xff;
if (i == len) {
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4)
| ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
sb.append("=");
break;
}
b3 = data[i++] & 0xff;
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4)
| ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[((b2 & 0x0f) << 2)
| ((b3 & 0xc0) >>> 6)]);
sb.append(base64EncodeChars[b3 & 0x3f]);
}
return sb.toString();
}
public static byte[] decode(String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
byte[] data = str.getBytes("US-ASCII");
int len = data.length;
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
/* b1 */
do {
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1)
break;
/* b2 */
do {
b2 = base64DecodeChars[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1)
break;
sb.append((char) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
/* b3 */
do {
b3 = data[i++];
if (b3 == 61)
return sb.toString().getBytes("ISO-8859-1");
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1)
break;
sb.append((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
/* b4 */
do {
b4 = data[i++];
if (b4 == 61)
return sb.toString().getBytes("ISO-8859-1");
b4 = base64DecodeChars[b4];
} while (i < len && b4 == -1);
if (b4 == -1)
break;
sb.append((char) (((b3 & 0x03) << 6) | b4));
}
return sb.toString().getBytes("ISO-8859-1");
}
}
\ No newline at end of file
package com.qcloud.Utilities.Json;
/*
Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* A JSONArray is an ordered sequence of values. Its external text form is a
* string wrapped in square brackets with commas separating the values. The
* internal form is an object having <code>get</code> and <code>opt</code>
* methods for accessing the values by index, and <code>put</code> methods for
* adding or replacing values. The values can be any of these types:
* <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
* <code>Number</code>, <code>String</code>, or the
* <code>JSONObject.NULL object</code>.
* <p>
* The constructor can convert a JSON text into a Java object. The
* <code>toString</code> method converts to JSON text.
* <p>
* A <code>get</code> method returns a value if one can be found, and throws an
* exception if one cannot be found. An <code>opt</code> method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
* <p>
* The generic <code>get()</code> and <code>opt()</code> methods return an
* object which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and type
* coercion for you.
* <p>
* The texts produced by the <code>toString</code> methods strictly conform to
* JSON syntax rules. The constructors are more forgiving in the texts they will
* accept:
* <ul>
* <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
* before the closing bracket.</li>
* <li>The <code>null</code> value will be inserted when there is <code>,</code>
* &nbsp;<small>(comma)</small> elision.</li>
* <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
* quote)</small>.</li>
* <li>Strings do not need to be quoted at all if they do not begin with a quote
* or single quote, and if they do not contain leading or trailing spaces, and
* if they do not contain any of these characters:
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and
* if they are not the reserved words <code>true</code>, <code>false</code>, or
* <code>null</code>.</li>
* </ul>
*
* @author JSON.org
* @version 2014-05-03
*/
public class JSONArray {
/**
* The arrayList where the JSONArray's properties are kept.
*/
private final ArrayList<Object> myArrayList;
/**
* Construct an empty JSONArray.
*/
public JSONArray() {
this.myArrayList = new ArrayList<Object>();
}
/**
* Construct a JSONArray from a JSONTokener.
*
* @param x
* A JSONTokener
* @throws JSONException
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
this();
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
if (x.nextClean() != ']') {
x.back();
for (;;) {
if (x.nextClean() == ',') {
x.back();
this.myArrayList.add(JSONObject.NULL);
} else {
x.back();
this.myArrayList.add(x.nextValue());
}
switch (x.nextClean()) {
case ',':
if (x.nextClean() == ']') {
return;
}
x.back();
break;
case ']':
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
}
}
/**
* Construct a JSONArray from a source JSON text.
*
* @param source
* A string that begins with <code>[</code>&nbsp;<small>(left
* bracket)</small> and ends with <code>]</code>
* &nbsp;<small>(right bracket)</small>.
* @throws JSONException
* If there is a syntax error.
*/
public JSONArray(String source) throws JSONException {
this(new JSONTokener(source));
}
/**
* Construct a JSONArray from a Collection.
*
* @param collection
* A Collection.
*/
public JSONArray(Collection<Object> collection) {
this.myArrayList = new ArrayList<Object>();
if (collection != null) {
Iterator<Object> iter = collection.iterator();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
}
}
/**
* Construct a JSONArray from an array
*
* @throws JSONException
* If not an array.
*/
public JSONArray(Object array) throws JSONException {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
}
/**
* Get the object value associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return An object value.
* @throws JSONException
* If there is no value for the index.
*/
public Object get(int index) throws JSONException {
Object object = this.opt(index);
if (object == null) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
return object;
}
/**
* Get the boolean value associated with an index. The string values "true"
* and "false" are converted to boolean.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The truth.
* @throws JSONException
* If there is no value for the index or if the value is not
* convertible to boolean.
*/
public boolean getBoolean(int index) throws JSONException {
Object object = this.get(index);
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
}
/**
* Get the double value associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
* @throws JSONException
* If the key is not found or if the value cannot be converted
* to a number.
*/
public double getDouble(int index) throws JSONException {
Object object = this.get(index);
try {
return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.");
}
}
/**
* Get the int value associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
* @throws JSONException
* If the key is not found or if the value is not a number.
*/
public int getInt(int index) throws JSONException {
Object object = this.get(index);
try {
return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.");
}
}
/**
* Get the JSONArray associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return A JSONArray value.
* @throws JSONException
* If there is no value for the index. or if the value is not a
* JSONArray
*/
public JSONArray getJSONArray(int index) throws JSONException {
Object object = this.get(index);
if (object instanceof JSONArray) {
return (JSONArray) object;
}
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
}
/**
* Get the JSONObject associated with an index.
*
* @param index
* subscript
* @return A JSONObject value.
* @throws JSONException
* If there is no value for the index or if the value is not a
* JSONObject
*/
public JSONObject getJSONObject(int index) throws JSONException {
Object object = this.get(index);
if (object instanceof JSONObject) {
return (JSONObject) object;
}
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
}
/**
* Get the long value associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
* @throws JSONException
* If the key is not found or if the value cannot be converted
* to a number.
*/
public long getLong(int index) throws JSONException {
Object object = this.get(index);
try {
return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.");
}
}
/**
* Get the string associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return A string value.
* @throws JSONException
* If there is no string value for the index.
*/
public String getString(int index) throws JSONException {
Object object = this.get(index);
if (object instanceof String) {
return (String) object;
}
throw new JSONException("JSONArray[" + index + "] not a string.");
}
/**
* Determine if the value is null.
*
* @param index
* The index must be between 0 and length() - 1.
* @return true if the value at the index is null, or if there is no value.
*/
public boolean isNull(int index) {
return JSONObject.NULL.equals(this.opt(index));
}
/**
* Make a string from the contents of this JSONArray. The
* <code>separator</code> string is inserted between each element. Warning:
* This method assumes that the data structure is acyclical.
*
* @param separator
* A string that will be inserted between the elements.
* @return a string.
* @throws JSONException
* If the array contains an invalid number.
*/
public String join(String separator) throws JSONException {
int len = this.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i += 1) {
if (i > 0) {
sb.append(separator);
}
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
}
return sb.toString();
}
/**
* Get the number of elements in the JSONArray, included nulls.
*
* @return The length (or size).
*/
public int length() {
return this.myArrayList.size();
}
/**
* Get the optional object value associated with an index.
*
* @param index
* The index must be between 0 and length() - 1.
* @return An object value, or null if there is no object at that index.
*/
public Object opt(int index) {
return (index < 0 || index >= this.length()) ? null : this.myArrayList
.get(index);
}
/**
* Get the optional boolean value associated with an index. It returns false
* if there is no value at that index, or if the value is not Boolean.TRUE
* or the String "true".
*
* @param index
* The index must be between 0 and length() - 1.
* @return The truth.
*/
public boolean optBoolean(int index) {
return this.optBoolean(index, false);
}
/**
* Get the optional boolean value associated with an index. It returns the
* defaultValue if there is no value at that index or if it is not a Boolean
* or the String "true" or "false" (case insensitive).
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* A boolean default.
* @return The truth.
*/
public boolean optBoolean(int index, boolean defaultValue) {
try {
return this.getBoolean(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional double value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
*/
public double optDouble(int index) {
return this.optDouble(index, Double.NaN);
}
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default value.
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
try {
return this.getDouble(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional int value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
*/
public int optInt(int index) {
return this.optInt(index, 0);
}
/**
* Get the optional int value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public int optInt(int index, int defaultValue) {
try {
return this.getInt(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional JSONArray associated with an index.
*
* @param index
* subscript
* @return A JSONArray value, or null if the index has no value, or if the
* value is not a JSONArray.
*/
public JSONArray optJSONArray(int index) {
Object o = this.opt(index);
return o instanceof JSONArray ? (JSONArray) o : null;
}
/**
* Get the optional JSONObject associated with an index. Null is returned if
* the key is not found, or null if the index has no value, or if the value
* is not a JSONObject.
*
* @param index
* The index must be between 0 and length() - 1.
* @return A JSONObject value.
*/
public JSONObject optJSONObject(int index) {
Object o = this.opt(index);
return o instanceof JSONObject ? (JSONObject) o : null;
}
/**
* Get the optional long value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The value.
*/
public long optLong(int index) {
return this.optLong(index, 0);
}
/**
* Get the optional long value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public long optLong(int index, long defaultValue) {
try {
return this.getLong(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional string value associated with an index. It returns an
* empty string if there is no value at that index. If the value is not a
* string and is not null, then it is coverted to a string.
*
* @param index
* The index must be between 0 and length() - 1.
* @return A String value.
*/
public String optString(int index) {
return this.optString(index, "");
}
/**
* Get the optional string associated with an index. The defaultValue is
* returned if the key is not found.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return A String value.
*/
public String optString(int index, String defaultValue) {
Object object = this.opt(index);
return JSONObject.NULL.equals(object) ? defaultValue : object
.toString();
}
/**
* Append a boolean value. This increases the array's length by one.
*
* @param value
* A boolean value.
* @return this.
*/
public JSONArray put(boolean value) {
this.put(value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
/**
* Put a value in the JSONArray, where the value will be a JSONArray which
* is produced from a Collection.
*
* @param value
* A Collection value.
* @return this.
*/
public JSONArray put(Collection<Object> value) {
this.put(new JSONArray(value));
return this;
}
/**
* Append a double value. This increases the array's length by one.
*
* @param value
* A double value.
* @throws JSONException
* if the value is not finite.
* @return this.
*/
public JSONArray put(double value) throws JSONException {
Double d = new Double(value);
JSONObject.testValidity(d);
this.put(d);
return this;
}
/**
* Append an int value. This increases the array's length by one.
*
* @param value
* An int value.
* @return this.
*/
public JSONArray put(int value) {
this.put(new Integer(value));
return this;
}
/**
* Append an long value. This increases the array's length by one.
*
* @param value
* A long value.
* @return this.
*/
public JSONArray put(long value) {
this.put(new Long(value));
return this;
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject which
* is produced from a Map.
*
* @param value
* A Map value.
* @return this.
*/
public JSONArray put(Map<String, Object> value) {
this.put(new JSONObject(value));
return this;
}
/**
* Append an object value. This increases the array's length by one.
*
* @param value
* An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object.
* @return this.
*/
public JSONArray put(Object value) {
this.myArrayList.add(value);
return this;
}
/**
* Put or replace a boolean value in the JSONArray. If the index is greater
* than the length of the JSONArray, then null elements will be added as
* necessary to pad it out.
*
* @param index
* The subscript.
* @param value
* A boolean value.
* @return this.
* @throws JSONException
* If the index is negative.
*/
public JSONArray put(int index, boolean value) throws JSONException {
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
/**
* Put a value in the JSONArray, where the value will be a JSONArray which
* is produced from a Collection.
*
* @param index
* The subscript.
* @param value
* A Collection value.
* @return this.
* @throws JSONException
* If the index is negative or if the value is not finite.
*/
public JSONArray put(int index, Collection<Object> value) throws JSONException {
this.put(index, new JSONArray(value));
return this;
}
/**
* Put or replace a double value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index
* The subscript.
* @param value
* A double value.
* @return this.
* @throws JSONException
* If the index is negative or if the value is not finite.
*/
public JSONArray put(int index, double value) throws JSONException {
this.put(index, new Double(value));
return this;
}
/**
* Put or replace an int value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index
* The subscript.
* @param value
* An int value.
* @return this.
* @throws JSONException
* If the index is negative.
*/
public JSONArray put(int index, int value) throws JSONException {
this.put(index, new Integer(value));
return this;
}
/**
* Put or replace a long value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index
* The subscript.
* @param value
* A long value.
* @return this.
* @throws JSONException
* If the index is negative.
*/
public JSONArray put(int index, long value) throws JSONException {
this.put(index, new Long(value));
return this;
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject that
* is produced from a Map.
*
* @param index
* The subscript.
* @param value
* The Map value.
* @return this.
* @throws JSONException
* If the index is negative or if the the value is an invalid
* number.
*/
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
this.put(index, new JSONObject(value));
return this;
}
/**
* Put or replace an object value in the JSONArray. If the index is greater
* than the length of the JSONArray, then null elements will be added as
* necessary to pad it out.
*
* @param index
* The subscript.
* @param value
* The value to put into the array. The value should be a
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
* If the index is negative or if the the value is an invalid
* number.
*/
public JSONArray put(int index, Object value) throws JSONException {
JSONObject.testValidity(value);
if (index < 0) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
if (index < this.length()) {
this.myArrayList.set(index, value);
} else {
while (index != this.length()) {
this.put(JSONObject.NULL);
}
this.put(value);
}
return this;
}
/**
* Remove an index and close the hole.
*
* @param index
* The index of the element to be removed.
* @return The value that was associated with the index, or null if there
* was no value.
*/
public Object remove(int index) {
return index >= 0 && index < this.length()
? this.myArrayList.remove(index)
: null;
}
/**
* Determine if two JSONArrays are similar.
* They must contain similar sequences.
*
* @param other The other JSONArray
* @return true if they are equal
*/
public boolean similar(Object other) {
if (!(other instanceof JSONArray)) {
return false;
}
int len = this.length();
if (len != ((JSONArray)other).length()) {
return false;
}
for (int i = 0; i < len; i += 1) {
Object valueThis = this.get(i);
Object valueOther = ((JSONArray)other).get(i);
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof JSONArray) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
}
return true;
}
/**
* Produce a JSONObject by combining a JSONArray of names with the values of
* this JSONArray.
*
* @param names
* A JSONArray containing a list of key strings. These will be
* paired with the values.
* @return A JSONObject, or null if there are no names or if this JSONArray
* has no values.
* @throws JSONException
* If any of the names are null.
*/
public JSONObject toJSONObject(JSONArray names) throws JSONException {
if (names == null || names.length() == 0 || this.length() == 0) {
return null;
}
JSONObject jo = new JSONObject();
for (int i = 0; i < names.length(); i += 1) {
jo.put(names.getString(i), this.opt(i));
}
return jo;
}
/**
* Make a JSON text of this JSONArray. For compactness, no unnecessary
* whitespace is added. If it is not possible to produce a syntactically
* correct JSON text then null will be returned instead. This could occur if
* the array contains an invalid number.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, transmittable representation of the
* array.
*/
public String toString() {
try {
return this.toString(0);
} catch (Exception e) {
return null;
}
}
/**
* Make a prettyprinted JSON text of this JSONArray. Warning: This method
* assumes that the data structure is acyclical.
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @return a printable, displayable, transmittable representation of the
* object, beginning with <code>[</code>&nbsp;<small>(left
* bracket)</small> and ending with <code>]</code>
* &nbsp;<small>(right bracket)</small>.
* @throws JSONException
*/
public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
}
/**
* Write the contents of the JSONArray as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
/**
* Write the contents of the JSONArray as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @param indent
* The indention of the top level.
* @return The writer.
* @throws JSONException
*/
Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
int length = this.length();
writer.write('[');
if (length == 1) {
JSONObject.writeValue(writer, this.myArrayList.get(0),
indentFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
for (int i = 0; i < length; i += 1) {
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, newindent);
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, indent);
}
writer.write(']');
return writer;
} catch (IOException e) {
throw new JSONException(e);
}
}
}
package com.qcloud.Utilities.Json;
/**
* The JSONException is thrown by the JSON.org classes when things are amiss.
*
* @author JSON.org
* @version 2014-05-03
*/
public class JSONException extends RuntimeException {
private static final long serialVersionUID = 0;
private Throwable cause;
/**
* Constructs a JSONException with an explanatory message.
*
* @param message
* Detail about the reason for the exception.
*/
public JSONException(String message) {
super(message);
}
/**
* Constructs a new JSONException with the specified cause.
* @param cause The cause.
*/
public JSONException(Throwable cause) {
super(cause.getMessage());
this.cause = cause;
}
/**
* Returns the cause of this exception or null if the cause is nonexistent
* or unknown.
*
* @return the cause of this exception or null if the cause is nonexistent
* or unknown.
*/
@Override
public Throwable getCause() {
return this.cause;
}
}
package com.qcloud.Utilities.Json;
/*
Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
* form is a string wrapped in curly braces with colons between the names and
* values, and commas between the values and names. The internal form is an
* object having <code>get</code> and <code>opt</code> methods for accessing
* the values by name, and <code>put</code> methods for adding or replacing
* values by name. The values can be any of these types: <code>Boolean</code>,
* <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
* <code>String</code>, or the <code>JSONObject.NULL</code> object. A
* JSONObject constructor can be used to convert an external form JSON text
* into an internal form whose values can be retrieved with the
* <code>get</code> and <code>opt</code> methods, or to convert values into a
* JSON text using the <code>put</code> and <code>toString</code> methods. A
* <code>get</code> method returns a value if one can be found, and throws an
* exception if one cannot be found. An <code>opt</code> method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
* <p>
* The generic <code>get()</code> and <code>opt()</code> methods return an
* object, which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and type
* coercion for you. The opt methods differ from the get methods in that they
* do not throw. Instead, they return a specified value, such as null.
* <p>
* The <code>put</code> methods add or replace values in an object. For
* example,
*
* <pre>
* myString = new JSONObject()
* .put(&quot;JSON&quot;, &quot;Hello, World!&quot;).toString();
* </pre>
*
* produces the string <code>{"JSON": "Hello, World"}</code>.
* <p>
* The texts produced by the <code>toString</code> methods strictly conform to
* the JSON syntax rules. The constructors are more forgiving in the texts they
* will accept:
* <ul>
* <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
* before the closing brace.</li>
* <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
* quote)</small>.</li>
* <li>Strings do not need to be quoted at all if they do not begin with a
* quote or single quote, and if they do not contain leading or trailing
* spaces, and if they do not contain any of these characters:
* <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and
* if they are not the reserved words <code>true</code>, <code>false</code>,
* or <code>null</code>.</li>
* </ul>
*
* @author JSON.org
* @version 2015-05-05
*/
public class JSONObject {
/**
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
* whilst Java's null is equivalent to the value that JavaScript calls
* undefined.
*/
private static final class Null {
/**
* There is only intended to be a single instance of the NULL object,
* so the clone method returns itself.
*
* @return NULL.
*/
@Override
protected final Object clone() {
return this;
}
/**
* A Null object is equal to the null value and to itself.
*
* @param object
* An object to test for nullness.
* @return true if the object parameter is the JSONObject.NULL object or
* null.
*/
@Override
public boolean equals(Object object) {
return object == null || object == this;
}
/**
* Get the "null" string value.
*
* @return The string "null".
*/
public String toString() {
return "null";
}
}
/**
* The map where the JSONObject's properties are kept.
*/
private final Map<String, Object> map;
/**
* It is sometimes more convenient and less ambiguous to have a
* <code>NULL</code> object than to use Java's <code>null</code> value.
* <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
* <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
*/
public static final Object NULL = new Null();
/**
* Construct an empty JSONObject.
*/
public JSONObject() {
this.map = new HashMap<String, Object>();
}
/**
* Construct a JSONObject from a subset of another JSONObject. An array of
* strings is used to identify the keys that should be copied. Missing keys
* are ignored.
*
* @param jo
* A JSONObject.
* @param names
* An array of strings.
* @throws JSONException
* @exception JSONException
* If a value is a non-finite number or if a name is
* duplicated.
*/
public JSONObject(JSONObject jo, String[] names) {
this();
for (int i = 0; i < names.length; i += 1) {
try {
this.putOnce(names[i], jo.opt(names[i]));
} catch (Exception ignore) {
}
}
}
/**
* Construct a JSONObject from a JSONTokener.
*
* @param x
* A JSONTokener object containing the source string.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this();
char c;
String key;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
}
for (;;) {
c = x.nextClean();
switch (c) {
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
x.back();
key = x.nextValue().toString();
}
// The key is followed by ':'.
c = x.nextClean();
if (c != ':') {
throw x.syntaxError("Expected a ':' after a key");
}
this.putOnce(key, x.nextValue());
// Pairs are separated by ','.
switch (x.nextClean()) {
case ';':
case ',':
if (x.nextClean() == '}') {
return;
}
x.back();
break;
case '}':
return;
default:
throw x.syntaxError("Expected a ',' or '}'");
}
}
}
/**
* Construct a JSONObject from a Map.
*
* @param map
* A map object that can be used to initialize the contents of
* the JSONObject.
* @throws JSONException
*/
public JSONObject(Map<String, Object> map) {
this.map = new HashMap<String, Object>();
if (map != null) {
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
Object value = entry.getValue();
if (value != null) {
this.map.put(entry.getKey(), wrap(value));
}
}
}
}
/**
* Construct a JSONObject from an Object using bean getters. It reflects on
* all of the public methods of the object. For each of the methods with no
* parameters and a name starting with <code>"get"</code> or
* <code>"is"</code> followed by an uppercase letter, the method is invoked,
* and a key and the value returned from the getter method are put into the
* new JSONObject.
*
* The key is formed by removing the <code>"get"</code> or <code>"is"</code>
* prefix. If the second remaining character is not upper case, then the
* first character is converted to lower case.
*
* For example, if an object has a method named <code>"getName"</code>, and
* if the result of calling <code>object.getName()</code> is
* <code>"Larry Fine"</code>, then the JSONObject will contain
* <code>"name": "Larry Fine"</code>.
*
* @param bean
* An object that has getter methods that should be used to make
* a JSONObject.
*/
public JSONObject(Object bean) {
this();
this.populateMap(bean);
}
/**
* Construct a JSONObject from an Object, using reflection to find the
* public members. The resulting JSONObject's keys will be the strings from
* the names array, and the values will be the field values associated with
* those keys in the object. If a key is not found or not visible, then it
* will not be copied into the new JSONObject.
*
* @param object
* An object that has fields that should be used to make a
* JSONObject.
* @param names
* An array of strings, the names of the fields to be obtained
* from the object.
*/
public JSONObject(Object object, String names[]) {
this();
Class<?> c = object.getClass();
for (int i = 0; i < names.length; i += 1) {
String name = names[i];
try {
this.putOpt(name, c.getField(name).get(object));
} catch (Exception ignore) {
}
}
}
/**
* Construct a JSONObject from a source JSON text string. This is the most
* commonly used JSONObject constructor.
*
* @param source
* A string beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>
* &nbsp;<small>(right brace)</small>.
* @exception JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
}
/**
* Construct a JSONObject from a ResourceBundle.
*
* @param baseName
* The ResourceBundle base name.
* @param locale
* The Locale to load the ResourceBundle for.
* @throws JSONException
* If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key != null) {
// Go through the path, ensuring that there is a nested JSONObject for each
// segment except the last. Add the value using the last segment's name into
// the deepest nested JSONObject.
String[] path = ((String) key).split("\\.");
int last = path.length - 1;
JSONObject target = this;
for (int i = 0; i < last; i += 1) {
String segment = path[i];
JSONObject nextTarget = target.optJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject();
target.put(segment, nextTarget);
}
target = nextTarget;
}
target.put(path[last], bundle.getString((String) key));
}
}
}
/**
* Accumulate values under a key. It is similar to the put method except
* that if there is already an object stored under the key then a JSONArray
* is stored under the key to hold all of the accumulated values. If there
* is already a JSONArray, then the new value is appended to it. In
* contrast, the put method replaces the previous value.
*
* If only one value is accumulated that is not a JSONArray, then the result
* will be the same as using put. But if multiple values are accumulated,
* then the result will be like append.
*
* @param key
* A key string.
* @param value
* An object to be accumulated under the key.
* @return this.
* @throws JSONException
* If the value is an invalid number or if the key is null.
*/
public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key,
value instanceof JSONArray ? new JSONArray().put(value)
: value);
} else if (object instanceof JSONArray) {
((JSONArray) object).put(value);
} else {
this.put(key, new JSONArray().put(object).put(value));
}
return this;
}
/**
* Append values to the array under a key. If the key does not exist in the
* JSONObject, then the key is put in the JSONObject with its value being a
* JSONArray containing the value parameter. If the key was already
* associated with a JSONArray, then the value parameter is appended to it.
*
* @param key
* A key string.
* @param value
* An object to be accumulated under the key.
* @return this.
* @throws JSONException
* If the key is null or if the current value associated with
* the key is not a JSONArray.
*/
public JSONObject append(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key, new JSONArray().put(value));
} else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value));
} else {
throw new JSONException("JSONObject[" + key
+ "] is not a JSONArray.");
}
return this;
}
/**
* Produce a string from a double. The string "null" will be returned if the
* number is not finite.
*
* @param d
* A double.
* @return A String.
*/
public static String doubleToString(double d) {
if (Double.isInfinite(d) || Double.isNaN(d)) {
return "null";
}
// Shave off trailing zeros and decimal point, if possible.
String string = Double.toString(d);
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
}
return string;
}
/**
* Get the value object associated with a key.
*
* @param key
* A key string.
* @return The object associated with the key.
* @throws JSONException
* if the key is not found.
*/
public Object get(String key) throws JSONException {
if (key == null) {
throw new JSONException("Null key.");
}
Object object = this.opt(key);
if (object == null) {
throw new JSONException("JSONObject[" + quote(key) + "] not found.");
}
return object;
}
/**
* Get the boolean value associated with a key.
*
* @param key
* A key string.
* @return The truth.
* @throws JSONException
* if the value is not a Boolean or the String "true" or
* "false".
*/
public boolean getBoolean(String key) throws JSONException {
Object object = this.get(key);
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a Boolean.");
}
/**
* Get the double value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public double getDouble(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object);
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.");
}
}
/**
* Get the int value associated with a key.
*
* @param key
* A key string.
* @return The integer value.
* @throws JSONException
* if the key is not found or if the value cannot be converted
* to an integer.
*/
public int getInt(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int.");
}
}
/**
* Get the JSONArray value associated with a key.
*
* @param key
* A key string.
* @return A JSONArray which is the value.
* @throws JSONException
* if the key is not found or if the value is not a JSONArray.
*/
public JSONArray getJSONArray(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof JSONArray) {
return (JSONArray) object;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONArray.");
}
/**
* Get the JSONObject value associated with a key.
*
* @param key
* A key string.
* @return A JSONObject which is the value.
* @throws JSONException
* if the key is not found or if the value is not a JSONObject.
*/
public JSONObject getJSONObject(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof JSONObject) {
return (JSONObject) object;
}
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONObject.");
}
/**
* Get the long value associated with a key.
*
* @param key
* A key string.
* @return The long value.
* @throws JSONException
* if the key is not found or if the value cannot be converted
* to a long.
*/
public long getLong(String key) throws JSONException {
Object object = this.get(key);
try {
return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long.");
}
}
/**
* Get an array of field names from a JSONObject.
*
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(JSONObject jo) {
int length = jo.length();
if (length == 0) {
return null;
}
Iterator<String> iterator = jo.keys();
String[] names = new String[length];
int i = 0;
while (iterator.hasNext()) {
names[i] = iterator.next();
i += 1;
}
return names;
}
/**
* Get an array of field names from an Object.
*
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(Object object) {
if (object == null) {
return null;
}
Class<?> klass = object.getClass();
Field[] fields = klass.getFields();
int length = fields.length;
if (length == 0) {
return null;
}
String[] names = new String[length];
for (int i = 0; i < length; i += 1) {
names[i] = fields[i].getName();
}
return names;
}
/**
* Get the string associated with a key.
*
* @param key
* A key string.
* @return A string which is the value.
* @throws JSONException
* if there is no string value for the key.
*/
public String getString(String key) throws JSONException {
Object object = this.get(key);
if (object instanceof String) {
return (String) object;
}
throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
}
/**
* Determine if the JSONObject contains a specific key.
*
* @param key
* A key string.
* @return true if the key exists in the JSONObject.
*/
public boolean has(String key) {
return this.map.containsKey(key);
}
/**
* Increment a property of a JSONObject. If there is no such property,
* create one with a value of 1. If there is such a property, and if it is
* an Integer, Long, Double, or Float, then add one to it.
*
* @param key
* A key string.
* @return this.
* @throws JSONException
* If there is already a property with this name that is not an
* Integer, Long, Double, or Float.
*/
public JSONObject increment(String key) throws JSONException {
Object value = this.opt(key);
if (value == null) {
this.put(key, 1);
} else if (value instanceof Integer) {
this.put(key, (Integer) value + 1);
} else if (value instanceof Long) {
this.put(key, (Long) value + 1);
} else if (value instanceof Double) {
this.put(key, (Double) value + 1);
} else if (value instanceof Float) {
this.put(key, (Float) value + 1);
} else {
throw new JSONException("Unable to increment [" + quote(key) + "].");
}
return this;
}
/**
* Determine if the value associated with the key is null or if there is no
* value.
*
* @param key
* A key string.
* @return true if there is no value associated with the key or if the value
* is the JSONObject.NULL object.
*/
public boolean isNull(String key) {
return JSONObject.NULL.equals(this.opt(key));
}
/**
* Get an enumeration of the keys of the JSONObject.
*
* @return An iterator of the keys.
*/
public Iterator<String> keys() {
return this.keySet().iterator();
}
/**
* Get a set of keys of the JSONObject.
*
* @return A keySet.
*/
public Set<String> keySet() {
return this.map.keySet();
}
/**
* Get the number of keys stored in the JSONObject.
*
* @return The number of keys in the JSONObject.
*/
public int length() {
return this.map.size();
}
/**
* Produce a JSONArray containing the names of the elements of this
* JSONObject.
*
* @return A JSONArray containing the key strings, or null if the JSONObject
* is empty.
*/
public JSONArray names() {
JSONArray ja = new JSONArray();
Iterator<String> keys = this.keys();
while (keys.hasNext()) {
ja.put(keys.next());
}
return ja.length() == 0 ? null : ja;
}
/**
* Produce a string from a Number.
*
* @param number
* A Number
* @return A String.
* @throws JSONException
* If n is a non-finite number.
*/
public static String numberToString(Number number) throws JSONException {
if (number == null) {
throw new JSONException("Null pointer");
}
testValidity(number);
// Shave off trailing zeros and decimal point, if possible.
String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
}
return string;
}
/**
* Get an optional value associated with a key.
*
* @param key
* A key string.
* @return An object which is the value, or null if there is no value.
*/
public Object opt(String key) {
return key == null ? null : this.map.get(key);
}
/**
* Get an optional boolean associated with a key. It returns false if there
* is no such key, or if the value is not Boolean.TRUE or the String "true".
*
* @param key
* A key string.
* @return The truth.
*/
public boolean optBoolean(String key) {
return this.optBoolean(key, false);
}
/**
* Get an optional boolean associated with a key. It returns the
* defaultValue if there is no such key, or if it is not a Boolean or the
* String "true" or "false" (case insensitive).
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return The truth.
*/
public boolean optBoolean(String key, boolean defaultValue) {
try {
return this.getBoolean(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional double associated with a key, or NaN if there is no such
* key or if its value is not a number. If the value is a string, an attempt
* will be made to evaluate it as a number.
*
* @param key
* A string which is the key.
* @return An object which is the value.
*/
public double optDouble(String key) {
return this.optDouble(key, Double.NaN);
}
/**
* Get an optional double associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
* string, an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public double optDouble(String key, double defaultValue) {
try {
return this.getDouble(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional int value associated with a key, or zero if there is no
* such key or if the value is not a number. If the value is a string, an
* attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @return An object which is the value.
*/
public int optInt(String key) {
return this.optInt(key, 0);
}
/**
* Get an optional int value associated with a key, or the default if there
* is no such key or if the value is not a number. If the value is a string,
* an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public int optInt(String key, int defaultValue) {
try {
return this.getInt(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional JSONArray associated with a key. It returns null if there
* is no such key, or if its value is not a JSONArray.
*
* @param key
* A key string.
* @return A JSONArray which is the value.
*/
public JSONArray optJSONArray(String key) {
Object o = this.opt(key);
return o instanceof JSONArray ? (JSONArray) o : null;
}
/**
* Get an optional JSONObject associated with a key. It returns null if
* there is no such key, or if its value is not a JSONObject.
*
* @param key
* A key string.
* @return A JSONObject which is the value.
*/
public JSONObject optJSONObject(String key) {
Object object = this.opt(key);
return object instanceof JSONObject ? (JSONObject) object : null;
}
/**
* Get an optional long value associated with a key, or zero if there is no
* such key or if the value is not a number. If the value is a string, an
* attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @return An object which is the value.
*/
public long optLong(String key) {
return this.optLong(key, 0);
}
/**
* Get an optional long value associated with a key, or the default if there
* is no such key or if the value is not a number. If the value is a string,
* an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public long optLong(String key, long defaultValue) {
try {
return this.getLong(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional string associated with a key. It returns an empty string
* if there is no such key. If the value is not a string and is not null,
* then it is converted to a string.
*
* @param key
* A key string.
* @return A string which is the value.
*/
public String optString(String key) {
return this.optString(key, "");
}
/**
* Get an optional string associated with a key. It returns the defaultValue
* if there is no such key.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return A string which is the value.
*/
public String optString(String key, String defaultValue) {
Object object = this.opt(key);
return NULL.equals(object) ? defaultValue : object.toString();
}
private void populateMap(Object bean) {
Class<?> klass = bean.getClass();
// If klass is a System class then set includeSuperClass to false.
boolean includeSuperClass = klass.getClassLoader() != null;
Method[] methods = includeSuperClass ? klass.getMethods() : klass
.getDeclaredMethods();
for (int i = 0; i < methods.length; i += 1) {
try {
Method method = methods[i];
if (Modifier.isPublic(method.getModifiers())) {
String name = method.getName();
String key = "";
if (name.startsWith("get")) {
if ("getClass".equals(name)
|| "getDeclaringClass".equals(name)) {
key = "";
} else {
key = name.substring(3);
}
} else if (name.startsWith("is")) {
key = name.substring(2);
}
if (key.length() > 0
&& Character.isUpperCase(key.charAt(0))
&& method.getParameterTypes().length == 0) {
if (key.length() == 1) {
key = key.toLowerCase();
} else if (!Character.isUpperCase(key.charAt(1))) {
key = key.substring(0, 1).toLowerCase()
+ key.substring(1);
}
Object result = method.invoke(bean, (Object[]) null);
if (result != null) {
this.map.put(key, wrap(result));
}
}
}
} catch (Exception ignore) {
}
}
}
/**
* Put a key/boolean pair in the JSONObject.
*
* @param key
* A key string.
* @param value
* A boolean which is the value.
* @return this.
* @throws JSONException
* If the key is null.
*/
public JSONObject put(String key, boolean value) throws JSONException {
this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONArray which is produced from a Collection.
*
* @param key
* A key string.
* @param value
* A Collection value.
* @return this.
* @throws JSONException
*/
public JSONObject put(String key, Collection<Object> value) throws JSONException {
this.put(key, new JSONArray(value));
return this;
}
/**
* Put a key/double pair in the JSONObject.
*
* @param key
* A key string.
* @param value
* A double which is the value.
* @return this.
* @throws JSONException
* If the key is null or if the number is invalid.
*/
public JSONObject put(String key, double value) throws JSONException {
this.put(key, new Double(value));
return this;
}
/**
* Put a key/int pair in the JSONObject.
*
* @param key
* A key string.
* @param value
* An int which is the value.
* @return this.
* @throws JSONException
* If the key is null.
*/
public JSONObject put(String key, int value) throws JSONException {
this.put(key, new Integer(value));
return this;
}
/**
* Put a key/long pair in the JSONObject.
*
* @param key
* A key string.
* @param value
* A long which is the value.
* @return this.
* @throws JSONException
* If the key is null.
*/
public JSONObject put(String key, long value) throws JSONException {
this.put(key, new Long(value));
return this;
}
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONObject which is produced from a Map.
*
* @param key
* A key string.
* @param value
* A Map value.
* @return this.
* @throws JSONException
*/
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
this.put(key, new JSONObject(value));
return this;
}
/**
* Put a key/value pair in the JSONObject. If the value is null, then the
* key will be removed from the JSONObject if it is present.
*
* @param key
* A key string.
* @param value
* An object which is the value. It should be of one of these
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
* If the value is non-finite number or if the key is null.
*/
public JSONObject put(String key, Object value) throws JSONException {
if (key == null) {
throw new NullPointerException("Null key.");
}
if (value != null) {
testValidity(value);
this.map.put(key, value);
} else {
this.remove(key);
}
return this;
}
/**
* Put a key/value pair in the JSONObject, but only if the key and the value
* are both non-null, and only if there is not already a member with that
* name.
*
* @param key string
* @param value object
* @return this.
* @throws JSONException
* if the key is a duplicate
*/
public JSONObject putOnce(String key, Object value) throws JSONException {
if (key != null && value != null) {
if (this.opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\"");
}
this.put(key, value);
}
return this;
}
/**
* Put a key/value pair in the JSONObject, but only if the key and the value
* are both non-null.
*
* @param key
* A key string.
* @param value
* An object which is the value. It should be of one of these
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
* If the value is a non-finite number.
*/
public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) {
this.put(key, value);
}
return this;
}
/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
* allowing JSON text to be delivered in HTML. In JSON text, a string cannot
* contain a control character or an unescaped quote or backslash.
*
* @param string
* A String
* @return A String correctly formatted for insertion in a JSON text.
*/
public static String quote(String string) {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
try {
return quote(string, sw).toString();
} catch (IOException ignored) {
// will never happen - we are writing to a string writer
return "";
}
}
}
public static Writer quote(String string, Writer w) throws IOException {
if (string == null || string.length() == 0) {
w.write("\"\"");
return w;
}
char b;
char c = 0;
String hhhh;
int i;
int len = string.length();
w.write('"');
for (i = 0; i < len; i += 1) {
b = c;
c = string.charAt(i);
switch (c) {
case '\\':
case '"':
w.write('\\');
w.write(c);
break;
case '/':
if (b == '<') {
w.write('\\');
}
w.write(c);
break;
case '\b':
w.write("\\b");
break;
case '\t':
w.write("\\t");
break;
case '\n':
w.write("\\n");
break;
case '\f':
w.write("\\f");
break;
case '\r':
w.write("\\r");
break;
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|| (c >= '\u2000' && c < '\u2100')) {
w.write("\\u");
hhhh = Integer.toHexString(c);
w.write("0000", 0, 4 - hhhh.length());
w.write(hhhh);
} else {
w.write(c);
}
}
}
w.write('"');
return w;
}
/**
* Remove a name and its value, if present.
*
* @param key
* The name to be removed.
* @return The value that was associated with the name, or null if there was
* no value.
*/
public Object remove(String key) {
return this.map.remove(key);
}
/**
* Determine if two JSONObjects are similar.
* They must contain the same set of names which must be associated with
* similar values.
*
* @param other The other JSONObject
* @return true if they are equal
*/
public boolean similar(Object other) {
try {
if (!(other instanceof JSONObject)) {
return false;
}
Set<String> set = this.keySet();
if (!set.equals(((JSONObject)other).keySet())) {
return false;
}
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
Object valueThis = this.get(name);
Object valueOther = ((JSONObject)other).get(name);
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof JSONArray) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
}
return true;
} catch (Throwable exception) {
return false;
}
}
/**
* Try to convert a string into a number, boolean, or null. If the string
* can't be converted, return the string.
*
* @param string
* A String.
* @return A simple JSON value.
*/
public static Object stringToValue(String string) {
Double d;
if (string.equals("")) {
return string;
}
if (string.equalsIgnoreCase("true")) {
return Boolean.TRUE;
}
if (string.equalsIgnoreCase("false")) {
return Boolean.FALSE;
}
if (string.equalsIgnoreCase("null")) {
return JSONObject.NULL;
}
/*
* If it might be a number, try converting it. If a number cannot be
* produced, then the value will just be a string.
*/
char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '-') {
try {
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1) {
d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = new Long(string);
if (string.equals(myLong.toString())) {
if (myLong == myLong.intValue()) {
return myLong.intValue();
} else {
return myLong;
}
}
}
} catch (Exception ignore) {
}
}
return string;
}
/**
* Throw an exception if the object is a NaN or infinite number.
*
* @param o
* The object to test.
* @throws JSONException
* If o is a non-finite number.
*/
public static void testValidity(Object o) throws JSONException {
if (o != null) {
if (o instanceof Double) {
if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
throw new JSONException(
"JSON does not allow non-finite numbers.");
}
} else if (o instanceof Float) {
if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
throw new JSONException(
"JSON does not allow non-finite numbers.");
}
}
}
}
/**
* Produce a JSONArray containing the values of the members of this
* JSONObject.
*
* @param names
* A JSONArray containing a list of key strings. This determines
* the sequence of the values in the result.
* @return A JSONArray of values.
* @throws JSONException
* If any of the values are non-finite numbers.
*/
public JSONArray toJSONArray(JSONArray names) throws JSONException {
if (names == null || names.length() == 0) {
return null;
}
JSONArray ja = new JSONArray();
for (int i = 0; i < names.length(); i += 1) {
ja.put(this.opt(names.getString(i)));
}
return ja;
}
/**
* Make a JSON text of this JSONObject. For compactness, no whitespace is
* added. If this would not result in a syntactically correct JSON text,
* then null will be returned instead.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>.
*/
public String toString() {
try {
return this.toString(0);
} catch (Exception e) {
return null;
}
}
/**
* Make a prettyprinted JSON text of this JSONObject.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>.
* @throws JSONException
* If the object contains an invalid number.
*/
public String toString(int indentFactor) throws JSONException {
StringWriter w = new StringWriter();
synchronized (w.getBuffer()) {
return this.write(w, indentFactor, 0).toString();
}
}
/**
* Make a JSON text of an Object value. If the object has an
* value.toJSONString() method, then that method will be used to produce the
* JSON text. The method is required to produce a strictly conforming text.
* If the object does not contain a toJSONString method (which is the most
* common case), then a text will be produced by other means. If the value
* is an array or Collection, then a JSONArray will be made from it and its
* toJSONString method will be called. If the value is a MAP, then a
* JSONObject will be made from it and its toJSONString method will be
* called. Otherwise, the value's toString method will be called, and the
* result will be quoted.
*
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The value to be serialized.
* @return a printable, displayable, transmittable representation of the
* object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>.
* @throws JSONException
* If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) {
return "null";
}
if (value instanceof JSONString) {
Object object;
try {
object = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
if (object instanceof String) {
return (String) object;
}
throw new JSONException("Bad value from toJSONString: " + object);
}
if (value instanceof Number) {
return numberToString((Number) value);
}
if (value instanceof Boolean || value instanceof JSONObject
|| value instanceof JSONArray) {
return value.toString();
}
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
return new JSONObject(map).toString();
}
if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
return new JSONArray(coll).toString();
}
if (value.getClass().isArray()) {
return new JSONArray(value).toString();
}
return quote(value.toString());
}
/**
* Wrap an object, if necessary. If the object is null, return the NULL
* object. If it is an array or collection, wrap it in a JSONArray. If it is
* a map, wrap it in a JSONObject. If it is a standard property (Double,
* String, et al) then it is already wrapped. Otherwise, if it comes from
* one of the java packages, turn it into a string. And if it doesn't, try
* to wrap it in a JSONObject. If the wrapping fails, then null is returned.
*
* @param object
* The object to wrap
* @return The wrapped value
*/
public static Object wrap(Object object) {
try {
if (object == null) {
return NULL;
}
if (object instanceof JSONObject || object instanceof JSONArray
|| NULL.equals(object) || object instanceof JSONString
|| object instanceof Byte || object instanceof Character
|| object instanceof Short || object instanceof Integer
|| object instanceof Long || object instanceof Boolean
|| object instanceof Float || object instanceof Double
|| object instanceof String) {
return object;
}
if (object instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) object;
return new JSONArray(coll);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) object;
return new JSONObject(map);
}
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
.getName() : "";
if (objectPackageName.startsWith("java.")
|| objectPackageName.startsWith("javax.")
|| object.getClass().getClassLoader() == null) {
return object.toString();
}
return new JSONObject(object);
} catch (Exception exception) {
return null;
}
}
/**
* Write the contents of the JSONObject as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONObject) {
((JSONObject) value).write(writer, indentFactor, indent);
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
new JSONObject(map).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
new JSONArray(coll).write(writer, indentFactor,
indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
} else if (value instanceof Number) {
writer.write(numberToString((Number) value));
} else if (value instanceof Boolean) {
writer.write(value.toString());
} else if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
} else {
quote(value.toString(), writer);
}
return writer;
}
static final void indent(Writer writer, int indent) throws IOException {
for (int i = 0; i < indent; i += 1) {
writer.write(' ');
}
}
/**
* Write the contents of the JSONObject as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return The writer.
* @throws JSONException
*/
Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
final int length = this.length();
Iterator<String> keys = this.keys();
writer.write('{');
if (length == 1) {
Object key = keys.next();
writer.write(quote(key.toString()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
while (keys.hasNext()) {
Object key = keys.next();
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
indent(writer, newindent);
writer.write(quote(key.toString()));
writer.write(':');
if (indentFactor > 0) {
writer.write(' ');
}
writeValue(writer, this.map.get(key), indentFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
indent(writer, indent);
}
writer.write('}');
return writer;
} catch (IOException exception) {
throw new JSONException(exception);
}
}
}
package com.qcloud.Utilities.Json;
/**
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
* method so that a class can change the behavior of
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>,
* and <code>JSONWriter.value(</code>Object<code>)</code>. The
* <code>toJSONString</code> method will be used instead of the default behavior
* of using the Object's <code>toString()</code> method and quoting the result.
*/
public interface JSONString {
/**
* The <code>toJSONString</code> method allows a class to produce its own JSON
* serialization.
*
* @return A strictly syntactically correct JSON text.
*/
public String toJSONString();
}